Allowing Multiple Programs to Play Sound

From ArchWiki

Jump to: navigation, search

In GNU/Linux, only one program can play a sound at a time unless you have a soundcard that supports hardware mixing (all/most onboard soundchips don't.) or unless one of the methods outlined below are being used. All the methods outlined below are using some form of software mixing.

Contents

dmix - Alsa's Software Mixing System.

Note: For ALSA 1.0.9rc2 and higher you don't need to setup dmix. Dmix is enabled as default for soundcards which don't support hardware mixing (from the alsa wiki). If you are still having problems: there are a lot of posts about flash + other programs that use sound. This happens because flash uses the alsa oss emulation instead of real alsa. The fix is easy, just disable oss (almost all programs are converted to alsa these days) by blacklisting the oss module in /etc/rc.conf in the MODULES section: !snd-pcm-oss

Note: dmix reduces the sound quality during mixing more or less noticeably. You should try another solution if this concerns you. (Regarding TeamSpeak; it doesn't support alsa - at least until version 3. Addendum: TeamSpeak 2 does seem to work under ALSA with OSS emulation if started using the aoss script, i.e.: $ aoss TeamSpeak. No matter what, the sound quality will not be top notch.)

Note: You may wish to read this article, Does dmix affect sound quality?


#/etc/asound.conf start:
pcm.!default {
    type plug
    slave.pcm "dmixer"
}


pcm.dsp0 {
    type plug
    slave.pcm "dmixer"
}
pcm.dmixer {
    type dmix
    ipc_key 1024
    slave {
        pcm "hw:0,0"
        period_time 0
        period_size 1024
        buffer_size 8192
        rate 44100   #many new cards are 48000 only
    }
    bindings {
        0 0
        1 1
    }
}

ctl.dmixer {
    type hw
    card 0
}

#end.

And magically, next time a program tries to use sound, dmix will kick in.

Problems with Availability to Only One User at a Time

You might find that only one user can use the dmixer at a time. This is probably ok for most, but for those who run mpd as a separate user this poses a problem. When mpd is playing a normal user can't play sounds though the dmixer. While its quite possible to just run mpd under a user's login account, another solution has been found. Adding the line ipc_key_add_uid 0 to the pcm.dmixer block disables this locking. The following is a snippet of the asound.conf, the rest is the same as above.

...
pcm.dmixer {
 type dmix
 ipc_key 1024
 ipc_key_add_uid 0 
slave {
...

Random Lack of Sound

If you randomly have no sound on startup, it may be because your system has multiple sound cards, and their order may sometimes change on startup. If this is the case, then change this section of /etc/asound.conf:

ctl.dmixer {
    type hw
    card FOO
}

Replace FOO with the desired audio device, as reported in the /proc/asound/cards file. An example of the file is shown below.

 0 [U0x46d0x9a1    ]: USB-Audio - USB Device 0x46d:0x9a1
                      USB Device 0x46d:0x9a1 at usb-0000:00:12.2-2, high speed
 1 [SB             ]: HDA-Intel - HDA ATI SB
                      HDA ATI SB at 0xf9ff4000 irq 16

Device 0 is the microphone built into a webcam, while device 1 is the integrated sound card. If you've copied the /etc/asound.conf from above as is, alsa will attempt to initialize the microphone as an audio output device, but will fail and you will have no sound. Rather than setting FOO to the number, you set it to the name next to the number, like so:

ctl.dmixer {
    type hw
    card SB
}

To make most programs use Alsa, make sure /etc/libao.conf only contains the following.

default_driver=alsa10

If you use mpd and the configuration tips above don't work for you, try reading this instead.

Specific Program Problems

For other programs who insist on their own audio setup, eg, XMMS or Mplayer, you would need to set their specific options.

For mplayer, open up ~/.mplayer/config (or /etc/mplayer/mplayer.conf for global setting) and add the following line:

ao=alsa

For XMMS/Beep Media Player, go into their options and make sure the sound driver is set to Alsa, not oss.

To do this in XMMS:

  • Open XMMS
    • Options -> preferences.
    • Choose the Alsa output plugin.

For applications which do not provide a ALSA output, you can use aoss from the alsa-oss package. To use aoss, when you run the program, prefix it with aoss, eg:

aoss realplay

pcm.!default{ ... } doesnt work for me anymore. but this does:

 pcm.default pcm.dmixer

vmix - OSS' Software Mixing System.

OSS (Open Sound System) is an open-source sound architecture (alternative to ALSA) that contains a software mixer (vmix) in kernel-space. The vmix software mixer comes enabled by default in OSS, and works out-of-the-box. See OSS for more information.

PulseAudio

PulseAudio (formerly Polypaudio) is a networked sound server, similar in theory to the Enlightened Sound Daemon (EsounD). PulseAudio is however much more advanced and has numerous features. The PulseAudio daemon and utilities are still under heavy development. Although they are generally considered stable, they haven't seen enough testing to warrant a first completely stable release.

More: PulseAudio

JACK

The Jack-Audio-Connection-Kit is a sound server for advanced use, and is generally used by professionals looking to record. Regardless it does support mixing, although to get non JACK aware applications to work, a plugin has to be provided. The alsa-plugins package will pull this in.

JACK2

Also called JACK-mp, it is the next version of JACK, with support for multi-processors and for network audio.

Hardware Mixing

Use soundcard that supports hardware mixing: old SoundBlaster Live! series (emu10k1 chip) and new Audigy series (emu10k2 chip) cards are known to work.

The SoundBlaster Live! 5.1 is fairly easy to find and is cheap to buy.

Note: new cheap Audigy SE, Audigy 2 NX, SoundBlaster Live! 24bit and SoundBlaster Live! 7.1 do not support hardware mixing (they use other chips).

99% of motherboards with integrated sound do not support hardware mixing.

An exception is the onboard VIA8237 chip which supports 4-stream hardware mixing. However it does only 3 for some motherboards (the 4th makes no sound), or is just broken. Even if it works, the quality is not good compared to other solutions.

For Arch64 and the 00:1b.0 Audio device: Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02), and to get sound from Enemy Territory, it works this by setting these:

echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss

Old, Obviated or Less Popular Methods

aRTS - KDE Project's Software Mixing System

pacman -S arts

Then if you use KDE, aRTS will be started for you automatically. If you don't use KDE and want arts to start whenever you start your computer, add this line to a system startup file such as /etc/rc.local:

/opt/kde/bin/artsd -a oss &

To hear the music, set your program's audio outputs to arts.

For apps that do not provide an aRTS output, you may prefix them with artsdsp, eg:

/opt/kde/bin/artsdsp -m xmms

or

/opt/kde/bin/artsdsp -m EnemyTerritory <-- although the sound will lagg behind.

If you find you have problems, try different arguments, which can be found by running the commands below.

artsd --help
artsdsp --help

EsounD (ESD) - The GNOME Project's Software Mixing System

EsounD is a dated solution that hasn't been updated for many years, however it has excellent application support and works well for basic desktop usage.

adding 'esd' to the daemons section in rc.conf may do the trick.

also see ESD for info on how to disable it, if it is hogging your sound devices.,etc

Info: http://www.tux.org/~ricdude/EsounD.html

ALSA + oss2jack

(UPDATE: Flash now uses alsa thus rendering an oss solution even less desirable. Even so there are alsa -> oss "converters".)

"oss2jack - A Linux kernel based approach to using OSS applications with the Jack Audio Connection Kit"

You might want to remove esd, artsd, polypaudio, etc., as you won't really be needing them unless an app only has artsd output for example.

To use oss2jack, you will need two packages, oss2jack and fusd-kor.

You will need to download the tarballs for each package and run makepkg to create the package. 1.

After having installed the packages, put the oss2jack after jack-audio-connection-kit in the DAEMONS array in /etc/rc.conf if you want oss2jack to start automagically on boot.

Edit /etc/conf.d/jack-audio-connection-kit and change the SERVER_PARAMS and DRIVER_PARAMS to suit your needs. The entries should look like this:

SERVER_PARAMS="-R -s -d alsa"
DRIVER_PARAMS="hw:0 -p 64 -r 44100"
  1. the reason you write "hw:0" is because the "-d" is already specified in /etc/rc.d/jack-audio-connection-kit thus making jackd run like "jackd -R -s -d alsa -d hw:0 -p 64 -r 44100".

If you have a slower computer, you may want to increase the period from 64 to 128 or 256.1 However, 512 or 1024 may be just fine, so that you don't waste your CPU.

This can be used as a solution to the "TEAMSPEAK and mic + ingame sound/music while using XMMS" or whatever problem one see all around the net. If you need more info see the oss2jack site (linked below).

Notes
  • Wine requires the audio to be 'Emulated' (change in winecfg's audio tab)
  • If you have MOD_AUTOLOAD="yes" in your rc.conf you might need to have !snd_pcm_oss !snd_seq_oss in your MODULES array to disable autoloading of these modules by udev.
  • If you need to start oss2jack as a user(with realtime capabilities), you need to compile and install the realtime-lsm module.
  • Some apps/games require the snd-mixer-oss kernel-module loaded(UT2004 for example), this might be(but probably isn't) unstable for the oss2jack daemon. Note that alsamixer will still let you set PCM, MASTER, etc volume regardless of if you have snd-mixer-oss loaded.
  • If you have issues setting the OSS volumes, try using audacity to set the volumes. Audacity is available in the Extra repository and is a sound editor that uses OSS. It has controls for input (mic) and output volumes. The volumes set with Audacity are OSS volumes, not ALSA volumes. You should only use this solution if using an ALSA mixer doesn't work for some reason, as setting the OSS volumes may unintentionally change the ALSA volumes due to oss2jack's emulation.
  • If you are running other OSS drivers in your kernel, you will have to bind oss2jack to a different device. To bind oss2jack to /dev/dsp2, edit /etc/rc.d/os2jack and change the line:
/usr/bin/oss2jack &

to this:

/usr/bin/oss2jack -n 2 &
More Information

oss2jack
oss2jack Installation Instructions
JACK Audio Connection Kit
Errors I encountered - Read this if you have other problems not mentioned here

joss(previously called q3jack)

recently a similar solution as mentioned above has come up which doesn't require a kernel module, but still forces other limitations. Note that it has no capture support as-of-yet (04-12-2006) and development have been stalled for some quite some time :/.

To use the package found in aur: joss, just start your app with

joss <appname>

You could manage some settings with command line options:

 -n, --name=NAME           use name to identify application to jack" (default: joss-PID)
 -o, --outputs=out_1,out_2 connect to specific outputs (default: first 2 physical outputs)
 -b, --blocks=N            set DMA block count (affects latency) (min: 4, max/default: 16)

Here are some examples from the README.

This just starts quake3 app with joss, getting an default name to jack and connects to the first 2 physical outputs.

joss quake3 

This start et with the specific name "enemy-territory" to jack. And links the output to alsa pcm playback device 3 and 4:

joss -n enemy-territory -o alsa_pcm:playback_3,alsa_pcm:playback_4 et 

With "-b" this example reduce DMA block count to 4 to reduce latency.

joss -b 4 lugaru

So you could tweak latency with "-b", change the jack name with "-n" to relieve identification in an jack control client. When you like to record output from an app started with joss you can link output with "-o" to an jack amplified recorder app.

site: http://www.konstruktiv.org/q3jack mirror: http://www.craknet.net/joss

NAS http://radscan.com/nas.html

Unique features. Check the readme. Has builtin mixing. You may get it to work with libaudioss, therefore nearly audio apps that have an OSS output will work with this, and it will be "lag-free". It does not have mmap support however, and this causes it to not work with many games.

USound - http://mattcamp.paunix.org/usound/

USound is somewhat similar to the Enlightened Sound Daemon (EsounD), but as the name suggests, USound aims to be more useful, particularly for consumer desktop and multimedia applications. At the time of writing (0.2) it has support for libao, mplayer, madplay and Speak Freely.

Troubleshooting

Permanently change the daemons priority

Take a look at "schedtoold" [1]

Skype

See Skype for a dmix how-to.

Other information

Personal tools