Google Site SearchFN Site Search FN Blog Login FN Blog Login
Site Navigation:
 
 

Sound Mixing with ALSA

by Andre Costa on Jan 22, 2004 (UPDATED 2004-09-19)

UPDATE 2004-09-19
Ondrej Svetlik send me a note saying that in order to make XMMS work properly he had to disable mmap mode on XMMS configuration. Sound mixing works for me as described even with mmap enabled, but he says he experienced some problems with it (like being unable to resume XMMS playback when some other application platyed a sound), so maybe this advice is useful for some. FWIW I disabled mmap on my setup too ;-) (it's on the "Advanced Settings" tab of the ALSA driver configuration dialog).


Introduction

If you have ever seen your DVD-player (xine, mplayer etc.) or IM application (e.g. gaim) hang for no obvious reason, just to see it coming back to life when you close xmms, then this article might interest you.

This HOWTO will (hopefully) teach you how to setup apps such as xmms, xine and gaim to take advantage of ALSA native sound mixing capabilities, allowing them to send data to sound devices concurrently.

Even though these apps are used as examples, configuration of any ALSA-aware app should be very similar. Also, everything described here is related to intel8x0 driver, but AFAICS confifuration for other types of hardware should be similar.

IMPORTANT: the instructions listed below work for me, but I don't have extensive knowledge of ALSA configuration. In fact, the core tips were adapted from nForce section of ALSA documentation. If you have any correction or suggestion, I will be glad to add them to this document.

ALSA sound-mixing configuration

IMPORTANT: ALSA installation happens at kernel-level and is beyond the scope of this document; please look for some guidance here. I will assume that you already have a working ALSA installation for your hardware.

First place to go is configuration file ~/.asoundrc (basic docs can be found here).

Edit the file so that it contains something like this:

pcm.!default {
type plug
slave.pcm "swmixer"
}

pcm.swmixer {
type dmix
ipc_key 1234
slave {
pcm "hw:0,0"
period_time 0
period_size 1024
buffer_size 4096
rate 44100
}
}

We have just created two multichannel-enabled devices default and swmixer (NOTE: this configuration is based on the contribution from Kalin Kozhiharov to the nForce section of ALSA documentation).

Now, all we have to do is configure apps to use ALSA and to use swmixer as the audio device.

Testing

In order to test the newly created mixing device, you could try:

aplay -Dplug:swmixer soundfile

If this works, great, basic functionality is there. Now, to the fun part: try the above command with a lengthy sound file on a window, and try it again on another window, a few seconds apart. If none of them block and no error messages are printed on the terminals, you should hear both sounds files playing concurrently. If this happened, congratulations: you've just mixed your first sound samples =)

NOTE: you will have to install alsa-utils package in order to have access to aplay

XMMS

  1. open configuration dialog (CTRL+P)
  2. choose "ALSA x.x.x output plugin [libALSA.so]"
  3. press "Configure" button for advanced configuration
  4. check the "user defined" toggle
  5. replace whatever is on the textbox for your new device swmixer (leave other settings untouched)

Hopefully xmms is now configured to send sound data to your mixing device. Try playing anything (MP3, Ogg etc.) with xmms; if it works, congratulations, xmms is properly configured.

NOTE: the above procedure can be performed for any output plugin you use (e.g. Crossfade) as long as the plugin itself is configured to send output to ALSA as described.

Xine

Xine can be configured two ways: directly on the config file (~/.xine/config) or via the GUI. I will address both options:

configuration file

there are many audio-related settings on the configuration file, and specifically some ALSA settings (they all begin with audio.alsa.*) the ones that need to be changed are audio.alsa_default_device and audio.alsa_front_device.

You must point these settings to your new mixing device (swmixer). Mine looks like this:

# device used for mono output
# string, default: default
audio.alsa_default_device:swmixer

# device used for stereo output
# string, default: front
audio.alsa_front_device:swmixer

Another setting that needs to be adjusted is gui.audio_mixer_method:

# Audio mixer control method
# { Sound card  Software }, default: 0
gui.audio_mixer_method:Software
GUI

advanced settings are hidden by default, and you must set Configuration experience level on the gui tab of xine preferences dialog to Advanced.

After this, scroll down on the same tab and set Audio mixer control method to Software. Last, go to audio tab and set device used for mono output and device used for stereo output to swmixer.

This should do the trick. Now, test it by playing a movie while your newly-configured xmms is playing some music, and you will hear both sounds merged.

NOTE: the above setup refers to a standard 2-speaker setup, but you can configure xine for more advanced audio setups like 5.1-channel output. Refer to xine documentation if you need to do it.

gaim

On gaim's preferences dialog, go to Sounds section, and choose Command on the Sound method setting. Then, on the Sound command textbox, enter:

aplay -Dplug:swmixer %s

Now gaim won't hang anymore whenever it plays a sound.

General tips

  • Any application that allows configuration of an external player application can be configured to use aplay as shown above (I did it for example with GKrellM)
  • applications that have ALSA support builtin (usually) can be configured to use a device different than the default

Final Words

This was not the ultimate sound mixing configuration that can be done -- in fact, it is no more than a working solution, maybe there are other better ways of accomplishing the same. JACK is a project which should be followed closely by those interested in working with sound and/or getting the best for their hardware; its development is evolving very fast, and when a stable version arrives it will probably become mainstream (ALSA can already be configured to use JACK).