libsndfile compiling.
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / floating_point_test.c
1 /*
2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #include <sndfile.h>
31
32 #include "dft_cmp.h"
33 #include "utils.h"
34
35 #define SAMPLE_RATE 16000
36
37 static void float_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) ;
38 static void double_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) ;
39
40 static void float_short_little_test (const char * filename) ;
41 static void float_short_big_test (const char * filename) ;
42 static void float_int_little_test (const char * filename) ;
43 static void float_int_big_test (const char * filename) ;
44 static void double_short_little_test (const char * filename) ;
45 static void double_short_big_test (const char * filename) ;
46 static void double_int_little_test (const char * filename) ;
47 static void double_int_big_test (const char * filename) ;
48
49
50 static double double_data [DFT_DATA_LENGTH] ;
51 static double double_test [DFT_DATA_LENGTH] ;
52
53 static float float_data [DFT_DATA_LENGTH] ;
54 static float float_test [DFT_DATA_LENGTH] ;
55
56 static double double_data [DFT_DATA_LENGTH] ;
57 static short short_data [DFT_DATA_LENGTH] ;
58 static int int_data [DFT_DATA_LENGTH] ;
59
60 int
61 main (int argc, char *argv [])
62 { int allow_exit = 1 ;
63
64 if (argc == 2 && ! strstr (argv [1], "no-exit"))
65 allow_exit = 0 ;
66
67 #if ((HAVE_LRINTF == 0) && (HAVE_LRINT_REPLACEMENT == 0))
68 puts ("*** Cannot run this test on this platform because it lacks lrintf().") ;
69 exit (0) ;
70 #endif
71
72 /* Float tests. */
73 float_scaled_test ("float.raw", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT, -163.0) ;
74
75 /* Test both signed and unsigned 8 bit files. */
76 float_scaled_test ("pcm_s8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_S8, -39.0) ;
77 float_scaled_test ("pcm_u8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_U8, -39.0) ;
78
79 float_scaled_test ("pcm_16.raw", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_16, -87.0) ;
80 float_scaled_test ("pcm_24.raw", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_24, -138.0) ;
81 float_scaled_test ("pcm_32.raw", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_32, -163.0) ;
82
83 float_scaled_test ("ulaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ULAW, -50.0) ;
84 float_scaled_test ("alaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ALAW, -49.0) ;
85
86 float_scaled_test ("ima_adpcm.wav", allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, -47.0) ;
87 float_scaled_test ("ms_adpcm.wav" , allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, -40.0) ;
88 float_scaled_test ("gsm610.raw" , allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_GSM610, -33.0) ;
89
90 float_scaled_test ("g721_32.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G721_32, -34.0) ;
91 float_scaled_test ("g723_24.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_24, -34.0) ;
92 float_scaled_test ("g723_40.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_40, -40.0) ;
93
94 /* PAF files do not use the same encoding method for 24 bit PCM data as other file
95 ** formats so we need to explicitly test it here.
96 */
97 float_scaled_test ("le_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -149.0) ;
98 float_scaled_test ("be_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -149.0) ;
99
100 float_scaled_test ("dwvw_12.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_12, -64.0) ;
101 float_scaled_test ("dwvw_16.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_16, -92.0) ;
102 float_scaled_test ("dwvw_24.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_24, -151.0) ;
103
104 float_scaled_test ("adpcm.vox", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, -40.0) ;
105
106 float_scaled_test ("dpcm_16.xi", allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_16, -90.0) ;
107 float_scaled_test ("dpcm_8.xi" , allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_8 , -41.0) ;
108
109 float_scaled_test ("pcm_s8.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_S8, -90.0) ;
110 float_scaled_test ("pcm_16.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_16, -140.0) ;
111 float_scaled_test ("pcm_24.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_24, -170.0) ;
112
113 #if HAVE_EXTERNAL_LIBS
114 float_scaled_test ("flac_8.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, -39.0) ;
115 float_scaled_test ("flac_16.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_16, -87.0) ;
116 float_scaled_test ("flac_24.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_24, -138.0) ;
117
118 float_scaled_test ("vorbis.oga", allow_exit, SF_FALSE, SF_FORMAT_OGG | SF_FORMAT_VORBIS, -31.0) ;
119 #endif
120
121 float_scaled_test ("replace_float.raw", allow_exit, SF_TRUE, SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT, -163.0) ;
122
123 /*==============================================================================
124 ** Double tests.
125 */
126
127 double_scaled_test ("double.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DOUBLE, -300.0) ;
128
129 /* Test both signed (AIFF) and unsigned (WAV) 8 bit files. */
130 double_scaled_test ("pcm_s8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_S8, -39.0) ;
131 double_scaled_test ("pcm_u8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_U8, -39.0) ;
132
133 double_scaled_test ("pcm_16.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_16, -87.0) ;
134 double_scaled_test ("pcm_24.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_24, -135.0) ;
135 double_scaled_test ("pcm_32.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_32, -184.0) ;
136
137 double_scaled_test ("ulaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ULAW, -50.0) ;
138 double_scaled_test ("alaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ALAW, -49.0) ;
139
140 double_scaled_test ("ima_adpcm.wav", allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, -47.0) ;
141 double_scaled_test ("ms_adpcm.wav" , allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, -40.0) ;
142 double_scaled_test ("gsm610.raw" , allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_GSM610, -33.0) ;
143
144 double_scaled_test ("g721_32.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G721_32, -34.0) ;
145 double_scaled_test ("g723_24.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_24, -34.0) ;
146 double_scaled_test ("g723_40.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_40, -40.0) ;
147
148 /* 24 bit PCM PAF files tested here. */
149 double_scaled_test ("be_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -151.0) ;
150 double_scaled_test ("le_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -151.0) ;
151
152 double_scaled_test ("dwvw_12.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_12, -64.0) ;
153 double_scaled_test ("dwvw_16.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_16, -92.0) ;
154 double_scaled_test ("dwvw_24.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_24, -151.0) ;
155
156 double_scaled_test ("adpcm.vox" , allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, -40.0) ;
157
158 double_scaled_test ("dpcm_16.xi", allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_16, -90.0) ;
159 double_scaled_test ("dpcm_8.xi" , allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_8 , -42.0) ;
160
161 double_scaled_test ("pcm_s8.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_S8, -90.0) ;
162 double_scaled_test ("pcm_16.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_16, -140.0) ;
163 double_scaled_test ("pcm_24.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_24, -180.0) ;
164
165 #if HAVE_EXTERNAL_LIBS
166 double_scaled_test ("flac_8.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, -39.0) ;
167 double_scaled_test ("flac_16.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_16, -87.0) ;
168 double_scaled_test ("flac_24.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_24, -138.0) ;
169
170 double_scaled_test ("vorbis.oga", allow_exit, SF_FALSE, SF_FORMAT_OGG | SF_FORMAT_VORBIS, -29.0) ;
171 #endif
172
173 double_scaled_test ("replace_double.raw", allow_exit, SF_TRUE, SF_FORMAT_RAW | SF_FORMAT_DOUBLE, -300.0) ;
174
175 putchar ('\n') ;
176 /* Float int tests. */
177 float_short_little_test ("float_short_little.au") ;
178 float_short_big_test ("float_short_big.au") ;
179 float_int_little_test ("float_int_little.au") ;
180 float_int_big_test ("float_int_big.au") ;
181 double_short_little_test ("double_short_little.au") ;
182 double_short_big_test ("double_short_big.au") ;
183 double_int_little_test ("double_int_little.au") ;
184 double_int_big_test ("double_int_big.au") ;
185
186
187 return 0 ;
188 } /* main */
189
190 /*============================================================================================
191 * Here are the test functions.
192 */
193
194 static void
195 float_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr)
196 { SNDFILE *file ;
197 SF_INFO sfinfo ;
198 double snr ;
199
200 print_test_name ("float_scaled_test", filename) ;
201
202 gen_windowed_sine_float (float_data, DFT_DATA_LENGTH, 1.0) ;
203
204 sfinfo.samplerate = SAMPLE_RATE ;
205 sfinfo.frames = DFT_DATA_LENGTH ;
206 sfinfo.channels = 1 ;
207 sfinfo.format = filetype ;
208
209 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
210 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
211
212 test_write_float_or_die (file, 0, float_data, DFT_DATA_LENGTH, __LINE__) ;
213
214 sf_close (file) ;
215
216 memset (float_test, 0, sizeof (float_test)) ;
217
218 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
219 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
220
221 exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
222 exit_if_true (sfinfo.frames < DFT_DATA_LENGTH, "\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
223 exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
224
225 check_log_buffer_or_die (file, __LINE__) ;
226
227 test_read_float_or_die (file, 0, float_test, DFT_DATA_LENGTH, __LINE__) ;
228
229 sf_close (file) ;
230
231 snr = dft_cmp_float (__LINE__, float_data, float_test, DFT_DATA_LENGTH, target_snr, allow_exit) ;
232
233 exit_if_true (snr > target_snr, "% 6.1fdB SNR\n\n Error : should be better than % 6.1fdB\n\n", snr, target_snr) ;
234
235 printf ("% 6.1fdB SNR ... ok\n", snr) ;
236
237 unlink (filename) ;
238
239 return ;
240 } /* float_scaled_test */
241
242 static void
243 double_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr)
244 { SNDFILE *file ;
245 SF_INFO sfinfo ;
246 double snr ;
247
248 print_test_name ("double_scaled_test", filename) ;
249
250 gen_windowed_sine_double (double_data, DFT_DATA_LENGTH, 0.95) ;
251
252 sfinfo.samplerate = SAMPLE_RATE ;
253 sfinfo.frames = DFT_DATA_LENGTH ;
254 sfinfo.channels = 1 ;
255 sfinfo.format = filetype ;
256
257 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
258 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
259
260 test_write_double_or_die (file, 0, double_data, DFT_DATA_LENGTH, __LINE__) ;
261
262 sf_close (file) ;
263
264 memset (double_test, 0, sizeof (double_test)) ;
265
266 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
267 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
268
269 exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
270 exit_if_true (sfinfo.frames < DFT_DATA_LENGTH, "\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
271 exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
272
273 check_log_buffer_or_die (file, __LINE__) ;
274
275 test_read_double_or_die (file, 0, double_test, DFT_DATA_LENGTH, __LINE__) ;
276
277 sf_close (file) ;
278
279 snr = dft_cmp_double (__LINE__, double_data, double_test, DFT_DATA_LENGTH, target_snr, allow_exit) ;
280
281 exit_if_true (snr > target_snr, "% 6.1fdB SNR\n\n Error : should be better than % 6.1fdB\n\n", snr, target_snr) ;
282
283 printf ("% 6.1fdB SNR ... ok\n", snr) ;
284
285 unlink (filename) ;
286
287 return ;
288 } /* double_scaled_test */
289
290 /*==============================================================================
291 */
292
293
294 static void
295 float_short_little_test (const char * filename)
296 { SNDFILE *file ;
297 SF_INFO sfinfo ;
298 unsigned k, max ;
299
300 print_test_name ("float_short_little_test", filename) ;
301
302 gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.98) ;
303
304 sfinfo.samplerate = SAMPLE_RATE ;
305 sfinfo.frames = ARRAY_LEN (short_data) ;
306 sfinfo.channels = 1 ;
307 sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT ;
308
309 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
310 test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
311 sf_close (file) ;
312
313 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
314
315 if (sfinfo.frames != ARRAY_LEN (float_data))
316 { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
317 exit (1) ;
318 } ;
319
320 if (sfinfo.channels != 1)
321 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
322 exit (1) ;
323 } ;
324
325 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
326
327 test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ;
328 sf_close (file) ;
329
330 max = 0 ;
331 for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
332 if ((unsigned) abs (short_data [k]) > max)
333 max = abs (short_data [k]) ;
334
335 if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01)
336 { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ;
337 exit (1) ;
338 } ;
339
340 unlink (filename) ;
341 puts ("ok") ;
342 } /* float_short_little_test */
343
344 static void
345 float_short_big_test (const char * filename)
346 { SNDFILE *file ;
347 SF_INFO sfinfo ;
348 unsigned k, max ;
349
350 print_test_name ("float_short_big_test", filename) ;
351
352 gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.98) ;
353
354 sfinfo.samplerate = SAMPLE_RATE ;
355 sfinfo.frames = ARRAY_LEN (short_data) ;
356 sfinfo.channels = 1 ;
357 sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT ;
358
359 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
360 test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
361 sf_close (file) ;
362
363 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
364
365 if (sfinfo.frames != ARRAY_LEN (float_data))
366 { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
367 exit (1) ;
368 } ;
369
370 if (sfinfo.channels != 1)
371 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
372 exit (1) ;
373 } ;
374
375 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
376
377 test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ;
378 sf_close (file) ;
379
380 max = 0 ;
381 for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
382 if ((unsigned) abs (short_data [k]) > max)
383 max = abs (short_data [k]) ;
384
385 if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01)
386 { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ;
387 exit (1) ;
388 } ;
389
390 unlink (filename) ;
391 puts ("ok") ;
392 } /* float_short_big_test */
393
394 static void
395 float_int_little_test (const char * filename)
396 { SNDFILE *file ;
397 SF_INFO sfinfo ;
398 unsigned k, max ;
399
400 print_test_name ("float_int_little_test", filename) ;
401
402 gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.98) ;
403
404 sfinfo.samplerate = SAMPLE_RATE ;
405 sfinfo.frames = ARRAY_LEN (int_data) ;
406 sfinfo.channels = 1 ;
407 sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT ;
408
409 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
410 test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
411 sf_close (file) ;
412
413 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
414
415 if (sfinfo.frames != ARRAY_LEN (float_data))
416 { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
417 exit (1) ;
418 } ;
419
420 if (sfinfo.channels != 1)
421 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
422 exit (1) ;
423 } ;
424
425 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
426
427 test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ;
428 sf_close (file) ;
429
430 max = 0 ;
431 for (k = 0 ; k < ARRAY_LEN (int_data) ; k++)
432 if ((unsigned) abs (int_data [k]) > max)
433 max = abs (int_data [k]) ;
434
435 if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01)
436 { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ;
437 exit (1) ;
438 } ;
439
440 unlink (filename) ;
441 puts ("ok") ;
442 } /* float_int_little_test */
443
444 static void
445 float_int_big_test (const char * filename)
446 { SNDFILE *file ;
447 SF_INFO sfinfo ;
448 unsigned k, max ;
449
450 print_test_name ("float_int_big_test", filename) ;
451
452 gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.98) ;
453
454 sfinfo.samplerate = SAMPLE_RATE ;
455 sfinfo.frames = ARRAY_LEN (int_data) ;
456 sfinfo.channels = 1 ;
457 sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT ;
458
459 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
460 test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
461 sf_close (file) ;
462
463 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
464
465 if (sfinfo.frames != ARRAY_LEN (float_data))
466 { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
467 exit (1) ;
468 } ;
469
470 if (sfinfo.channels != 1)
471 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
472 exit (1) ;
473 } ;
474
475 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
476
477 test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ;
478 sf_close (file) ;
479
480 max = 0 ;
481 for (k = 0 ; k < ARRAY_LEN (int_data) ; k++)
482 if ((unsigned) abs (int_data [k]) > max)
483 max = abs (int_data [k]) ;
484
485 if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01)
486 { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ;
487 exit (1) ;
488 } ;
489
490 unlink (filename) ;
491 puts ("ok") ;
492 } /* float_int_big_test */
493
494 static void
495 double_short_little_test (const char * filename)
496 { SNDFILE *file ;
497 SF_INFO sfinfo ;
498 unsigned k, max ;
499
500 print_test_name ("double_short_little_test", filename) ;
501
502 gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.98) ;
503
504 sfinfo.samplerate = SAMPLE_RATE ;
505 sfinfo.frames = ARRAY_LEN (short_data) ;
506 sfinfo.channels = 1 ;
507 sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE ;
508
509 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
510 test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ;
511 sf_close (file) ;
512
513 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
514
515 if (sfinfo.frames != ARRAY_LEN (double_data))
516 { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
517 exit (1) ;
518 } ;
519
520 if (sfinfo.channels != 1)
521 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
522 exit (1) ;
523 } ;
524
525 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
526
527 test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ;
528 sf_close (file) ;
529
530 max = 0 ;
531 for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
532 if ((unsigned) abs (short_data [k]) > max)
533 max = abs (short_data [k]) ;
534
535 if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01)
536 { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ;
537 exit (1) ;
538 } ;
539
540 unlink (filename) ;
541 puts ("ok") ;
542 } /* double_short_little_test */
543
544 static void
545 double_short_big_test (const char * filename)
546 { SNDFILE *file ;
547 SF_INFO sfinfo ;
548 unsigned k, max ;
549
550 print_test_name ("double_short_big_test", filename) ;
551
552 gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.98) ;
553
554 sfinfo.samplerate = SAMPLE_RATE ;
555 sfinfo.frames = ARRAY_LEN (short_data) ;
556 sfinfo.channels = 1 ;
557 sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE ;
558
559 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
560 test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ;
561 sf_close (file) ;
562
563 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
564
565 if (sfinfo.frames != ARRAY_LEN (double_data))
566 { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
567 exit (1) ;
568 } ;
569
570 if (sfinfo.channels != 1)
571 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
572 exit (1) ;
573 } ;
574
575 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
576
577 test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ;
578 sf_close (file) ;
579
580 max = 0 ;
581 for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
582 if ((unsigned) abs (short_data [k]) > max)
583 max = abs (short_data [k]) ;
584
585 if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01)
586 { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ;
587 exit (1) ;
588 } ;
589
590 unlink (filename) ;
591 puts ("ok") ;
592 } /* double_short_big_test */
593
594 static void
595 double_int_little_test (const char * filename)
596 { SNDFILE *file ;
597 SF_INFO sfinfo ;
598 unsigned k, max ;
599
600 print_test_name ("double_int_little_test", filename) ;
601
602 gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.98) ;
603
604 sfinfo.samplerate = SAMPLE_RATE ;
605 sfinfo.frames = ARRAY_LEN (int_data) ;
606 sfinfo.channels = 1 ;
607 sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE ;
608
609 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
610 test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ;
611 sf_close (file) ;
612
613 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
614
615 if (sfinfo.frames != ARRAY_LEN (double_data))
616 { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
617 exit (1) ;
618 } ;
619
620 if (sfinfo.channels != 1)
621 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
622 exit (1) ;
623 } ;
624
625 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
626
627 test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ;
628 sf_close (file) ;
629
630 max = 0 ;
631 for (k = 0 ; k < ARRAY_LEN (int_data) ; k++)
632 if ((unsigned) abs (int_data [k]) > max)
633 max = abs (int_data [k]) ;
634
635 if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01)
636 { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ;
637 exit (1) ;
638 } ;
639
640 unlink (filename) ;
641 puts ("ok") ;
642 } /* double_int_little_test */
643
644 static void
645 double_int_big_test (const char * filename)
646 { SNDFILE *file ;
647 SF_INFO sfinfo ;
648 unsigned k, max ;
649
650 print_test_name ("double_int_big_test", filename) ;
651
652 gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.98) ;
653
654 sfinfo.samplerate = SAMPLE_RATE ;
655 sfinfo.frames = ARRAY_LEN (int_data) ;
656 sfinfo.channels = 1 ;
657 sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE ;
658
659 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
660 test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ;
661 sf_close (file) ;
662
663 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
664
665 if (sfinfo.frames != ARRAY_LEN (double_data))
666 { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
667 exit (1) ;
668 } ;
669
670 if (sfinfo.channels != 1)
671 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
672 exit (1) ;
673 } ;
674
675 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
676
677 test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ;
678 sf_close (file) ;
679
680 max = 0 ;
681 for (k = 0 ; k < ARRAY_LEN (int_data) ; k++)
682 if ((unsigned) abs (int_data [k]) > max)
683 max = abs (int_data [k]) ;
684
685 if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01)
686 { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ;
687 exit (1) ;
688 } ;
689
690 unlink (filename) ;
691 puts ("ok") ;
692 } /* double_int_big_test */
693
694