Allow multiple programs to play sound at once

From ArchWiki

Jump to: navigation, search
i18n
English
Slovensky
Español

In 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 one of the methods outlined below are being used. All the methods 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: I see 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. (Also TeamSpeak 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. The sound quality is not top notch however.)


Note: dmix affect sound quality?

A question more than a comment: how does dmix affect sound quality? I use a Chaintech AV710, which uses the snd-ice1724 driver. After it was set to use dmix by default by an upstream ALSA, I noticed that alsamixer was showing sample rate 48000 while I was playing back a CD (all CDs are 44100). If I forced it to 44100 with rate locking so nothing could automatically change it and played the CD, it was obviously wrong (too slow). This suggests to me that something in the chain is forcing audio played through dmix to be resampled to 48KHz, which is not optimal for sound quality. When I commented out the parts of ICE1724.conf that enable dmix, I found this no longer happens - alsamixer now displays 44100 when I'm playing a CD. So for audio geeks like me who deeply care about audio quality, is dmix not advisable?

Answer:

Dmix by default uses 48kHz sample rate. So, if your source is 44.1kHz, it will be upsampled to 48khz. If you want pure 44.1kHz output, you should set the rate parameter:

pcm.swmixer {
    type dmix
    ipc_key 1234
    slave {
        pcm "hw:0,0" #for ICE1724's analog output
        #pcm "hw:0,1" # for ICE1724's digital output
        #format S32_LE # needed only for ICE1724's digital output.
        period_size 1024
        buffer_size 4096
        rate 44100
    }
}

Also, you should keep in mind, that software mixing is possible only, if all signals are at the same sample rate. So, if you want to play DVD's, which use 48kHz sample rate for audio, with this kind of plug, everything will be downsampled to 44.1kHz. Unfortunatelly it does'nt work wery well (there are some syncronization problems with some players). In this case you have two opportunities, either use a separate plug (and loose ability to playback mp3 and dvd on the same time) or tell your DVD player to sample audio down to 44.1khz, for example:

mplayer -af resample=44100


#/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.

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

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

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 above config doesn't work for you, try this one instead.

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 put the following line in:

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 does not support hardware mixing (they use other chips).

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

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

For my Arch64 and

00:1b.0 Audio device: Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02)

to get sound from Enemy Territory works this

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

Old, discouraged 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) - also 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 did the trick for me.

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" but i haven't tested it)

"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. too 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. I make the entries 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" :) geddit? good.

If you have a slower computer, you may want to increase the period from 64 to 128 or 256.1 (Note by AqD: 512 or 1024 is just fine, don't waste your CPU)

This can be used as 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(up until now it has been called q3jack..)

recently a similar solution as the above has come up which doesn't require a kernel module but has 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 install use the package 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