Tutorial - Recording Computer Playback on Linux

From Audacity Development Manual
Revision as of 15:28, 16 December 2011 by Edgar (talk | contribs) (ednote)
Jump to: navigation, search
ToDo-2 Ready for Proofreading Peter 2Aug11
  • needs a Linux user to review it.

Ed 16Dec11: proofed for spelling, grammar and style

On Linux there are in general two different sound systems that provide drivers to your sound device, OSS and the more recent ALSA. Additionally, advanced sound servers such as PulseAudio and JACK can use ALSA to provide professional audio features such as routing of inputs and outputs, high quality mixing and resampling, aggregation of devices or sound cards and (in the case of JACK) very low latencies.

PulseAudio

PulseAudio is a sound server providing professional audio features such as mixing and input/output routing. It is the default audio device on many Linux distributions including Ubuntu, Open Suse and many Debian/Ubuntu based distributions. PulseAudio operates as a proxy between sound applications and the audio hardware (usually via ALSA).

Recording audio playing on the computer is made possible by configuring PulseAudio so that Audacity receives audio from the "Monitor" of the audio device that is being used for playback. The configuration can be made using the PulseAudio Volume Control (pavucontrol).

Installing the optional PulseAudio Device Chooser and PulseAudio Volume Control lets you connect applications like Audacity to record directly from playback applications like Firefox or Rhythmbox. This avoids recording possibly unwanted system sounds. For details of how to configure Audacity to use PulseAudio Volume Control, see Stereo Mix with Ubuntu 10 on the Audacity wiki (the principles stated there apply to most Linux distributions where PulseAudio can be used).

JACK

Jack Audio System is commonly used in Linux distributions that specialise in media creation such as Ubuntu Studio, Studio 64 and Musix.

To use Audacity with Jack it is useful or in some cases essential to have a few additional packages installed. This documentation assumes that you have Jack Audio System working on your machine. For help with setting up Jack, please consult the documentation for your Linux distribution.

As of Audacity 1.3.13, recording sounds playing from other applications is rather easy with Jack. Jack may be started using QjackCtl or from the command line. If Audacity is launched after Jack is started then "Jack Audio Connection Kit" will be an available host option in the Device Toolbar. If Jack is started after Audacity has been launched then it will be necessary to Transport > Rescan Audio Devices for new audio devices before jack is available to Audacity.

Any Jack compatible applications that are running before Audacity is launched will have their ports available in the Device Toolbar. To make an application available to Audacity that is launched after Audacity it is necessary to rescan audio devices again. After rescanning audio devices, input and output ports from other applications will be available in the Record and Playback settings of the Device Toolbar.

In this image the output of Hydrogen drum machine is being selected as the recording source. This will enable Audacity to record directly from the audio output from Hydrogen. If "System" is selected as the recording source then Audacity will record from the physical sound card input.

Selecting the recording source in the Device Toolbar


Applications that do not directly support Jack may also be used with Jack on a system that uses PulseAudio (such as Ubuntu and Debian based distributions) by installing "pulseaudio-module-jack". This provides the modules "Jack Source" and "Jack Sink" that allow PulseAudio to use Jack. For example, to record sounds playing through Firefox, PulseAudio Volume Control (pavucontrol) can be used to direct the output from Firefox to Jack Sink. The recording input for Audacity can then be set to record from "PulseAudio Jack Sink" and the sound will be recorded.

ALSA

Alsamixer

To record computer playback with Audacity you can use the alsamixer mixer device to select the "capture" device of your sound card and adjust its input volume. This will record all audio playing through the sound card, including system sounds and possibly sounds from applications other than the one you want to record. To capture sound directly from the application producing it, try using the ALSA PCM file or (on many Linux distributions) PulseAudio.

Alsamixer should already be installed. Alsamixer is a command line tool with "view" options to represent the sound device graphically.

To start alsamixer, open a shell/terminal and start the program by typing alsamixer. There are three different views: Playback (default), Capture and All. This is the Playback view, which you can get to at any time by pressing TAB or F3

Alsamixer Playback view

Top left you should see some information about your sound card. Below are volume bars for adjusting the output volume for each named device: examples are Master, PCM, Line, CD and so on. The currently selected item appears in red and the volume percentages are displayed for left and right channels above the device name. For a mono device, only one value is shown.

You can scroll between devices using the left/right arrow keys. Use up/down arrow keys to adjust the volume. Press the M key to mute/unmute the device. When a device is turned off, M (mute) appears below the volume bar. When it's turned on, O in green appears instead.

Press the TAB key or F4 to switch to Capture view. This is preferable to switching to All view with Enter, because All may not show all of the capture devices for your card.

Alsamixer Capture view

Select the device called Capture with the spacebar. The red CAPTURE flag should appear underneath the Capture bar to indicate that capture is turned on for that device, and L and R letters will indicate that left and right channels are turned on. Devices whose capture are turned off are shown by ------. Select the "Mix" device (this might sometimes be called "Master").

