HOWTO: Sound mixing in software with JACK and oss2jack
I originally wrote this HOWTO for Breezy some months ago. Then I wrote an updated and cleaned up version for Dapper and I also updated my packages used by this HOWTO. Lately I update all stuff to be used on Feisty (NEW), too. The old one for Breezy is still around: http://www.ubuntuforums.org/showpost...90&postcount=6
Problem: ALSA supports software mixing by its dmix plugin (for sound cards which do not support that in hardware). dmix is working with most ALSA applications. But, there are some ALSA applications which do not work with dmix, they just block the whole ALSA device (as if dmix wasn't there).
Furthermore OSS apps which are forced to use ALSA OSS emulation can't be dmix'd, they also block the whole sound device.
So, this HOWTO is for those of you who use OSS only applications and want them to be able to play sound at the same time, without using wrappers for sound servers or general ones (e.g. aoss).
Examples of OSS only applications are: Skype (< 1.3.x), TeamSpeak2, Audacity, ET, ...
PROBLEM: this HOWTO & TeamSpeak2
Some months ago it was discovered and confirmed by several people that TeamSpeak2 doesn't work with the software used in this HOWTO any more (crash, "Runtime error ...", "Segmentation fault"), although it used to work with the old HOWTO on Breezy. I still don't have any glue why. It's very very strange. As with Dapper, this problem also exists on Feisty - sorry. 
PROBLEM 2: this HOWTO & Feisty & Audacity
Till now, I couldn't convince Audacity to work through it's OSS output feature - neither playing nor recording works for me. This hasn't been confirmed by others yet, so if you try it, tell me about your findings!
Solution: Sound servers: artsd (KDE), esd, polyp (GNOME), ... others. Sound applications have to have special output drivers to use them and most have.
But some applications don't have such drivers. Then you would have to use wrappers (artsdsp, esdsp) which "try" to catch the calls of the applications (in userspace) and reroute them to their sound servers.
These wrappers sometimes work great (artsdsp/KDE) or bad (esddsp/GNOME) (at least on my computer).
Solution described here: Use of JACK as sound server (although JACK is more than a sound server, I know ...) and oss2jack, a special tool which emulates an OSS device and reroutes the data to JACK.
Then all OSS only application will be able to open the sound device at the same time and play at the same time.
For other applications (all true ALSA applications) there two alternatives now, which depend on how jackd is started (commandline arguments):
(jack-only-alternative) This is the original one, used in this HOWTO till now. jackd blocks the whole ALSA sound device and all other sound applications will have to use application specific jackd output drivers/plugins (like xmms, mplayer, ... have) and play through jackd.
(NEW) (jack-as-dmix/dsnoop-client-alternative) This is the second, the new one. Here jackd is started with the commandline arguments "-P dmix -C dsnoop" and therefore jackd will act as a dmix/dsnoop client, which means that ALSA applications which also use dmix/dsnoop can still use the sound device and play at the same time. This means, that you don't have to reconfigure any of your ALSA applications (esd, totem, xmms, mplayer ...) - they just play their sound as usual (through dmix/dsnoop).
The drawback is, that you cannot use the commandline argument "-p 256" anymore and therefore you might get latency problems (default is a value of 1024 (frames per period), which may be too much)!
The steps (1. to 10.) you see below, are marked with the bold words from above if they only apply to one specific alternative. You will see, that the second alternative effectivly uses steps 1 to 3, 5, 6.B and 7. only!
Important assumptions made in this HOWTO: You are using Ubuntu 6.06 (Dapper) or Ubuntu 7.04 (Feisty) with GNOME and ALSA. Your sound card (hardware) basically works.
Note 1: I'm neither an expert in sound stuff nor an ubuntu developer, so tell me, if I'm saying something wrong.
Note 2: If you really need *perfect* sound mixing, IMO you will still have to buy a "real" sound card with a sound chip which is able to do it in hardware.
Back then, when I started to write this HOWTO, I tested this solution with xmms, mplayer playing files and talking to somebody in skype at the same time. I also tested it with TS2 and ET at the same time.
Installing needed packages:
1. Add the following (my) repository to your /etc/apt/sources.list file:
Dapper
Code:
deb http://neogate.homelinux.org/debian/ dapper sound
Feisty
Code:
deb http://neogate.homelinux.org/debian/ feisty sound
Don't forget to update your package system. To do this you can do:
Code:
sudo apt-get update
2.A Then install jackd from dapper and oss2jack
from the repository given above by typing:
Code:
sudo apt-get install jackd oss2jack
NOTE: During the installation of the package "oss2jack" you'll get asked 2 questions.
The first one says "Do you want to disable the ALSA OSS modules?". For this HOWTO to work, you have to choose "Yes".
The second question says: "Do you want to unload the ALSA OSS modules now?". You may choose "Yes" here, too. You could do that manually, too (see step 7), but it won't hurt to let the installation try to unload them.
Now do step 2.B OR step 2.C, NOT BOTH, only one of them depending on the kernel you have installed:
2.B In my repository are pre-build kernel modules for dapper kernel 2.6.15-23, 2.6.15-25, 2.6.15-26, 2.6.15-28, 2.6.15-29 and feisty kernel 2.6.20-15, 2.6.20-16. If you have installed one of these kernels, do:
Code:
sudo apt-get install fusd-kor-module-`uname -r` fusd-kor
2.C If you have an other dapper/feisty kernel or a selfmade one, you will have to install fusd-kor-source instead of the pre-build module and build your own fusd-kor-module package.
So, first you have to prepare your system for module compilation. To do that, install module-assistant:
Code:
sudo apt-get install module-assistant
sudo m-a prepare
sudo apt-get install gcc-`grep LINUX_COMPILER /usr/src/linux/include/linux/compile.h | sed 's/.* \([0-9]\+\.[0-9]\+\).*/\1/'`
The second command will make module-assitant (m-a) install the needed linux-headers packages. (If you have a selfmade linux-kernel package, you (probably) will have to manually install your own linux-headers package.) The last command here, makes sure that you have got installed the right version of gcc (GNU C Compiler).
Then, install fusd-kor-source and build your own fusd-kor-module packages:
Code:
sudo apt-get install fusd-kor-source
sudo m-a a-i fusd-kor
sudo apt-get install fusd-kor
In the second command m-a will do all the stuff needed to configure, build and install your own fusd-kor-module package automatically. The last command installs the package "fusd-kor".
NOTE on step 2.B and 2.C: During the installation of the package "fusd-kor" you'll get asked 2 questions.
The first one says "Do you want the fusd-kor kernel module to be loaded during the boot process?". For this HOWTO to work, you have to choose "Yes".
The second question says: "If needed, you can specify commandline parameters ...". Do not enter anything there, leave it blank.
Now do step 3.A OR step 3.B, NOT BOTH, only one of them depending on the kernel you have installed:
3.A I also installed the realtime kernel module, so that jackd and oss2jack are able to run with a higher priority. In my repository are pre-build kernel modules for dapper kernel 2.6.15-23, 2.6.15-25, 2.6.15-26, 2.6.15-28, 2.6.15-29 and feisty kernel 2.6.20-15, 2.6.20-16. If you have installed one of these kernels, do first:
Code:
sudo apt-get install realtime-lsm-module-`uname -r`
then
Code:
sudo apt-get install realtime-lsm
3.B If you have an other dapper/feisty kernel or a selfmade one, you will have to install realtime-lsm-source instead of the pre-build module and build your own realtime-lsm-module package.
So, first you have to prepare your system for module compilation. To do that, install module-assistant:
Code:
sudo apt-get install module-assistant
sudo m-a prepare
sudo apt-get install gcc-`grep LINUX_COMPILER /usr/src/linux/include/linux/compile.h | sed 's/.* \([0-9]\+\.[0-9]\+\).*/\1/'`
The second command will make module-assitant (m-a) install the needed linux-headers packages. (If you have a selfmade linux-kernel package, you (probably) will have to manually install your own linux-headers package.) The last command here, makes sure that you have got installed the right version of gcc (GNU C Compiler).
Then, install realtime-lsm-source and build your own realtime-lsm-module packages:
Code:
sudo apt-get install realtime-lsm-source realtime-lsm
sudo m-a a-i realtime-lsm
In the last command m-a will do all the stuff needed to configure, build and install your own realtime-lsm-module package automatically.
4. (jackd-only-alternative) Usually the package "libesd-alsa0" is installed to allow esd to output to ALSA. But since we are going to use JACK (and jackd blocks the ALSA sound device by default), you need to replace "libesd-alsa0" with "libesd0", which enables esd to output to OSS devices.
Code:
sudo apt-get install libesd0
This way, esd (which is responsible for all "GNOME desktop sounds") will output its stuff to our emulated OSS device by oss2jack.
5. To be able to run jackd and oss2jack with a higher priority, you have to make sure that you are a member of the "audio" group. This is also needed due to the owner/group settings of the device files of the kfusd kernel module. If in doubt, do:
Code:
sudo adduser <your_username> audio
Start jackd and oss2jack after login (automatically)
6.A (jackd-only-alternative) GNOME automatically starts its esd (GNOME's sound server) after login, so that you can hear the login sound etc. In the first section you configured esd to output to OSS devices (step 4.).
So we need to start our jackd/oss2jack combination before esd gets started. To do that we use the ~/.gnomerc file, which is executed before any GNOME stuff gets started after login. You will find the script I use in /usr/share/doc/oss2jack/examples/.gnomerc , so copy it over to your home directory:
Code:
cp /usr/share/doc/oss2jack/examples/.gnomerc ~/.gnomerc
I admit, that my script is some kind of a (dirty) hack ... just in case you wonder about the loop at the end for example.
Note that this script assumes, that you followed step 3. That means: in theory you could do without step 3, but that's not recommended. You would have to edit the script and remove the "nice" adjustments and run jackd/oss2jack without higher (realtime) priority.
6.B (jackd-as-dmix/dsnoop-client-alternative) We need to start our jackd/oss2jack combination after login. To do that we use the ~/.gnomerc file, which is executed before any GNOME stuff gets started. You will find the script I use in /usr/share/doc/oss2jack/examples/.gnomerc-dmixsnoop , so copy it over to your home directory:
Dapper/Feisty
Code:
cp /usr/share/doc/oss2jack/examples/.gnomerc-dmixsnoop ~/.gnomerc
I admit, that my script is some kind of a (dirty) hack ... just in case you wonder about the loop at the end for example.
Note that this script assumes, that you followed step 3. That means: in theory you could do without step 3, but that's not recommended. You would have to edit the script and remove the "nice" adjustments and run jackd/oss2jack without higher (realtime) priority.
If you are using Feisty you may also copy the example file /usr/share/doc/oss2jack/examples/.asoundrc to your home directory:
Feisty
Code:
cp /usr/share/doc/oss2jack/examples/.asoundrc ~/.asoundrc
It contains a configuration item which creates a mixer element named "dmix". If this is not there, jackd complains about not being able to open a mixer device.
It's nearly done 
7. Only needed if you don't want to reboot now. You will have to load the realtime module manually. Next time you do a reboot it will get loaded automatically (step 3). Furthermore you have to unload the ALSA OSS emulation modules. If you choose to unload them during the installtion of the package oss2jack (step 2.A), they shouldn't be there anymore, but it doesn't hurt to "rmmod" them again (so there might appear an error message saying "ERROR: Module XXXX does not exist ..." - just ignore it). On future reboots they will be prevented from being loaded automatically, too (step 2.A).
Code:
sudo /etc/init.d/realtime start
sudo /etc/init.d/fusd-kor start
sudo rmmod snd-pcm-oss
sudo rmmod snd-mixer-oss
So, the first command should suffice here, the other three are just there to really make sure that nothing bad happened during the steps above while loading and unloading.
NOTE: If your are using Feisty, this step 7 shouldn't be necessary at all. But again: it can't hurt.
8. If you didn't reboot in step 7, now at least you have to logout and login again, so that your ~/.gnomerc script gets executed.
Configuring software
9. (jackd-only-alternative) Usually gstreamer is installed and configured to autodetect and chose one output "method". But - same reason as with esd (step 4.) - we need it to output to our emulated OSS device by oss2jack. Go to:
System -> Preferences -> Multimedia Systems Selector
and chose OSS (output and input). If this menuitem does not exist, start the Multimedia Systems Selector by this command:
Code:
gstreamer-properties
and chose OSS (output and input).
In Feisty there is another dialog, which might need some changes:
System -> Preferences -> Sound
In tab "Devices" you might have to chose "OSS" explicitly - but ATM I'm not sure about "for what" these configuration items are really good for. The default, called "Autodetect", just works in my case.
10.A (jackd-only-alternative) Configuring (other) sound applications
In general, it is a good idea to leave the configuration of your applications alone - at least at the beginning. Try them, if they still work and if not, change teir configuration.
Here is a list of applications and with which output driver I used them:
- esd ... oss (configured above, step 4.)
- gstreamer ... oss (configured above, step 9.)
- xmms ... jack
- mplayer ... jack in Dapper, oss in Feisty
- vlc ... oss
- gxine/xine ... oss
- licq ... use "esdplay" in Licq Menu -> Options -> OnEvent: Command
- wine ... oss (wine might need a patch, not sure at the moment, see the resources section below)
Note that the tool "esdplay" mentioned above is contained in the package "esound-clients". If you need "esdplay" (e.g. if you use licq) you have to install it:
Code:
sudo apt-get install esound-clients
10.B (jackd-as-dmix/dsnoop-client-alternative) Configuring (other) sound applications
As already mentioned, using jackd as a simple dmix/dsnoop client has the advantage of not having to reconfigure any ALSA application. jackd is just one more client to the ALSA library's software mixing feature (dmix/dsnoop) - like all other ALSA applications are (esd, xmms, mplayer, totem, vlc, ...).
NOTE on step 10.A and 10.B : Applications like Audacity, Skype, ET, ... can use OSS only, so they will use our emulated OSS device by oss2jack.
================================================== ===================
END OF HOWTO
================================================== ===================
If you don't like the result of this HOWTO or if you have to reverse the whole thing because of some other reason, here's a
list, what you have to do to
switch back to ALSA/dmix. I used the same step numbers as in my HOWTO:
6. Remove ~/.gnomerc, ~/.asoundrc and terminate the tools:
Code:
rm ~/.gnomerc ~/.asoundrc
killall esd
killall oss2jack
killall jackd
9. (jackd-only-alternative) Go to
System -> Preferences -> Multimedia Systems Selector
and select Autodetect for input and ALSA for output. If this menuitem does not exist, start the Multimedia Systems Selector by this command:
Code:
gstreamer-properties
and select Autodetect for input and ALSA for output.
4. (jackd-only-alternative) Replace the package "libesd0" with "libesd-alsa0", by doing:
Code:
sudo apt-get install libesd-alsa0
2.A(B|C) + 3. Now uninstall the rest of the software packages installed by the HOWTO:
Code:
sudo apt-get remove --purge oss2jack fusd-kor* jackd realtime-lsm*
2.A If you don't want to reboot now, you have to load the ALSA OSS emulation modules again:
Code:
sudo modprobe snd-mixer-oss
sudo modprobe snd-pcm-oss
8. Now at least you have to logout and login again, so that esd is getting started again by GNOME.
Bookmarks