From 2bb5b88d82bdfaf1e3e5c8318147b23cc223c235 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 2 Sep 2024 16:00:17 +0200 Subject: [PATCH] PDM: fix setGain() for dfsdm If the gain was too high, the right bit shift RecBuff[i] >> attenuation would result in undefined behaviour https://www.iso-9899.info/n1570.html#6.5.7 Take the chance to also fix the (approximate) conversion from gain (in dB) to bit shift --- libraries/PDM/src/STM32H747_dfsdm/audio.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/PDM/src/STM32H747_dfsdm/audio.c b/libraries/PDM/src/STM32H747_dfsdm/audio.c index ab8d6e902..23c4cacb1 100644 --- a/libraries/PDM/src/STM32H747_dfsdm/audio.c +++ b/libraries/PDM/src/STM32H747_dfsdm/audio.c @@ -391,7 +391,10 @@ int py_audio_init(size_t channels, uint32_t frequency) void py_audio_gain_set(int gain_db) { - attenuation = 8 - gain_db; + attenuation = 8 - (gain_db / 3); + if (attenuation < 0) { + attenuation = 0; + } } void py_audio_deinit()