/* GRH Audio Program * * Author: Paul Kavan, Project Engineer * Date: July 3, 2007 */ #include #include #include /************************************* * Variable and Function Declarations *************************************/ int audio_type; //0 = sleep; 1 = capture; 2 = playback int nchannels = 1; int buffer_size = 512; int sample_rate = 8000; int bits = 8; char *snd_device_in = "hw:0,0"; char *snd_device_out = "hw:0,0"; void grh_capture(void); void grh_playback(void); /************************************* * Main Program Section *************************************/ int main(void) { //Testing routine printf("Press enter to begin recording\n"); getchar(); grh_capture(); printf("Audio Captured. Press enter to playback\n"); getchar(); /************************************ * Decide between playback or capture ************************************/ //TODO Determine best way to receive interrupts/mgs to parse // between playback and capture /************************************ * Call the appropriate function for * either playback or capture ************************************/ /*if(audio_type==1) grh_capture(); if(audio_type==2) grh_playback();*/ return 0; } /************************************************* * Capture Function: * This fuction should capture audio from the PCM * and store it in a buffer for transmission *************************************************/ void grh_capture(void) { int i; int err; short buf[buffer_size]; snd_pcm_t *capture_handle; snd_pcm_hw_params_t *hw_params; /************************************ * Open the pcm interface ************************************/ if ((err = snd_pcm_open(&capture_handle, snd_device_in, SND_PCM_STREAM_CAPTURE, 0)) < 0) { fprintf(stderr, "Failed to open input audio device %s: %s\n", snd_device_out, snd_strerror(err)); exit(1); } /************************************ * Configure the devices ************************************/ if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) { fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err)); exit (1); } 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); } 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); } if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S8)) < 0) { fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &sample_rate, 0)) < 0) { fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, nchannels)) < 0) { fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) { fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err)); exit (1); } snd_pcm_hw_params_free (hw_params); if ((err = snd_pcm_prepare (capture_handle)) < 0) { fprintf (stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror (err)); exit (1); } /****************************************** * Perform capture and store to buffer ******************************************/ for (i = 0; i < 10; ++i) { if ((err = snd_pcm_readi (capture_handle, buf, buffer_size)) != buffer_size) { fprintf (stderr, "read from audio interface failed (%s)\n", snd_strerror (err)); exit (1); } } /******************************************* * Close PCM interface and exit *******************************************/ snd_pcm_close (capture_handle); exit (0); } /************************************************ * Playback Function: * This function should retrieve audio from a * buffer and send it to the PCM for playback ************************************************/ void grh_playback(void) { int i; int err; short buf[buffer_size]; snd_pcm_t *playback_handle; snd_pcm_hw_params_t *hw_params; /************************************ * Open the pcm interface ************************************/ if ((err = snd_pcm_open(&playback_handle, snd_device_out, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { fprintf(stderr, "Failed to open output audio device %s: %s\n", snd_device_in, snd_strerror(err)); exit(1); } /************************************ * Configure the devices ************************************/ if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) { fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) { fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S8)) < 0) { fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &sample_rate, 0)) < 0) { fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, nchannels)) < 0) { fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) { fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err)); exit (1); } snd_pcm_hw_params_free (hw_params); if ((err = snd_pcm_prepare (playback_handle)) < 0) { fprintf (stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror (err)); exit (1); } /****************************************** * Playback from buffer ******************************************/ for (i = 0; i < 10; ++i) { if ((err = snd_pcm_writei (playback_handle, buf, buffer_size)) != buffer_size) { fprintf (stderr, "write to audio interface failed (%s)\n", snd_strerror (err)); exit (1); } } /******************************************* * Close PCM interface and exit *******************************************/ snd_pcm_close (playback_handle); exit (0); }