]>
Commit | Line | Data |
---|---|---|
8529da43 MG |
1 | /* |
2 | ** Copyright (c) 2003-2016, Erik de Castro Lopo <erikd@mega-nerd.com> | |
3 | ** All rights reserved. | |
4 | ** | |
5 | ** This code is released under 2-clause BSD license. Please see the | |
6 | ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING | |
7 | */ | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
12 | ||
13 | #include <samplerate.h> | |
14 | ||
15 | #include "util.h" | |
16 | ||
17 | #define BUFFER_LEN 10000 | |
18 | ||
19 | static void float_to_short_test (void) ; | |
20 | static void short_to_float_test (void) ; | |
21 | ||
22 | static void float_to_int_test (void) ; | |
23 | static void int_to_float_test (void) ; | |
24 | ||
25 | int | |
26 | main (void) | |
27 | { | |
28 | puts ("") ; | |
29 | ||
30 | float_to_short_test () ; | |
31 | short_to_float_test () ; | |
32 | ||
33 | float_to_int_test () ; | |
34 | int_to_float_test () ; | |
35 | ||
36 | puts ("") ; | |
37 | ||
38 | return 0 ; | |
39 | } /* main */ | |
40 | ||
41 | /*===================================================================================== | |
42 | */ | |
43 | ||
44 | static void | |
45 | float_to_short_test (void) | |
46 | { | |
47 | static float fpos [] = | |
48 | { 0.95, 0.99, 1.0, 1.01, 1.1, 2.0, 11.1, 111.1, 2222.2, 33333.3 | |
49 | } ; | |
50 | static float fneg [] = | |
51 | { -0.95, -0.99, -1.0, -1.01, -1.1, -2.0, -11.1, -111.1, -2222.2, -33333.3 | |
52 | } ; | |
53 | ||
54 | static short out [MAX (ARRAY_LEN (fpos), ARRAY_LEN (fneg))] ; | |
55 | ||
56 | int k ; | |
57 | ||
58 | printf ("\tfloat_to_short_test ............................. ") ; | |
59 | ||
60 | src_float_to_short_array (fpos, out, ARRAY_LEN (fpos)) ; | |
61 | ||
62 | for (k = 0 ; k < ARRAY_LEN (fpos) ; k++) | |
63 | if (out [k] < 30000) | |
64 | { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ; | |
65 | exit (1) ; | |
66 | } ; | |
67 | ||
68 | src_float_to_short_array (fneg, out, ARRAY_LEN (fneg)) ; | |
69 | ||
70 | for (k = 0 ; k < ARRAY_LEN (fneg) ; k++) | |
71 | if (out [k] > -30000) | |
72 | { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ; | |
73 | exit (1) ; | |
74 | } ; | |
75 | ||
76 | puts ("ok") ; | |
77 | ||
78 | return ; | |
79 | } /* float_to_short_test */ | |
80 | ||
81 | /*------------------------------------------------------------------------------------- | |
82 | */ | |
83 | ||
84 | static void | |
85 | short_to_float_test (void) | |
86 | { | |
87 | static short input [BUFFER_LEN] ; | |
88 | static short output [BUFFER_LEN] ; | |
89 | static float temp [BUFFER_LEN] ; | |
90 | ||
91 | int k ; | |
92 | ||
93 | printf ("\tshort_to_float_test ............................. ") ; | |
94 | ||
95 | for (k = 0 ; k < ARRAY_LEN (input) ; k++) | |
96 | input [k] = (k * 0x8000) / ARRAY_LEN (input) ; | |
97 | ||
98 | src_short_to_float_array (input, temp, ARRAY_LEN (temp)) ; | |
99 | src_float_to_short_array (temp, output, ARRAY_LEN (output)) ; | |
100 | ||
101 | for (k = 0 ; k < ARRAY_LEN (input) ; k++) | |
102 | if (ABS (input [k] - output [k]) > 0) | |
103 | { printf ("\n\n\tLine %d : index %d %d -> %d\n", __LINE__, k, input [k], output [k]) ; | |
104 | exit (1) ; | |
105 | } ; | |
106 | ||
107 | puts ("ok") ; | |
108 | ||
109 | return ; | |
110 | } /* short_to_float_test */ | |
111 | ||
112 | /*===================================================================================== | |
113 | */ | |
114 | ||
115 | static void | |
116 | float_to_int_test (void) | |
117 | { | |
118 | static float fpos [] = | |
119 | { 0.95, 0.99, 1.0, 1.01, 1.1, 2.0, 11.1, 111.1, 2222.2, 33333.3 | |
120 | } ; | |
121 | static float fneg [] = | |
122 | { -0.95, -0.99, -1.0, -1.01, -1.1, -2.0, -11.1, -111.1, -2222.2, -33333.3 | |
123 | } ; | |
124 | ||
125 | static int out [MAX (ARRAY_LEN (fpos), ARRAY_LEN (fneg))] ; | |
126 | ||
127 | int k ; | |
128 | ||
129 | printf ("\tfloat_to_int_test ............................... ") ; | |
130 | ||
131 | src_float_to_int_array (fpos, out, ARRAY_LEN (fpos)) ; | |
132 | ||
133 | for (k = 0 ; k < ARRAY_LEN (fpos) ; k++) | |
134 | if (out [k] < 30000 * 0x10000) | |
135 | { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ; | |
136 | exit (1) ; | |
137 | } ; | |
138 | ||
139 | src_float_to_int_array (fneg, out, ARRAY_LEN (fneg)) ; | |
140 | ||
141 | for (k = 0 ; k < ARRAY_LEN (fneg) ; k++) | |
142 | if (out [k] > -30000 * 0x1000) | |
143 | { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ; | |
144 | exit (1) ; | |
145 | } ; | |
146 | ||
147 | puts ("ok") ; | |
148 | ||
149 | return ; | |
150 | } /* float_to_int_test */ | |
151 | ||
152 | /*------------------------------------------------------------------------------------- | |
153 | */ | |
154 | ||
155 | static void | |
156 | int_to_float_test (void) | |
157 | { | |
158 | static int input [BUFFER_LEN] ; | |
159 | static int output [BUFFER_LEN] ; | |
160 | static float temp [BUFFER_LEN] ; | |
161 | ||
162 | int k ; | |
163 | ||
164 | printf ("\tint_to_float_test ............................... ") ; | |
165 | ||
166 | for (k = 0 ; k < ARRAY_LEN (input) ; k++) | |
167 | input [k] = (k * 0x80000000) / ARRAY_LEN (input) ; | |
168 | ||
169 | src_int_to_float_array (input, temp, ARRAY_LEN (temp)) ; | |
170 | src_float_to_int_array (temp, output, ARRAY_LEN (output)) ; | |
171 | ||
172 | for (k = 0 ; k < ARRAY_LEN (input) ; k++) | |
173 | if (ABS (input [k] - output [k]) > 0) | |
174 | { printf ("\n\n\tLine %d : index %d %d -> %d\n", __LINE__, k, input [k], output [k]) ; | |
175 | exit (1) ; | |
176 | } ; | |
177 | ||
178 | puts ("ok") ; | |
179 | ||
180 | return ; | |
181 | } /* int_to_float_test */ | |
182 |