Hack33.Pass the Mic and Record Audio

Hack 33. Pass the Mic and Record Audio

You don't need a fancy GUI tool to record audio under Linux. Use SoX's rec utility to record audio from your microphoneand do it all from the command line.

Some people might wonder, with all the different graphical audio recording tools out there, why you would want to record sound from the command line. Audacity has everything you need to record, edit, and save audio in a nice interface, and I even cover how to use it to record audio in [Hack #34]. Even with these tools available, there are certain advantages to recording audio from the command line. For one, it's completely scriptable and can be easily scheduled with tools like cron and can be used to create a radio TiVo of sorts. For example, at one point there was a weekly radio show that I sometimes wasn't home to listen to. I wanted to record the broadcast and listen to it later, so I hooked up my computer microphone input with my radio's headphone output and left the radio tuned to that channel. Then I set a cron job to turn up my mic volume with aumix [Hack #13] and then run the recording program. This hack tells you how to use the SoX sound sample translator and its included rec frontend to start recording audio with just a few keystrokes.

The SoX sound sample translator is a very powerful audio editing tool. It not only can convert between a number of audio formats, but it can also modify audio input, adding echoes, fade in and out, and other effects. For this hack, I use a frontend included with SoX called rec, which uses SoX behind the scenes to record audio from your system's microphone.

The first step is to install SoX. Many other audio tools rely on SoX for their audio processing, so there's a really good chance that SoX is already installed on your distribution. If not, look for it with your distributions software installation tool. If for some reason SoX isn't available in a precompiled form, download the latest version from the official page at http://sox.sourceforge.net and compile it according to the included installation instructions.

The rec tool has only a few basic arguments, and for the simplest example you only need to pass it the output format and output file to use. Adjust your microphone volume in the mixer and type the following:

 $ rec -t .wav  output.wav  Send break (control-c) to end recording 

Start talking into the microphone, and press Ctrl-C when you are finished. If you run the file command on the output file, you will be able to see the file format:

 $ file  output.wav  output.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 8 bit, mono 8000 Hz 

By default rec will save to a monoaural file at 8,000 Hz. This format might work just fine for you if you intend to play it back with tools like SoX's play tool or even tools like mplayer, but if you want to encode it into something like an MP3 later on, certain encoding tools might have trouble with the format. To solve this problem change the sample rate that rec uses to 44,100 Hz with the -r argument:

 $ rec -r 44100  output.wav   Send break (control-c) to end recording  $ file  output.wav   output.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 8 bit,  mono 44100 Hz 

As you can see, the file is now encoded at 44,100 Hz. If you want to record in stereo sound, add the -c option to specify the number of channels to use, and set it to 2:

 $ rec -r 44100 -c 2  output.wav   Send break (control-c) to end recording  $ file  output.wav   output.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 8 bit,  stereo 44100 Hz 

That's all there is to it. In addition to the radio TiVo I mentioned above, there are a number of other useful applications for this. The fact that it's a command-line tool means it would be simple to write a short shell script to adjust the microphone volume and start recording, and then bind the script to a key. Then you could start recording (say during a meeting) at the push of a button.

Linux Multimedia Hacks
Linux Multimedia Hacks
ISBN: 596100760
Year: 2005
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net