How to setup external Audio Input on Raspberry Pi 2?

I’m trying to compile the audioInputExample from examples/sound, but I’m running into some issues opening the device. I have a small USB sound card with an external microphone attached and I can see the device listed by lsusb:

Bus 001 Device 006: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller

in OF however I’m getting this output:

[notice ] ofRtAudioSoundStream: device 0 hw:bcm2835 ALSA,0
[notice ] ofRtAudioSoundStream: maximum output channels 2
[notice ] ofRtAudioSoundStream: maximum input channels 0
[notice ] ofRtAudioSoundStream: -----------------------------------------
[notice ] ofRtAudioSoundStream: device 1 hw:bcm2835 ALSA,1
[notice ] ofRtAudioSoundStream: maximum output channels 2
[notice ] ofRtAudioSoundStream: maximum input channels 0
[notice ] ofRtAudioSoundStream: -----------------------------------------
[notice ] ofRtAudioSoundStream: device 2 hw:USB PnP Sound Device,0
[notice ] ofRtAudioSoundStream: maximum output channels 2
[notice ] ofRtAudioSoundStream: maximum input channels 1
[notice ] ofRtAudioSoundStream: -----------------------------------------

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

[notice ] ofRtAudioSoundStream: device 3 default
[notice ] ofRtAudioSoundStream: maximum output channels 10000
[notice ] ofRtAudioSoundStream: maximum input channels 0
[notice ] ofRtAudioSoundStream: -----------------------------------------

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

[notice ] ofAppEGLWindow: runAppViaInfiniteLoop(): setting up notifications complete
-nan
-nan
-nan
-nan
-nan
-nan
-nan
-nan
-nan
-nan
-nan
-nan
-nan
-nan

I noticed the USB sound card is at index 2 and there is a single audio input channel so I setup like this:

soundStream.setDeviceID(2);
soundStream.setup(this, 0, 1, 44100, bufferSize, 4);

(where bufferSize is 256)
I see -nan when printing scaledVol to the console and I’m not sure where I’m going wrong.

I’m guessing the right sound card is chosen and opened, but maybe other settings (sample/rate/etc. are wrong) ?

Has anyone else came across a similar situation ? How can I use an external mic with on a basic usb sound card on Raspberry PI 2 with OF ?

ooops! Just noticed the audioIn callback is doing the math for 2 channels not 1, hence the nan.

I’ve changed the for loop to:

for(int i = 0 ; i < bufferSize; i++){
                left[i] = input[i] * 0.5;
                curVol += left[i]*left[i];
                numCounted+=2;
        }

and now I see values changing based on volume.

I’m not 100% I’ve got the values accurate though.
The stereo loop looked like:

for (int i = 0; i < bufferSize; i++){
                left[i]         = input[i*2]*0.5;
                right[i]        = input[i*2+1]*0.5;

                curVol += left[i] * left[i];
                curVol += right[i] * right[i];
                numCounted+=2;
        }

I’m taking reach sample as opposed to every other (like above), but I’m still dividing the value by half.
Should I still do that ? (If so, why, where can I learn more ?) Similarly should increment numCounted by 2 ?

Thank you,
George

Hi, sorry to resurrect an old thread - just wondering if this ended up working for you? I need to analyse audio as well as output so audio. Was this the device you used?

Hey @nay,

I used a very cheap USB sound card from ebay, it looked just like this one. I vaguely remember the chipset being C-Media as well, but don’t recall having issues setting it up.

Before you go on the oF side of things, first check your sound card’s driver is loaded and working.
The adafruit article mentions running lsusb. I recommend also running alsamixer.
The alsamixer application will allow you to view the sound devices visible (hopefully the sound card will be there) and there’s a key (I can’t remember off the top of my head, F6 maybe ?) that will show go to the input options and you can set the volume for the mic for the sound card. You will also notice there if the sound card has mono or stereo input. These small sound cards tend to have mono input, which why I had issues before using the of sample as it was (setup for stereo). There are utilities such as arecord and aplay which will allow you to record and playback a wav file. Use the right flags to use the sound card and mono input and make sure the recording sounds as you’d expect.

Once you’ve tested the above, make sure you’ve set the correct device ID in OF and if it’s mono, modify the audioIn callback to handle mono data (see notes above).

Good luck

Great, thanks so much for the tips!