Change sampling rate in ALSA

Alsa by default uses the same sampling rate and format as the source. It is however possible to force the sample rate up (or down).

Here is one way you could do it. (in /etc/asound.conf or ~/.asoundrc)

pcm.device{
        format S24_LE
        rate 96000
        type hw
        card 0
        device 0
}

Then just make that pcm a slave to another one, like your default(stereo) pcm (warning: the 24 bit format won't work unless you have a compatible card, I normally use S32_LE which works on most cards.)

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

the above will however only work for stereo sources (so if you play from a 5.1 source that will by default use the surround51 pcm, things get a bit more complicated)

The one way I know (and personally use) about how to force the sample rate of non-stereo sources is taking parts from this asound.conf/asoundrc(dead link) and modifying them. (Note: I did not write that file myself)

(You can find my asound.conf in this question and just copy that directly instead of going through the hassle of manually modifying it below below)

First copy the "dmixed" pcm, and modify it's hardware section to the desired sample rate and format.

Then copy the !default, surround40 and surround51 pcms from it as they are. This will effectively dmix all output and upconvert the sample rate of all 2.0, 4.0 and 5.1 sources to the sample rate that was set in the dmixed pcm.

In that configuration file you may also find the "upmix20_51" pcm interesting as it is an effictive way to upmix 2.0 sources to 5.1 by selectively duplicating the sounds to the other channels. If you want to use it you will have to change the type of it from "plug" to "route" and then set it as a slave to the default pcm.

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

As "surround51" which that pcm uses uses "dmixed" as a slave, the forced sample rate will apply to this pcm as well.

Sadly with the way things are now, mono sources will only output from the left speaker, but that is hardly a problem as mono sound sources are rare these dadys. The example I just gave would be exactly how I have my own asound.conf configured, enjoy if you have a 5.1 speaker setup and want to force 96khz sample rates.

In theory I would think that forcing up the sample rate won't achieve anything for you (as it is an absurd thought that it would improve the quality of the audio from it's source which had a lower sample rate) as such you will be a lot better off configuring the application to use 96khz output.

But for me I had some problems with crackling sounds (in specific games, like Xcom: Enemy Unknown) if I didn't force either 96khz or 44.1khz frequency. Seeing as I don't lose anything at all from forcing 96khz (it neither increases nor decreses quality of 44.1/48khz sources) it was a perfect solution.


The device's sample rate is by default what the application has configured for it, i.e., the sample rate of the original file.


The other answers are somewhat correct.

Most sound cards don't have multi-channeling by default.If you have one of those cards that does have multi-channel mixer in them - you probably don't need to read here. ALSA does mixing of multiple sounds in software.

Generally speaking 2 options are possible:

  1. Bypass mixing software output all together and you may enjoy larger range of sound.(higher sample rate and bit rate for example, channeling to 6 or more speakers).

    Only one program is allowed to work with sound card!

  2. Enable software mixing, and loose some quality which you will probably not notice anyway.(Asus Xonair DX with Sennheiser HD598 tested, didn't notice difference whatsoever, used 96 khz 6 channel sample rate flacs and 192 khz 2 channel ones).

    More than one program can work that way! I'm not an audiophile, mind you, not yet anyways.

Second option is enabled by default if your hardware does not support mixing of several streams at the same time.

NOTE: There is also third option. ALSA can be configured with re-sampling in software, but from my experience, while it is possible to adjust all apps to be re-sampled to max highest sample rate and bit rate - it introduced noticeable lag in FPS games, which is NOT OK for a gamer.(third of a second or so, when i shot the bullets from gun, which annoyed hell out of me).

Be advised, that the third option is probably not practical for a few reasons:

  1. I did not test it well, but I guess some apps may still have issues with it.
  2. Most audio collections are in 44.1/44 khz sample rate with 16 bit rate, unless you use a lot of FLAC, but even then, FLAC has to have higher sample rate in it.
  3. up-sampling does NOT provide better quality of sound (theoretically it does, but if you use cheap headphones - you won't notice it, I'm 99% sure).
  4. lag in games is very real, unless I did something wrong in my .asoundrc file.

To make things more confusing it may get even worse in a good way. You can create "devices" with different settings(channels/rate/sample rate/physical output and input configuration). And some apps can use devices you manually create, some not. It annoys the hell out of me.

Most notorious and problematic ones: flash player.

This answer is true for someone who is not using USB connected earphones. They have their own built in sound cards and I have limited knowledge on that, probably it's more true to those with built in motherboard sound cards. May still be applied to USB headsets, but take whole thing with grain of salt. I am guessing probably around if not lee 10% of all produced sound cards would be using hardware mixing anyway as hardware would lack it as an option. Optical output is one big exception. AFAIK optical output does not need mixer. De-multiplexing is going on in "receiver", where it's done is hardware. Don't take my word for it.

Tags:

Ubuntu

Audio

Alsa