View Single Post
Old 07-09-2012, 12:28 PM   #47
twobob
( ͡° ͜ʖ ͡°){ʇlnɐɟ ƃǝs}Týr
twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.
 
twobob's Avatar
 
Posts: 6,586
Karma: 6299991
Join Date: Jun 2012
Location: uti gratia usura (Yao ying da ying; Mo ying da yieng)
Device: PW-WIFI|K5-3G+WIFI| K4|K3-3G|DXG|K2| Rooted Nook Touch
just enough to get you going with mic input.

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:

Last edited by twobob; 07-10-2012 at 11:54 AM. Reason: added call info: added mem leak
twobob is offline   Reply With Quote