Here is a simple test program that pulls some data in from the mic and splats it to stderr.
By itself it is, again, almost pointless and feature free but it's enough to get you going with access to mic data.
Spoiler:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
main (int argc, char *argv[])
{
//
int i;
int err;
// Just a tiny bit of space
short buf[128];
// and an index
int counter = sizeof(buf);
// The card
snd_pcm_t *capture_handle;
// the cards params
snd_pcm_hw_params_t *hw_params;
// This takes the device as the first argument for testing purposes.
//hw is a sensible choice here.
if ((err = snd_pcm_open (&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
argv[1],
snd_strerror (err));
exit (1);
}
// make a little space
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
// set it up
if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
// go for the INTERLEAVED type of access.
if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n",
snd_strerror (err));
exit (1);
}
// Signed 16 Bit Little Endian sampling
if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit (1);
}
// We are going to hard set this to 48k for now
if ((err = snd_pcm_hw_params_set_rate (capture_handle, hw_params, 48000, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
// Make it stereo
if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit (1);
}
// Make it so
if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
exit (1);
}
// I forgot to mention the nasty mem leak if you forget to do this...
snd_pcm_hw_params_free (hw_params);
// Prepare for recording
if ((err = snd_pcm_prepare (capture_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
// It would probably be pertinent in the real world to loop this or do via callback
// Let's actually grab something
for (i = 0; i < 10; ++i) {
if ((err = snd_pcm_readi (capture_handle, buf, 128)) != 128) {
fprintf (stderr, "read from audio interface failed (%s)\n",
snd_strerror (err));
exit (1);
}
}
// Assuming we got this far we actually have something in our buffer.
// We could now shovel this out somewhere. Let's go for stderr at this point.
while(counter){
counter--;
fprintf (stderr, "buf: (%d)\n",buf[counter]);
}
// The cycle is complete.
snd_pcm_close (capture_handle);
//Live long and prosper
exit (0);
}
remember to compile it
-lasound and
--static against x-compiled alsa-libs

with
-o example or whatever
call it like
example hw
This program is incredibly trivial - it simply output a short buffer of numbers to stderr.
The principle in what is important.
screenie of the output: