Friday, December 12, 2008

How to Capture Sound From an Analog Microphone in ALSA Using Linux.


This is, in the compute biz, what we call a "wid". Wid is short for "What I Did". Not a lot of theory, I just show you what I did to record sound from my crappy on board VIA 8237 sound chip using Linux and a $10 microphone.

The sound isn't professional by any stretch of the imagination, but it's great for voice notes and sending messages.

The Prep.

This assumes that you already have ALSA hooked up and sound is working.

Start your output volume at mid-range. The level coming in from microphones varies wildly. You don't want it too low or you'll miss it if you get it working and you don't want it too high and have it blow a speaker.

Find a test .wav file and play it with the "aplay" command. This is just to make sure everything is working. You can search for wave files on your Linux box with the command

locate wav | egrep 'wav$'

If you can't find one, pull something down from the Net.

Hook Up the Microphone.

Plug in your microphone to the proper port on your sound card. It's usually red or pink and often has a little, hard to read, glyph of a microphone beside it.

Set Up the Mixer.

Start alsamixer.

I use the command line alsamixer to activate the microphone. A graphic mixer might get the job done, but I don't feel like fiddling around them.

I put alsamixer in it's own window so I can tweak things while testing.

Use the TAB key to set the "View" Mode to "All".

Some of the capture options show up in the playback screen. To minimize looking for the right setting, view all of them at once.

Turn on the Required Options.

This is the list of options that I believe need to be set. Because different sound chips boot up in different modes this list may not be complete. Also, the values I list are just values that work for me. There's nothing magic about the them.

Use the arrow keys to move from option to option.

Move to the "Mic" option. Hit the space bar. The work "CAPTUR" should show up in red. I set the level to 71.

Select "Mic Boost". If it's listed as off (under "Item"), then press the "M" key to turn it on.

If you've plugged your microphone into a jack in the front panel of your computer, (make *SURE* the panel is wired up), then use the "M" key to turn on "Mic Front Input".

If you have more than one microphone port, make sure "Mic Select" is set to the proper microphone. I use Mic1.

Select "Capture". Hit the space bar. The work "CAPTUR" should show up in red. I set the level to 67.

Try a Capture.

Now to try a capture. I use the following, which captures sound from the microphone and plays it out the speakers (with a slight delay). It keeps running until you exit with Control-C, so you can keep it running while you tweak things.

arecord -f dat -t wav -D hw:0,0 - | aplay -

The hw:0,0 comes from Card 0, Device 0, which I got from

arecord --list-devices

Don't worry if the sound is too soft. You can fix that with sound processing software. Make sure you have "Mic Boost" on if your chip supports it.

To save your sound to a .wav file, use:

arecord -f dat -t wav -D hw:0,0 foo.wav

From there you convert it as you see fit.

What I Really Use.

At this point almost any sound program should work for you. I'm pretty hardcore command line, so I use "sox" to tweak the sound.

To turn up the sound on a .wav file under Sox use something like

sox foo.wav -v 7 bar.wav

Because sox can also do audio capture while tweaking the volume I do both in one command.

sox -t alsa default -v 7 foo.wav ; aplay foo.wav


This is as far as I've gotten and all seems well. I'm sure I'll use this technology to change the world, but for now I'm having too much fun singing bad Sinatra.