#include #include #include #define BUF_BYTES 128 int main (int argc, char *argv[]) { int err, rc ; unsigned char buf_right[BUF_BYTES]; unsigned char buf_left[BUF_BYTES]; //char *buf; int size; snd_pcm_t *playback_handle_right; snd_pcm_t *capture_handle_right; snd_pcm_t *playback_handle_left; snd_pcm_t *capture_handle_left; #if 0 char* device = "default"; if (argc > 1) device = argv[1]; #endif char* crdevice ; char* prdevice ; char* cldevice ; char* pldevice ; unsigned int rate ; unsigned int latancy; unsigned int nchannels = 1; snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE; unsigned int buf_frames_right = BUF_BYTES / nchannels / 2; unsigned int buf_frames_left = BUF_BYTES / nchannels / 2; if(argc != 3 ) { printf("\nUsage is : application sampling_rate latancy \n\n\t Ex: ./gsm_application_target 8000 \n\n\t Supported sampling rates are: \n\t 1) 8000 \n\t 2) 16000 \n\t 3) 32000 \n\t 4) 44100 \n\t 5) 48000 \n\n NOTE: Any number other than given sampling rate takes default as 48000\n\n"); exit(1); } rate = (int)atoi(argv[1]); latancy = (int)atoi(argv[2]); if(rate == 8000) { crdevice = "VINR8"; prdevice = "VOUTL8"; cldevice = "VINL8"; pldevice = "VOUTR8"; }else if(rate == 16000) { crdevice = "VINR16"; prdevice = "VOUTR16"; cldevice = "VINL16"; pldevice = "VOUTL16"; }else if(rate == 32000) { crdevice = "VINR32"; prdevice = "VOUTR32"; cldevice = "VINL32"; pldevice = "VOUTL32"; }else if(rate == 44100) { crdevice = "VINR44"; prdevice = "VOUTR44"; cldevice = "VINL44"; pldevice = "VOUTL44"; }else { crdevice = "VINR48"; prdevice = "VOUTL48"; cldevice = "VINL48"; pldevice = "VOUTR48"; rate = 48000; } ///// RIGHT CHANNEL newStart: if ((err = snd_pcm_open (&playback_handle_right, prdevice, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { fprintf (stderr, "cannot open playback right audio device %s (%s)\n", prdevice, snd_strerror (err)); exit (1); } else { printf("Opened Playback right audio device successfully %s \n", prdevice); } if ((err = snd_pcm_set_params(playback_handle_right, format, SND_PCM_ACCESS_RW_INTERLEAVED, nchannels, rate, 1, latancy)) < 0) { /* 0.5sec */ fprintf(stderr, "Playback right open error: %s\n", snd_strerror(err)); exit(1); } if ((err = snd_pcm_open (&capture_handle_right, crdevice, SND_PCM_STREAM_CAPTURE, 0)) < 0) { fprintf (stderr, "cannot open capture right audio device %s (%s)\n", crdevice, snd_strerror (err)); exit (1); } else { printf("Opened Capture right audio device successfully %s \n", crdevice); } if ((err = snd_pcm_set_params(capture_handle_right, format, SND_PCM_ACCESS_RW_INTERLEAVED, nchannels, rate, 1, latancy)) < 0) { /* 0.5sec */ fprintf(stderr, "capture right open error: %s\n", snd_strerror(err)); exit(1); } ////LEFT CHANNEL if ((err = snd_pcm_open (&playback_handle_left, pldevice, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { fprintf (stderr, "cannot open playback left audio device %s (%s)\n", pldevice, snd_strerror (err)); exit (1); } else { printf("Opened Playback left audio device successfully %s \n", pldevice); } if ((err = snd_pcm_set_params(playback_handle_left, format, SND_PCM_ACCESS_RW_INTERLEAVED, nchannels, rate, 1, latancy)) < 0) { /* 0.5sec */ fprintf(stderr, "Playback left open error: %s\n", snd_strerror(err)); exit(1); } if ((err = snd_pcm_open (&capture_handle_left, cldevice, SND_PCM_STREAM_CAPTURE, 0)) < 0) { fprintf (stderr, "cannot open capture left audio device %s (%s)\n", cldevice, snd_strerror (err)); exit (1); } else { printf("Opened Capture left audio device successfully %s \n", crdevice); } if ((err = snd_pcm_set_params(capture_handle_left, format, SND_PCM_ACCESS_RW_INTERLEAVED, nchannels, rate, 1, latancy)) < 0) { /* 0.5sec */ fprintf(stderr, "capture left open error: %s\n", snd_strerror(err)); exit(1); } while(1) { ///// RIGHT CHANNEL /***** Read the Frames from the Capture Device ******/ snd_pcm_prepare(capture_handle_right); err = snd_pcm_readi(capture_handle_right, buf_right, buf_frames_right); if(err < 0) { printf("capture right func: overrun!"); rc = snd_pcm_prepare(capture_handle_right); if(rc < 0) { printf("Fail to recover from problem, Need to reopen the device\n"); } } if(err < 0) { fprintf (stderr, "read from audio interface right failed (%s)\n", snd_strerror (err)); rc = snd_pcm_recover(capture_handle_right,err,0); if(rc < 0) { printf("Fail to recover from problem, Need to reopen the device\n"); goto reStart; } }else if(err != (int)buf_frames_right) { fprintf(stderr, "short read, read %d frames\n", err); } snd_pcm_prepare(playback_handle_right); /**** Write the capture frame to playback ******/ err = snd_pcm_writei (playback_handle_right, buf_right, buf_frames_right); if(err == -EPIPE) { printf("Playback right func: overrun!"); rc = snd_pcm_prepare(playback_handle_right); if(rc < 0) { printf("Fail to recover from problem, Need to reopen the device\n"); } } if(err < 0) { fprintf (stderr, "write to audio interface right failed (%s)\n", snd_strerror (err)); rc = snd_pcm_recover(playback_handle_right, err, 0); if(rc < 0) { printf("Fail to recover from problem, Need to reopen the device\n"); goto reStart; } }else if(err != (int)buf_frames_right) { fprintf(stderr, "short write, write %d frames\n", err); } ////LEFT CHANNEL snd_pcm_prepare(capture_handle_left); /***** Read the Frames from the Capture Device ******/ err = snd_pcm_readi(capture_handle_left, buf_left, buf_frames_left); if(err == -EPIPE) { printf("capture right func: overrun!"); rc = snd_pcm_prepare(capture_handle_left); if(rc < 0) { printf("Fail to recover from problem, Need to reopen the device\n"); } } if(err < 0) { fprintf (stderr, "read from audio interface right failed (%s)\n", snd_strerror (err)); rc = snd_pcm_recover(capture_handle_left,err,0); if(rc < 0) { printf("Fail to recover from problem, Need to reopen the device\n"); goto reStart; } }else if(err != (int)buf_frames_left) { fprintf(stderr, "short read, read %d frames\n", err); } snd_pcm_prepare(playback_handle_left); /**** Write the capture frame to playback ******/ err = snd_pcm_writei (playback_handle_left, buf_left, buf_frames_left); if(err == -EPIPE) { printf("Playback right func: overrun!"); rc = snd_pcm_prepare(playback_handle_left); if(rc < 0) { printf("Fail to recover from problem, Need to reopen the device\n"); goto reStart; } } if(err < 0) { fprintf (stderr, "write to audio interface right failed (%s)\n", snd_strerror (err)); rc = snd_pcm_recover(playback_handle_left, err, 0); if(rc < 0) { printf("Fail to recover from problem, Need to reopen the device\n"); goto reStart; } }else if(err != (int)buf_frames_left) { fprintf(stderr, "short write, write %d frames\n", err); } } reStart: fprintf (stderr, "close handles\n"); snd_pcm_close (playback_handle_right); snd_pcm_close (capture_handle_right); snd_pcm_close (playback_handle_left); snd_pcm_close (capture_handle_left); sleep(2); goto newStart; return 0; }