From d5ef09ea0c6af2a642e9f501267636296997b01a Mon Sep 17 00:00:00 2001 From: mkmer Date: Sun, 23 Mar 2025 21:04:50 -0400 Subject: [PATCH] utils.h: Add rounding to float conversion to int. Quote from an audio engineer NR9V: There is a minor issue of a small amount of crossover distortion though as a result of `ast_slinear_saturated_multiply_float()` not rounding the float. This could result in some quiet but potentially audible distortion artifacts in lower volume parts of the signal. If you have for example a sign wave function with a max amplitude of just a few samples, all samples between -1 and 1 will be truncated to zero, resulting in the waveform no longer being a sine wave and in harmonic distortion. Resolves: #1176 --- include/asterisk/utils.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h index 30913fc05d..ae09e1ab68 100644 --- a/include/asterisk/utils.h +++ b/include/asterisk/utils.h @@ -495,8 +495,11 @@ static force_inline void ast_slinear_saturated_multiply_float(short *input, floa *input = 32767; else if (res < -32768) *input = -32768; + else if (res > 0) + *input = (short) (res + 0.5); else - *input = (short) res; + *input = (short) (res - 0.5); + } static force_inline void ast_slinear_saturated_divide(short *input, short *value) @@ -511,8 +514,11 @@ static force_inline void ast_slinear_saturated_divide_float(short *input, float *input = 32767; else if (res < -32768) *input = -32768; + else if (res > 0) + *input = (short) (res + 0.5); else - *input = (short) res; + *input = (short) (res - 0.5); + } #ifdef localtime_r -- 2.47.2