Reading data from USB microphone in C/C++

Is there a way to read data from a USB microphone in C/C++ and store the data in an array?


You may use arecord:

$ arecord -l
card 2: M [Plantronics Blackwire C220 M], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0
$ arecord -D hw:2,0 -c2 -d 10 -r 44100 -f S16_LE a.wav

Given that the original posted asked for C/C++ code, a shell command isn’t all that helpful.

In general, you either open and read from the /dev/dsp device directly, or you use whatever audio routing/mixing system is installed on your desktop environment.
Unfortunately, because sound was such a clusterfock on Linux for a long while, a number of different projects tried to solve it separately, and it’s now still a clusterfock, because there is no one standard.
Then a bunch of libraries trying to abstract the different standards/libraries popped up, and now there’s also no standard abstraction library!

One such standard abstraction library is PortAudio:

The Ubuntu desktop on the Nano seems to be running PulseAudio, so you could start looking at that API:

Not sure if the sour code helps. FYI. It demonstrates how to use pcm APIs.

I think source code is more helpful for the original poster.

Beware that arecord uses the raw (low-level) ALSA library and device interface, and thus won’t work well with Jack or PulseAudio or similar desktop-level systems. But often, especially for embedded devices, that’s totally fine!