]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
utils.h: Add rounding to float conversion to int.
authormkmer <m_kasper@sbcglobal.net>
Mon, 24 Mar 2025 01:04:50 +0000 (21:04 -0400)
committergithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Fri, 27 Jun 2025 15:38:36 +0000 (15:38 +0000)
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

index 30913fc05d89ee7a8883bd6a07a46a737435d993..ae09e1ab68f784f87ba182ed0d02b1c1cb7b8164 100644 (file)
@@ -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