Create virtual device in .asoundrc file
I solved this issue as follows:
First, load the snd-aloop
module:
sudo modprobe snd-aloop
This will create a new device called Loopback
:
± % cat /proc/asound/cards !10017
0 [PCH ]: HDA-Intel - HDA Intel PCH
HDA Intel PCH at 0xc1814000 irq 60
1 [HDMI ]: HDA-Intel - HDA Intel HDMI
HDA Intel HDMI at 0xc1810000 irq 61
2 [Loopback ]: Loopback - Loopback
Loopback 1
From the information shown above we have to create two hw
devices:
hw:0,0
(PCH is my main sound card).hw:2,1
(TheLoopback
virtual device).
Normally, hw
devices have the form: hw:X,Y
. For our purposes, it seems that Y
for the main card is always 0
while for the Loopback
we have Y
equals 1
.
Now, in order to make all of this work, we need a ~/.asoundrc
file with the following:
pcm.!default {
type asym
playback.pcm "LoopAndReal"
#capture.pcm "looprec"
capture.pcm "hw:X1,Y1"
}
pcm.looprec {
type hw
card "Loopback"
device 1
subdevice 0
}
pcm.LoopAndReal {
type plug
slave.pcm mdev
route_policy "duplicate"
}
pcm.mdev {
type multi
slaves.a.pcm pcm.MixReale
slaves.a.channels 2
slaves.b.pcm pcm.MixLoopback
slaves.b.channels 2
bindings.0.slave a
bindings.0.channel 0
bindings.1.slave a
bindings.1.channel 1
bindings.2.slave b
bindings.2.channel 0
bindings.3.slave b
bindings.3.channel 1
}
pcm.MixReale {
type dmix
ipc_key 1024
slave {
pcm "hw:X1,Y1"
rate 48000
#rate 44100
periods 128
period_time 0
period_size 1024 # must be power of 2
buffer_size 8192
}
}
pcm.MixLoopback {
type dmix
ipc_key 1025
slave {
pcm "hw:Loopback,0,0"
rate 48000
#rate 44100
periods 128
period_time 0
period_size 1024 # must be power of 2
buffer_size 8192
}
}
Modify hw:X1,Y1
with the values for your main card (in my case hw:0,0
).
You can test that this configuration works by playing something in your computer. If you hear it, so it is fine. Now let's play something in the computer, and record it from this virtual device with ffmpeg
:
ffmpeg -f alsa -i hw:X2,Y2 -c:a libmp3lame -b:a 256k -vn capture.mp3
Where hw:X2,Y2
has to be replaced by the Loopback
device, in this case
hw:2,1
.
I suppose that using arecord
should work too.
Related videos on Youtube
gm_
Updated on September 18, 2022Comments
-
gm_ over 1 year
I have been trying to create a virtual device to record the internal audio of my sound card using pure ALSA. After googling a lot, I found a
.asoundrc
file¹ that is very near of what I am looking for:pcm.mycard { type hw card 0 } ctl.mycard { type hw card 0 } pcm.myconvert { type plug slave { pcm "myrecord" format S16_LE } } pcm.myrecord { type file format "raw" slave.pcm "mycard" file "| oggenc -Q -q6 -r -B %b -C %c -R %r -o /tmp/record-$(date +%%Y-%%m-%%d_%%H%%M%%S).ogg -" } pcm.!default{ type asym playback.pcm "myconvert" capture.pcm "mycard" }
This configuration makes possible to record a
ogg
file with all audio played in my computer. What I would like to do now, is to modify thepcm.myrecord
part of the configuration, so that the audio is "streamed" in a virtual card that I could capture later with let's say,arecord
. I would be glad if someone could help me with this.I tried this: How do I create virtual ALSA device from which I can record everything that is played? but that did not work for me. Below, the output from
arecord -L
:muammar@zarathustra /tmp % arecord -L !10015 null Discard all samples (playback) or generate zero samples (capture) default:CARD=PCH HDA Intel PCH, CS4208 Analog Default Audio Device sysdefault:CARD=PCH HDA Intel PCH, CS4208 Analog Default Audio Device front:CARD=PCH,DEV=0 HDA Intel PCH, CS4208 Analog Front speakers dmix:CARD=PCH,DEV=0 HDA Intel PCH, CS4208 Analog Direct sample mixing device dsnoop:CARD=PCH,DEV=0 HDA Intel PCH, CS4208 Analog Direct sample snooping device hw:CARD=PCH,DEV=0 HDA Intel PCH, CS4208 Analog Direct hardware device without any conversions plughw:CARD=PCH,DEV=0 HDA Intel PCH, CS4208 Analog Hardware device with all software conversions default:CARD=Loopback Loopback, Loopback PCM Default Audio Device sysdefault:CARD=Loopback Loopback, Loopback PCM Default Audio Device front:CARD=Loopback,DEV=0 Loopback, Loopback PCM Front speakers surround21:CARD=Loopback,DEV=0 Loopback, Loopback PCM 2.1 Surround output to Front and Subwoofer speakers surround40:CARD=Loopback,DEV=0 Loopback, Loopback PCM 4.0 Surround output to Front and Rear speakers surround41:CARD=Loopback,DEV=0 Loopback, Loopback PCM 4.1 Surround output to Front, Rear and Subwoofer speakers surround50:CARD=Loopback,DEV=0 Loopback, Loopback PCM 5.0 Surround output to Front, Center and Rear speakers surround51:CARD=Loopback,DEV=0 Loopback, Loopback PCM 5.1 Surround output to Front, Center, Rear and Subwoofer speakers surround71:CARD=Loopback,DEV=0 Loopback, Loopback PCM 7.1 Surround output to Front, Center, Side, Rear and Woofer speakers dmix:CARD=Loopback,DEV=0 Loopback, Loopback PCM Direct sample mixing device dmix:CARD=Loopback,DEV=1 Loopback, Loopback PCM Direct sample mixing device dsnoop:CARD=Loopback,DEV=0 Loopback, Loopback PCM Direct sample snooping device dsnoop:CARD=Loopback,DEV=1 Loopback, Loopback PCM Direct sample snooping device hw:CARD=Loopback,DEV=0 Loopback, Loopback PCM Direct hardware device without any conversions hw:CARD=Loopback,DEV=1 Loopback, Loopback PCM Direct hardware device without any conversions plughw:CARD=Loopback,DEV=0 Loopback, Loopback PCM Hardware device with all software conversions plughw:CARD=Loopback,DEV=1 Loopback, Loopback PCM Hardware device with all software conversions
Thanks.
-
Admin over 7 yearsPossible duplicate of How do I create virtual ALSA device from which I can record everything that is played?
-
Admin over 7 years@CL as I wrote above, I tried it and it did not work. However, I think I have come with a solution. I will post it later when I am 100% sure.
-
-
Akhilesh Dhar Dubey about 5 yearsThank you so much for this. I've been trying for years to get this to work, and your example worked for me as-is.
-
gm_ about 5 years@Pistos I am glad it could help.