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