Alsa: Keep 8 samples in input buffer to avoid reading corrupted samples

This commit is contained in:
Andreas Eversberg
2016-08-13 15:19:12 +02:00
parent 541eafe59e
commit 7d111546c4

View File

@@ -218,6 +218,8 @@ int sound_write(void *inst, int16_t *samples_left, int16_t *samples_right, int n
return rc; return rc;
} }
#define KEEP_FRAMES 8 /* minimum frames not to read, due to bug in ALSA */
int sound_read(void *inst, int16_t *samples_left, int16_t *samples_right, int num) int sound_read(void *inst, int16_t *samples_left, int16_t *samples_right, int num)
{ {
sound_t *sound = (sound_t *)inst; sound_t *sound = (sound_t *)inst;
@@ -227,12 +229,12 @@ int sound_read(void *inst, int16_t *samples_left, int16_t *samples_right, int nu
/* get samples in rx buffer */ /* get samples in rx buffer */
in = snd_pcm_avail(sound->chandle); in = snd_pcm_avail(sound->chandle);
/* if less than 2 frames available, try next time */ /* if not more than KEEP_FRAMES frames available, try next time */
if (in < 2) if (in <= KEEP_FRAMES)
return 0; return 0;
/* read one frame less than in buffer, because snd_pcm_readi() seems /* read some frames less than in buffer, because snd_pcm_readi() seems
* to corrupt last frame */ * to corrupt last frames */
in--; in -= KEEP_FRAMES;
if (in > num) if (in > num)
in = num; in = num;