Gilzad is a music composer and a software developer. Most of his life he is busy chilling.


01 Aug - 31 Aug 2010
01 Sep - 30 Sep 2010
01 Nov - 30 Nov 2010
01 Dez - 31 Dez 2011
01 Nov - 30 Nov 2012
01 Feb - 28 Feb 2013
01 Mai - 31 Mai 2013
01 Jun - 30 Jun 2013
01 Mai - 31 Mai 2014
01 Apr - 30 Apr 2015


Gilzad's music
Sounding People
Soundevotion Compo
Musical Android


neueste Kommentare


Powered by Pivot - 1.40.7: 'Dreadwind' 
XML-Feed (RSS 1.0) 
XML: Atom Feed 

« Repairing the Terrate… | Home |

Automating the choice of Jackaudio's backend

13 04 15 - 00:47 So you've set up your machine to have a realtime/preemptive kernel, got it hooked up with your professional audio interface, but wait!
It's a notebook and you might need the internal soundcard as well.

Then it's nice to recognise your pro audio interface and preferably use it when it's there. The internal soundcard can serve as a fallback solution then.
Since most pro audio applications use the Jackaudio layer, we'll also take care of routing all consumer audio through jack. The reason behind that is: Your music application can't have low latency if it's not streaming through jackaudio or else it'll need to occupy your alsa driver in an exclusive mode. But let's do the autodetecting of your audio interface first. The following bash script takes care of doing this. I named it jack_autostart.sh and put it in /usr/bin. My audio interface is a firewire device and it has the product name "PHASE X24 FW". You can replace it by the name of your own device.


findDevice="PHASE X24 FW";
result=$(grep -r . /sys/bus/firewire/devices/* 2>/dev/null | grep "${findDevice}");

checkJack ()
 execStr="/usr/bin/jackd -T -P79 -t5000 -d${1} -r48000 -p128 -n2 &";
 result=$(ps aux | grep -v grep | grep jackd)
 if [ -z "$result" ];
  $execStr &
  sleep 5.3;
  /usr/bin/loop2jack.sh &
   echo "Jack's running on ${1} already. Stop Jack first.";
if [ ! -z "$result" ];
  echo "${findDevice} is connected. Routing Jack through FFADO.";
  checkJack "firewire";

  echo "${findDevice} is NOT connected. Routing Jack through ALSA.";
  checkJack "alsa";
result=$(ps aux | grep -v grep | grep xfce4-panel)
if [ ! -z
"$result" ];

 /usr/bin/qjackctl &

Here's some explanation:
Since my interface is a firewire device, I have to read the content from a device path instead of using tools such as "lspci" or "lsusb". Using the JuJu stack, this can be obtained from /sys/bus/firewire/devices/*.
The function checkJack() will be called from the code further below. It checks if jack is running on the desired device already by matching the whole execution string with all its parameters. This will save you some headache from avoidable errors. If all is good checkJack() will just invoke the execution string (that is start jack on the desired device) and in a further step it'll route the consumer audio through jack (loop2jack.sh, more on that later).
Now let's get to the part of the script right below checkJack(). Those lines will be executed immediately once you call this script. They basically check if the external interface could be found at all. If so, jack's backend will be the external audio interface. If not (say, you're on the move and only have your notebook), then jack's backend will be the internal soundcard. That's where all the automagic happens. Note that instead of
checkJack() "firewire"   you can call
checkJack() "alsa -d hw:2"   or
checkJack() "alsa -d hw:0"   if you have to use the alsa backend in both cases.
Finally, the last lines check if there's a desktop available by matching against a GUI process. On my system that's xfce4-panel. In that case the GUI tool for jack (qjackctl) will be started to offer some visual comfort on the desktop. qjackctl has an option to avoid several instances itself, that's why there's no further checks in my script. Now it's time to look at loop2jack.sh.

# script loop2jack, located in /usr/bin

pactl load-module module-jack-sink channels=2
pactl load-module module-jack-source channels=2
pacmd set-default-sink jack_out
pacmd set-default-source jack_in
exit 0

This script just starts pulseaudio and tells it to route all sounds to jack. I got these lines from here. That's how you can have all your consumer audio and low latency audio being played back through one device at the same time. By now, at the time of this writing you don't have to worry about stuttering sound from the flash plugin or other issues anymore. Pulseaudio really does a good job. Hence this version of loop2jack.sh has nothing in common with the original version anymore. That version was made for purely using alsa's dmix interface and alsa's loopback driver snd_loop and had served me a couple of painless years when neither alsa's jack-interface nor pulseaudio's jack-plugin were stable enough. Luckily those times have changed.
As a final note: If you decide to let your desktop automatically start jack_autostart.sh, make sure you disable your desktop's default way of starting up pulseaudio. Because once jack_autostart.sh calls loop2jack.sh, pulseaudio will be started anyway.

Trackback link:

Bitte aktiviere JavaScript in Deinem Browser, um eine Trackback URL generieren zu können.