Bluetooth Audio Output from Patchbox (RPI 4)?

Problem: How to play or route Bluetooth Audio Output on a Pi 4 with Patchbox?
I’m enjoying Patchbox OS on a RPI 4 to process incoming MIDI from a MIDIHub and play through instruments hosted in Carla. To date, I have been using the 3.5mm audio output, which is fine for my purposes. However, I have an emerging need to have my Pi and a speaker in different locations (without a cable). I have no Latency concerns for this need - I just want the Pi’s audio to play via Bluetooth.

UPDATE: Full solution here:

When I turn on Bluetooth I can connect to a Bluetooth speaker. When I right click on the audio icon on the Pi’s desktop, I can select the Bluetooth speaker for output. However, I cannot produce any sound, via Carla hosted instruments, or even using Chromium to play audio from a site like Soundcloud via Bluetooth.

I removed the device using the GUI and tried again via commandline (bluetoothctl), but I still can’t get any sound to play. I’m wondering if Patchbox, by design, is blocking bluetooth audio, or if there is another step for linux audio I need to explore?

Thanks!
Capture

While connected to the Bluetooth speaker, what is the output of:

aplay -l

Just the default soundcard is listed (which is working well):

patch@patchbox:~ $ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
Subdevices: 6/7
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 IEC958/HDMI [bcm2835 IEC958/HDMI]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 0: ALSA [bcm2835 ALSA], device 2: bcm2835 IEC958/HDMI1 [bcm2835 IEC958/HDMI1]
Subdevices: 1/1
Subdevice #0: subdevice #0

From what I have picked up - it seems many (RPI users) have troubles with bluetooth audio output. I was considering installing Pipewire to see if that would give me new options, but will likely want to make an image backup first - really my Patchbox setup is solid and predictable except for the lack of Bluetooth as new output. It’s not clear to me where BT Audio output would even show up, for example, in Patchbay (or Carla).

OK - so I just had success at connecting my Patchbox RPI4 to a bluetooth speaker, using Carla to play instruments wirelessly. I am close to having a set of steps I could share with others (using what I believe to be a standard patchbox setup). The last thing I am trying to figure out is how to get my setup to boot correctly. I found the jackd startup lin in /etc/jackdrc. However jackd fails on boot when I try to modify it with bluealsa parameters that would work in a user session. My instinct is to delay the jackd start until bluetooth has had a chance to start connect. How can I do this?


This command works when I run it from the command line:
jackd -t 2000 -R -P 95 -d alsa -r 44100 -p 512 -P bluealsa -n 3 -s -S -o 2 -X seq

However when I put it in the jackdrc script that starts jack it fails:
Oct 08 15:04:19 patchbox jackdrc[434]: ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition ‘defaults.bluealsa.device’
Oct 08 15:04:19 patchbox jackdrc[434]: ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or di
Oct 08 15:04:19 patchbox jackdrc[434]: ALSA lib conf.c:5036:(snd_config_expand) Args evaluate error: No such file or directory
Oct 08 15:04:19 patchbox jackdrc[434]: ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM bluealsa
Oct 08 15:04:19 patchbox jackdrc[434]: ALSA: Cannot open PCM device alsa_pcm for playback. Falling back to capture-only mode
Oct 08 15:04:19 patchbox jackdrc[434]: Cannot initialize driver
Oct 08 15:04:19 patchbox jackdrc[434]: JackServer::Open failed with -1
Oct 08 15:04:19 patchbox jackdrc[434]: Failed to open server
Oct 08 15:04:19 patchbox systemd[1]: jack.service: Main process exited, code=exited, status=255/EXCEPTION
Oct 08 15:04:19 patchbox systemd[1]: jack.service: Failed with result ‘exit-code’.

Try customizing the jack.service file:

# First make a copy of the jack.service at /etc/... so it overrides the 
# one that comes with blokas-jack package.
sudo cp /lib/systemd/system/jack.service /etc/systemd/system/jack.service

# Edit it:
sudo nano /etc/systemd/system/jack.service

# Make systemd aware of changes:
sudo systemctl daemon-reload

# Restart the jack.service:
sudo systemctl restart jack

Try editing the After= line so it says:

After=sound.target bluetooth.target

If that doesn’t work, you’ll have to figure out the way how “bluealsa” is started, and set the appropriate configuration for jack.service so it starts at the right moment.

1 Like

I tried it, no luck yet. If it informs other solutions, the way I manually start jack that directs audio to bluetooth is:
exec /usr/bin/jackd -r -d alsa -P bluealsa -n 3 -S -o 2 -X seq

