Change sampling rate in ALSA

78,025

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

Think, that it is not true. Had same issue. Alsamixer is always resampling all streams to default 44,1 kHz. I cannot imagine other way how to mix streams with different sampling rate than resample them to one common. Proper file for change this is ~/.asoundrc

http://www.alsa-project.org/main/index.php/Asoundrc

You can check result here: cat /proc/asound/card0/pcm0p/sub0/hw_params (or card1 if you have 2 cards)

Share:
78,025

Related videos on Youtube

Taranfx
Author by

Taranfx

Updated on September 18, 2022

Comments

  • Taranfx
    Taranfx over 1 year

    I'm a Computer Audiophile, I've used Windows (with ASIO4ALL)and OSX for Audio. Now I'm making Ubuntu 13 my HTPC/Stereo Audio. To begin with I disabled Pulse audio and using GooMusicplayer with ALSA.

    My DAC is capable of 24bit 96khz. I can't seem to find a configuration for ALSA to set output sampling rate. How can I configure it?

  • Jeff Hewitt
    Jeff Hewitt almost 11 years
    Please add an explanation of what your link contains.
  • Cestarian
    Cestarian about 9 years
    Nope, might be a year too late here, but my favorite media player right now, audacious. That one can mix up to any number of khz if I remember correctly.
  • sezanzeb
    sezanzeb about 2 years
    The sample rate that the application configured is visible via cat /proc/asound/card0/pcm0p/sub0/hw_params (depending on your card and device id, see arecord --list-devices)
  • Admin
    Admin almost 2 years
    @sezanzeb How do you change those rates? It's setting pcm0p to 48000 and pcm0c to 44100, which are not supported simultaneously by the hardware.
  • Admin
    Admin almost 2 years
    @endolith As sezanzeb said, the sample rate is configured by the application. You'd have to tell the application to change it.
  • Admin
    Admin almost 2 years
    @CL. I mean the system wide setting in Ubuntu, not an application. Like when you open the Settings panel and select a device. It uses different sample rates for recording and playback, but there's no option to change it.
  • Admin
    Admin almost 2 years
    @endolith Depends on what you use. in pulseaudio you can configure default-sample-rate, alternate-sample-rate and avoid-resampling via config files. In jack you can set a fixed sample rate in e.g. cadence. In pipewire there is probably also some way to configure it.
  • Admin
    Admin almost 2 years
    @sezanzeb It's just the default Ubuntu setup, so PulseAudio? default-sample-rate doesn't affect both capture and playback?