Dmix, the pain At mpd we are having an issue with high cpu usage. This is caused by resampling. I never hit this problem and kind of assumed that the soundcards it happens on only supported 48khz sample rate. (with some crappy old on-board soundcards this is the case). But currently a lot of crappy onboard cards support 44.1khz up to 192khz and 24bit audio. (support the format, not that they actually can reproduce it). So why high cpu usage playing back music (44.1khz) on a card that supported 44.1khz? Well a quick look in "/proc/asound"
cat /proc/asound/card0/pcm0p/sub0/hw_paramsShowed that the card was in 48khz mode. After some googling i found this link: http://mpd.wikia.com/wiki/Tuning#Force_ALSA_dmix_to_44.1khz I quote:
Dmix by default uses 48kHz sample rate.What? lets read this again:
Dmix by default uses 48kHz sample rate.ok it realy sais it. So even if you are playing 44.1khz sound, on a card that supports it it resamples. Why is resampling bad? well 2 things: 1. It is lossy, espicialy 44.1->48khz as it is irrational conversion. 2. It takes lot of cpu. The better you want the conversion, the more cpu it takes. The internal resampler of alsa does not use a lot of cpu, but is terrible. If you let mpd resample using libsamplerate it will cost you 10-30% cpu. depending on your machine. So how to solve it? There are several solutions. 1. Let mpd bypass dmix. You can do this by setting plughw:0,0 (for the first soundcard, first output). However only mpd can play audio, you don't have mixing anymore. (unless your soundcard supports this in hardware) 2. Force dmix to use 44.1khz. Adding the following to my /etc/asound.conf
pcm.!default {
type plug
slave.pcm {
type dmix
ipc_key 1024
slave {
pcm "hw:0,0"
rate 44100
}
}
}
This makes my mpd (using autodetect output) play at 44.1khz. The cpu usage is now dropped to 1-2 % on a intel atom running at 800mhz. This was previously 12-15%.
Just beware that if you try to play audio at 48khz, it will be down-sampled to 44.1khz.