Advertisement
Guest User

Test application

a guest
Jan 16th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. /*
  2.  *  This extra small demo sends a random samples to your speakers.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include "../include/alsa/asoundlib.h"
  8.  
  9. static char *device = "playback_0";           /* playback device */
  10. snd_output_t *output = NULL;
  11. //unsigned char buffer[16*1024];              /* some random data */
  12. unsigned char buffer[1024 * 16];              /* some random data */
  13. static const char *help="usage: ./program wav_file.wav\n";
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17.     int err;
  18.     unsigned int i;
  19.     snd_pcm_t *handle;
  20.     snd_pcm_sframes_t frames;
  21.     int rret;
  22.     static snd_output_t *output = NULL;
  23.  
  24.     FILE *file;
  25.  
  26.     if (argc < 2) {
  27.         printf("%s", help);
  28.         return -1;
  29.     }
  30.  
  31.     if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
  32.         printf("Playback open error: %s\n", snd_strerror(err));
  33.         exit(EXIT_FAILURE);
  34.     }
  35.     if ((err = snd_pcm_set_params(handle,
  36.                                   SND_PCM_FORMAT_S16_LE,
  37.                                   SND_PCM_ACCESS_RW_INTERLEAVED,
  38.                                   2,
  39.                                   44100,
  40.                                   1,
  41.                                   500000)) < 0) {   /* 0.5sec */
  42.         printf("Playback open error: %s\n", snd_strerror(err));
  43.         exit(EXIT_FAILURE);
  44.     }
  45.  
  46.     if ( (file = fopen(argv[1], "rb")) == NULL) {
  47.         printf("Can not open the wav file !");
  48.         return -1;
  49.     }
  50.  
  51.     err = snd_output_stdio_attach(&output, stdout, 0);
  52.     if (err < 0) {
  53.         printf("Output failed: %s\n", snd_strerror(err));
  54.         return 0;
  55.     }
  56.  
  57.     snd_pcm_dump(handle, output);
  58.  
  59.    while ( (rret = fread(buffer, 1024 * 2, 4, file))) {
  60.         printf("rret = %d\n", rret);
  61.         frames = snd_pcm_writei(handle, buffer, 1024 * 2);
  62.         printf("snd_pcm_writei:frames: %d\n", frames);
  63.         if (frames < 0)
  64.                 frames = snd_pcm_recover(handle, frames, 0);
  65.         if (frames < 0) {
  66.                 printf("snd_pcm_writei failed: %s\n", snd_strerror(err));
  67.                 break;
  68.         }
  69.         if (frames > 0 && frames < (long)sizeof(buffer))
  70.                 printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
  71.  
  72.         // sleep needed to see that the frame is repeated.
  73.         usleep(5000000);
  74.     }
  75.  
  76.     snd_pcm_close(handle);
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement