Question

This program works fine in Ubuntu but segfaults on Raspberry Pi. No error during compilation also. I am playing a music and the songs works properly in Ubuntu.

#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#define PB_DEVICE ("plughw:0,0")
#define SAMPLING_RATE (48000)

int main (int argc, char *argv[])
{
   FILE *fp;
   int err;
   short buf[10],aud[10];
   snd_pcm_t *playback_handle;
   snd_pcm_hw_params_t *hw_params;
   char *pdevice=PB_DEVICE;
   int val=48000;
   int read_len = 0;

   fp=fopen("../inc/test2.wav","rb");
   if(NULL == fp)
   {
      printf("File Open Failed:\n");
   }

   if ((err = snd_pcm_open(&playback_handle, pdevice, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
      fprintf (stderr, "cannot open audio device %s (%s)\n", 
               pdevice,
               snd_strerror (err));
      exit (1);
   }
   if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
      fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
               snd_strerror (err));
      exit (1);
   }

   if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
      fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
               snd_strerror (err));
      exit (1);
   }

   if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
      fprintf (stderr, "cannot set access type (%s)\n",
               snd_strerror (err));
      exit (1);
   }

   if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
      fprintf (stderr, "cannot set sample format (%s)\n",
               snd_strerror (err));
      exit (1);
   }
   if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params,&val,0)) < 0) {
      fprintf (stderr, "cannot set sample rate (%s)\n",
               snd_strerror (err));
      exit (1);
   }

   if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 1)) < 0) {
      fprintf (stderr, "cannot set channel count (%s)\n",
               snd_strerror (err));
      exit (1);
   }

   if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
      fprintf (stderr, "cannot set parameters (%s)\n",
               snd_strerror (err));
      exit (1);
   }

   snd_pcm_hw_params_free (hw_params);

   if ((err = snd_pcm_prepare (playback_handle)) < 0) {
      fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
               snd_strerror (err));
      exit (1);
   }
   while((read_len = fread(buf,2,sizeof(aud),fp)) > 0)
   {

      if((err = snd_pcm_writei(playback_handle, buf, read_len)) != read_len)
      {
         fprintf (stderr, "write to audio interface failed ReadLen [ %d ] (%s)\n",
                  read_len,
                  snd_strerror (err));
         exit (1);
      }
      read_len=0;
   }

   snd_pcm_close (playback_handle);
   exit (0);
   fclose(fp);
}
Was it helpful?

Solution

You are misunderstanding what sizeof does and so reading twice as much data as your buffer can contain. This may or may not cause a memory protection error or program failure depending on implementation details.

while((read_len = fread(buf,2,sizeof(aud),fp)) > 0)

Try adding this line early in the program and you will understand your problem:

printf("sizeof(aud)=%d\n", sizeof(aud));

sizeof returns the size in bytes, but you try to use it as a count of elements and since each of your elements is 2 bytes you try to tell it to read 20 items of 2 bytes each, instead of 10 items of 2 bytes each.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top