So to try to automate this, I modified /etc/jackdrc, replacing
exec /usr/bin/jackd -t 2000 -R -P 95 -d alsa -d hw:ALSA -r 44100 -p 512 -n 3 -X seq -s -S
with
exec /usr/bin/jackd -r -d alsa -P bluealsa -n 3 -S -o 2 -X seq
*I realize I omitted realtime options. I don’t need them for my (plant generated music) project. That allowed me to have zero xruns with Carla instruments. So with the jack service start, I will have to explore how to make sure bluealsa runs first, or where I can put a connecting command to initiate the speaker connection, before jack runs.

jackdrc contents:

#!/bin/sh
#exec is used to ‘morph’ the shell interpreter process into jackd process, saving some system resources.
exec /usr/bin/jackd -r -d alsa -P bluealsa -n 3 -S -o 2 -X seq
#Original one here - out of box patchbox
#exec /usr/bin/jackd -t 2000 -R -P 95 -d alsa -d hw:ALSA -r 44100 -p 512 -n 3 -X seq -s -S

Loaded: loaded (/etc/systemd/system/jack.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2021-10-10 10:09:47 CDT; 8min ago
Process: 425 ExecStart=/etc/jackdrc (code=exited, status=255/EXCEPTION)
Main PID: 425 (code=exited, status=255/EXCEPTION)

Results of sudo systemctl status jack.service:
Oct 10 10:09:47 patchbox jackdrc[425]: ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device
Oct 10 10:09:47 patchbox jackdrc[425]: ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such f
Oct 10 10:09:47 patchbox jackdrc[425]: ALSA lib conf.c:5036:(snd_config_expand) Args evaluate error: No such file or directory
Oct 10 10:09:47 patchbox jackdrc[425]: ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM bluealsa
Oct 10 10:09:47 patchbox jackdrc[425]: ALSA: Cannot open PCM device alsa_pcm for playback. Falling back to capture-only mode
Oct 10 10:09:47 patchbox jackdrc[425]: Cannot initialize driver
Oct 10 10:09:47 patchbox jackdrc[425]: JackServer::Open failed with -1
Oct 10 10:09:47 patchbox jackdrc[425]: Failed to open server
Oct 10 10:09:47 patchbox systemd[1]: jack.service: Main process exited, code=exited, status=255/EXCEPTION
Oct 10 10:09:47 patchbox systemd[1]: jack.service: Failed with result ‘exit-code’.
I do not get these errors when I run the command manually from the desktop/patch user session.

What did you have to do for the ‘bluealsa’ device to appear on the system at all? Just trying to figure out what’s the moment it appears at on the system. :slight_smile:

These are the significant pieces I have done so jack would start with bluetooth/bluealsa as the destination for output audio.

1.

Use Raspberry Pi GUI (pair bluetooth speaker, then select as audio output) or bluetoothctl commands to register speaker. I’m sure I have tried both ways, since I obtained the bluetooth address of my speaker.

2.

Add patch user to bluetooth group - not sure if this was neccessary
sudo usermod -a -G bluetooth patch

3.

in /boot/config.txt
Comment out onboard audio. This was supposed to help ensure bluetooth was used instead of onboard audio. My recollection is that, if I didn’t do this, my .asoundrc file in step 4 kept getting replaced with another configuration.
#Enable audio (loads snd_bcm2835)
#dtparam=audio=on

4.

Create or edit ~/.asound.rc file:
*This seems to be the definition that jack can’t see during boot, but is accessed just fine after login.
**
SOLUTION UPDATE (create this file as /etc/asound.conf for the ‘solution’ setup)

defaults.bluealsa.service "org.bluealsa"
defaults.bluealsa.device "XX:XX:XX:XX:XX:XX" *(bluetooth speaker address)*
defaults.bluealsa.profile "a2dp"
defaults.bluealsa.delay 10000
pcm.!default {
	type asym
	playback.pcm {
		type plug
		slave.pcm "output"
	}
	capture.pcm {
		type plug
		slave.pcm "input"
	}
}

pcm.output {
	type bluealsa
	device "XX:XX:XX:XX:XX:XX" *(bluetooth speaker address)*
	profile "a2dp"
}

ctl.!default {
	type bluealsa
}

5.

Edit /etc/jackdrc
comment out existing jack starting line
Put in a new one that references bluealsa (the bluetooth stack). This one works for me:
exec /usr/bin/jackd -r -d alsa -P bluealsa -n 3 -S -o 2 -X seq
Now audio from Jack comes out of bluetooth speaker, when using KX-Studio Carla as a MIDI processor and instrument host.

What is left is to sequence or set commands such that Jack setup does not fail during boot. I want a foolproof pi setup that I can put in a museum exhibit, that someone can just power cycle if there is a problem. I was thinking that maybe the jack.service/jackdrc files didn’t have access to the .asoundrc for some reason, and also tried adding jack or even root to the bluetooth group, with no improvement. The errors posted above suggest to me that Jack can’t ‘see’ the definition present in (.asoundrc). I am certain there is a solution to this problem, but I am new enough to linux I don’t understand the order and dependencies of these things.

Try moving this file to the system wide location: /etc/asound.conf - it’d make sense that only the patch user is aware of the ‘bluealsa’ device, since it’s configured in the user’s home directory. Maybe moving it to system wide location will make the ‘jack’ user able to use it too from its service.

1 Like

Genius - It worked! I spent many hours trying to get all of this to work!
:grinning: :joy: :grinning:
Thank you so much!

Now that this is sorted out, I could even go headless on this setup, which will allow me to layer more sounds and eek more CPU out of my Pi4! Your feedback also helped me pick up a couple more things about linux (e.g. the /etc/ folder versus user locations).

1 Like

Glad to hear you finally got it working! :slight_smile:

Btw, the above guide you wrote seems very close to being a complete Bluetooth audio with Jack setup guide. :slight_smile: Could you update it slightly so it only shows the correct steps to take, as well as share the working slightly edited jack.service file you created in /etc/...

Or even you could post it as an entirely new topic. :wink:

Yes - I need to check a few things. I found several places I had tried added a command to connect the speaker including rc.local. I tried removing the connection line and after a reboot it was not working so I added it back. I will make an image backup this weekend and go back to a Patchbox image where I had not made the changes yet, to ensure I can reliably recreate this. Thank you for your help!

1 Like

I see I can’t edit my ‘step by step post’ anymore. This thread will be the messy one until I make a proper guide from base Patchbox.
#5 needs an update to go from manual to "boot up and ready to go!"
New 5:
edit /etc/jackdrc
comment out existing jack starting line
Put in a new one that references bluealsa (the bluetooth stack). This one works for me:
exec /usr/bin/jackd -r -d alsa -P bluealsa -n 3 -S -o 2 -X seq

I’ve edited the 5th step. Just in case, if you’d like to create a new topic with the finalized guide, I’ve copied the ‘markdown’ syntax of your post here: https://pastebin.com/10FkdFfX

Are the changes of jack.service to start after Bluetooth is up necessary to make this setup work?

So - I tried starting from scratch - fresh Patchbox OS, noting everything I do for a guide. But I have returned to a familiar place. I can start jack from the command line, but it does not see bluealsa at start. I have done the numbered steps above.
Oct 13 15:35:14 patchbox jackdrc[427]: creating alsa driver … bluealsa|-|1024|3|48000|0|2|nomon|swmeter|-|16bit
Oct 13 15:35:14 patchbox jackdrc[427]: ALSA lib bluealsa-ctl.c:980:(_snd_ctl_bluealsa_open) Couldn’t get BlueALSA PCM
Oct 13 15:35:14 patchbox jackdrc[427]: control open “bluealsa” (No such device)
Oct 13 15:35:16 patchbox jackdrc[427]: ALSA lib bluealsa-pcm.c:763:(_snd_pcm_bluealsa_open) Couldn’t get BlueALSA PCM:
Oct 13 15:35:16 patchbox jackdrc[427]: ALSA: Cannot open PCM device alsa_pcm for playback. Falling back to capture-onl
Oct 13 15:35:16 patchbox jackdrc[427]: Cannot initialize driver
Oct 13 15:35:16 patchbox jackdrc[427]: JackServer::Open failed with -1
Oct 13 15:35:16 patchbox jackdrc[427]: Failed to open server
Oct 13 15:35:16 patchbox systemd[1]: jack.service: Main process exited, code=exited, status=255/EXCEPTION
Oct 13 15:35:16 patchbox systemd[1]: jack.service: Failed with result ‘exit-code’.

I’m not sure what I am missing this time around. (Thankfully I do have a backup image that works!). I don’t know why it would matter but, my original Patchbox install used ORAC module. This time I just did the plain patchbox without any modules (and update/upgrade). I keep looking at the files for a typo and don’t see one yet. It’s frustrating that sudo systemctl restart jack works just fine.

*Also if I do get this working again - I’ll post the updated step-by-step in the pastebin you shared.

Did you add After = bluetooth to jack.service?

patch@patchbox:/etc/systemd/system $ cat jack.service
[Unit]
Description=JACK Server
After=sound.target bluetooth.target

[Service]
LimitRTPRIO=95
LimitMEMLOCK=infinity
Environment=JACK_NO_AUDIO_RESERVATION=1
Environment=JACK_PROMISCUOUS_SERVER=jack
ExecStart=/etc/jackdrc
User=jack
Group=jack

[Install]
WantedBy=multi-user.target

The service seems ok, did you install OS updates via apt-get?

I did - I remember doing that with my working config as well. I am missing something. Or there is a typo or bad copy/paste somewhere. I will try again this evening. We got it working before, it will work again!

1 Like