Monday, May 16, 2022

Better Linux Sound Managment With ALSA, Part 2

Last week we learned how to configure soundcards for playback with ALSA. It’s easy, it works on all Linuxes, it doesn’t create conflicts or introduce latency, and it doesn’t require the X Window System. Today we’re going to use ALSA for recording, and learn how to test our sound system.

A quick and horrid way to see if sound is working is to pipe the contents of a file to dev/dsp:

$ cat  /dev/urandom > /dev/dsp

A more pleasant way is to use alsa-utils. It includes a number of useful commands for testing your setup. The speaker-testcommand tests your speaker channels. This example is for a simple two-speaker stereo setup:

$ speaker-test -t wav -D plug:front -c2 -l2  

speaker-test 1.0.14

Playback device is plug:front
Stream parameters are 48000Hz, S16_LE, 2 channels
WAV file(s)
Rate set to 48000Hz (requested 48000Hz)
Buffer size range from 16 to 16384
Period size range from 8 to 8192
Using max buffer size 16384
Periods = 4
was set period_size = 4096
was set buffer_size = 16384
 0 - Front Left
 1 - Front Right
Time per period = 2.731660
 0 - Front Left
 1 - Front Right
Time per period = 2.986495

You should hear a pleasant woman’s voice saying “Front left, front right”. -c2 means two channels, and -l2means run the test twice.

It’s common to have 5:1 surround sound even with onboard sound chips, so you can test this too:

$ speaker-test -t wav -D plug:surround51 -c6 -l2 

Note how the different channels are numbered. To test a single speaker, select it with the -soption. This example generates a sine wave tone at 45 Hz, which sounds like power hum, on the LFE channel:

$ speaker-test -t sine -f 45 -D plug:surround51 -c6 -s6 

Beware the channel-numbering gotcha- speaker-test output numbers from zero, but the -soption starts from one. Aren’t computers fun!

LFE means Low Frequency Effects, which usually means your subwoofer. Of course it’s more complex than that; see What is the LFE Channel?for more information.

Next: Digging Into Sound Cards »

How do you know what type of output your sound card supports? aplaytells the tale:

$ aplay -L
VIA 8237, VIA 8237
Front speakers
VIA 8237, VIA 8237
4.0 Surround output to Front and Rear speakers
VIA 8237, VIA 8237
4.1 Surround output to Front,
Rear and Subwoofer speakers
VIA 8237, VIA 8237
5.0 Surround output to Front, 
Center and Rear speakers
VIA 8237, VIA 8237
5.1 Surround output to Front, Center, 
Rear and Subwoofer speakers
VIA 8237, VIA 8237
IEC958 (S/PDIF) Digital Audio Output

If you ever need to specify the PCM device, this is where you find it: front, surround40, surround41, and so forth are PCM devices that should have been created automatically at startup. You could test these with aplay, like this:

$ aplay -D front test.wav

Playing WAVE 'test.wav' : Signed 16 bit Little Endian,
 Rate 44100 Hz, Stereo

You may also also test playback by using the card+device numbers. This is sort of a “raw” test, since it doesn’t depend on having a correct PCM devices configuration, but uses the card IDs assigned by the kernel. aplay -lshows the “raw” card+device numbers. 0,0 is the first device, 1,0 is the second device, and so on:

$ aplay -D hw:1,0 test.wav

If you have a second sound device, such as a USB headset, you can configure the PCM device names either globally in /etc/asound.conf or per-user in ~.asoundrc. Something as simple as this works just fine:

pcm.v8237 {type hw card V8237}
ctl.v8237 {type hw card V8237}
pcm.headset {type hw card Headset}
ctl.headset {type hw card Headset}

For pcm.v8237, the pcm part is required. The v8237 can be anything you want, and the same goes for the ctl lines. type hw card is required; V8237 and Headset come from the output of aplay -l. You could also use asoundconf:

$ asoundconf list
Names of available sound cards:

Now instead of using aplay -D hw:1,0 test.wav I can use aplay -D headset test.wav. You want to use the names (V8237, Headset) in your configuration file instead of the kernel IDs (0,0- 1,0), because the kernel IDs can change, especially udev devices.

Next: Recording With ALSA »

arecord is a recording utility included with ALSA. It’s not a mighty mixer recorder with trainloads of features, but a simple utility for making audio recordings in WAV format. You can fine-tune your sampling rate, interrupts intervals, buffer size, sample format, and all manner of finicky settings. Me, I stick with the shortcuts: the -f cd option records in CD format, and -f datfor digital audio. It’s great for scripting, and good quick test of your recording and playback:

$ arecord -vv  -D headset -fcd  test.wav
$ aplay -vv -D v8237 test.wav

Hit CTRL +C stop either command.

A couple of random nuggets: run cat /proc/asound/versionto get your ALSA version.

The examples are from the onboard sound on my low-budget ECS motherboard. As you can see, it supports digital output (S/PDIF = Sony-Phillips Digital Interface) in addition to analog. However, I can’t use it–there is neither connector nor headers on the motherboard. Beware when you are buying sound cards, because they often claim “24-bit digital sound!” or some such. All computer sound cards support digital sound- their main job is converting digital sound files for analog speakers. If you want digital output to digital speakers, read the specs carefully to be sure you’re getting real 100% digital.


Carla Schroder is the author of the Linux Cookbook and the newly-released Linux Networking Cookbook, and is a regular contributor to LinuxPlanet.

This article was first published on

Similar articles

Latest Articles