help for snd_pcm_writei

160 views
Skip to first unread message

Quentin

unread,
Jun 28, 2013, 10:53:27 AM6/28/13
to beagl...@googlegroups.com
hi,

I would like play a wav file with "snd_pcm_writei()". i try this but it does not work:

/*
 * This extra small demo sends a random samples to your speakers.
 */
#include <alsa/asoundlib.h>
#include <stdio.h>
#include <stdlib.h>

snd_output_t *output = NULL;
unsigned char buffer[16*1024]; /* some random data */
int main(void)
{
   int err;
   unsigned int i;
   snd_pcm_t *handle;
   snd_pcm_sframes_t frames;
   for (i = 0; i < sizeof(buffer); i++)
      buffer[i] = random() & 0xff;
   if ((err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
      printf("Playback open error: %s\n", snd_strerror(err));
      exit(EXIT_FAILURE);
   }
   if ((err = snd_pcm_set_params(handle,
               SND_PCM_FORMAT_U8,
               SND_PCM_ACCESS_RW_INTERLEAVED,
               1,
               32000,
               1,
               500000)) < 0) { /* 0.5sec */
      printf("Playback open error: %s\n", snd_strerror(err));
      exit(EXIT_FAILURE);
   }
   int ok;
   long lSize;
   char * buffer2;
   ok = open("balle_32.wav", O_RDONLY);
   if(ok < 0)
   {
      perror("open");
      exit (1);
   }
   lSize = lseek (ok , 0 , SEEK_END);
   lseek (ok , 0 , SEEK_SET);
   buffer2 = (char*) malloc (sizeof(char)*lSize);
   if (buffer == NULL)
   {
      fputs ("erreur memoire",stderr);
      exit (2);
   }
   ok = read (ok,buffer2,lSize);
   if(ok != lSize)
   {
      perror("read");
      exit (3);
   }
for (i = 0; i < 16; i++) {
   for(int w=0;w<(1024);w++)
   {
      buffer[w]=buffer2[w+1024*i];
      //printf("%x.",buffer[w]);
   }
   frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
   if (frames < 0)
      frames = snd_pcm_recover(handle, frames, 0);
   if (frames < 0) {
      printf("snd_pcm_writei failed: %s\n", snd_strerror(err));
      break;
   }
   if (frames > 0 && frames < (long)sizeof(buffer))
      printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
}
snd_pcm_close(handle);
return 0;
}

Could you help me or could you send me a example with a wav file?
ps:
 file ../../BeagleBone/balle_32.wav
../../BeagleBone/balle_32.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 32000 Hz

thank you
Reply all
Reply to author
Forward
0 new messages