Reputation: 1048
Heyho,
so I'm trying to play pcm audio with libao. My target is to make the playback pausable and cancelable.
For some reason the output over the alsa driver stutters and is played back very very fast. It's like if you're scrubbing the output. If I just play the whole buffer it works fine.
Here's the play loop:
uint_32 next_read = 8192;
while (1) {
ao_play(device, (char *) buffer, next_read);
buffer += next_read;
if (cancel_playback) {
printf("Cancel\n");
break;
}
}
That's the code to prepare my buffer. I'm loading the whole stream into memory:
ao_device *device;
ao_sample_format format;
SF_INFO sfinfo;
int default_driver;
short *buffer;
uint_32 buf_size;
signal(SIGINT, on_cancel_playback);
SNDFILE *file = sf_open("test.wav", SFM_READ, &sfinfo);
printf("Samples: %d\n", sfinfo.frames);
printf("Sample rate: %d\n", sfinfo.samplerate);
printf("Channels: %d\n", sfinfo.channels);
ao_initialize();
default_driver = ao_default_driver_id();
switch (sfinfo.format & SF_FORMAT_SUBMASK) {
case SF_FORMAT_PCM_16:
format.bits = 16;
break;
case SF_FORMAT_PCM_24:
format.bits = 24;
break;
case SF_FORMAT_PCM_32:
format.bits = 32;
break;
case SF_FORMAT_PCM_S8:
format.bits = 8;
break;
case SF_FORMAT_PCM_U8:
format.bits = 8;
break;
default:
format.bits = 16;
break;
}
format.channels = sfinfo.channels;
format.rate = sfinfo.samplerate;
format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;
device = ao_open_live(default_driver, &format, NULL);
if (device == NULL) {
fprintf(stderr, "Error opening device.\n");
return 1;
}
buf_size = (uint_32) (format.channels * sfinfo.frames * sizeof(short));
buffer = calloc(buf_size, sizeof(char));
printf("buffer-size: %d\n", buf_size);
printf("Loading file into buffer...\n");
sf_readf_short(file, buffer, buf_size);
printf("Closing file...\n");
sf_close(file);
printf("Playback...\n");
Upvotes: 2
Views: 375
Reputation: 1048
Found my own answer :D
short *buffer;
->
char *buffer;
The problem was that I wanted to skip next_read bytes in this line:
buffer += next_read;
but the type was short * so I skipped 2 bytes instread of one. :/
Upvotes: 1