29 OctALSA and Jack Cooperate using PulseAudio

One of my principal problems to solve, has been how to get ALSA and Jack to cooperate in both (a) the high-test live audio milieu, where latency has to be low, responsiveness immediate, and bloat minimal, and (b) the general audio milieu, where we have to cooperate with the myriad API needs of general multimedia applications.  Some will do ALSA only, some prefer other things.  What to do?

And for high-test audio, jackd has to be given total access to its audio hardware, without impediment.  Jackd uses ALSA as its driver set — but jackd must not be required to share its channels, or we are defeating our first purpose.  So the easiest method, really, is to have two sets of audio hardware, perhaps USB speakers for the general multimedia and a pro-audio sound card with RCA jacks for the high-test work.  But let’s say we don’t want to have the extra hardware lying around; we want everything to go to the PA!  What then?

Well, general multimedia applications usually do not use Jackd.  They usually want to talk either directly to ALSA as jackd does, or more and more often as an alternative, something else called PulseAudio.

But the simplest approach appears to try to configure everything to use ALSA.  There was an attempt to solve this problem called an “ALSA Jack plugin”, which focusses on the fact that jackd must not share its ALSA hardware output.  When this is implemented,  jackd application sound still runs appropriately — still straight out, App–>Jack–>ALSA–>hardware — but general non-jackd ALSA application sound then runs App–>ALSA–>Jack–>ALSA–>hardware. There is no theoretical reason why this shouldn’t work very well; unfortunately the ALSA Jack plug-in has bugs. In my testing, it worked fine for several hours of system uptime, but then lost sync or cohesion or something, and began introducing static into the stream, gradually increasing in latency and xruns and quality decreasing over time. It would return to good behavior after reboot, and then after hours of either use or idleness, begin acting up again.

I would love to see the ALSA jack plug-in fixed — I don’t like adding layers to my systems — but we should use what works best.  I could just add a set of USB speakers…but meanwhile, while I am waiting to be healed from my bad cold, perhaps I can get this thing to work :-)   It appears I need to do what the desktop distros are doing:  I need to add PulseAudio.

PulseAudio is a layer, a program which sits quietly and takes up resources as needed.  I am surprised and happy to report that it sits most quietly, and has not disturbed any of the many apps I need.  It has also served its purpose quite well:  it is the “glue” which is permitting many apps to understand how to use the audio on my systems.  It would be rather unhelpful to try it on a box with 256M RAM — it seems to be using about that much RAM doing nothing right now — but as I have 4G here, that is OK.  If you have 1G RAM or less, or a slow CPU or slower RAM, go buy yourself some USB speakers and call it done :-)

But continuing.  This post is written for Debian Testing and its very close relatives.  It  presumes that you already have a working Jack audio setup, that you have identified the alphanumeric ALSA name of your audio hardware, and that Jack is using that alphanumeric name in its device configuration.  Here is a post containing info about alphanumeric names.

Commencing:

First you’ll need to get the PulseAudio packages. pulseaudio, pulseaudio-module-jack, and pulseaudio-utils, plus all dependencies.  You’ll also need the ‘sudo’ package.

Now get to root privileges (su or sudo -s), go to /etc/pulse, create directory “originals” and copy everything into there as a backup.  Then:

system.pa and default.pa should be made the same, should both contain:

#!/usr/bin/pulseaudio -nF
#
load-module module-jack-sink
load-module module-jack-source
load-module module-native-protocol-unix
load-module module-stream-restore
load-module module-default-device-restore
load-module module-rescue-streams
load-module module-suspend-on-idle
.ifexists module-gconf.so
.nofail
load-module module-gconf
.fail
.endif
.ifexists module-x11-publish.so
.nofail
load-module module-x11-publish
.fail
.endif

daemon.conf should remain the same, except for one line, originally reading:

; daemonize = no

Change it thusly:

daemonize = yes

The above will attempt run Pulse in an appropriate fashion, at boot.  But if you reboot right now, it will try to latch onto a Jack process which isn’t there.  So we need to run Jack at boot too.  With Jack (perhaps through qjackctl) running and working properly, do this in a command shell:

ps aux | grep jackd

On my machine, I get:

jeb       3777  1.8  0.7  29388 29404 ?        S<Lsl 09:58   0:59 /usr/bin/jackd -Z -R -t1000 -dalsa -dhw:HD2 -r96000 -p128 -n2 -s -m -Xseq -H -M
jeb       7139  0.0  0.0   3188   728 pts/1    R+   10:52   0:00 grep jackd

The info I need is therefore:

/usr/bin/jackd -Z -R -t1000 -dalsa -dhw:HD2 -r96000 -p128 -n2 -s -m -Xseq -H -M

We need to put this into /etc/rc.local.  My /etc/rc.local reads:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

sudo -u jeb taskset -c 1 nice --adjustment=-19 /usr/bin/jackd \
  -Z -R -t1000 -dalsa -dhw:HD2 -r96000 -p128 -n2 -s -m -Xseq -H -M

exit 0

‘sudo’ comes in at this point, because Jack has to be run as the user I log in with, for permissions.  I am also using taskset for CPU affinity (I have a quad, -c 1 means really core #2), and nice -19 is near-absolute-max priority (I don’t like -20, the kernel should have some leeway).

The last step is relatively simple.  The following is the contents of /home/jeb/.asoundrc:

pcm.!default {
   type pulse
}

ctl.!default {
   type pulse
}

Now reboot. It is working for me rather nicely. Jack apps run well App–>Jack–>ALSA–>hardware, at absolute minimum latency; ALSA-only apps run App–>ALSA–>PulseAudio–>Jack–>ALSA–>hardware; and those apps that can do the PulseAudio API natively — a growing number — run App–>PulseAudio–>Jack–>ALSA–>hardware.

11 Responses to “ALSA and Jack Cooperate using PulseAudio”

  1. freqrush says:

    I followed this howto, and it doesn’t work better than the ALSA jack plug-in for me.

    Here’s the error I get in QJackCtl, while watching a Google video in firefox, (AVLinux2rc2.2):

    subgraph starting at PulseAudio JACK Source timed out (subgraph_wait_fd=21, status=0, state=Finished, pollret=0 revents=0×0)

    after which the connection breaks and sound is gone. The ALSA jack plug-in keeps working, only starts to crackle the sound after a while … :p

    Anyway, thanks for the nice howto!

  2. That is interesting. Have you tried updating Pulse and ALSA? Also, what is your hardware, especially RAM and CPU? You should probably turn off ACPI at boot just for starters. Can you tell me what your frames per period is set to, in the Jackd configuration?

  3. Josh says:

    Just as an aside, I recently learned that Xubuntu (the Ubuntu derivative that uses the XFCE desktop environment) does not install PulseAudio at all. Might be a good option for those who want Ubuntu but don’t want to fool with getting around PA.

  4. denDED says:

    чтобы добавлять свои статьи, обязательно ли регистрироватся?

  5. You can take a look here to solve your firefox problem: http://en.gentoo-wiki.com/wiki/JACK#Firefox

    It is for gentoo, but it must not be dificult to adapt it to another distro.

  6. Indeed, registration is mandatory for posting.

  7. What kind of sound hardware and CPU do you have?

  8. freqrush says:

    My soundcard is an M-Audio Delta 1010 , CPU is AMD Phenom X3 , RAM is 3 Gig DDR2 667 dual channel.
    Pulseaudio version is 0.9.19-2 , I’m upgrading the alsa packages right now to see if it improves anything …
    @Dominique: Some packages listed there are not available in Synaptic, so I’m not sure about how difficult it is to adapt the howto. Thanks anyway.

  9. iro4ka says:

    Dear Author linuxlive.joshuacorps.org !
    It exclusively your opinion

  10. No, the opinions in this post and all the others on this site are not exclusively mine.

  11. whammo says:

    It has been pointed out that Xubuntu does not implement Pulse, I’d like to point out Kubuntu does not either. Pretty sure any distro that does not depend on current Gnome will not force it.

    It’s nice to try to find the silver lining, but for matters of latency in realtime, there can be no compromise.

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment