Advertisement
Guest User

test application

a guest
Aug 4th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.43 KB | None | 0 0
  1. /*
  2.  *  This extra small demo sends a random samples to your speakers.
  3.  */
  4.  
  5. #include <alsa/asoundlib.h>
  6. #include <unistd.h>
  7.  
  8. static char *device = "MEDIA_playback_0";           /* playback device */
  9.  
  10. static snd_pcm_uframes_t last_size = 20; /* Will be modifed by gdb */
  11.  
  12. snd_output_t *output = NULL;
  13. unsigned char buffer[2*8*1024];              /* some random data */
  14. unsigned char last_buff[4096*2];
  15.  
  16. int main(void)
  17. {
  18.     int err;
  19.     unsigned int i;
  20.     snd_pcm_t *handle;
  21.     snd_pcm_sframes_t frames;
  22.     static snd_output_t *output = NULL;
  23.     long a, b, c;
  24.  
  25.     snd_pcm_hw_params_t *hwparams;
  26.     snd_pcm_sw_params_t *swparams;
  27.  
  28.     err = snd_output_stdio_attach(&output, stdout, 0);
  29.     if (err < 0) {
  30.         printf("Output failed: %s\n", snd_strerror(err));
  31.         return 0;
  32.     }
  33.  
  34.     for (i = 0; i < sizeof(buffer); i++)
  35.             buffer[i] = random() & 0xff;
  36.  
  37.     for (i = 0; i < sizeof(last_buff); i++)
  38.         last_buff[i] = random() & 0xff;
  39.  
  40.     if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
  41.         printf("Playback open error: %s\n", snd_strerror(err));
  42.         exit(EXIT_FAILURE);
  43.     }
  44.  
  45.     err = snd_pcm_hw_params_malloc(&hwparams);
  46.     if (err < 0)
  47.         goto exit;
  48.  
  49.     err = snd_pcm_hw_params_any(handle, hwparams);
  50.         if (err < 0)
  51.         goto exit;
  52.     err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
  53.         if (err < 0)
  54.         goto exit;
  55.     err = snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16_LE);
  56.     //err = snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_U8);
  57.         if (err < 0)
  58.         goto exit;
  59.     err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
  60.         if (err < 0)
  61.         goto exit;
  62.     unsigned int rate = 18900;
  63.     err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, 0);
  64.         if (err < 0)
  65.         goto exit;
  66.     snd_pcm_uframes_t  size = 1024;
  67.     err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &size, 0);
  68.         if (err < 0)
  69.         goto exit;
  70.     snd_pcm_uframes_t  buffer2 = 16384;
  71.     err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer2);
  72.         if (err < 0)
  73.         goto exit;
  74.     err = snd_pcm_hw_params(handle, hwparams);
  75.         if (err < 0)
  76.         goto exit;
  77.     snd_pcm_hw_params_free(hwparams);
  78.  
  79.     err = snd_pcm_sw_params_malloc(&swparams);
  80.         if (err < 0)
  81.         goto exit;
  82.     err = snd_pcm_sw_params_current(handle, swparams);
  83.         if (err < 0)
  84.         goto exit;
  85.     err = snd_pcm_sw_params(handle, swparams);
  86.         if (err < 0)
  87.         goto exit;
  88.     snd_pcm_sw_params_free(swparams);
  89.  
  90.     snd_pcm_dump(handle, output);
  91.  
  92. // (3) PREPARE
  93.     err = snd_pcm_prepare(handle);
  94.         if (err < 0)
  95.         goto exit;
  96.  
  97.     a = snd_pcm_avail(handle);
  98.     printf("a: %ld\n", a);
  99.  
  100. // (4) Start playback
  101.  
  102.     b = snd_pcm_avail_update(handle);
  103.     printf("b: %ld\n", b);
  104.  
  105.     for (i = 0; i < 64; i++) {
  106.         usleep(50000);
  107.         b = snd_pcm_avail_update(handle);
  108.         if( b > 1024 ) {
  109.             frames = snd_pcm_writei(handle, buffer, size);
  110.             if (frames < 0)
  111.                 frames = snd_pcm_recover(handle, frames, 0);
  112.             if (frames < 0) {
  113.                 printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
  114.                 break;
  115.             }
  116.             if (frames > 0 && frames < (long)sizeof(buffer))
  117.                 printf("Short write (expected %li, wrote %li), c:%ld\n", (long)sizeof(buffer), frames, (a-b));
  118.         }
  119.     }
  120.  
  121. // LAST WRITE - NOT COMPLETE
  122.  
  123.     printf("frames: %ld, sizeof(buffer): %d\n", (long int)frames, sizeof(last_buff));
  124.     frames = snd_pcm_writei(handle, last_buff, last_size);
  125.     if (frames < 0)
  126.         frames = snd_pcm_recover(handle, frames, 0);
  127.     if (frames < 0) {
  128.         printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
  129.     }
  130.     if (frames > 0 && frames < (long)sizeof(last_buff))
  131.         printf("Short write (expected %li, wrote %li)\n", (long)sizeof(last_buff), frames);
  132.  
  133. // check buffer fill level every 100ms
  134.     do {
  135.         b = snd_pcm_avail(handle);
  136.         c = a - b;
  137.         printf("c:%ld\n", c);
  138.         usleep(100000);
  139.     } while(c > 4096);
  140.  
  141.     snd_pcm_drain(handle);
  142.     snd_pcm_drop(handle);
  143.     usleep(100000);
  144.     snd_pcm_hw_free(handle);
  145.     snd_pcm_close(handle);
  146.     return 0;
  147.  
  148. exit:
  149.     printf("Error occured!\n");
  150.     return -1;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement