I am trying to play audio files programmatically using alsa library. My platform is Linux and my end product will be an embedded board. I have modified the code found here https://gist.github.com/ghedo/963382/ as my stating point. Instead of playing from the stdin in the code from above link, I will be using wav files.
The sample wave files played are from the location https://www2.cs.uic.edu/~i101/SoundFiles/ e.g https://www2.cs.uic.edu/~i101/SoundFiles/PinkPanther30.wav
My code
#include <alsa/asoundlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
unsigned int pcm, tmp, dir;
int rate, channels, seconds;
snd_pcm_t *pcm_handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
char *buff;
int buff_size, loops;
int fd;
if (argc < 5) {
printf("Usage: %s <sample_rate> <channels> <seconds> <file-wav> <interface>\n", argv[0]);
return -1;
}
fd = open(argv[4], O_RDONLY, 0);
if (fd < 0) {
printf("File not found %s\n", argv[4]);
return 0;
}
lseek(fd, 44, SEEK_SET);
rate = atoi(argv[1]);
channels = atoi(argv[2]);
seconds = atoi(argv[3]);
/* Open the PCM device in playback mode */
if (pcm = snd_pcm_open(&pcm_handle, argv[5], SND_PCM_STREAM_PLAYBACK, 0) < 0)
printf("ERROR: Can't open \"%s\" PCM device. %s\n", argv[5], snd_strerror(pcm));
/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcm_handle, params);
/* Set parameters */
if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S16_LE) < 0)
printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0)
printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0)
printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
/* Write parameters */
if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0)
printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
/* Resume information */
printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
snd_pcm_hw_params_get_channels(params, &tmp);
printf("channels: %i ", tmp);
if (tmp == 1)
printf("(mono)\n");
else if (tmp == 2)
printf("(stereo)\n");
snd_pcm_hw_params_get_rate(params, &tmp, 0);
printf("rate: %d bps\n", tmp);
printf("seconds: %d\n", seconds);
/* Allocate buffer to hold single period */
snd_pcm_hw_params_get_period_size(params, &frames, 0);
buff_size = frames * channels * 2 /* 2 -> sample size */;
buff = (char *) malloc(buff_size);
snd_pcm_hw_params_get_period_time(params, &tmp, NULL);
while (pcm = read(fd, buff, buff_size) > 0) {
if (pcm = snd_pcm_writei(pcm_handle, buff, frames) == -EPIPE) {
printf("XRUN.\n");
snd_pcm_prepare(pcm_handle);
} else if (pcm < 0) {
printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
}
usleep(10);
}
snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
free(buff);
return 0;
}
The major changes in the code are
fd = open(argv[4], O_RDONLY, 0);
if (fd < 0) {
printf("File not found %s\n", argv[4]);
return 0;
}
lseek(fd, 44, SEEK_SET);
Lseek was suggested in the comments
The issue: I find that the file is played faster, i.e. when I play the same files with aplay this code plays the wav file at a different tempo.
I have compared the code from aplay, the playback code as shown here https://github.com/alsa-project/alsa-utils/blob/master/aplay/aplay.c#L3032 and could not find any drastic difference.
I have also tried the code from this question ALSA: wav file playing and find that the file is played faster that the normal tempo.
Could any one point out what am i missing here?