Skip to content

Linux: Capturing audio on loopback device #48

@boedy

Description

@boedy

I've been trying to capture audio on a loopback device in Linux. I've enabled a loopback device through:

modprobe snd-aloop

This adds a new virtual card to my environment:

arecord -l
**** List of CAPTURE Hardware Devices ****
card 2: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 2: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7

To test if the loopback device is working a ran the following examples:

1st terminal:

arecord -D hw:2,1,0 -r 44100 -c 2 -f S16_LE record.wav

2nd terminal (sample wav):

aplay -D hw:2,0,0 -f S16_LE -c2 play.wav

Afterwards I'm able to successfully play the recorded file back:

aplay record.wav

Next it was time to try capturing the input through Audify. The below code snippet tries to capture data from Loopback device.

const { RtAudio, RtAudioFormat, RtAudioApi } = require('audify');

const rtAudio = new RtAudio(RtAudioApi.LINUX_ALSA);
const audioDevices = rtAudio.getDevices();
console.log('Device #, Name, # of Channels');
for(let i = 0; i < audioDevices.length; i++){
  let device = audioDevices[i];
  if(device.inputChannels > 0){
  	console.log(i, device.name, device.inputChannels);
  }
}

rtAudio.openStream(null, { deviceId: 2, nChannels: 2, firstChannel: 0 }, RtAudioFormat.RTAUDIO_FLOAT32, 48000, 48, 'testing', (outputBuffer) => {
  console.log(outputBuffer);
});
rtAudio.start();

Outputs:

Device #, Name, # of Channels
0 Default ALSA Device 32
1 PulseAudio Sound Server 32
2 Loopback (Loopback PCM) 32
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 334 more bytes>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 334 more bytes>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 334 more bytes>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 334 more bytes>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 334 more bytes>
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 334 more bytes>
etc..

The empty buffers indicate no audio data is captured.

@almoghamdani Have you by any chance tested loopback devices? Might be I'm doing something wrong tho.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions