shithub: libsamplerate

Download patch

ref: a80207556e53a7f6875c72558585c9aece2b1211
parent: c8a94e015b67f6f894acd311f4a4d4cd064366f0
author: Erik de Castro Lopo <erikd@mingus>
date: Mon Apr 17 23:11:55 EDT 2006

Add functions src_int_to_float_array and src_float_to_int_array, add tests and document.

--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2006-04-18  Erik de Castro Lopo  <erikd AT mega-nerd DOT com>
+
+    * src/samplerate.[ch] src/Version_script.in
+    Add functions src_int_to_float_array and src_float_to_int_array.
+
+    * tests/float_short_test.c
+    Update test to including testing of above.
+
+    * doc/api_misc.html
+    Update docs fro the above addition.
+
 2006-03-31  Erik de Castro Lopo  <erikd AT mega-nerd DOT com>
 
     * src/common.h src/src_*.c src/samplerate.c
--- a/doc/api_misc.html
+++ b/doc/api_misc.html
@@ -198,19 +198,21 @@
 <A NAME="Aux"></A>
 <H3><BR>Auxillary Functions</H3>
 <P>
-There are two auxillary functions for converting arrays of float data
-to and from short data.
+There are four auxillary functions for converting arrays of float data
+to and from short or int data.
 These functions are defined as:
 </P>
 <PRE>
     void src_short_to_float_array (const short *in, float *out, int len) ;
     void src_float_to_short_array (const float *in, short *out, int len) ;
+    void src_int_to_float_array (const int *in, float *out, int len) ;
+    void src_float_to_int_array (const float *in, int *out, int len) ;
 </PRE>
 <P>
 The float data is assumed to be in the range [-1.0, 1.0] and it is
 automatically scaled on the conversion to and from float.
-On the float to short conversion path, any data values which would overflow
-the range of short data are clipped.
+On the float to short/int conversion path, any data values which would overflow
+the range of short/int data are clipped.
 </P>
 
 </DIV>
--- a/src/Version_script.in
+++ b/src/Version_script.in
@@ -46,6 +46,8 @@
 
 		src_short_to_float_array ;
 		src_float_to_short_array ;
+		src_int_to_float_array ;
+		src_float_to_int_array ;
 	local:
 		*;
 };
--- a/src/samplerate.c
+++ b/src/samplerate.c
@@ -477,6 +477,39 @@
 
 } /* src_float_to_short_array */
 
+void
+src_int_to_float_array (const int *in, float *out, int len)
+{
+	while (len)
+	{	len -- ;
+		out [len] = in [len] / (8.0 * 0x10000000) ;
+		} ;
+
+	return ;
+} /* src_int_to_float_array */
+
+void
+src_float_to_int_array (const float *in, int *out, int len)
+{	float scaled_value ;
+
+	while (len)
+	{	len -- ;
+
+		scaled_value = in [len] * (8.0 * 0x10000000) ;
+		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
+		{	out [len] = 0x7fffffff ;
+			continue ;
+			} ;
+		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
+		{	out [len] = -1 - 0x7fffffff ;
+			continue ;
+			} ;
+
+		out [len] = lrintf (scaled_value) ;
+		} ;
+
+} /* src_float_to_int_array */
+
 /*==============================================================================
 **	Private functions.
 */
--- a/src/samplerate.h
+++ b/src/samplerate.h
@@ -188,6 +188,9 @@
 void src_short_to_float_array (const short *in, float *out, int len) ;
 void src_float_to_short_array (const float *in, short *out, int len) ;
 
+void src_int_to_float_array (const int *in, float *out, int len) ;
+void src_float_to_int_array (const float *in, int *out, int len) ;
+
 
 #ifdef __cplusplus
 }		/* extern "C" */
--- a/tests/float_short_test.c
+++ b/tests/float_short_test.c
@@ -30,6 +30,9 @@
 static void float_to_short_test (void) ;
 static void short_to_float_test (void) ;
 
+static void float_to_int_test (void) ;
+static void int_to_float_test (void) ;
+
 int
 main (void)
 {
@@ -38,6 +41,9 @@
 	float_to_short_test () ;
 	short_to_float_test () ;
 
+	float_to_int_test () ;
+	int_to_float_test () ;
+
 	puts ("") ;
 
 	return 0 ;
@@ -113,6 +119,77 @@
 
 	return ;
 } /* short_to_float_test */
+
+/*=====================================================================================
+*/
+
+static void
+float_to_int_test (void)
+{
+	static float fpos [] =
+	{	0.95, 0.99, 1.0, 1.01, 1.1, 2.0, 11.1, 111.1, 2222.2, 33333.3
+		} ;
+	static float fneg [] =
+	{	-0.95, -0.99, -1.0, -1.01, -1.1, -2.0, -11.1, -111.1, -2222.2, -33333.3
+		} ;
+
+	static int out [MAX (ARRAY_LEN (fpos), ARRAY_LEN (fneg))] ;
+
+	int k ;
+
+	printf ("\tfloat_to_int_test ............................... ") ;
+
+	src_float_to_int_array (fpos, out, ARRAY_LEN (fpos)) ;
+
+	for (k = 0 ; k < ARRAY_LEN (fpos) ; k++)
+		if (out [k] < 30000 * 0x10000)
+		{	printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ;
+			exit (1) ;
+			} ;
+
+	src_float_to_int_array (fneg, out, ARRAY_LEN (fneg)) ;
+
+	for (k = 0 ; k < ARRAY_LEN (fneg) ; k++)
+		if (out [k] > -30000 * 0x1000)
+		{	printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ;
+			exit (1) ;
+			} ;
+
+	puts ("ok") ;
+
+	return ;
+} /* float_to_int_test */
+
+/*-------------------------------------------------------------------------------------
+*/
+
+static void
+int_to_float_test (void)
+{
+	static int input	[BUFFER_LEN] ;
+	static int output	[BUFFER_LEN] ;
+	static float temp	[BUFFER_LEN] ;
+
+	int k ;
+
+	printf ("\tint_to_float_test ............................... ") ;
+
+	for (k = 0 ; k < ARRAY_LEN (input) ; k++)
+		input [k] = (k * 0x80000000) / ARRAY_LEN (input) ;
+
+	src_int_to_float_array (input, temp, ARRAY_LEN (temp)) ;
+	src_float_to_int_array (temp, output, ARRAY_LEN (output)) ;
+
+	for (k = 0 ; k < ARRAY_LEN (input) ; k++)
+		if (ABS (input [k] - output [k]) > 0)
+		{	printf ("\n\n\tLine %d : index %d   %d -> %d\n", __LINE__, k, input [k], output [k]) ;
+			exit (1) ;
+			} ;
+
+	puts ("ok") ;
+
+	return ;
+} /* int_to_float_test */
 
 
 /*