Skip to content

Audio configuration

Jason Gray edited this page Nov 13, 2020 · 1 revision

The purpose of this page is to guide the novice user though the steps required to get a functional default audio output.

*Users of DAC HATs please consult the supplier or manufacturer for information on how to enable your DAC.

Find your card

aplay -l

Should output something like this:

**** List of PLAYBACK Hardware Devices ****
card 0: sndrpihifiberry [snd_rpi_hifiberry_dac], device 0: HifiBerry DAC HiFi pcm5102a-hifi-0 [HifiBerry DAC HiFi pcm5102a-hifi-0]
  Subdevices: 0/1
  Subdevice #0: subdevice #0

Find your card's supported format(s) and sampling rate(s)

While no other audio is being played:

aplay -Dhw:<card #>,<device #> --dump-hw-params /usr/share/sounds/alsa/Front_Right.wav

For example:

aplay -Dhw:0,0 --dump-hw-params /usr/share/sounds/alsa/Front_Right.wav

The output should look something like this if the card and device combination is correct:

ACCESS:  MMAP_INTERLEAVED RW_INTERLEAVED
FORMAT:  S16_LE S24_LE S32_LE
SUBFORMAT:  STD
SAMPLE_BITS: [16 32]
FRAME_BITS: [32 64]
CHANNELS: 2
RATE: [8000 192000]
...

The above card supports formats S16_LE, S24_LE, and S32_LE, at up to a sampling rate of 192000.

Check to see if your sound card has hardware volume controls

amixer -c<card #> scontrols

For example:

amixer -c0 scontrols

The output should look something like this if your card has hardware volume control:

Simple mixer control 'PCM',0
...

So by now we should know the card # the device # the supported formats, sample rates and if the card has hardware volume control.

Now let's do a quick test to see if we're right.

This will be loud so take your headphones off and/or turn down whatever you Pi is connected to!!!

While no other audio is being played:

*speaker-test does not support S24_LE for some reason?

speaker-test -l1 -c2 -Dhw:<card #>,<device #> -r<samplerate> -F<format>

So for example if we wanted to make sure the card used in the above examples supported CD quality audio:

speaker-test -l1 -c2 -Dhw:0,0 -r44100 -FS16_LE

If you don't get any errors and you hear white noise you're all set.

You can use all of your new found information to configure a functional default output with the help of the below asound.conf by changing:

defaults.ctl.card
defaults.pcm.card
defaults.pcm.device

slave.pcm

in

pcm.!default

And optionally installing higher quality sample rate converters and uncommenting defaults.pcm.rate_converter.

Edit to your needs and copy and paste to /etc/asound.conf (the file does not exist by default).

  # /etc/asound.conf

###############################################################################

pcm.hqstereo20 {
    @args [
        SAMPLE_RATE FORMAT BUFFER_PERIODS
        BUFFER_PERIOD_TIME VOL_MIN_DB
        VOL_MAX_DB VOL_RESOLUTION VOL_NAME
    ]
    # Sampling rate in Hz:
    # 44100, 48000, 88200, 96000...
    # Defaults to 44.1 kHz (CD Quality).
    @args.SAMPLE_RATE {
        type integer
        default 44100
    }
    # Format:
    # S16_LE, S24_LE, S24_3LE, S32_LE...
    # Defaults to S16_LE (CD Quality).
    @args.FORMAT {
        type string
        default S16_LE
    }
    # Periods per buffer.
    @args.BUFFER_PERIODS {
        type integer
        default 4
    }
    # Period size in time.
    # Defaults to 125ms (0.125 sec).
    # BUFFER_PERIODS * BUFFER_PERIOD_TIME = buffer time/size
    @args.BUFFER_PERIOD_TIME {
        type integer
        default 125000
    }
    # Minimal dB value of the software volume control.
    @args.VOL_MIN_DB {
        type real
        default -51.0
    }
    # Maximal dB value of the software volume control.
    @args.VOL_MAX_DB {
        type real
        default 0.0
    }
    # How many steps between min and max volume.
    @args.VOL_RESOLUTION {
        type integer
        default 256
    }
    # The name of the software volume control.
    # If your card does not have hardware volume control
    # naming it PCM will cause most apps that use alsa volume
    # to use the software volume control.
    @args.VOL_NAME {
        type string
        default Softvol
    }
    type softvol
    min_dB $VOL_MIN_DB
    max_dB $VOL_MAX_DB
    resolution $VOL_RESOLUTION
    control {
        name $VOL_NAME
        card {
            @func refer
            name defaults.ctl.card
        }
    }
    slave.pcm {
        type plug
        slave.pcm {
            type dmix
            ipc_key {
                @func refer
                name defaults.pcm.ipc_key
            }
            ipc_gid {
                @func refer
                name defaults.pcm.ipc_gid
            }
            ipc_perm {
                @func refer
                name defaults.pcm.ipc_perm
            }
            slowptr 1
            hw_ptr_alignment roundup
            slave {
                pcm {
                    type hw
                    nonblock {
                        @func refer
                        name defaults.pcm.nonblock
                    }
                    card {
                        @func refer
                        name defaults.pcm.card
                    }
                    device {
                        @func refer
                        name defaults.pcm.device
                    }
                    subdevice {
                        @func refer
                        name defaults.pcm.subdevice
                    }
                }
                channels 2
                period_size 0
                buffer_size 0
                buffer_time 0
                period_time $BUFFER_PERIOD_TIME
                periods $BUFFER_PERIODS
                rate $SAMPLE_RATE
                format $FORMAT
            }
            bindings {
                0 0
                1 1
            }
        }
    }
}

###############################################################################

# Change to the card number that you want to be the default control card.
# Default: 0
defaults.ctl.card 0

# Change to the card number that you want to be the default playback card.
# It should usually be the same as defaults.ctl.card.
# Default: 0
defaults.pcm.card 0

# Change to the device number that you want to be the default device on the default card.
# 0 or 1 is usually the correct device number.
# Default: 0
defaults.pcm.device 0

# Change to the subdevice number that you want to be the default subdevice on the default device.
# Should rarely need to be changed.
# Default: -1
defaults.pcm.subdevice -1

# To install high quality samplerate converters on Debian based systems:
# sudo apt install -y --no-install-recommends libasound2-plugins 

# To list available rate_converter's:
# echo "$(ls /usr/lib/*/alsa-lib | grep "libasound_module_rate_")" | sed -e "s/^libasound_module_rate_//" -e "s/.so$//"

# Uncomment and replace speexrate_medium with the rate_converter of your choice.
# defaults.pcm.rate_converter speexrate_medium

pcm.!default {
    type empty
    # Optional args:
    # SAMPLE_RATE: default: 44100 
    # FORMAT: default: S16_LE
    # BUFFER_PERIODS: default: 4
    # BUFFER_PERIOD_TIME: default: 125000
    # VOL_MIN_DB: default: -51.0
    # VOL_MAX_DB: default: 0.0
    # VOL_RESOLUTION: default: 256
    # VOL_NAME: default: Softvol

    # Example:
    # hifiberry dac+ zero on a pi zero.
    # slave.pcm "hqstereo20:FORMAT=S32_LE,BUFFER_PERIOD_TIME=250000,VOL_MIN_DB=-48.0"

    slave.pcm "hqstereo20"
}

###############################################################################

ctl.!default {
    type hw
    card {
        @func refer
        name defaults.ctl.card
    }
}
Clone this wiki locally