From 9d440a15e3452fdb07e4e5f55d03204a70841f7d Mon Sep 17 00:00:00 2001 From: Martin Piatka Date: Tue, 6 Apr 2021 16:36:17 +0200 Subject: [PATCH] audio_utils: float2int: clamp input to valid range This avoids pops and buzzing caused by integer overflow when the input sample falls outside of the range [-1.0, 1.0] (I encountered this when using jack capture). --- src/audio/utils.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/audio/utils.cpp b/src/audio/utils.cpp index 6defd824d..8ebf9a3fa 100644 --- a/src/audio/utils.cpp +++ b/src/audio/utils.cpp @@ -321,7 +321,10 @@ void float2int(char *out, const char *in, int len) int items = len / sizeof(int32_t); while(items-- > 0) { - *outi++ = *inf++ * INT_MAX; + float sample = *inf++; + if(sample > 1.0) sample = 1.0; + if(sample < -1.0) sample = -1.0; + *outi++ = sample * INT_MAX; } }