Linux

在 .asoundrc 文件中創建虛擬設備

  • November 3, 2019

我一直在嘗試創建一個虛擬設​​備來使用純 ALSA 錄製我的音效卡的內部音頻。在Google搜尋了很多之後,我發現了一個.asoundrc非常接近我正在尋找的文件¹:

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"
}

此配置可以錄製一個ogg文件,其中包含我的電腦中播放的所有音頻。我現在想做的是修改pcm.myrecord配置的一部分,以便音頻在虛擬卡中“流式傳輸”,我以後可以使用arecord. 如果有人可以幫助我,我會很高興。

我試過這個:如何創建可以記錄播放的所有內容的虛擬 ALSA 設備?但這對我不起作用。下面,來自的輸出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

謝謝。

  1. https://bbs.archlinux.org/viewtopic.php?id=167830

我解決了這個問題如下:

首先,載入snd-aloop模組:

sudo modprobe snd-aloop

這將創建一個名為的新設備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

根據上面顯示的資訊,我們必須創建兩個hw設備:

  1. hw:0,0(PCH 是我的主要音效卡)。
  2. hw:2,1Loopback虛擬設備)。

通常,hw設備具有以下形式:hw:X,Y. 就我們的目的而言,似乎Y對於主卡來說總是0,而對於Loopback我們有Yequals 1

現在,為了完成所有這些工作,我們需要一個~/.asoundrc包含以下內容的文件:

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
 }
}

使用您的主卡的值進行修改hw:X1,Y1(在我的情況下hw:0,0)。

您可以通過在電腦中播放一些東西來測試此配置是否有效。如果你聽到了,那很好。現在讓我們在電腦上播放一些東西,並從這個虛擬設備上錄製它ffmpeg

ffmpeg -f alsa -i hw:X2,Y2 -c:a libmp3lame -b:a 256k -vn capture.mp3

在這種情況下, hw:X2,Y2必須更換設備的位置。Loopback``hw:2,1

我想使用arecord也應該有效。

引用自:https://unix.stackexchange.com/questions/310164