Saturday, April 20, 2013

GUIDE: 'Stereo Mix' without PulseAudio

[Cut to the chase]

Introduction

So I recently upgraded (yes, upgraded) from Windows to Linux a while ago, right, and now I want to record my gaming the way DXtory let me (except for free, of course).

So a quick bit of Googling revealed that I could use PulseAudio's 'Monitor of' feature. Great! Except I don't have PulseAudio, only ALSA... (the reason for which is a story for another time)

So several hours of Googling and trial-and-error later, here is my method for getting 'Stereo Mix' on ALSA:

snd-aloop

snd-aloop is this kernel module for Linux that sets up two loopback audio devices, each with 8 subdevices.

Playback device hw:1,0 is linked to capture device hw:1,1 - all audio a program outputs to device hw:1,0 can be captured by recording from hw:1,1.

The module is loaded with
# modprobe snd-aloop

Using arecord and aplay to route audio

arecord and aplay are simple command line tools to (you guessed it!) record and play audio from arbitrary audio devices. The best part, though, is that you can pipe audio from one to the other easily!

So, if my actual speakers are at hw:0,0 then I can pipe audio from my loopback speakers to my actual speakers with:
$ arecord -D hw:1,1 -f dat | aplay -D hw:0,0

The -D hw:1,1 tells arecord to record from hw:1,1 (the capture device my virtual speakers are linked to) and the -f dat tells arecord to record 48kHz stereo. By default, arecord outputs to stdout.

The pipe (|) tells the system to pipe the output of arecord to the input of aplay. aplay by default inputs from stdin, and we tell it to play that sound to hw:0,0 (my actual speakers).

Setting my virtual speakers as default

To make my applications output to my virtual speakers by default (so I can record them), I need to edit (or create) the .asoundrc file in my home directory:

pcm.duplex {

    type asym
    playback.pcm "hw:1,0"
    capture.pcm "hw:0,0"
}
pcm.!default {
    type plug
    slave.pcm "duplex"
}


The pcm.duplex { ... } part allows us to create an asymmetrical audio device. Here, a program playing audio will be directed to hw:1,0 (our virtual speakers), and a program recording audio will be directed to hw:0,0 (the real microphone - we don't need to use a virtual one).

The pcm.!default { ... } part just sets the duplex audio device as the default device for all programs.

Now, reboot, load the snd-aloop module and try to play some sound. You shouldn't hear anything. Now, run the arecord | aplay command, and if everything was set up correctly, you should hear sound through your speakers!

Recording the audio

Now that we've fixed our audio up so that it works properly, we can start recording audio.

First, go to the terminal where arecord | aplay is running, and press Control-C to exit it.

In order to record audio, replace the arecord | aplay command with:
$ arecord -D hw:1,1 -f dat | tee audio.wav | aplay -D hw:0,0

The tee command copies the output of arecord to a file (recording it) and passes it on to aplay (so we can still hear it).

All together now!

In ~/.asoundrc
pcm.duplex {
    type asym
    playback.pcm "hw:1,0"
    capture.pcm "hw:0,0"
}
pcm.!default {
    type plug
    slave.pcm "duplex"
}


After logging in, run
# modprobe snd-aloop

In order to hear sound (no recording), run
$ arecord -D hw:1,1 -f dat | aplay -D hw:0,0

In order to hear sound and record it, close arecord and run
$ arecord -D hw:1,1 -f dat | tee audio.wav | aplay -D hw:0,0

Congratulations!

If everything worked properly, you have now configured your computer to record a 'stereo mix'!

Labels: , ,

2 Comments:

At March 5, 2014 at 4:17 AM , Blogger Mauricio Rojas said...

Wow you are great man!!

Thanks for this golden information!!!, now I can record my audio on Audacity too jijijiji.

Saludos!!

 
At May 29, 2014 at 2:40 PM , Blogger Florian Dr said...

Hello I have tried your tutorial but I have a question: Is it working with flash player?

I have in flash player à new loopbak device but there is no sound in it!

Is there a way to use that for flash video chat?

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home