# A simple and dramatic improvement of sound quality

## octoploid

This is a tip for all soundcards that internally resample everything to 48khz.

(e.g.:AC'97 onboard chipsets, all Creative Live and Audigy series, Hercules Fortissimo I/II/III)

Due to their limited power and flexibility, they perform quite crappy resampling on non-48khz material

like most music (44.1khz mp3s, flac , etc.). 

The solution is to use Alsa's Dmix resampler with the "samplerate" plugin. This plugin offers

excellent quality and you can immediately hear the difference. 

First emerge  media-plugins/alsa-plugins with the "libsamplerate" USE flag.

Then create /etc/asound.conf :

```
defaults.pcm.rate_converter "samplerate_best"

pcm.!default {

       type plug

       slave.pcm "dmixer"

   }

   pcm.dmixer {

       type dmix

       ipc_key 1024

       slave {

           pcm "hw:0,1" #That's for my VIA 8237 on-board soundchip all others should use hw:0,0

           period_time 0

           period_size 1024

           buffer_size 16384

           rate 48000

       }                                                                                                                                   

       bindings {

           0 0

           1 1

       }

   }

   ctl.dmixer {

       type hw

       card 0

   }

```

The following converter types are available:

  - samplerate_best     Use SRC_SINC_BEST_QUALITY

  - samplerate_medium   Use SRC_SINC_MEDIUM_QUALITY

  - samplerate          Use SRC_SINC_FASTEST

  - samplerate_order    Use SRC_ZERO_ORDER_HOLD

  - samplerate_linear   Use SRC_LINEAR

samplerate_best uses ~10% of my CPU (Athlon64) when playing back music.

The other converters need less processing power. Just experiment a little with the converters.

Further reading:

http://www.mega-nerd.com/SRC/index.html

http://www.hydrogenaudio.org/forums/index.php?s=b6c927e64fdd78973cbe46f3408e88e2&showtopic=47591&st=0

http://ubuntuforums.org/showthread.php?t=344599

----------

## swimmer

Since I'm not very fluent in asound.conf language I dare to ask you: how does such an asound.conf for 2 cards look like?  :Wink: 

Greetz

swimmer

----------

## Akkara

Thanks for the tip!

Something to try, if most of the music is 44.1 KHz files and your sound card supports it, try resampling to 88.2 KHz.  I find I can get away with using samplerate_medium at that rate, while not constraining bandwidth for the 48KHz sources.

Something I've been wondering / hoping about: Is it possible to configure it so that when there's only one sound playing, alsa/dmix uses whatever native sampling rate that sound was recorded at, and only firing up the resampler when there's another concurrent sound with a different rate than the first?  It seems most of the time there's only one sound playing and this would give the cleanest possible sound as well as use the least resources.

Edit:  I just read your links. OMG! I can't believe the default dmix is to use linear "resampling".  No wonder it had sounded so crappy (and I had not been using dmix on account of that).  Linear resampling audio is like using bubblesort in a database app - it simply shouldn't be done, ever, IMHO.

----------

## Insanity5902

Question :: if I change the rate 44100 and everything still works, would it be better to leave it there, or continue to let it resample.

I have the newer HD Audo from the ICH7?

My initial thought it is, 48000 provide better sound, so why not use it if I have the horsepower (which I do)

----------

## Akkara

If most of your music is at 44100, having dmix operate at 44100 is probably a good idea.

You'll lose some of the extreme highs when playing 48KHz sources but if you don't listen to 48KHz material as much, it woun't matter that it's not optimized.

----------

## octoploid

 *swimmer wrote:*   

> Since I'm not very fluent in asound.conf language I dare to ask you: how does such an asound.conf for 2 cards look like? 

 

I would duplicate the slave section with pcm "hw:1,0" ...

 *Quote:*   

> Question :: if I change the rate 44100 and everything still works, would it be better to leave it there, or continue to let it resample. 

 

It's always better not to resample if it is not necessary. So if your soundcard handles 44100 Hz fine then 

just leave dmix there.

----------

## Non_E

Hi,

what was your former alsa-plugins use flag settings? This is mine:

```
media-plugins/alsa-plugins-1.0.14  USE="ffmpeg -debug -jack -libsamplerate -pulseaudio"
```

Will there be any gain in using libsamplerate instead of ffmpeg?

----------

## octoploid

 *Non_E wrote:*   

> Hi,
> 
> what was your former alsa-plugins use flag settings? This is mine:
> 
> ```
> ...

 

Yes ffmpeg is another possibility. I haven't tested it myself yet, but I guess the plugin

is not so resource hungry as libsamplerate and the sound quality will be lower.

Update:

OK I've tested the plugin and cannot recommend it. It's quite awful. The resulting sound

is distorted. Playing back udial.wav produces strange artifacts. None of this happens if I use libsamplerate.

Update 2:

While I was testing the ffmpeg plugin I discoverd that there is another undocumented 

resampler in the alsa library called speexrate. There are three settings avialable:

speexrate

speexrate_medium

speexrate_best

The quality seems to be excellent. I could not hear any difference compared to libsamplerate.

The CPU usage is also lower. I'm using speexrate as my new default converter now.

----------

## fank

how to use lattest mentioned resampler e.g. "speexrate"

should I change "samplerate_best" to "speexrate_best" in asound.conf to test it?

----------

## octoploid

 *fank wrote:*   

> how to use lattest mentioned resampler e.g. "speexrate"
> 
> should I change "samplerate_best" to "speexrate_best" in asound.conf to test it?

 

Yes   :Smile: 

----------

## Non_E

I added the asound.conf with samplerate_best settings and the sound is not distorted with samplerate_best settings. I also noticed that my xine-lib can make udial.wav sound really ugly  :Sad: 

----------

## Insanity5902

Yeah, I had to take it off b/c flash movies quiet playing and I haven't had time to look at it.

----------

## Non_E

 *Insanity5902 wrote:*   

> Yeah, I had to take it off b/c flash movies quiet playing and I haven't had time to look at it.

 Thanks for info, I did not know the reason flash movies stopped working for me.

----------

## octoploid

 *Insanity5902 wrote:*   

> Yeah, I had to take it off b/c flash movies quiet playing and I haven't had time to look at it.

 

Yeah, I had the same problem, but I solved it by hand-installing a 32-bit version of

libasound_module_rate_speexrate.so . 

Basically I just downloaded a 32-bit rpm version here.

Then I ran rpm2targz libalsa-plugins-1.0.14-1mamba.i586.rpm and copied 

libasound_module_rate_speexrate.so

libasound_module_rate_speexrate.la

libasound_module_rate_speexrate_medium.so 

libasound_module_rate_speexrate_best.so 

from the tarball to /usr/lib32/alsa-lib .

Thats all. Sound works now for all 32-bit clients on my 64-bit machine.

----------

## Insanity5902

LOL, forgot flash is 32bit, oops  :Smile:  Thanks for the fix.

----------

## Thesee

Hi,

I tried to follow this post, but I couldn't work with MPD anymore. I looked around, and found this from the alsa-lib ebuild:

 *Quote:*   

>  * Starting from alsa 1.0.11_rc3 the configuration for dmix is changed.
> 
>  * Leaving around old asound.conf or ~/.asoundrc might make all apps
> 
>  * using ALSA output crash.

 

So should I still try to make it work or not?

Thanks  :Smile: 

----------

## Cazzantonio

libsamplerate can be compiled with fftw use flag.

I suppose that would mean that the library will use sci-libs/fftw to do the samplig...

Has anyone tried this out? Did you find any improvement over the non-fftw version?

----------

## octoploid

 *Thesee wrote:*   

> Hi,
> 
> I tried to follow this post, but I couldn't work with MPD anymore. I looked around, and found this from the alsa-lib ebuild:
> 
>  *Quote:*    * Starting from alsa 1.0.11_rc3 the configuration for dmix is changed.
> ...

 

You could check out the MPD wiki.

And also the Gentoo ALSA Howto.

 *Quote:*   

> libsamplerate can be compiled with fftw use flag.
> 
> I suppose that would mean that the library will use sci-libs/fftw to do the samplig...
> 
> Has anyone tried this out? Did you find any improvement over the non-fftw version?

 

Libsamplerate needs sci-libs/fftw version 2, so you have to mask version 3 to give it a try.

I didn't bother with it myself, because I use the speexrate SRC.

----------

