From ee035599c36e3c0bc95add78aa5398af2a9627ab Mon Sep 17 00:00:00 2001 From: karlstav Date: Sun, 18 Nov 2018 11:59:58 +0100 Subject: [PATCH] getting hw params in alsa init (ref #240) used snd_pcm_get_params to get the period size from alsa in case this has been set by asoundrc the period size is then used to create the audio buffer and with the sound format calcualte the number of frames --- input/alsa.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/input/alsa.c b/input/alsa.c index 8b5594d..3ed8e7e 100644 --- a/input/alsa.c +++ b/input/alsa.c @@ -96,23 +96,32 @@ const int size) { #define FRAMES_NUMBER 256 void* input_alsa(void* data) { - int err; + int err; struct audio_data* audio = (struct audio_data*)data; snd_pcm_t* handle; + snd_pcm_uframes_t buffer_size; + snd_pcm_uframes_t period_size; snd_pcm_uframes_t frames = FRAMES_NUMBER; - int16_t buf[FRAMES_NUMBER * 2]; + initialize_audio_parameters(&handle, audio, &frames); + snd_pcm_get_params(handle, &buffer_size, &period_size); + + int16_t buf[period_size]; + frames = period_size / ((audio->format / 8) * CHANNELS_COUNT); + //printf("period size: %lu\n", period_size); + //exit(0); + // frames * bits/8 * channels - const int size = frames * (audio->format / 8) * CHANNELS_COUNT; - signed char* buffer = malloc(size); - int n = 0; + //const int size = frames * (audio->format / 8) * CHANNELS_COUNT; + signed char* buffer = malloc(period_size); + int n = 0; while (1) { switch (audio->format) { case 16: err = snd_pcm_readi(handle, buf, frames); - for (int i = 0; i < FRAMES_NUMBER * 2; i += 2) { - if (audio->channels == 1) audio->audio_out_l[n] = (buf[i] + buf[i + 1]) / 2; + for (int i = 0; i < frames * 2; i += 2) { + if (audio->channels == 1) audio->audio_out_l[n] = (buf[i] + buf[i + 1]) / 2; //stereo storing channels in buffer if (audio->channels == 2) { audio->audio_out_l[n] = buf[i]; @@ -123,8 +132,8 @@ void* input_alsa(void* data) { } break; default: - err = snd_pcm_readi(handle, buffer, frames); - fill_audio_outs(audio, buffer, size); + err = snd_pcm_readi(handle, buffer, frames); + fill_audio_outs(audio, buffer, period_size); break; }