Hi,
Am trying to achieve something trivial: reading 1 second of audio at a time, but something goes wrong:
Sound 1277148008.835094 Sound 1277148008.838224 Sound 1277148010.834839 Sound 1277148010.838001 Sound 1277148012.834523 Sound 1277148012.837580 Sound 1277148014.835240 Sound 1277148014.838333
As you can see; audio is returned for 2 seconds, then it sleeps 2 secondsn, then 2 seconds of audio, etc.
What I do is:
if (snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) { }
if (snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE) < 0) { }
exact_rate = 44100; if (snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &exact_rate, 0) < 0) { error_exit("Error setting rate.\n"); } if (*rate != exact_rate) { fprintf(stderr, "The rate %d Hz is not supported by your hardware.\n ==> Using %d Hz instead.\n", *rate, exact_rate); *rate = exact_rate; }
if (snd_pcm_hw_params_set_channels(pcm_handle, hwparams, 2) < 0) { }
int periods = 2; if (snd_pcm_hw_params_set_periods(pcm_handle, hwparams, periods, 0) < 0) { }
if (snd_pcm_hw_params_set_buffer_size(pcm_handle, hwparams, exact_rate * 2 * n_channels) < 0) { }
if (snd_pcm_hw_params(pcm_handle, hwparams) < 0) { }
return pcm_handle; }
void get_audio_1s(snd_pcm_t *pcm_handle, short *buffer, int rate, int channels) { int samples = channels * rate, index = 0;
while(samples > 0) { int cur = samples, pcmreturn;
while ((pcmreturn = snd_pcm_readi(pcm_handle, &buffer[index], cur)) < 0) { snd_pcm_prepare(pcm_handle); printf(" underrun\n"); }
index += pcmreturn; samples -= pcmreturn; } }
So I call this get_audio_1s() with the samplerate (44100) and the number of channels (2). Then it seems to return the first second at once, then the second second of audio too, then sleeps for 2 seconds(!) and then repeats.
Folkert van Heusden