Skip to content

Commit

Permalink
Prevent ALSA audio corruption
Browse files Browse the repository at this point in the history
When using the ALSA driver, corruption would occur if `snd_pcm_writei`
was unable to consume the entire sound buffer. This would occur
frequently on the Raspberry Pi 3 which uses the `snd_bcm2835` audio
driver.

This bug resulted from incorrect pointer math on line 187, resulting in
the sample source pointer being advanced by `total * ad->channels` bytes
instead of `total * ad->channels` samples. In my opinion, the best fix
is to change `*src` to type `int16_t`, since that is the sample type in
use.

Fixes #43927.
  • Loading branch information
charasyn committed Nov 27, 2020
1 parent 828d1ea commit 25b2f82
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion drivers/alsa/audio_driver_alsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void AudioDriverALSA::thread_func(void *p_udata) {
int total = 0;

while (todo && !ad->exit_thread) {
uint8_t *src = (uint8_t *)ad->samples_out.ptr();
int16_t *src = (int16_t *)ad->samples_out.ptr();
int wrote = snd_pcm_writei(ad->pcm_handle, (void *)(src + (total * ad->channels)), todo);

if (wrote > 0) {
Expand Down

0 comments on commit 25b2f82

Please sign in to comment.