shithub: libsamplerate

Download patch

ref: a2e60106af2e1b327680ea0fa837795f7f687ba3
parent: ab8b482def718b2fbaadb7c7b49d745d499659e9
author: Alexander Grund <[email protected]>
date: Fri Aug 9 14:50:35 EDT 2019

Remove shift of signed int in src_float_to_short_array

Scale by -SHORT_MIN
Clip if float is outside range of short
Otherwise round and truncate (guaranteed to fit to short)
Uses float instead of double as float can fit all 16 bit values

--- a/src/samplerate.c
+++ b/src/samplerate.c
@@ -495,24 +495,18 @@
 
 void
 src_float_to_short_array (const float *in, short *out, int len)
-{	double scaled_value ;
-
+{
 	while (len)
-	{	len -- ;
-
-		scaled_value = in [len] * (8.0 * 0x10000000) ;
-		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
-		{	out [len] = 32767 ;
-			continue ;
-			} ;
-		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
-		{	out [len] = -32768 ;
-			continue ;
-			} ;
-
-		out [len] = (short) (lrint (scaled_value) >> 16) ;
-		} ;
-
+	{	float scaled_value ;
+		len -- ;
+		scaled_value = in [len] * 32768.f ;
+		if (scaled_value >= 32767.f)
+			out [len] = 32767 ;
+		else if (scaled_value <= -32768.f)
+			out [len] = -32768 ;
+		else
+			out [len] = (short) (lrintf (scaled_value)) ;
+	}
 } /* src_float_to_short_array */
 
 void