If you use Ubuntu, then they chose for you to use PulseAudio as a layer on top of Alsa by default. It was a good choice. At first glance the power of PulseAudio is not readily apparent. It’s not really clear what it can do for you other than further confuse the messy state of audio on Linux to begin with.
It’s actually a very powerful layer that allows you to control internal audio streams much like a patchbay or mixer of inputs and outputs and the wires that connect them together. It’s also useful to workaround bugs in Alsa. =) For example, the Intel HDA audio controllers are well known for having bugs with Alsa. Personnally I have experienced these Alsa bugs related to low volume, recording, and problems with capture mixer settings and so forth. PulseAudio allows me to work around these issues by using Pulse to re-route my audio streams to where I want them to be.
I recommend reading this guide and following each item step by step. It has helped me a great deal to understand how Pulse and Jack work. You really do need to spend the 30 minutes or so to read through that guide and make the changes that are discussed, Pulse and Jack won’t work properly unless you do.
Once you have done that, I have outlined some examples below of using Pulse and Jack along with programs such as gtk-recordMyDesktop which will allow you to make screen captures along with the internal audio mix, as well as making audio-only captures of the “stereo mix” (audio that comes out of your speakers) with programs like arecord, and doing real time MP3 capture of this audio. The uses for these tips are endless, especially if you need to capture some internet media that can’t be grabbed by sniffing out a URL.
GTK-RecordMyDesktop video grabbing with audio:
Start JACK Control and start the Jack server.
Open the application that will play the audio/video (your web-browser for example) and then using the PulseAudio Volume Control, move the browser’s playback stream to the Jack sink.
Open one each of the PulseAudio playback and recording VU meters, move both of their streams to the PulseAudio JACK Sink.
In output devices, set the Jack sink as default. In input devices, set the Jack source as default.
In gtk-recordmydesktop, set the audio to be captured from JACK, select both system:capture_1 and system:capture_2.
Set up your recordmydesktop window and start the recording, open the Jack connection box and connect the PulseAudio JACK Sink to the recordMyDesktop input port.
Now you should end up with a video that contains the same audio that you heard through your speakers. You have to use the Jack method with recordmydesktop because apparently it only works with alsa and oss, and gtk-recordmydesktop is only acting as a frontend to write out the command line. It would be an interesting experiment to try using the Pulse wrapper (padsp) but anyway the Jack method works perfectly and is easier to do with the GUIs.
Stereo Mix audio capture using PulseAudio routing:
Start the playback of the audio source, for example an audio stream coming from some website.
In the PulseAudio Volume Control, set the ALSA PCM as the default in the input and output devices.
Open one each of the PulseAudio playback and recording VU meters, move the stream of the recording meter to the Monitor Source of the ALSA PCM. As the audio is playing, you should see both meters response following each other.
Now you are ready to record. Use arecord without specifying a device name (-D).
arecord -f cd -vv test.wav
arecord -f cd -t wav | lame -h -V2 – high_quality_output.mp3
arecord -f cd -t wav | lame -m s -a -b 64 – mono_low_44khz_output.mp3
arecord -f S16_LE -c1 -r22050 -t raw | lame -r -s 22.05 -m m -b 64 – mono_low_22khz_output.mp3
The first command will grab the audio with “CD” quality (44.1 kHz, stereo). The second command is an example of piping arecord’s capture into lame and making a real-time MP3 with the -V2 vbr quality. The other commands are just further examples of using different sampling rates, mono, low bitrate, etc.. Read the man pages for arecord and lame and you can make up your own commands as desired.