Monday, November 02, 2009

Capturing sound in Linux using a USB headset (Plantronics DA40)

I picked up a Plantronics DA40 USB headset today and figured out how to capture .wav files to disk and play them back through the headset.

There are lots of other options, but I like this one because it's USB and I could use it with any Linux system, whereas if I used the native souncard, I'd probably have to do more per-machine troubleshooting. dmesg showed that it was detected okay:

[14563.672694] input: Plantronics DA40 Adapter as /devices/pci0000:00/0000:00:1d.3/usb4/4-2/4-2:1.3/input/input6
[14563.728138] input,hidraw3: USB HID v1.00 Device [Plantronics DA40 Adapter] on usb-0000:00:1d.3-2
[14563.909787] usbcore: registered new interface driver snd-usb-audio

Usually the mic comes muted by default, so I pulled up alsamixer to check. It came up with my native sound card first. So I ran "alsamixer -c 1" to try it with the second card, and that was indeed the DA40. Hitting TAB let met toggle between record and playback settings.

In the playback tab, there's a Mic option. You want to mute that ("m"), since that just loops the mic back through the speakers. On the Capture tab, there's a "Mic" field, and that's the one that actually sets the mic gain.

To record, I ran:

arecord -r 44100 -f S16_LE -D hw:1,0 foo.wav

(Hit control-c to end capture.) hw:1,0 let me record from card 1, the second sound card. It refused to record at its default setting of 8000Hz, so the 44100 and S16_LE were necessary. Then I was able to play back foo.wav with:

mplayer -ao alsa:device=hw=1.0 foo.wav

So far so good. Next, I wanted to know how much deviation I was getting (was the gain turned up enough?) so I downloaded a nifty, very old program called Wavesurfer, available as a statically linked binary:

I selected a "waveform" panel and loaded up foo.wav, and could see that I was only getting around +-500 units max (out of +-32767). In File... Preferences... Sound I/O, I set the Input and Output device to /dev/dsp1 (which I believe is the OSS version of the second sound card). Now I can record and playback from wavesurfer. I used alsamixer to tweak the Mic setting (remember to use the Record tab!) until I got +-10000 or so on my recordings. Anything over +-1000 would probably have been fine.

(BTW, wavesurfer will also show you nifty spectrograms, from which I learned a lot about the human voice.)

Here's a trivial script to start arecord recording in the background, and another script to kill it when you've had enough.


arecord -r 44100 -f S16_LE -D hw:1,0 /tmp/recording-in-progress.wav &
echo Recording. Wrote arecord pid of $ARECORD_PID to /tmp/
echo $ARECORD_PID > /tmp/


kill "`cat /tmp/`"

No comments: