{"id":27,"date":"2015-09-28T11:33:00","date_gmt":"2015-09-28T11:33:00","guid":{"rendered":"https:\/\/patryk.rzski.com\/?p=27"},"modified":"2016-02-28T13:56:14","modified_gmt":"2016-02-28T13:56:14","slug":"jack-and-alsa-sound-through-multiple-devices","status":"publish","type":"post","link":"https:\/\/patryk.rzski.com\/?p=27","title":{"rendered":"Jack and ALSA: sound through multiple devices"},"content":{"rendered":"<p>I have designed a simple audio-video solution for my entertainment room at home, where I have a:<\/p>\n<ul>\n<li>High quality headphones connected to a decent pre\/post amplifier which connects to the computer through USB (and registers as USB audio device with my Windows10 and Gentoo linux).<\/li>\n<li>Not so high (but not bad at all!) on-board sound chip provided with my motherboard &#8211; this one I&#8217;d like to connect to my TV.<\/li>\n<\/ul>\n<p>The goal is to have sound via the on-board card to the TV for less demanding tasks, such as watching movies and have sound go via USB to the amp while when I listen to music.<\/p>\n<p><span class=\"postbody\">After some reseach it appears ALSA on its own cannot do this job, but Jack can be used. At first I did not like the overhead, but quickly realised it is minimal. The below howto is based on JACK. On the 4.3 kernel (no -rt stuff since most of the good work for real time processing is long in the mainline kernel), it works well.<\/span><\/p>\n<p>First of all it is good to ensure all packages are build with jack support (and USE flags in Gentoo make this a trivial task) and install the Jack daemon (jackd). Now, for some reason it is a daemon that does not have a startup script, so you&#8217;ll need to figure out how you want to start it &#8211; for me it ended up in the xfce session controller (so it starts with my user ID when I log in) &#8211; but you can write a script too, just ensure it depends on alsasound.<\/p>\n<p>There is a gotcha here &#8211; for running the jackd without root privileges, you need to edit limtis.conf to give the audio group rt permissions:<\/p>\n<table border=\"0\" width=\"90%\" cellspacing=\"1\" cellpadding=\"3\" align=\"center\">\n<tbody>\n<tr>\n<td><span class=\"genmed\"><b>Code:<\/b><\/span><\/td>\n<\/tr>\n<tr>\n<td class=\"code\"><strong>root@ryba ~ # grep audio \/etc\/security\/limits.conf <\/strong><br \/>\n@audio\u00a0 \u00a0 \u00a0 &#8211;\u00a0 rtprio\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 99<br \/>\n@audio\u00a0 \u00a0\u00a0 \u00a0&#8211;\u00a0 memlock\u00a0 \u00a0\u00a0 \u00a0unlimited<br \/>\n@audio\u00a0 \u00a0\u00a0 \u00a0&#8211;\u00a0 nice\u00a0 \u00a0\u00a0 \u00a0-10<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span class=\"postbody\"><br \/>\nAnd of course, have your user in the audio group. As always with groups &#8211; for them to take effect, all sessions must be finished and re-started.<\/span><\/p>\n<p>Now, trick is you still need alsasound daemon running and a proper \/etc\/asound.conf which mixes multiple devices. Basically the following config works for the first two sound cards, because it uses the indexes instead of the device names (you get those from aplay -l. An exception is with the last config section, because it is apparently a dummy control, alsa needs it):<\/p>\n<table border=\"0\" width=\"90%\" cellspacing=\"1\" cellpadding=\"3\" align=\"center\">\n<tbody>\n<tr>\n<td><span class=\"genmed\"><b>Code:<\/b><\/span><\/td>\n<\/tr>\n<tr>\n<td class=\"code\"><strong>root@ryba ~ # cat \/etc\/asound.conf<\/strong><br \/>\npcm.both {<br \/>\ntype route;<br \/>\nslave.pcm {<br \/>\ntype multi;<br \/>\nslaves.a.pcm &#8220;plughw:0,0&#8221;<br \/>\nslaves.b.pcm &#8220;plughw:1,0&#8221;<br \/>\nslaves.a.channels 2;<br \/>\nslaves.b.channels 2;<br \/>\nbindings.0.slave a;<br \/>\nbindings.0.channel 0;<br \/>\nbindings.1.slave a;<br \/>\nbindings.1.channel 1;<br \/>\nbindings.2.slave b;<br \/>\nbindings.2.channel 0;<br \/>\nbindings.3.slave b;<br \/>\nbindings.3.channel 1;<br \/>\n}<br \/>\nttable.0.0 1;<br \/>\nttable.1.1 1;<br \/>\nttable.0.2 1;<br \/>\nttable.1.3 1;<br \/>\n}pcm.jack {<br \/>\ntype jack<br \/>\nplayback_ports {<br \/>\n0 system:playback_1<br \/>\n1 system:playback_2<br \/>\n}<br \/>\ncapture_ports {<br \/>\n0 system:capture_1<br \/>\n1 system:capture_2<br \/>\n}<br \/>\n}<\/p>\n<p>pcm.!default {<br \/>\ntype plug;<br \/>\nslave.pcm &#8220;jack&#8221;;<br \/>\n}<\/p>\n<p>ctl.!default {<br \/>\ntype hw;<br \/>\ncard 0;<br \/>\n}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span class=\"postbody\"> Next, I need to tell jack to start WITH this config too, so my line that starts it under xfce session starter is as follows:<br \/>\n<\/span><\/p>\n<table border=\"0\" width=\"90%\" cellspacing=\"1\" cellpadding=\"3\" align=\"center\">\n<tbody>\n<tr>\n<td><span class=\"genmed\"><b>Code:<\/b><\/span><\/td>\n<\/tr>\n<tr>\n<td class=\"code\">\/usr\/bin\/jackd -d alsa -P both<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span class=\"postbody\"> Explanation: run jackd using -Device alsa and -Playback both &#8211; because that&#8217;s how my device is named in \/etc\/asound.conf: pcm.both<\/span><\/p>\n<p>Final step is to tell your software, such as mplayer, moc music on console, or whatever else you use, to switch from alsa to jack &#8211; that&#8217;s fairly easy, just rtfm, however you can also leave it as alsa, since this one is routed into the jack as well, making jackd the catch-all solution. It is quite important to do so because if you use Firefox and would like sound from flash and HTML5, FF will first try pulseaudio and if it is not present (like in my case, because I don&#8217;t want that overhead), then it will try alsa &#8211; but not jack and there is no config handle for that. So the above config basically uses jackd every time something tries to use alsa.<\/p>\n<p><span class=\"postbody\">Needed: alsa-plugins, jack-audio-connection-kit<br \/>\nNot needed: alsa_out, qjackctl (the gui thing), gst-plugins-alsa, pulseaudio<br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have designed a simple audio-video solution for my entertainment room at home, where I have a: High quality headphones connected to a decent pre\/post amplifier which connects to the computer through USB (and registers as USB audio device with my Windows10 and Gentoo linux). Not so high (but not bad at all!) on-board sound [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/posts\/27"}],"collection":[{"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27"}],"version-history":[{"count":2,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":48,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions\/48"}],"wp:attachment":[{"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}