Posts Tagged ‘alsa’

ALSA Equalizer & Crossfeed on Debian Stable (Squeeze)

January 22, 2012

This is for ALSA, and is not suitable for systems using pulseaudio.

The aim:
to have a real time, audio equalizer (optionally systemwide if preferred)

to have high quality crossfeed to enhance playback with headphones

to have the option to use the above with all apps (systemwide) and otherwise to be able to choose or toggle the eq and crossfeed on/off when using mpd or mplayer.

What is needed:
ALSA uses plugins via an API called LADSPA. The Debian package swh-plugins contains a good LADSPA equalizer plugin, mbeq. A very nice crossfeed DSP, bs2b-ladspa, is available. The best real time controller for the equalizer is alsaequal, which isn’t in Debian Squeeze but is very easy to build (it’s available in Debian Testing/Wheezy).

Install mbeq, bs2b, and anything necessary to build alsaequal:

# apt-get install build-essential caps libasound2-dev libasound2-plugins bs2b-ladspa swh-plugins

Obtain alsaequal:

$ wget http://www.thedigitalmachine.net/tools/alsaequal-0.6.tar.bz2

Build alsaequal:
This only takes a minute.
Unpack alsaequal-0.6.tar.bz2 and after cd into alsaequal:
$ make
# make install

Create a suitable ~/.asoundrc:
The following ~/.asoundrc assumes use of the default soundcard with dmix as the default mixer because on 99% of systems this is how ALSA is automatically set up. If you have multiple sound cards and want to set one as default this can be done by editing /etc/modprobe.d/alsa-base.conf. For example I have an integrated hda-intel card and a USB card and I want the USB card to be the default sound device. In /etc/modprobe.d/alsa-base.conf I created the lines:

# Make usb audio the default card
options snd slots=snd-usb-audio,snd-hda-intel

So now the USB device driver always loads first on boot and the USB device is always the default. The integrated intel card is still available if I want to use it.

Back to the ~/.asoundrc

# eq controller
ctl.equal {
type equal
library "/usr/lib/ladspa/mbeq_1197.so"
module "mbeq"
}

#eq plug passes output to dmix – no crossfeed
pcm.equal {
type plug
slave.pcm plugequal
}
pcm.plugequal {
type equal
slave.pcm “plug:dmix”
library “/usr/lib/ladspa/mbeq_1197.so”
module “mbeq”
}

#eq plug passes output to crossfeed
pcm.plugequal2 {
type equal
slave.pcm crossfeed
library “/usr/lib/ladspa/mbeq_1197.so”
module “mbeq”
}
pcm.equal2{
type plug
slave.pcm plugequal2
}

#crossfeed outputs to dmix
pcm.crossfeed {
type plug
slave.pcm “bs2b”
}
pcm.bs2b {
type ladspa
slave.pcm “plug:dmix”
path “/usr/lib/ladspa/”
plugins [{
label bs2b
input {
controls [ 650 9.5 ]
}
}]
}

This has created several devices: an equalizer, a means to control the equalizer’s settings in real time, and a crossfeed based on Jan Meier’s famous crossfeed hardware (the dmix device already exists and is defined in /usr/share/alsa/pcm/dmix.conf – there is no need to touch it). There are two ways to access the equalizer: one passes the equalized output directly to dmix, and the other passes it to the crossfeed device, after which the crossfeed passes its output to the dmix mixer device. The mixer in this example is plug:dmix which can accept and mix multiple audio sources and is enabled automatically in any modern ALSA system. If you want every sound from all applications passed through the eq then change

pcm.equal {
to
pcm.!default {

Configure MPD’s outputs:
If you set the eq systemwide then you can remove the third output device from the following mpdconf example.

I run mpd under my own user name and my ~/.mpdconf contains the following audio outputs:

audio_output {
type "alsa"
name "My ALSA Device"
auto_resample "no"
mixer_device "default" # optional
mixer_control "PCM" # optional
}
audio_output {
type "alsa"
name "bs2b"
device "crossfeed"
auto_resample "no"
mixer_control "PCM"
mixer_device "default"
}
audio_output {
type "alsa"
name "Eq"
device "equal"
auto_resample "no"
mixer_control "PCM"
mixer_device "default"
}
audio_output {
type "alsa"
name "EQ+bs2b"
device "equal2"
auto_resample "no"
mixer_control "PCM"
mixer_device "default"
}

This allows me to toggle mpd output between the following states:

No Eq, no crossfeed
Crossfeed, no Eq
Eq, no crossfeed
Eq + crossfeed

Restart alsa and mpd so the changes can take effect:

# alsa force-reload && /etc/init.d/mpd restart

Configure the Equalizer:
This is easily done in a terminal using alsamixer, which is already installed as part of ALSA. So play some music using mpd with one of the equalizer outputs (or mplayer-ao alsa:device=equal ) if you’re using pcm.equal { then you can do

$ alsamixer -D equal

and if you’re using Eq systemwide with pcm.!default { then play audio with any app and you need only do

$ alsamixer

and now you can use the Eq sliders and hear the effect. When you’re happy with it and want to save the settings so they always take effect on boot you can run:

# alsactl store

MPlayer:
Now for the best way to use mplayer with both crossfeed and the configured equalizer. MPlayer has its own meier crossfeed so use this and then pass the audio to the ALSA ‘equal’ device just created and configured:

mplayer -af bs2b=profile=jmeier -ao alsa:device=equal

Alias for convenience:
On my playback system I don’t use any Eq with my speakers or my old Sennheiser Hi-Fi headphones but it’s very useful to add a little extra bass for use with my Sony studio monitor headphones, so they sound more like a Hi-Fi headphone and a little less like a slightly lightweight and sterile monitor. I have the above command aliased as mplayer-headphones in ~/.bash_aliases:

#use meier crossfeed emulation for headphones
alias mplayer-headphones='mplayer -af bs2b=profile=jmeier -ao alsa:device=equal'

Of course if you have set up Eq systemwide then you need only do:

mplayer -af bs2b=profile=jmeier

So there it is: high quality Eq and/or Meier-like crossfeed available to mplayer, mpd and clients, and (if you prefer) to any app which produces sound.

Resource useage:
On my system (Athlon64 4050e dual core 2.0Ghz running 32-bit Debian) running mpd and alsa with crossfeed and eq uses about 4% more CPU than without either, which is to say that CPU useage goes from 1% to 5% when no samplerate conversion is required. A similar change, 4 or 5 percentage points difference, is seen when samplerate conversion is required i.e. 48000Hz audio resampled to my system default 44100Hz using libsamplerate “samplerate_medium”.