ofSoundStream input from virtual mic using ALSA snd-aloop

Hi! I am trying to find a way how to test mic input without actually having one. So far I managed to enable the snd-aloop module on Raspbian Linux.

sudo modprobe snd-aloop
sudo echo 'snd-aloop' >> /etc/modules

This gives me two Loopback devices, when I type aplay -L I get

...
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
 ...
  Subdevice #7: subdevice #7
card 1: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
 ...
  Subdevice #7: subdevice #7

I am able to play a LTC wav file through the loopback device like this.

aplay -D hw:1,0,0 ltc.wav

To record the sound, I used the following with great sucess.

arecord -t wav -f S16_LE -r44100 -c1 -D hw:1,1,0 rec.wav

I am able to play back the rec.wav and it has the sound of ltc.wav.

The problem

I am not able to capture the sound from the loopback device by using ofSoundStream in openFrameworks. This is what I see when I use printDeviceList();

[notice ] ofBaseSoundStream::printDeviceList:
[0] hw:bcm2835 ALSA,0 [in:0 out:2]
[1] hw:bcm2835 ALSA,1 [in:0 out:2]
[2] hw:Loopback,0 [in:32 out:32]
[3] hw:Loopback,1 [in:1 out:32]
[4] default [in:0 out:10000]

To open the device 3 the following code is used.

snd.setDeviceID(3);
snd.setup(this, 0, 1, 44100, 256, 4); // 0 out, 1 in, 44100Hz

And I am not getting input. It works on Mac OS X using Soundflower. Here is another chunk of text that I get when trying to open the stream.

RtApiAlsa::getDeviceInfo: snd_pcm_open error for device (default), No such file or directory.


RtApiAlsa: dump hardware params just after device open:

ACCESS:  MMAP_INTERLEAVED RW_INTERLEAVED
FORMAT:  S16_LE S16_BE S32_LE S32_BE FLOAT_LE FLOAT_BE
SUBFORMAT:  STD
SAMPLE_BITS: [16 32]
FRAME_BITS: [16 1024]
CHANNELS: [1 32]
RATE: [8000 192000]
PERIOD_TIME: (5 65536000]
PERIOD_SIZE: [1 524288]
PERIOD_BYTES: [64 1048576]
PERIODS: [1 1024]
BUFFER_TIME: (5 131072000]
BUFFER_SIZE: [1 1048576]
BUFFER_BYTES: [64 2097152]
TICK_TIME: ALL

RtApiAlsa: dump hardware params after installation:

ACCESS:  RW_INTERLEAVED
FORMAT:  FLOAT_LE
SUBFORMAT:  STD
SAMPLE_BITS: 32
FRAME_BITS: 32
CHANNELS: 1
RATE: 44100
PERIOD_TIME: (5804 5805)
PERIOD_SIZE: 256
PERIOD_BYTES: 1024
PERIODS: 4
BUFFER_TIME: (23219 23220)
BUFFER_SIZE: 1024
BUFFER_BYTES: 4096
TICK_TIME: 0

RtApiAlsa: dump software params after installation:

tstamp_mode: NONE
period_step: 1
avail_min: 256
start_threshold: 256
stop_threshold: -1
silence_threshold: 0
silence_size: 1073741824
boundary: 1073741824

It actually works : ] Take this as a working example. In case you use ofxLtcReader, remember to change the channel to 1, like the following.

void ofApp::audioIn(float * input, int bufferSize, int nChannels){
    reader.readLtc(input, nChannels, 1);
}

Have fuun!