Bill 07May11: At this point we're getting into stuff that should be in Setting up Audacity to record from your chosen input. Is this different enough from the generic instructions on that page that we should leave it here? Also, we may want to rewrite this to emphasize using the Device Toolbar.

Gale: Also for this and recording computer playback on Windows I guess we have to consider what is happening to the Wiki pages. I was envisaging they would not be replaced and would have more detail / mention more esoteric methods than the Manual (possibly to the extent of having an "image for every step" which is what Windows novices possibly need).
Bill: Indeed we will need to decide how much detail to put in these manual pages, and when to send people to the wiki. I've just copied everything for now. Easier, I think, to trim than to add. So, for example, we could lose the (empty) Jack and OSS sections on this page rather than try to write those sections, as they could be considered "esoteric".

Launch Audacity and open the preferences using CTRL + P or navigate to it by clicking Edit > Preferences then click on the Devices tab. Select Alsa: default as playback device and Alsa:<Vendor ID>:<Vendor ID> (hw:0,0) as recording device ("0" represents the default sound card). Click OK to exit Preferences, then press the red Record button or the R shortcut to start recording the audio playing on the computer. If the audio is too soft or too loud so it distorts, you will need to check both the output levels in the Playback view of alsamixer, and adjust the recording level in Audacity (or do so in the Capture view of alsamixer).

Using the ALSA PCM file

It is not necessary to use Audacity to record sounds playing on the computer. You can capture the sound from the application producing it before it reaches the sound device, which avoids capturing unwanted system sounds. This solution can be used even if there is no sound device. Note: this solution may not work on a system using PulseAudio but you can use PulseAudio Volume Control instead.

First you need to create (or edit if it exists) ~/.asoundrc file. Do it with your favorite plain text editor. Note: ~ is your home directory so change that to /home/john or whatever if you are not working in a Linux shell.

Ed 16Dec11: This "edit or create" section is a bit terse. I would guess (wrongly) that one would copy all these gray text boxes and paste them into a newly created file, but what happens with a file I am editing? Do I erase what is there and replace with this or add this at the end/beginning?
pcm.teeraw {
   type empty
   slave.pcm "tee:default,'/tmp/out.raw',raw"
   # 48000 S16_LE 2ch (aplay -t raw -f dat)
}

Now tell the application from which you are trying to record to use the "teeraw" ALSA device for playback - read the application's manual for help. Although there is no interface to do this for Adobe Flash, it can be achieved by setting the FLASH_ALSA_DEVICE environment variable to the name of the sound device to use, e.g.

FLASH_ALSA_DEVICE=teeraw firefox

Thanks to wensley.org for this tip.

Run the application. Stop the application. Now /tmp/out.raw should contain your sound with perfect quality. If you change "default" to "null" in the above text, you will not hear anything but sound will still go to the file. If "empty" does not work with your ALSA library try with "copy".

If you want to record all sounds played through the computer, you'll need to set the teeraw device as your default device with something like:

pcm.!default{
   type empty
   slave.pcm "teeraw"
}

In this case though, you will need to change "tee:default,'/tmp/out.raw',raw" to "tee:null,'/tmp/out.raw',raw" or something else because otherwise you create a loop. In many cases changing "null" to "dmix" should do the trick - you record any application audio and still hear it through your speakers. Note that you'll pick up random sounds from other applications such as system "dings" and the raw file could get corrupted or overwritten. Unless your ALSA install allows you a setup like below you must absolutely make sure that only the desired application is producing sound while you are recording this way.

With newer ALSA versions you can try these parameters for convenience (also you can adapt to use a different encoder than lame):

pcm.teewav {
   type copy
   slave.pcm "tee:default,'/tmp/out.raw',wav"
}
pcm.teemp3 {
    type file
    format "raw"
    slave.pcm "default"
    file '|/usr/local/bin/lame -S -r -s %r --bitwidth %b -m j - /tmp/asound-$$.mp3'
}


OSS

OSS was the standard audio API in Linux up through the 2.4 series of official (kernel.org) Linux kernels. ALSA was added starting with 2.5, in those versions, Linux kernel authors marked OSS as deprecated.

Development of OSS was continued by 4Front Technology, initially as proprietary software but later released as partially open source. Possible solutions to recording audio playing on a computer using OSS are described here.


Use a loopback cable

This is a simple and effective method of recording what is playing on the computer. It has the disadvantage that the audio signal is converted from digital to analog, then from analog to digital, instead of staying in the digital domain. This may result in some slight increase in noise and distortion.

Plug your line-out or headphones-out to the line-in using a cable with a stereo mini-plug at each end, then record from line-in. To hear the sound while recording, use a single to double stereo connector to give you a jack for the speakers or headphones.

If you have only mic-in and no line-in is selectable through alsamixer, you can build an attenuator to reduce the line level output to a level suitable for mic-in. But probably this will only provide mono recording, because mic-in is mono in most cases. As well, the quality may be questionable.

Links

|< Tutorial - Recording audio playing on the computer