QjackCtl script for use with jackdbus and pulseaudio

Optimize your system for ultimate performance.

Moderators: MattKingUSA, khz

Post Reply
KoBass
Posts: 2
Joined: Tue Jan 26, 2021 6:56 pm

QjackCtl script for use with jackdbus and pulseaudio

Post by KoBass »

I use Qjackctl/jackdbus together with Pulseaudio (pa) and you probably guess it. It causes some troubles. Most of the time it's because after starting or stopping jack, pa clients stop working. So I had a deep dive into the pulse docs and pulse CLI tools to get it working.

The first thing I found, is that the in pa the jack-sink/source modules are "only" virtual devices. They do not replace the original sound card, they only add the jack-sink/source. The sound card that is now used by jackdbus is still fully enabled in pa, but blocked by jack. Pa clients connected to it will block and no audio is played. Once the clients are manually set to jack-sink/source, they work and pa will remember the connection

This is the second thing. Pa will reconnect to one of the rememdered sinks or sources, but you will have to do it at least once manually. Or it will use the fallback device, or something you don't expect.

What do I expect?
When the jack modules are loaded the existing clients of the sound card should be moved to the jack-sink/source and moved back when jack is stopped. New pa clients should not try to connect to the blocked audio card.

I have created a script that will do the trick. It uses the pa module module-null-sink and creates a transit device to host the existing clients while the jack modules are loaded or unloaded. The output profile of the audio card is set to off when jack is started, and set to a default when stopped. This way, new clients will not try to connect to the blocked audio card. The pa internal logic for reconnecting clients is bypassed by temporarily unloading module-switch-on-connect.
Have a look at the main functions:

Code: Select all

function on_startup() {
	get_jack_card
	load_transit
	move_clients "sink" $jack_card_sink_id $transit_sink_id
	move_clients "source" $jack_card_source_id $transit_source_id
	pacmd set-card-profile $jack_card_id off
}

function after_startup() {
	get_transit
	get_jack_audio
	move_clients "sink" $transit_sink_id $jack_audio_sink_id
	move_clients "source" $transit_source_id $jack_audio_source_id
	unload_transit
}

function on_shutdown() {
	load_transit
	get_jack_audio
	move_clients "sink" $jack_audio_sink_id $transit_sink_id
	move_clients "source" $jack_audio_source_id $transit_source_id
}

function after_shutdown() {
	get_transit
	get_jack_card
	pacmd set-card-profile $jack_card_id $jack_card_profile
	move_clients "sink" $transit_sink_id $jack_card_sink_id
	move_clients "source" $transit_source_id $jack_card_source_id
	unload_transit
}
The script assumes that the pa modules module-jackdbus-detect and module-switch-on-connect are loaded.
Some variables for the sound card have to be set manually in the script, because I did not find an easy way to map the sound from QjackCTL (can be passed with %i) to the pa card.

The commands to find the values for the variables are in the comments of the script. Use them before starting jack.
The settings to be done:

Code: Select all

# Requirements:
# pulse audio modules module-jackdbus-detect and module-switch-on-connect are loaded:
# pacmd list-modules |egrep 'module-jackdbus-detect|module-switch-on-connect'

# pacmd list-sinks | egrep 'index: |card: '
jack_card_name="<alsa_card.usb-Focusrite_Scarlett_2i2_USB-00>"

# pacmd list-cards | egrep 'index: |name: |active profile: '
# '<' '>' not included
jack_card_profile="output:analog-stereo+input:analog-stereo"

# pacmd list-sinks | egrep 'index: |name: '
jack_card_sink_name="<alsa_output.usb-Focusrite_Scarlett_2i2_USB-00.analog-stereo>"

# pacmd list-sources | egrep 'index: |name: '
jack_card_source_name="<alsa_input.usb-Focusrite_Scarlett_2i2_USB-00.analog-stereo>"

# End requirements
The script has one argument for the phase it is used:

Code: Select all

jack-pulse-switcher.sh {on-startup|after-startup|on-shutdown|after-shutdown}
QjackCtl is configured like this:
qjackctl.png
qjackctl.png (34.42 KiB) Viewed 801 times

If you use QjackCtl with jackdbus, and Pulseaudio with the modules module-jackdbus-detect and module-switch-on-connect, you might give it a try.
jack-pulse-switcher.sh.txt
(3.48 KiB) Downloaded 81 times
User avatar
bluebell
Established Member
Posts: 1923
Joined: Sat Sep 15, 2012 11:44 am
Location: Saarland, Germany
Has thanked: 112 times
Been thanked: 119 times

Re: QjackCtl script for use with jackdbus and pulseaudio

Post by bluebell »

I don't stop jackd at all while I am logged in.

If you restart it to change the bufsize (I do it often) then you can change it on the fly instead with jack_bufsize or a little graphical tool that I wrote:

http://marzen.de/Holger/jack_bufsize/

Linux – MOTU UltraLite AVB – Qtractor – http://suedwestlicht.saar.de/

KoBass
Posts: 2
Joined: Tue Jan 26, 2021 6:56 pm

Re: QjackCtl script for use with jackdbus and pulseaudio

Post by KoBass »

bluebell wrote: Sat Jan 30, 2021 8:54 am you can change it on the fly instead with jack_bufsize
Nice to know. I'm used to stick with one buffsize because I don't want to restart jack.
I did some checks with QjackCtl from my distro which is version 0.5.0. It's not able to change the buffsize on the fly.
But the recent appimage of QjackCtl with the version 0.9.0 can dot it. :)
Post Reply