1b815c7f3Sopenharmony_ci/*
2b815c7f3Sopenharmony_ci** Copyright (C) 2015-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
3b815c7f3Sopenharmony_ci**
4b815c7f3Sopenharmony_ci** This program is free software; you can redistribute it and/or modify
5b815c7f3Sopenharmony_ci** it under the terms of the GNU General Public License as published by
6b815c7f3Sopenharmony_ci** the Free Software Foundation; either version 2 of the License, or
7b815c7f3Sopenharmony_ci** (at your option) any later version.
8b815c7f3Sopenharmony_ci**
9b815c7f3Sopenharmony_ci** This program is distributed in the hope that it will be useful,
10b815c7f3Sopenharmony_ci** but WITHOUT ANY WARRANTY; without even the implied warranty of
11b815c7f3Sopenharmony_ci** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12b815c7f3Sopenharmony_ci** GNU General Public License for more details.
13b815c7f3Sopenharmony_ci**
14b815c7f3Sopenharmony_ci** You should have received a copy of the GNU General Public License
15b815c7f3Sopenharmony_ci** along with this program; if not, write to the Free Software
16b815c7f3Sopenharmony_ci** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17b815c7f3Sopenharmony_ci*/
18b815c7f3Sopenharmony_ci
19b815c7f3Sopenharmony_ci#include "sfconfig.h"
20b815c7f3Sopenharmony_ci
21b815c7f3Sopenharmony_ci#include <stdio.h>
22b815c7f3Sopenharmony_ci#include <stdlib.h>
23b815c7f3Sopenharmony_ci#include <string.h>
24b815c7f3Sopenharmony_ci#include <math.h>
25b815c7f3Sopenharmony_ci#include <inttypes.h>
26b815c7f3Sopenharmony_ci
27b815c7f3Sopenharmony_ci#if HAVE_UNISTD_H
28b815c7f3Sopenharmony_ci#include <unistd.h>
29b815c7f3Sopenharmony_ci#else
30b815c7f3Sopenharmony_ci#include "sf_unistd.h"
31b815c7f3Sopenharmony_ci#endif
32b815c7f3Sopenharmony_ci
33b815c7f3Sopenharmony_ci#include <sndfile.h>
34b815c7f3Sopenharmony_ci
35b815c7f3Sopenharmony_ci#include "dft_cmp.h"
36b815c7f3Sopenharmony_ci#include "utils.h"
37b815c7f3Sopenharmony_ci
38b815c7f3Sopenharmony_ci#define	BUFFER_LENGTH		10000
39b815c7f3Sopenharmony_ci#define	SAMPLE_RATE			44010
40b815c7f3Sopenharmony_ci
41b815c7f3Sopenharmony_cistatic void	short_lrw_test	(const char *filename, int filetype, const short * output, int out_len) ;
42b815c7f3Sopenharmony_cistatic void	int_lrw_test	(const char *filename, int filetype, const int * output, int out_len) ;
43b815c7f3Sopenharmony_cistatic void	float_lrw_test	(const char *filename, int filetype, const float * output, int out_len) ;
44b815c7f3Sopenharmony_cistatic void	double_lrw_test	(const char *filename, int filetype, const double * output, int out_len) ;
45b815c7f3Sopenharmony_ci
46b815c7f3Sopenharmony_ci
47b815c7f3Sopenharmony_cistatic short	short_data [BUFFER_LENGTH] ;
48b815c7f3Sopenharmony_cistatic int		int_data [BUFFER_LENGTH] ;
49b815c7f3Sopenharmony_cistatic float	float_data [BUFFER_LENGTH] ;
50b815c7f3Sopenharmony_cistatic double	double_data [BUFFER_LENGTH] ;
51b815c7f3Sopenharmony_ci
52b815c7f3Sopenharmony_ciint
53b815c7f3Sopenharmony_cimain (int argc, char *argv [])
54b815c7f3Sopenharmony_ci{	int do_all ;
55b815c7f3Sopenharmony_ci	size_t k ;
56b815c7f3Sopenharmony_ci
57b815c7f3Sopenharmony_ci	if (argc != 2)
58b815c7f3Sopenharmony_ci	{	printf ("Usage : %s <test>\n", argv [0]) ;
59b815c7f3Sopenharmony_ci		printf ("    Where <test> is one of the following:\n") ;
60b815c7f3Sopenharmony_ci		printf ("           alac        - test CAF/ALAC file functions\n") ;
61b815c7f3Sopenharmony_ci		printf ("           all         - perform all tests\n") ;
62b815c7f3Sopenharmony_ci		exit (1) ;
63b815c7f3Sopenharmony_ci		} ;
64b815c7f3Sopenharmony_ci
65b815c7f3Sopenharmony_ci	for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
66b815c7f3Sopenharmony_ci	{	int value = k / 32 ;
67b815c7f3Sopenharmony_ci		int_data [k] = (value & 1 ? -1 : 1) * value ;
68b815c7f3Sopenharmony_ci		short_data [k] = int_data [k] ;
69b815c7f3Sopenharmony_ci		float_data [k] = int_data [k] / 32000.0f ;
70b815c7f3Sopenharmony_ci		double_data [k] = int_data [k] / 32000.0 ;
71b815c7f3Sopenharmony_ci		}
72b815c7f3Sopenharmony_ci
73b815c7f3Sopenharmony_ci	do_all = ! strcmp (argv [1], "all") ;
74b815c7f3Sopenharmony_ci
75b815c7f3Sopenharmony_ci	if (do_all || strcmp (argv [1], "alac") == 0)
76b815c7f3Sopenharmony_ci	{	short_lrw_test	("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_16, short_data, ARRAY_LEN (short_data)) ;
77b815c7f3Sopenharmony_ci		int_lrw_test	("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, int_data, ARRAY_LEN (int_data)) ;
78b815c7f3Sopenharmony_ci		float_lrw_test	("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, float_data, ARRAY_LEN (float_data)) ;
79b815c7f3Sopenharmony_ci		double_lrw_test	("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, double_data, ARRAY_LEN (double_data)) ;
80b815c7f3Sopenharmony_ci		} ;
81b815c7f3Sopenharmony_ci
82b815c7f3Sopenharmony_ci	return 0 ;
83b815c7f3Sopenharmony_ci} /* main */
84b815c7f3Sopenharmony_ci
85b815c7f3Sopenharmony_ci/*============================================================================================
86b815c7f3Sopenharmony_ci *	Here are the test functions.
87b815c7f3Sopenharmony_ci */
88b815c7f3Sopenharmony_ci
89b815c7f3Sopenharmony_cistatic void
90b815c7f3Sopenharmony_cishort_lrw_test (const char *filename, int filetype, const short * output, int out_len)
91b815c7f3Sopenharmony_ci{	SNDFILE		*file ;
92b815c7f3Sopenharmony_ci	SF_INFO		sfinfo ;
93b815c7f3Sopenharmony_ci	int			k ;
94b815c7f3Sopenharmony_ci	short		input [BUFFER_LENGTH] ;
95b815c7f3Sopenharmony_ci
96b815c7f3Sopenharmony_ci	print_test_name ("short_lrw_test", filename) ;
97b815c7f3Sopenharmony_ci
98b815c7f3Sopenharmony_ci	exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
99b815c7f3Sopenharmony_ci
100b815c7f3Sopenharmony_ci	sfinfo.samplerate	= SAMPLE_RATE ;
101b815c7f3Sopenharmony_ci	sfinfo.frames		= out_len ;
102b815c7f3Sopenharmony_ci	sfinfo.channels		= 1 ;
103b815c7f3Sopenharmony_ci	sfinfo.format		= filetype ;
104b815c7f3Sopenharmony_ci
105b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
106b815c7f3Sopenharmony_ci
107b815c7f3Sopenharmony_ci	test_write_short_or_die (file, 0, output, out_len, __LINE__) ;
108b815c7f3Sopenharmony_ci
109b815c7f3Sopenharmony_ci	sf_close (file) ;
110b815c7f3Sopenharmony_ci
111b815c7f3Sopenharmony_ci	memset (input, 0, sizeof (input)) ;
112b815c7f3Sopenharmony_ci
113b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
114b815c7f3Sopenharmony_ci
115b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
116b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
117b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
118b815c7f3Sopenharmony_ci
119b815c7f3Sopenharmony_ci	check_log_buffer_or_die (file, __LINE__) ;
120b815c7f3Sopenharmony_ci
121b815c7f3Sopenharmony_ci	test_read_short_or_die (file, 0, input, out_len, __LINE__) ;
122b815c7f3Sopenharmony_ci
123b815c7f3Sopenharmony_ci	sf_close (file) ;
124b815c7f3Sopenharmony_ci
125b815c7f3Sopenharmony_ci	for (k = 0 ; k < out_len ; k++)
126b815c7f3Sopenharmony_ci		exit_if_true (input [k] != output [k],
127b815c7f3Sopenharmony_ci			"\n\nLine: %d: Error on input %d, expected %d, got %d\n", __LINE__, k, output [k], input [k]) ;
128b815c7f3Sopenharmony_ci
129b815c7f3Sopenharmony_ci	puts ("ok") ;
130b815c7f3Sopenharmony_ci	unlink (filename) ;
131b815c7f3Sopenharmony_ci
132b815c7f3Sopenharmony_ci	return ;
133b815c7f3Sopenharmony_ci} /* short_lrw_test */
134b815c7f3Sopenharmony_ci
135b815c7f3Sopenharmony_cistatic void
136b815c7f3Sopenharmony_ciint_lrw_test (const char *filename, int filetype, const int * output, int out_len)
137b815c7f3Sopenharmony_ci{	SNDFILE		*file ;
138b815c7f3Sopenharmony_ci	SF_INFO		sfinfo ;
139b815c7f3Sopenharmony_ci	int			k ;
140b815c7f3Sopenharmony_ci	int			input [BUFFER_LENGTH] ;
141b815c7f3Sopenharmony_ci
142b815c7f3Sopenharmony_ci	print_test_name ("int_lrw_test", filename) ;
143b815c7f3Sopenharmony_ci
144b815c7f3Sopenharmony_ci	exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
145b815c7f3Sopenharmony_ci
146b815c7f3Sopenharmony_ci	sfinfo.samplerate	= SAMPLE_RATE ;
147b815c7f3Sopenharmony_ci	sfinfo.frames		= out_len ;
148b815c7f3Sopenharmony_ci	sfinfo.channels		= 1 ;
149b815c7f3Sopenharmony_ci	sfinfo.format		= filetype ;
150b815c7f3Sopenharmony_ci
151b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
152b815c7f3Sopenharmony_ci
153b815c7f3Sopenharmony_ci	test_write_int_or_die (file, 0, output, out_len, __LINE__) ;
154b815c7f3Sopenharmony_ci
155b815c7f3Sopenharmony_ci	sf_close (file) ;
156b815c7f3Sopenharmony_ci
157b815c7f3Sopenharmony_ci	memset (input, 0, sizeof (input)) ;
158b815c7f3Sopenharmony_ci
159b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
160b815c7f3Sopenharmony_ci
161b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
162b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too int). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
163b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
164b815c7f3Sopenharmony_ci
165b815c7f3Sopenharmony_ci	check_log_buffer_or_die (file, __LINE__) ;
166b815c7f3Sopenharmony_ci
167b815c7f3Sopenharmony_ci	test_read_int_or_die (file, 0, input, out_len, __LINE__) ;
168b815c7f3Sopenharmony_ci
169b815c7f3Sopenharmony_ci	sf_close (file) ;
170b815c7f3Sopenharmony_ci
171b815c7f3Sopenharmony_ci	for (k = 0 ; k < out_len ; k++)
172b815c7f3Sopenharmony_ci		exit_if_true (input [k] != output [k],
173b815c7f3Sopenharmony_ci			"\n\nLine: %d: Error on input %d, expected %d, got %d\n", __LINE__, k, output [k], input [k]) ;
174b815c7f3Sopenharmony_ci
175b815c7f3Sopenharmony_ci	puts ("ok") ;
176b815c7f3Sopenharmony_ci	unlink (filename) ;
177b815c7f3Sopenharmony_ci
178b815c7f3Sopenharmony_ci	return ;
179b815c7f3Sopenharmony_ci} /* int_lrw_test */
180b815c7f3Sopenharmony_ci
181b815c7f3Sopenharmony_cistatic void
182b815c7f3Sopenharmony_cifloat_lrw_test (const char *filename, int filetype, const float * output, int out_len)
183b815c7f3Sopenharmony_ci{	SNDFILE		*file ;
184b815c7f3Sopenharmony_ci	SF_INFO		sfinfo ;
185b815c7f3Sopenharmony_ci	int			k ;
186b815c7f3Sopenharmony_ci	float		input [BUFFER_LENGTH] ;
187b815c7f3Sopenharmony_ci
188b815c7f3Sopenharmony_ci	print_test_name ("float_lrw_test", filename) ;
189b815c7f3Sopenharmony_ci
190b815c7f3Sopenharmony_ci	exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
191b815c7f3Sopenharmony_ci
192b815c7f3Sopenharmony_ci	sfinfo.samplerate	= SAMPLE_RATE ;
193b815c7f3Sopenharmony_ci	sfinfo.frames		= out_len ;
194b815c7f3Sopenharmony_ci	sfinfo.channels		= 1 ;
195b815c7f3Sopenharmony_ci	sfinfo.format		= filetype ;
196b815c7f3Sopenharmony_ci
197b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
198b815c7f3Sopenharmony_ci
199b815c7f3Sopenharmony_ci	test_write_float_or_die (file, 0, output, out_len, __LINE__) ;
200b815c7f3Sopenharmony_ci
201b815c7f3Sopenharmony_ci	sf_close (file) ;
202b815c7f3Sopenharmony_ci
203b815c7f3Sopenharmony_ci	memset (input, 0, sizeof (input)) ;
204b815c7f3Sopenharmony_ci
205b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
206b815c7f3Sopenharmony_ci
207b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
208b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too float). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
209b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
210b815c7f3Sopenharmony_ci
211b815c7f3Sopenharmony_ci	check_log_buffer_or_die (file, __LINE__) ;
212b815c7f3Sopenharmony_ci
213b815c7f3Sopenharmony_ci	test_read_float_or_die (file, 0, input, out_len, __LINE__) ;
214b815c7f3Sopenharmony_ci
215b815c7f3Sopenharmony_ci	sf_close (file) ;
216b815c7f3Sopenharmony_ci
217b815c7f3Sopenharmony_ci	for (k = 0 ; k < out_len ; k++)
218b815c7f3Sopenharmony_ci		exit_if_true (fabs (input [k] - output [k]) > 0.00001,
219b815c7f3Sopenharmony_ci			"\n\nLine: %d: Error on input %d, expected %f, got %f\n", __LINE__, k, output [k], input [k]) ;
220b815c7f3Sopenharmony_ci
221b815c7f3Sopenharmony_ci	puts ("ok") ;
222b815c7f3Sopenharmony_ci	unlink (filename) ;
223b815c7f3Sopenharmony_ci
224b815c7f3Sopenharmony_ci	return ;
225b815c7f3Sopenharmony_ci} /* float_lrw_test */
226b815c7f3Sopenharmony_ci
227b815c7f3Sopenharmony_cistatic void
228b815c7f3Sopenharmony_cidouble_lrw_test (const char *filename, int filetype, const double * output, int out_len)
229b815c7f3Sopenharmony_ci{	SNDFILE		*file ;
230b815c7f3Sopenharmony_ci	SF_INFO		sfinfo ;
231b815c7f3Sopenharmony_ci	int			k ;
232b815c7f3Sopenharmony_ci	double		input [BUFFER_LENGTH] ;
233b815c7f3Sopenharmony_ci
234b815c7f3Sopenharmony_ci	print_test_name ("double_lrw_test", filename) ;
235b815c7f3Sopenharmony_ci
236b815c7f3Sopenharmony_ci	exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
237b815c7f3Sopenharmony_ci
238b815c7f3Sopenharmony_ci	sfinfo.samplerate	= SAMPLE_RATE ;
239b815c7f3Sopenharmony_ci	sfinfo.frames		= out_len ;
240b815c7f3Sopenharmony_ci	sfinfo.channels		= 1 ;
241b815c7f3Sopenharmony_ci	sfinfo.format		= filetype ;
242b815c7f3Sopenharmony_ci
243b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
244b815c7f3Sopenharmony_ci
245b815c7f3Sopenharmony_ci	test_write_double_or_die (file, 0, output, out_len, __LINE__) ;
246b815c7f3Sopenharmony_ci
247b815c7f3Sopenharmony_ci	sf_close (file) ;
248b815c7f3Sopenharmony_ci
249b815c7f3Sopenharmony_ci	memset (input, 0, sizeof (input)) ;
250b815c7f3Sopenharmony_ci
251b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
252b815c7f3Sopenharmony_ci
253b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
254b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too double). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
255b815c7f3Sopenharmony_ci	exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
256b815c7f3Sopenharmony_ci
257b815c7f3Sopenharmony_ci	check_log_buffer_or_die (file, __LINE__) ;
258b815c7f3Sopenharmony_ci
259b815c7f3Sopenharmony_ci	test_read_double_or_die (file, 0, input, out_len, __LINE__) ;
260b815c7f3Sopenharmony_ci
261b815c7f3Sopenharmony_ci	sf_close (file) ;
262b815c7f3Sopenharmony_ci
263b815c7f3Sopenharmony_ci	for (k = 0 ; k < out_len ; k++)
264b815c7f3Sopenharmony_ci		exit_if_true (fabs (input [k] - output [k]) > 0.00001,
265b815c7f3Sopenharmony_ci			"\n\nLine: %d: Error on input %d, expected %f, got %f\n", __LINE__, k, output [k], input [k]) ;
266b815c7f3Sopenharmony_ci
267b815c7f3Sopenharmony_ci	puts ("ok") ;
268b815c7f3Sopenharmony_ci	unlink (filename) ;
269b815c7f3Sopenharmony_ci
270b815c7f3Sopenharmony_ci	return ;
271b815c7f3Sopenharmony_ci} /* double_lrw_test */
272b815c7f3Sopenharmony_ci
273