From: mkmer Date: Mon, 24 Mar 2025 01:04:50 +0000 (-0400) Subject: utils.h: Add rounding to float conversion to int. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bd462f07750ceb2ee2e9cc5d6f8f45949c12c274;p=thirdparty%2Fasterisk.git 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 (cherry picked from commit 55f4294e821fef183db71bf2f897d906c808670e) --- diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h index 3c06e834eb..9c98050c13 100644 --- a/include/asterisk/utils.h +++ b/include/asterisk/utils.h @@ -490,8 +490,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) @@ -506,8 +509,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