Wednesday, February 27, 2019

Setting up a Raspberry Pi 3 as a Bluetooth Speaker with NO Pulseaudio...

I really don't have much against pulseaudio. I use it daily on my daily computing machine. But believe it or not, I don't do my daily computing on a bluetooth speaker. No, I'm of the persuasion that pulse is a great desktop audio system (though I have high hopes for pipewire), except for when it isn't. For pro-audio (making my own music) I use JACK and it's awesome, and you already know I use pulse for daily web browsing, music listening etc, but neither of these cases are very much like a bluetooth speaker, are they?... We need an audio system that's basically going to serve as part of an appliance. But what else is there?!

OSS?!?!!

No, there is not oss.
I mean, there is, but it was replaced for good reason.

ALSA. I'm trying to lead you to alsa. Alsa is actually the audio driver behind both JACK and pulseaudio so its lower level, but you've got a headless system for crying out loud that just connects bluetooth and streams the audio, what do you need high level stuff for?






I was quite surprised that every blog post and stackoverflow discussing using a raspberry pi for a bluetooth speaker build uses the pulseaudio bluetooth plugin. So I'm writing one on what I consider a better way to do it.


Spoiler Alert!: I never got it to work well. There was always an odd rhythmic chatter sound in the background, a bit like a CD thats a little scratched by still plays. I'm posting this now for completeness, and perhaps something has changed in the last year since I was playing with it, but I've repurposed my pi for a zythian box.


Since I'm headless and I don't have monitors and keyboards lying around I used my usual method to ssh into the pi through a hardwire connection according to the 'macgyver' method.  

First thing to do for me is setup your wifi though you might skip this if you aren't using the pi as a dlna renderer also.

https://github.com/Arkq/bluez-alsa


follow part of this:
https://raspberrypi.stackexchange.com/questions/47708/setup-raspberry-pi-3-as-bluetooth-speaker

but ignore the pulse stuff. Instead use:
https://github.com/Arkq/bluez-alsa


#update
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install vim
sudo rpi-update
sudo reboot now




#install bluez-alsa from source
sudo apt-get install autoconf libsbc-dev libtool alsa-base libasound2-dev bluez bluez-tools libbluetooth-dev libglib2.0-dev
wget https://github.com/Arkq/bluez-alsa/archive/master.zip
unzip master.zip
cd bluez-alsa-master/
autoreconf --install
mkdir build && cd build
../configure --enable-debug
make
sudo make install









#set up bluetooth
cd
sudo vim /etc/bluetooth/main.conf
sudo vim /etc/bluetooth/audio.conf 


#just run a test setup
sudo hciconfig hci0  up
sudo hciconfig hci0 piscan
sudo bluealsa &

#if you do a scan the pi should be visible to a phone's bluetooth, but it won't accept the connection yet
sudo bluetoothctl
devices #shows known devices, the phone should show up if you've tried to connect
trust XX:XX:XX:XX:XX:XX
quit
#configure wifi
sudo cat /etc/wpa_supplicant/wpa_supplicant.conf >> tmp
cat tmp
wpa_passphrase "MySSID" "mypassword" >> tmp
#remove plaintext password
vim tmp
sudo cp tmp /etc/wpa_supplicant/wpa_supplicant.conf
rm tmp
wpa_cli -i wlan0 reconfigure
ifconfig #just check you are connected

#find your device you want to output to from this list
aplay -L
#mine was iec958,... now use the bluealsa-aplay to stream to it
bluealsa-aplay -d iec958:CARD=Device,DEV=0 XX:XX:XX:XX:XX:XX

#YAY it worked!

#Now set it up to do the connection automatically at boot
sudo vim /etc/rc.local
#add these 3 lines:
#hciconfig hci0  up
#hciconfig hci0 piscan
#bluealsa &
#bluealsa-aplay -d iec958:CARD=Device,DEV=0 00:00:00:00:00:00

#the 00:00... address tells bluealsa-aplay to accept any device and play from it

#the only problem is the wifi and bluetooth don't play nicely together, so audio has a lot of xruns (dropouts/clicks/pops/studders).
#set up a script that will automatically disable wifi when the bluetooth connects.

Problem was that the sound wasn't very good. There was a chattering sound in the background all the time, like a cd thats scratched just enough that the player can read it but not cleanly.

So back to the drawing board. I'd read people used AAC for bluez-alsa. I don't know what this really mean, though I assume its the encoding for the wireless digital audio transfer. The reason I skipped it was because it was work to download and build the library necessary for it. But its not that bad:

wget https://github.com/mstorsjo/fdk-aac/archive/master.zip
unzip master.zip
cd master
libtoolize
autoreconf --install
mkdir build && cd build
../configure
make
make install

then go back to bluez-alsa with
../configure --enable-aac --enable-debug --disable-payloadcheck



This however did not fix the issue. :( After trying all this it seems a shame to give up, but I don't have time to fight it any more, and now I have a new TV connected to my sound system that streams from my media server anyway. Hope this will help someone.

No comments: