Difference between revisions of "JACK Audio Connection Kit"

From ArchWiki
Jump to navigation Jump to search
(Introduction to dbus control of Jack2)
(Adjust the use of terminology so that it's correct)
(231 intermediate revisions by 60 users not shown)
Line 1: Line 1:
{{stub}}
+
[[Category:Sound]]
 +
[[fr:Jack]]
 +
[[es:JACK Audio Connection Kit]]
 +
[[ja:JACK Audio Connection Kit]]
 +
{{Related articles start}}
 +
{{Related|Sound system}}
 +
{{Related|Professional audio}}
 +
{{Related articles end}}
 +
From [[Wikipedia:JACK Audio Connection Kit]]:
 +
:JACK Audio Connection Kit (or JACK; a recursive acronym) is a professional sound server daemon that provides real-time, low-latency connections for both audio and MIDI data between applications that implement its API.
  
If you know anything about JACK, please expand this article. I've uninstalled it, so I can't really expand it any further.
+
==Installation==
 +
In order for JACK to work properly, your user needs to be [[Users and groups#Group management|added]] to the {{ic|realtime}} group for access to higher ulimits defined in {{ic|/etc/security/limits.d/99-realtime-privileges.conf}} (provided by the {{Pkg|realtime-privileges}} package), which is needed for realtime audio processing.
 +
{{Note|You need to manually add your user to the {{ic|realtime}} group even if you're using logind, since logind just handles access to direct hardware.}}
  
=Install=
+
There are two JACK implementations, see [https://github.com/jackaudio/jackaudio.github.com/wiki/Q_difference_jack1_jack2 this comparison] for the difference between the two. In short, Jack 1 and Jack 2 are equivalent implementations of the same protocol. Programs compiled against Jack 1 will work with Jack 2 without recompile (and vice versa).  
==Multiprocessor implementation (soon: JACK2)==
 
[http://www.grame.fr/~letz/jackdmp.html Here's] a mp version of JACK which will also be used as base for JACK2.  
 
  
[[AUR]] package: [http://aur.archlinux.org/packages.php?ID=22488 jack-audio-connection-kit-mp]
+
===JACK2===
 +
'''JACK2''' is a C++ implementation with SMP support. [[Install]] it with the {{Pkg|jack2}} package. If you are on a 64-bit installation and need to run 32-bit applications that require JACK, also install the {{Pkg|lib32-jack2}} package from the [[multilib]] repository.
  
You can compile Jack2 with the new dbus-based control system or the 'classic' interface. Which one you have depends on whether you compiled Jack2 with the --dbus configure option or not.
+
====JACK2 D-Bus====
 +
JACK2 with [[D-Bus]] can be installed via {{Pkg|jack2-dbus}}. It is the same as the {{Pkg|jack2}} package but does not provide the legacy "jackd" server.
  
Jack2 without --dbus executes similarly to Jack1. With --dbus, control is done using the jack_control utility. Run `jack_control` to see what it does. The important commands are listed below:-
+
It is controlled by the {{ic|jack_control}} utility. The jack_control utility requires you to also install the {{pkg|python2-dbus}} package as well.
 +
 
 +
The important commands are listed below:
 
  jack_control start  -  starts the jack server
 
  jack_control start  -  starts the jack server
 
  jack_control stop  - stops the jack server
 
  jack_control stop  - stops the jack server
Line 18: Line 31:
 
  jack_control dps period 256  -  set the driver parameter period to 256
 
  jack_control dps period 256  -  set the driver parameter period to 256
  
You may also need to play with the driver parameters nperiods and rate.
+
===JACK===
 +
'''JACK''' uses a C API and supports more than one soundcard on Linux (plus MIDI). [[Install]] it with the {{pkg|jack}} package. If you are on a 64-bit installation and need to run 32-bit applications that require JACK, also install the {{Pkg|lib32-jack}} package from the [[multilib]] repository.
 +
 
 +
===GUI===
 +
 
 +
* {{App|Cadence|Set of tools useful for audio production. It performs system checks, manages JACK, calls other tools and make system tweaks.|https://kxstudio.linuxaudio.org/Applications:Cadence|{{Pkg|cadence}}}}
 +
* {{App|Patchage|Modular patch bay for audio and MIDI systems based on JACK and ALSA.|https://drobilla.net/software/patchage|{{Pkg|patchage}}}}
 +
* {{App|PatchMatrix|JACK patch bay in flow matrix style.|https://git.open-music-kontrollers.ch/lad/patchmatrix/about/|{{Pkg|patchmatrix}}}}
 +
* {{App|[[Wikipedia:Qjackctl|QjackCtl]]|Simple Qt application to control the JACK sound server daemon.|https://qjackctl.sourceforge.io/|{{Pkg|qjackctl}}}}
 +
 
 +
==Basic Configuration==
 +
 
 +
===Overview===
 +
The right configuration for your hardware and application needs depends on several factors.  Your sound card and CPU will heavily affect how low of latency you can achieve when using JACK.
 +
 
 +
The mainline Linux kernel now supports realtime scheduling, so using a patched kernel is no longer necessary.  However, {{AUR|linux-rt}} in the AUR is a patched kernel that has some extra patches that can help to get lower latencies.
 +
 
 +
===A shell-based example setup===
 +
The D-Bus edition of JACK2 can make startup much easier.  Formerly, QjackCtl was used to start it, or a daemonizer was used, or some other method.  But using {{Pkg|jack2-dbus}}, JACK2 can be easily started and configured via a shell script.
 +
 
 +
Create a shell script that can be executed at X login:
 +
 
 +
{{hc|start_jack.sh|
 +
#!/bin/bash
 +
 
 +
jack_control start
 +
jack_control ds alsa
 +
jack_control dps device hw:HD2
 +
jack_control dps rate 48000
 +
jack_control dps nperiods 2
 +
jack_control dps period 64
 +
sleep 10
 +
a2jmidid -e &
 +
sleep 10
 +
qjackctl &
 +
}}
 +
 
 +
The above will start a working JACK instance which other programs can then utilize.  Details of each line follow.  When discovering your own best configuration, it is helpful to do trial and error using QjackCtl's GUI with a non-D-Bus JACK2 version.
 +
 
 +
====Details of the shell-based example setup====
 +
 
 +
jack_control start
 +
Starts JACK if it is not already started.
 +
jack_control ds alsa
 +
Sets JACK to use the ALSA driver set.
 +
jack_control dps device hw:HD2
 +
Sets JACK to use ALSA-compatible sound card named HD2.  One can find the names with {{ic|cat /proc/asound/cards}}.  Most ALSA tutorials and default configurations use card numbers, but this can get confusing when external MIDI devices are in use; names make it easier.
 +
jack_control dps rate 48000
 +
Sets JACK to use 48000 khz sampling.  Happens to work very well with this card.  Some cards only do 44100, many will go much higher.  The higher you go, the lower your latency, but the better your card and your CPU have to be, and software has to support this as well.
 +
jack_control dps nperiods 2
 +
Sets JACK to use 2 periods.  2 is right for motherboard, PCI, PCI-X, etc.; 3 for USB.
 +
jack_control dps period 64
 +
Sets JACK to use 64 frames per period.  Lower is less latency, but the setting in this script gives 2.67 ms latency, which is nicely low without putting too much stress on the particular hardware this example was built for.  If a USB sound system were in use it might be good to try 32.  Anything less than 3-4 ms should be fine for realtime synthesis and/or FX, 5 ms is the smallest a human being can detect.  QjackCtl will tell you how you are doing; at no-load, which means no clients attached, you will want a max of 3-5% CPU usage, and if you cannot get that without xruns (the red numbers which mean the system cannot keep up with the demands), you will have to improve your hardware.
 +
sleep 10
 +
Wait for the above to settle.
 +
a2jmidid -e &
 +
Start the ALSA-to-JACK MIDI bridge.  Good for mixing in applications which take MIDI input through ALSA but not JACK.
 +
sleep 10
 +
Wait for the above to settle.
 +
qjackctl &
 +
Load QjackCtl.  GUI configuration tells it to run in the system tray.  It will pick up the JACK session started by D-Bus just fine, and very smoothly too.  It maintains the patchbay, the connections between these applications and any other JACK-enabled apps to be started manually.  The patchbay is set up using manual GUI, but connections pre-configured in the patchbay are automatically created by QjackCtl itself when apps are started.
 +
 
 +
===A GUI-based example setup===
 +
This example setup utilizes a more GUI focused configuration and management of JACK
 +
 
 +
* Install {{Pkg|jack2-dbus}}.
 +
* Install {{Pkg|qjackctl}}, and tell your GUI window/desktop system to run it at startup.
 +
* Make sure QjackCtl is told to:
 +
** use the D-Bus interface,
 +
** run at startup,
 +
** save its configuration to the default location,
 +
** start the JACK audio server on application startup,
 +
** enable the system tray icon, and
 +
** start minimized to system tray.
 +
* Reboot.
 +
* After logging in, you will see QjackCtl in your system tray. Left-click on it.
 +
* Tweak settings in the QjackCtl GUI to lower latency.  The Frame Size, Frame Buffer, and Bitrate settings all affect latency.  Larger frame sizes lower latency, lower frame buffers lower latency, and higher bitrate settings lower latency, but all increase load on the sound card and your CPU.  A Latency of about ~5ms is desirable for direct monitoring of instruments or microphones, as the latency begins to become perceptible at higher latencies.
 +
 
 +
===Playing nice with ALSA===
 +
{{Out of date|A {{ic|pcm.jack}} port is already defined since {{pkg|alsa-plugins}} 1.1.7 (50-jack.conf)|Talk:JACK_Audio_Connection_Kit}}
 +
 
 +
To allow Alsa programs to play while jack is running you must install the jack plugin for alsa with {{Pkg|alsa-plugins}}.
 +
 
 +
And enable it by editing (or creating) /etc/asound.conf (system wide settings) to have these lines:
 +
{{bc|<nowiki>
 +
# convert alsa API over jack API
 +
# use it with
 +
# % aplay foo.wav
 +
 
 +
# use this as default
 +
pcm.!default {
 +
    type plug
 +
    slave { pcm "jack" }
 +
}
 +
 
 +
ctl.mixer0 {
 +
    type hw
 +
    card 1
 +
}
 +
 
 +
# pcm type jack
 +
pcm.jack {
 +
    type jack
 +
    playback_ports {
 +
        0 system:playback_1
 +
        1 system:playback_2
 +
    }
 +
    capture_ports {
 +
        0 system:capture_1
 +
        1 system:capture_2
 +
    }
 +
}</nowiki>}}
 +
 
 +
You need not restart your computer or anything.  Just edit the alsa config files, start up jack, and there you go...
 +
 
 +
Remember to start it as a '''user'''. If you start it with {{ic|jackd}} -d alsa" as user X, it will not work for user Y.
 +
 
 +
Another approach, using ALSA loopback device (more complex but probably more robust), is described in [https://alsa.opensrc.org/Jack_and_Loopback_device_as_Alsa-to-Jack_bridge this article].
 +
 
 +
=== GStreamer ===
 +
 
 +
GStreamer requires the {{pkg|gst-plugins-good}} package to work with JACK, which contains the jackaudiosink plugin that adds JACK playback support.
 +
 
 +
Further information (outdated): http://jackaudio.org/faq/gstreamer_via_jack.html
 +
 
 +
=== PulseAudio ===
 +
If you need to keep {{Pkg|pulseaudio}} installed (in the event it is required by other packages, like {{Pkg|gnome-settings-daemon}}), you may want to prevent it from spawning automatically with X and taking over from JACK.
 +
 
 +
Edit {{ic|/etc/pulse/client.conf}}, uncomment "autospawn" and set it to "no":
 +
;autospawn = yes
 +
autospawn = no
 +
 
 +
''If you want both to play along, see: [[PulseAudio/Examples#PulseAudio through JACK]]''
 +
 
 +
=== Firewire ===
 +
In order to prevent ALSA from messing around with your firewire devices you have to blacklist all firewire related kernel modules. This also prevents PulseAudio from using firewire. Create the following file:
 +
 
 +
{{hc|/etc/modprobe.d/alsa-no-jack.conf|
 +
blacklist snd-fireworks
 +
blacklist snd-bebob
 +
blacklist snd-oxfw
 +
blacklist snd-dice
 +
blacklist snd-firewire-digi00x
 +
blacklist snd-firewire-tascam
 +
blacklist snd-firewire-lib
 +
blacklist snd-firewire-transceiver
 +
blacklist snd-fireface
 +
blacklist snd-firewire-motu
 +
}}
 +
 
 +
''The list of modules is the most recent available at the time of writing at [https://github.com/takaswie/snd-firewire-improve Alsa Firewire Improve Repository].''
 +
 
 +
Now you can unload your loaded firewire modules or reboot.
 +
 
 +
=== Network / remote audio ===
 +
 
 +
JACK can be configured to send audio data over a network to a "master" machine, which then outputs the audio to a physical device.  This can be useful to mix audio from a number of "slave" computers without requiring additional cables or hardware mixers, and keeping the audio path digital for as long as possible (as hardware mixers with digital inputs are very rare).
 +
 
 +
The configuration is very simple, however it requires a network that supports multicast traffic (i.e. IGMP snooping must be enabled on managed network switches), and it requires all machines be running the same JACK major version (JACK1 or JACK2) as the protocols are not interoperable between versions.  For JACK2, the {{ic|netmanager}} module must be loaded:
 +
 
 +
{{bc|<nowiki>
 +
master$ jack_load netmanager -i -c
 +
</nowiki>}}
 +
 
 +
The {{ic|-i -c}} option tells the netmanager to automatically map any incoming connections to the default audio device.  Without this, each incoming connection would have to be manually mapped on each connection.  You can use {{ic|-i -h}} instead to see all available options, however note that the options are printed in the {{ic|jackd}} server output, the {{ic|jack_load}} command will not show anything.
 +
 
 +
On the client, JACK must be started in network mode:
 +
 
 +
{{bc|<nowiki>
 +
slave$ jackd -d net
 +
</nowiki>}}
 +
 
 +
The two machines will connect and on the master the new audio source will be visible:
 +
 
 +
{{bc|<nowiki>
 +
master$ jack_lsp
 +
system:playback_1
 +
system:playback_2
 +
remotehost:from_slave_1
 +
remotehost:from_slave_2
 +
</nowiki>}}
 +
 
 +
If you passed the {{ic|-c}} option to {{ic|jack_load}} as above, then the remote system will now be able to play audio.
 +
 
 +
==MIDI==
  
==JACK==
+
JACK can handle one soundcard very well, and an arbitrary number of MIDI devices (connected e.g. via USB).
Alternatively, there is the normal version from the extra repository, install it with:
+
If you start JACK and want to use a MIDI keyboard or a synthesizer or some other pure MIDI device, you have to start JACK with a proper soundcard (one that actually outputs or inputs PCM sound).
pacman -S jack-audio-connection-kit
+
As soon you have done that, you can connect the MIDI device. E.g. with QjackCtl ({{pkg|qjackctl}}), you click on the connect button and you will find your device listed under JACK-MIDI or ALSA-MIDI, depending on the driver.
  
=== Jack for a multi-user system ===
+
For JACK-MIDI, you may want to set the '''MIDI Driver''' to '''seq''' or '''raw''' in QjackCtl ''Setup > Settings''. This should make your MIDI device appear under the ''MIDI'' tab. You can also change the name of the client (from a generic "midi_capture_1" to something more descriptive), if you enable ''Setup > Display > Enable client/port aliases'' and then ''Enable client/port aliases editing (rename)''.
So, you have a descent multiuser system as it was designed more than 20 years ago, and now some developers decided that sound is only for a mono-user system... No I can not believe it !
 
  
<b>Before following the below instructions, please note that there is a security risk to any service running as root, and, more importantly, the developers for jack do not test it for running as root. In other words, it could eat your babies, data, or both.</b>
+
For ALSA-MIDI, make sure to turn on '''Enable ALSA Sequencer support''' in QjackCtl ''Setup > Misc''. This will add the ''ALSA'' tab in QjackCtl ''Connect'' window where your MIDI controller will show up.
  
Fortunately some time ago someone convinced the developers to allow jack to run as a system wide daemon. Here is the procedure to follow:
+
{{Note| {{pkg|jack2}} does not come with bridging support for legacy ALSA MIDI only applications. Therefore {{pkg|a2jmidid}} is required [https://github.com/jackaudio/jack2/issues/362 until upstream achieves feature parity on this].}}
 +
For bridging ALSA-MIDI to JACK-MIDI, you may consider using a2jmidid ({{Pkg|a2jmidid}}). The following command will export all available ALSA MIDI ports to JACK MIDI ports:
 +
$ a2jmidid -e
 +
They will be visible in QjackCtl under the ''MIDI'' tab labelled "a2j" client.
 +
You can automate starting of a2jmidid by adding to QjackCtl ''Setup > Options > Execute script after Startup'': {{ic|/usr/bin/a2jmidid -e &}}
 +
{{note|When connecting MIDI keyboard controllers in QjackCtl, make sure to ''Expand All'' first and connect the desired ''Output Ports'' (below the ''Readable Clients'') to the ''Input Ports'' (below the ''Writable Clients''). As a shortcut, if you select a writable client instead of individual ports as your destination, it should connect all its currently displayed output ports underneath.}}
  
'''Create a <tt>/etc/profile.d/jack.sh</tt> file''' containing:
+
*'''Q:''' What is the difference between JACK-MIDI and ALSA-MIDI?
export JACK_PROMISCUOUS_SERVER=""
+
*'''A:''' The former has improved timing and sample accurate MIDI event alignment. It extends or may even replace the latter but at this point they both co-exist.
  
'''Replace <tt>/etc/rc.d/jack-audio-connection-kit</tt>''' with the following content
+
To install some M-Audio MIDI keyboards, you will need the firmware package {{AUR|midisport-firmware}} in the [[AUR]]. Also, the snd_usb_audio module has to be available.
<pre>
+
For more information about specific USB MIDI devices, see http://alsa.opensrc.org/USBMidiDevices.
#!/bin/bash
 
  
. /etc/rc.conf
+
==Troubleshooting==
. /etc/rc.d/functions
+
==="Cannot lock down memory area (Cannot allocate memory)" message on startup===
  
# source application-specific settings
+
See [[Realtime process management#Configuring PAM]] and ensure that the user is in the {{ic|realtime}} [[user group]].
[ -f /etc/conf.d/jack-audio-connection-kit ] && . /etc/conf.d/jack-audio-connection-kit
 
  
PID=`pidof -o %PPID /usr/bin/jackd`
+
===jack2-dbus and qjackctl errors===
 +
Still having the "Cannot allocate memory" and/or "Cannot connect to server socket err = No such file or directory" error(s) when pressing qjackctl's start button (assuming that you have package jack2-dbus installed) ?
  
[ -n "$JACKUSER" ] && HOME="/home/$JACKUSER"
+
Please delete {{ic|~/.jackdrc}}, {{ic|~/.config/jack/conf.xml}}, {{ic|~/.config/rncbc.org/QjackCtl.conf}}. Kill ''jackdbus'' and restart from scratch :)
[ -z "$JACK_PARAMS" ] && JACK_PARAMS=$(sed 's:/usr/bin/jackd ::' $HOME/.jackdrc)
+
(Thanks to nedko)
  
case "$1" in
+
Also try running
  start)
+
$ fuser /dev/snd/*
    stat_busy "Starting JACK"
+
and check the resulting PID's with
    if [ -z "$PID" ]; then
+
$ ps ax | grep [PID here]
      if [ -n "$JACKUSER" ]; then
+
This will hopefully show the conflicting programs.
        su - $JACKUSER -c 'export JACK_PROMISCUOUS_SERVER="" && . /etc/conf.d/jack-audio-connection-kit && umask 0000 && /usr/bin/jackd $JACK_PARAMS &> /dev/null &'
 
      else
 
export JACK_PROMISCUOUS_SERVER=""
 
umask 0000
 
        /usr/bin/jackd $JACK_PARAMS &> /dev/null &
 
      fi
 
    fi
 
  
    if [ ! -z "$PID" -o $? -gt 0 ]; then
+
==="ALSA: cannot set channel count to 1 for capture" error in logs===
      stat_fail
+
Change ALSA input and output channels from 1 to 2
    else
 
      add_daemon jack
 
      stat_done
 
    fi
 
    ;;
 
  stop)
 
    stat_busy "Stopping JACK"
 
    [ ! -z "$PID" ]  && kill $PID &> /dev/null
 
    if [ $? -gt 0 ]; then
 
      stat_fail
 
    else
 
      rm_daemon jack
 
      stat_done
 
    fi
 
    ;;
 
  restart)
 
    $0 stop
 
    sleep 1
 
    $0 start
 
    ;;
 
  *)
 
    echo "usage: $0 {sta
 
</pre>
 
  
Where my '''<tt>/etc/conf.d/jack-audio-connection-kit</tt>''' is
+
===Crackling or pops in audio===
<pre>
+
Your CPU or sound card is too weak to handle your settings for JACK.  Lower the bitrate, lower the frame size, and raise the frame period in small increments until crackling stops.
# Configuration for starting JACK at boot
 
  
# Uncomment this to run as user (recommended)
+
===Problems with specific applications===
#JACKUSER="root"
+
====VLC - no audio after starting JACK====
 +
Run VLC and change the following menu options:
 +
* Tools > Preferences
 +
* Show settings: All
 +
* Audio > Output modules > Audio output module: JACK audio output
 +
* Audio > Output modules > JACK: Automatically connect to writable clients (enable)
  
# Uncomment this to not source ~/.jackdrc
+
==See also==
JACK_PARAMS="-R -P89 -dalsa -dhw:1 -r48000 -p512 -n3"
 
</pre>
 
  
=Related Articles=
+
* [https://github.com/jackaudio/jackaudio.github.com/wiki/Q_difference_jack1_jack2 Differences between JACK 1 and JACK2]
[[Pro Audio]]
+
* [http://jackaudio.org/faq/ JACK FAQ]

Revision as of 05:15, 30 April 2019

From Wikipedia:JACK Audio Connection Kit:

JACK Audio Connection Kit (or JACK; a recursive acronym) is a professional sound server daemon that provides real-time, low-latency connections for both audio and MIDI data between applications that implement its API.

Installation

In order for JACK to work properly, your user needs to be added to the realtime group for access to higher ulimits defined in /etc/security/limits.d/99-realtime-privileges.conf (provided by the realtime-privileges package), which is needed for realtime audio processing.

Note: You need to manually add your user to the realtime group even if you're using logind, since logind just handles access to direct hardware.

There are two JACK implementations, see this comparison for the difference between the two. In short, Jack 1 and Jack 2 are equivalent implementations of the same protocol. Programs compiled against Jack 1 will work with Jack 2 without recompile (and vice versa).

JACK2

JACK2 is a C++ implementation with SMP support. Install it with the jack2 package. If you are on a 64-bit installation and need to run 32-bit applications that require JACK, also install the lib32-jack2 package from the multilib repository.

JACK2 D-Bus

JACK2 with D-Bus can be installed via jack2-dbus. It is the same as the jack2 package but does not provide the legacy "jackd" server.

It is controlled by the jack_control utility. The jack_control utility requires you to also install the python2-dbus package as well.

The important commands are listed below:
jack_control start  -  starts the jack server
jack_control stop  - stops the jack server
jack_control ds alsa  -  selects alsa as the driver (backend)
jack_control eps realtime True  -  set engine parameters, such as realtime
jack_control dps period 256  -  set the driver parameter period to 256

JACK

JACK uses a C API and supports more than one soundcard on Linux (plus MIDI). Install it with the jack package. If you are on a 64-bit installation and need to run 32-bit applications that require JACK, also install the lib32-jack package from the multilib repository.

GUI

  • Cadence — Set of tools useful for audio production. It performs system checks, manages JACK, calls other tools and make system tweaks.
https://kxstudio.linuxaudio.org/Applications:Cadence || cadence
  • Patchage — Modular patch bay for audio and MIDI systems based on JACK and ALSA.
https://drobilla.net/software/patchage || patchage
  • PatchMatrix — JACK patch bay in flow matrix style.
https://git.open-music-kontrollers.ch/lad/patchmatrix/about/ || patchmatrix
  • QjackCtl — Simple Qt application to control the JACK sound server daemon.
https://qjackctl.sourceforge.io/ || qjackctl

Basic Configuration

Overview

The right configuration for your hardware and application needs depends on several factors. Your sound card and CPU will heavily affect how low of latency you can achieve when using JACK.

The mainline Linux kernel now supports realtime scheduling, so using a patched kernel is no longer necessary. However, linux-rtAUR in the AUR is a patched kernel that has some extra patches that can help to get lower latencies.

A shell-based example setup

The D-Bus edition of JACK2 can make startup much easier. Formerly, QjackCtl was used to start it, or a daemonizer was used, or some other method. But using jack2-dbus, JACK2 can be easily started and configured via a shell script.

Create a shell script that can be executed at X login:

start_jack.sh
#!/bin/bash

jack_control start
jack_control ds alsa
jack_control dps device hw:HD2
jack_control dps rate 48000
jack_control dps nperiods 2
jack_control dps period 64
sleep 10
a2jmidid -e &
sleep 10
qjackctl &

The above will start a working JACK instance which other programs can then utilize. Details of each line follow. When discovering your own best configuration, it is helpful to do trial and error using QjackCtl's GUI with a non-D-Bus JACK2 version.

Details of the shell-based example setup

jack_control start

Starts JACK if it is not already started.

jack_control ds alsa

Sets JACK to use the ALSA driver set.

jack_control dps device hw:HD2

Sets JACK to use ALSA-compatible sound card named HD2. One can find the names with cat /proc/asound/cards. Most ALSA tutorials and default configurations use card numbers, but this can get confusing when external MIDI devices are in use; names make it easier.

jack_control dps rate 48000

Sets JACK to use 48000 khz sampling. Happens to work very well with this card. Some cards only do 44100, many will go much higher. The higher you go, the lower your latency, but the better your card and your CPU have to be, and software has to support this as well.

jack_control dps nperiods 2

Sets JACK to use 2 periods. 2 is right for motherboard, PCI, PCI-X, etc.; 3 for USB.

jack_control dps period 64

Sets JACK to use 64 frames per period. Lower is less latency, but the setting in this script gives 2.67 ms latency, which is nicely low without putting too much stress on the particular hardware this example was built for. If a USB sound system were in use it might be good to try 32. Anything less than 3-4 ms should be fine for realtime synthesis and/or FX, 5 ms is the smallest a human being can detect. QjackCtl will tell you how you are doing; at no-load, which means no clients attached, you will want a max of 3-5% CPU usage, and if you cannot get that without xruns (the red numbers which mean the system cannot keep up with the demands), you will have to improve your hardware.

sleep 10

Wait for the above to settle.

a2jmidid -e &

Start the ALSA-to-JACK MIDI bridge. Good for mixing in applications which take MIDI input through ALSA but not JACK.

sleep 10

Wait for the above to settle.

qjackctl &

Load QjackCtl. GUI configuration tells it to run in the system tray. It will pick up the JACK session started by D-Bus just fine, and very smoothly too. It maintains the patchbay, the connections between these applications and any other JACK-enabled apps to be started manually. The patchbay is set up using manual GUI, but connections pre-configured in the patchbay are automatically created by QjackCtl itself when apps are started.

A GUI-based example setup

This example setup utilizes a more GUI focused configuration and management of JACK

  • Install jack2-dbus.
  • Install qjackctl, and tell your GUI window/desktop system to run it at startup.
  • Make sure QjackCtl is told to:
    • use the D-Bus interface,
    • run at startup,
    • save its configuration to the default location,
    • start the JACK audio server on application startup,
    • enable the system tray icon, and
    • start minimized to system tray.
  • Reboot.
  • After logging in, you will see QjackCtl in your system tray. Left-click on it.
  • Tweak settings in the QjackCtl GUI to lower latency. The Frame Size, Frame Buffer, and Bitrate settings all affect latency. Larger frame sizes lower latency, lower frame buffers lower latency, and higher bitrate settings lower latency, but all increase load on the sound card and your CPU. A Latency of about ~5ms is desirable for direct monitoring of instruments or microphones, as the latency begins to become perceptible at higher latencies.

Playing nice with ALSA

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: A pcm.jack port is already defined since alsa-plugins 1.1.7 (50-jack.conf) (Discuss in Talk:JACK_Audio_Connection_Kit)

To allow Alsa programs to play while jack is running you must install the jack plugin for alsa with alsa-plugins.

And enable it by editing (or creating) /etc/asound.conf (system wide settings) to have these lines:

# convert alsa API over jack API
# use it with
# % aplay foo.wav

# use this as default
pcm.!default {
    type plug
    slave { pcm "jack" }
}

ctl.mixer0 {
    type hw
    card 1
}

# pcm type jack
pcm.jack {
    type jack
    playback_ports {
        0 system:playback_1
        1 system:playback_2
    }
    capture_ports {
        0 system:capture_1
        1 system:capture_2
    }
}

You need not restart your computer or anything. Just edit the alsa config files, start up jack, and there you go...

Remember to start it as a user. If you start it with jackd -d alsa" as user X, it will not work for user Y.

Another approach, using ALSA loopback device (more complex but probably more robust), is described in this article.

GStreamer

GStreamer requires the gst-plugins-good package to work with JACK, which contains the jackaudiosink plugin that adds JACK playback support.

Further information (outdated): http://jackaudio.org/faq/gstreamer_via_jack.html

PulseAudio

If you need to keep pulseaudio installed (in the event it is required by other packages, like gnome-settings-daemon), you may want to prevent it from spawning automatically with X and taking over from JACK.

Edit /etc/pulse/client.conf, uncomment "autospawn" and set it to "no":

;autospawn = yes
autospawn = no

If you want both to play along, see: PulseAudio/Examples#PulseAudio through JACK

Firewire

In order to prevent ALSA from messing around with your firewire devices you have to blacklist all firewire related kernel modules. This also prevents PulseAudio from using firewire. Create the following file:

/etc/modprobe.d/alsa-no-jack.conf
blacklist snd-fireworks
blacklist snd-bebob
blacklist snd-oxfw
blacklist snd-dice
blacklist snd-firewire-digi00x
blacklist snd-firewire-tascam
blacklist snd-firewire-lib
blacklist snd-firewire-transceiver
blacklist snd-fireface
blacklist snd-firewire-motu

The list of modules is the most recent available at the time of writing at Alsa Firewire Improve Repository.

Now you can unload your loaded firewire modules or reboot.

Network / remote audio

JACK can be configured to send audio data over a network to a "master" machine, which then outputs the audio to a physical device. This can be useful to mix audio from a number of "slave" computers without requiring additional cables or hardware mixers, and keeping the audio path digital for as long as possible (as hardware mixers with digital inputs are very rare).

The configuration is very simple, however it requires a network that supports multicast traffic (i.e. IGMP snooping must be enabled on managed network switches), and it requires all machines be running the same JACK major version (JACK1 or JACK2) as the protocols are not interoperable between versions. For JACK2, the netmanager module must be loaded:

master$ jack_load netmanager -i -c

The -i -c option tells the netmanager to automatically map any incoming connections to the default audio device. Without this, each incoming connection would have to be manually mapped on each connection. You can use -i -h instead to see all available options, however note that the options are printed in the jackd server output, the jack_load command will not show anything.

On the client, JACK must be started in network mode:

slave$ jackd -d net

The two machines will connect and on the master the new audio source will be visible:

master$ jack_lsp
system:playback_1
system:playback_2
remotehost:from_slave_1
remotehost:from_slave_2

If you passed the -c option to jack_load as above, then the remote system will now be able to play audio.

MIDI

JACK can handle one soundcard very well, and an arbitrary number of MIDI devices (connected e.g. via USB). If you start JACK and want to use a MIDI keyboard or a synthesizer or some other pure MIDI device, you have to start JACK with a proper soundcard (one that actually outputs or inputs PCM sound). As soon you have done that, you can connect the MIDI device. E.g. with QjackCtl (qjackctl), you click on the connect button and you will find your device listed under JACK-MIDI or ALSA-MIDI, depending on the driver.

For JACK-MIDI, you may want to set the MIDI Driver to seq or raw in QjackCtl Setup > Settings. This should make your MIDI device appear under the MIDI tab. You can also change the name of the client (from a generic "midi_capture_1" to something more descriptive), if you enable Setup > Display > Enable client/port aliases and then Enable client/port aliases editing (rename).

For ALSA-MIDI, make sure to turn on Enable ALSA Sequencer support in QjackCtl Setup > Misc. This will add the ALSA tab in QjackCtl Connect window where your MIDI controller will show up.

Note: jack2 does not come with bridging support for legacy ALSA MIDI only applications. Therefore a2jmidid is required until upstream achieves feature parity on this.

For bridging ALSA-MIDI to JACK-MIDI, you may consider using a2jmidid (a2jmidid). The following command will export all available ALSA MIDI ports to JACK MIDI ports:

$ a2jmidid -e

They will be visible in QjackCtl under the MIDI tab labelled "a2j" client. You can automate starting of a2jmidid by adding to QjackCtl Setup > Options > Execute script after Startup: /usr/bin/a2jmidid -e &

Note: When connecting MIDI keyboard controllers in QjackCtl, make sure to Expand All first and connect the desired Output Ports (below the Readable Clients) to the Input Ports (below the Writable Clients). As a shortcut, if you select a writable client instead of individual ports as your destination, it should connect all its currently displayed output ports underneath.
  • Q: What is the difference between JACK-MIDI and ALSA-MIDI?
  • A: The former has improved timing and sample accurate MIDI event alignment. It extends or may even replace the latter but at this point they both co-exist.

To install some M-Audio MIDI keyboards, you will need the firmware package midisport-firmwareAUR in the AUR. Also, the snd_usb_audio module has to be available. For more information about specific USB MIDI devices, see http://alsa.opensrc.org/USBMidiDevices.

Troubleshooting

"Cannot lock down memory area (Cannot allocate memory)" message on startup

See Realtime process management#Configuring PAM and ensure that the user is in the realtime user group.

jack2-dbus and qjackctl errors

Still having the "Cannot allocate memory" and/or "Cannot connect to server socket err = No such file or directory" error(s) when pressing qjackctl's start button (assuming that you have package jack2-dbus installed) ?

Please delete ~/.jackdrc, ~/.config/jack/conf.xml, ~/.config/rncbc.org/QjackCtl.conf. Kill jackdbus and restart from scratch :) (Thanks to nedko)

Also try running

$ fuser /dev/snd/*

and check the resulting PID's with

$ ps ax | grep [PID here]

This will hopefully show the conflicting programs.

"ALSA: cannot set channel count to 1 for capture" error in logs

Change ALSA input and output channels from 1 to 2

Crackling or pops in audio

Your CPU or sound card is too weak to handle your settings for JACK. Lower the bitrate, lower the frame size, and raise the frame period in small increments until crackling stops.

Problems with specific applications

VLC - no audio after starting JACK

Run VLC and change the following menu options:

  • Tools > Preferences
  • Show settings: All
  • Audio > Output modules > Audio output module: JACK audio output
  • Audio > Output modules > JACK: Automatically connect to writable clients (enable)

See also