Advertisement
wandrake

Untitled

Feb 11th, 2012
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <alsa/asoundlib.h>
  3.  
  4. #define CHECK(x) \
  5.     if ((x) < 0) {\
  6.         printf("Error in line %d", __LINE__);\
  7.         fflush(stdout);\
  8.     }
  9.  
  10. int main() {
  11.     int i;
  12.     int err;
  13.     int16_t buf[128];
  14.     int rate = 44100;
  15.  
  16.     snd_pcm_t* playback_handle;
  17.     snd_pcm_hw_params_t* hw_params;
  18.  
  19.     CHECK(snd_pcm_open(&playback_handle, "hw:0,0", SND_PCM_STREAM_PLAYBACK, 0));
  20.  
  21.     CHECK(snd_pcm_hw_params_malloc(&hw_params));
  22.     CHECK(snd_pcm_hw_params_any(playback_handle, hw_params));
  23.  
  24.     CHECK(snd_pcm_hw_params_set_access(playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED));
  25.     CHECK(snd_pcm_hw_params_set_format(playback_handle, hw_params, SND_PCM_FORMAT_S16_LE));
  26.     CHECK(snd_pcm_hw_params_set_rate_near(playback_handle, hw_params, &rate, 0));
  27.     CHECK(snd_pcm_hw_params_set_channels(playback_handle, hw_params, 2));
  28.  
  29.     CHECK(snd_pcm_hw_params(playback_handle, hw_params));
  30.  
  31.     snd_pcm_hw_params_free(hw_params);
  32.  
  33.     CHECK(snd_pcm_prepare(playback_handle));
  34.  
  35.     for (i = 0; i < 1000; ++i) {
  36.         if(snd_pcm_writei(playback_handle, buf, 128) != 128) {
  37.             printf("Ooops, there was something wrong.\n");  
  38.             fflush(stdout);
  39.         }
  40.     }
  41.  
  42.     snd_pcm_close(playback_handle);
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement