1 [+ AutoGen5 template c +]
3 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #define M_PI 3.14159265358979323846264338
40 #define HALF_BUFFER_SIZE (1 << 12)
41 #define BUFFER_SIZE (2 * HALF_BUFFER_SIZE)
44 #define MAX_ERROR 0.0202
48 +]static void [+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +] (const char *filename, int filetype, float maxval) ;
50 +][+ ENDFOR float_type +]
54 +]static void [+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test (const char *filename, int filetype) ;
56 +][+ ENDFOR float_type +]
60 +]static void [+ (get "int_type_name") +]_[+ (get "float_short_name") +]_scale_write_test (const char *filename, int filetype) ;
62 +][+ ENDFOR int_type +]
65 { double dbl [BUFFER_SIZE] ;
66 float flt [BUFFER_SIZE] ;
68 short s [BUFFER_SIZE] ;
72 static BUFFER buffer_out ;
73 static BUFFER buffer_in ;
78 flt_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
79 flt_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
81 dbl_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
82 dbl_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
85 ** Now use SF_FORMAT_AU where possible because it allows both
86 ** big and little endian files.
89 flt_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
90 flt_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
91 flt_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
92 flt_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
93 flt_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
94 flt_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
96 dbl_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
97 dbl_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
98 dbl_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
99 dbl_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
100 dbl_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
101 dbl_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
103 flt_short_clip_read_test ("flt_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
104 flt_int_clip_read_test ("flt_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
105 dbl_short_clip_read_test ("dbl_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
106 dbl_int_clip_read_test ("dbl_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
108 short_flt_scale_write_test ("short_flt.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
109 int_flt_scale_write_test ("int_flt.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
110 short_dbl_scale_write_test ("short_dbl.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
111 int_dbl_scale_write_test ("int_dbl.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
116 /*============================================================================================
117 ** Here are the test functions.
123 [+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +] (const char *filename, int filetype, float maxval)
127 [+ (get "float_type_name") +] *data_out, *data_in ;
128 double diff, clip_max_diff ;
130 print_test_name ("[+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +]", filename) ;
132 data_out = buffer_out.[+ (get "float_short_name") +] ;
133 data_in = buffer_in.[+ (get "float_short_name") +] ;
135 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
136 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
137 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
140 sfinfo.samplerate = 44100 ;
141 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
142 sfinfo.channels = 1 ;
143 sfinfo.format = filetype ;
146 ** Write two versions of the data:
147 ** normalized and clipped
148 ** un-normalized and clipped.
151 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
152 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
153 test_write_[+ (get "float_type_name") +]_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
154 sf_command (file, SFC_SET_NORM_[+ (get "float_upper_name") +], NULL, SF_FALSE) ;
155 test_write_[+ (get "float_type_name") +]_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
158 memset (&buffer_in, 0, sizeof (buffer_in)) ;
160 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
162 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
164 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
165 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
169 if (sfinfo.frames != BUFFER_SIZE)
170 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
174 if (sfinfo.channels != 1)
175 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
179 check_log_buffer_or_die (file, __LINE__) ;
181 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
182 sf_command (file, SFC_SET_NORM_[+ (get "float_upper_name") +], NULL, SF_FALSE) ;
183 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
186 /* Check normalized version. */
187 clip_max_diff = 0.0 ;
188 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
189 { if (fabs (data_in [k]) > 1.0)
190 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
194 if (data_out [k] * data_in [k] < 0.0)
195 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
199 if (fabs (data_out [k]) > 1.0)
202 diff = fabs (data_out [k] - data_in [k]) ;
203 if (diff > clip_max_diff)
204 clip_max_diff = diff ;
207 if (clip_max_diff < 1e-20)
208 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
212 if (clip_max_diff > [+ (get "error_val") +])
213 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
217 /* Check the un-normalized data. */
218 clip_max_diff = 0.0 ;
219 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
220 { if (fabs (data_in [k]) > maxval)
221 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
225 if (data_out [k] * data_in [k] < 0.0)
226 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
230 if (fabs (data_out [k]) > maxval)
233 diff = fabs (data_out [k] - data_in [k]) ;
234 if (diff > clip_max_diff)
235 clip_max_diff = diff ;
238 if (clip_max_diff < 1e-20)
239 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
243 if (clip_max_diff > 1.0)
244 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
250 } /* [+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +] */
254 [+ ENDFOR float_type +]
256 /*==============================================================================
261 +]static void [+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test (const char *filename, int filetype)
264 [+ (get "float_type_name") +] *data_out ;
265 [+ (get "int_type_name") +] *data_in, max_value ;
268 print_test_name ("[+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test", filename) ;
270 data_out = buffer_out.[+ (get "float_short_name") +] ;
271 data_in = buffer_in.[+ (get "int_short_name") +] ;
273 for (k = 0 ; k < BUFFER_SIZE ; k++)
274 data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
275 data_out [BUFFER_SIZE / 8] = 1.0 ;
276 data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
277 data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
278 data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
280 memset (&sfinfo, 0, sizeof (sfinfo)) ;
281 sfinfo.samplerate = 44100 ;
282 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
283 sfinfo.channels = 1 ;
284 sfinfo.format = filetype ;
286 /* Save unclipped data to the file. */
287 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
288 test_write_[+ (get "float_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
291 memset (&sfinfo, 0, sizeof (sfinfo)) ;
293 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
294 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
296 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
298 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
299 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
303 if (sfinfo.frames != BUFFER_SIZE)
304 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
308 if (sfinfo.channels != 1)
309 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
313 check_log_buffer_or_die (file, __LINE__) ;
315 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
316 test_read_[+ (get "int_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
317 /*-sf_command (file, SFC_SET_NORM_[+ (get "float_upper_name") +], NULL, SF_FALSE) ;-*/
320 /* Check the first half. */
322 for (k = 0 ; k < sfinfo.frames ; k++)
323 { /* Check if data_out has different sign from data_in. */
324 if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
325 { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ;
328 max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
333 } /* [+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test */
335 +][+ ENDFOR float_type +]
337 /*==============================================================================
342 +]static void [+ (get "int_type_name") +]_[+ (get "float_short_name") +]_scale_write_test (const char *filename, int filetype)
345 [+ (get "int_type_name") +] *data_out ;
346 [+ (get "float_type_name") +] *data_in, max_value ;
349 print_test_name ("[+ (get "int_type_name") +]_[+ (get "float_short_name") +]_clip_write_test", filename) ;
351 data_out = buffer_out.[+ (get "int_short_name") +] ;
352 data_in = buffer_in.[+ (get "float_short_name") +] ;
354 for (k = 0 ; k < BUFFER_SIZE ; k++)
355 data_out [k] = [+ (get "float_to_int") +] ([+ (get "int_max_value") +] * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
357 memset (&sfinfo, 0, sizeof (sfinfo)) ;
358 sfinfo.samplerate = 44100 ;
359 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
360 sfinfo.channels = 1 ;
361 sfinfo.format = filetype ;
363 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
364 test_write_[+ (get "int_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
365 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
366 test_write_[+ (get "int_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
367 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
368 test_write_[+ (get "int_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
371 memset (&sfinfo, 0, sizeof (sfinfo)) ;
373 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
375 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
377 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
378 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
382 if (sfinfo.frames != 3 * BUFFER_SIZE)
383 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, 3 * BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
387 if (sfinfo.channels != 1)
388 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
392 check_log_buffer_or_die (file, __LINE__) ;
394 /* Check the first section. */
395 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
398 for (k = 0 ; k < BUFFER_SIZE ; k++)
399 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
401 if (max_value < 1000.0)
402 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
406 /* Check the second section. */
407 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
410 for (k = 0 ; k < BUFFER_SIZE ; k++)
411 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
414 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
418 /* Check the third section. */
419 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
422 for (k = 0 ; k < BUFFER_SIZE ; k++)
423 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
425 if (max_value < 1000.0)
426 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
434 } /* [+ (get "int_type_name") +]_[+ (get "float_short_name") +]_scale_write_test */
436 +][+ ENDFOR int_type +]