1 /*
2 ** Copyright (C) 2001-2016 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 "utils.h"
36
37 #define BUFFER_LEN (1 << 15)
38 #define LOG_BUFFER_SIZE 1024
39
40
41 static void test_float_peak (const char *filename, int filetype) ;
42 static void read_write_peak_test (const char *filename, int filetype) ;
43
44 static void check_logged_peaks (char *buffer) ;
45
46 /* Force the start of this buffer to be double aligned. Sparc-solaris will
47 ** choke if its not.
48 */
49 static double data [BUFFER_LEN] ;
50 static char log_buffer [LOG_BUFFER_SIZE] ;
51
52 int
main(int argc,char * argv[])53 main (int argc, char *argv [])
54 { int do_all = 0 ;
55 int test_count = 0 ;
56
57 if (argc != 2)
58 { printf ("Usage : %s <test>\n", argv [0]) ;
59 printf (" Where <test> is one of the following:\n") ;
60 printf (" aiff - test AIFF file PEAK chunk\n") ;
61 printf (" caf - test CAF file PEAK chunk\n") ;
62 printf (" wav - test WAV file peak chunk\n") ;
63 printf (" all - perform all tests\n") ;
64 exit (1) ;
65 } ;
66
67 do_all = ! strcmp (argv [1], "all") ;
68
69 if (do_all || ! strcmp (argv [1], "wav"))
70 { test_float_peak ("peak_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
71 test_float_peak ("peak_float.wavex", SF_FORMAT_WAVEX | SF_FORMAT_FLOAT) ;
72 test_float_peak ("peak_float.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
73
74 read_write_peak_test ("rw_peak.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
75 read_write_peak_test ("rw_peak.wavex", SF_FORMAT_WAVEX | SF_FORMAT_FLOAT) ;
76 test_count++ ;
77 } ;
78
79 if (do_all || ! strcmp (argv [1], "aiff"))
80 { test_float_peak ("peak_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
81
82 read_write_peak_test ("rw_peak.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
83 test_count++ ;
84 } ;
85
86 if (do_all || ! strcmp (argv [1], "caf"))
87 { test_float_peak ("peak_float.caf", SF_FORMAT_CAF | SF_FORMAT_FLOAT) ;
88
89 read_write_peak_test ("rw_peak.caf", SF_FORMAT_CAF | SF_FORMAT_FLOAT) ;
90 test_count++ ;
91 } ;
92
93 if (do_all || ! strcmp (argv [1], "rf64"))
94 { test_float_peak ("peak_float.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ;
95
96 read_write_peak_test ("rw_peak.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ;
97 test_count++ ;
98 } ;
99
100 if (test_count == 0)
101 { printf ("Mono : ************************************\n") ;
102 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
103 printf ("Mono : ************************************\n") ;
104 return 1 ;
105 } ;
106
107 return 0 ;
108 } /* main */
109
110 /*============================================================================================
111 ** Here are the test functions.
112 */
113
114 static void
test_float_peak(const char * filename,int filetype)115 test_float_peak (const char *filename, int filetype)
116 { SNDFILE *file ;
117 SF_INFO sfinfo ;
118 int k, frames, count ;
119
120 print_test_name ("test_float_peak", filename) ;
121
122 memset (&sfinfo, 0, sizeof (sfinfo)) ;
123 sfinfo.samplerate = 44100 ;
124 sfinfo.format = filetype ;
125 sfinfo.channels = 4 ;
126 sfinfo.frames = 0 ;
127
128 frames = BUFFER_LEN / sfinfo.channels ;
129
130 /* Create some random data with a peak value of 0.66. */
131 for (k = 0 ; k < BUFFER_LEN ; k++)
132 data [k] = (rand () % 2000) / 3000.0 ;
133
134 /* Insert some larger peaks a know locations. */
135 data [4 * (frames / 8) + 0] = (frames / 8) * 0.01 ; /* First channel */
136 data [4 * (frames / 6) + 1] = (frames / 6) * 0.01 ; /* Second channel */
137 data [4 * (frames / 4) + 2] = (frames / 4) * 0.01 ; /* Third channel */
138 data [4 * (frames / 2) + 3] = (frames / 2) * 0.01 ; /* Fourth channel */
139
140 /* Write a file with PEAK chunks. */
141 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;
142
143 /* Try to confuse the header writer by adding a removing the PEAK chunk. */
144 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
145 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
146 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
147
148 /* Write the data in four passed. The data is designed so that peaks will
149 ** be written in the different calls to sf_write_double ().
150 */
151 for (count = 0 ; count < 4 ; count ++)
152 test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ;
153
154 sf_close (file) ;
155
156 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ;
157
158 if (sfinfo.format != filetype)
159 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
160 exit (1) ;
161 } ;
162
163 if (sfinfo.frames != frames)
164 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__, frames, (long) sfinfo.frames) ;
165 exit (1) ;
166 } ;
167
168 if (sfinfo.channels != 4)
169 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
170 exit (1) ;
171 } ;
172
173 /* Check these two commands. */
174 if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double)) == SF_FALSE)
175 { printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ;
176 exit (1) ;
177 } ;
178
179 if (fabs (data [0] - (frames / 2) * 0.01) > 0.01)
180 { printf ("\n\nLine %d: Bad peak value (%f should be %f) for command SFC_GET_SIGNAL_MAX.\n", __LINE__, data [0], (frames / 2) * 0.01) ;
181 exit (1) ;
182 } ;
183
184 if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels) == SF_FALSE)
185 { printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ;
186 exit (1) ;
187 } ;
188
189 if (fabs (data [3] - (frames / 2) * 0.01) > 0.01)
190 { printf ("\n\nLine %d: Bad peak value (%f should be %f) for command SFC_GET_MAX_ALL_CHANNELS.\n", __LINE__, data [0], (frames / 2) * 0.01) ;
191 exit (1) ;
192 } ;
193
194 /* Get the log buffer data. */
195 log_buffer [0] = 0 ;
196 sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ;
197
198 if (strlen (log_buffer) == 0)
199 { printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ;
200 exit (1) ;
201 } ;
202
203 check_logged_peaks (log_buffer) ;
204
205 sf_close (file) ;
206
207 /* Write a file ***without*** PEAK chunks. */
208 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;
209
210 /* Try to confuse the header writer by adding a removing the PEAK chunk. */
211 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
212 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
213 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
214
215 /* Write the data in four passed. The data is designed so that peaks will
216 ** be written in the different calls to sf_write_double ().
217 */
218 for (count = 0 ; count < 4 ; count ++)
219 test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ;
220
221 sf_close (file) ;
222
223 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ;
224
225 /* Check these two commands. */
226 if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double)))
227 { printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ;
228 exit (1) ;
229 } ;
230
231 if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels))
232 { printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ;
233 exit (1) ;
234 } ;
235
236 /* Get the log buffer data. */
237 log_buffer [0] = 0 ;
238 sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ;
239
240 if (strlen (log_buffer) == 0)
241 { printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ;
242 exit (1) ;
243 } ;
244
245 if (strstr (log_buffer, "PEAK :") != NULL)
246 { printf ("\n\nLine %d: Should not have a PEAK chunk in this file.\n\n", __LINE__) ;
247 puts (log_buffer) ;
248 exit (1) ;
249 } ;
250
251 sf_close (file) ;
252
253 unlink (filename) ;
254 printf ("ok\n") ;
255 } /* test_float_peak */
256
257 static void
check_logged_peaks(char * buffer)258 check_logged_peaks (char *buffer)
259 { char *cptr ;
260 int k, chan, channel_count, position ;
261 float value ;
262
263 if (strstr (buffer, "should") || strstr (buffer, "*"))
264 { printf ("\n\nLine %d: Something wrong in buffer. Dumping.\n", __LINE__) ;
265 puts (buffer) ;
266 exit (1) ;
267 } ;
268
269 channel_count = 0 ;
270 cptr = strstr (buffer, "Channels") ;
271 if (cptr && sscanf (cptr, "Channels : %d", &k) == 1)
272 channel_count = k ;
273 else if (cptr && sscanf (cptr, "Channels / frame : %d", &k) == 1)
274 channel_count = k ;
275 else
276 { printf ("\n\nLine %d: Couldn't find channel count.\n", __LINE__) ;
277 exit (1) ;
278 } ;
279
280 if (channel_count != 4)
281 { printf ("\n\nLine %d: Wrong channel count (4 ->%d).\n", __LINE__, channel_count) ;
282 exit (1) ;
283 } ;
284
285 if (! (cptr = strstr (buffer, "Ch Position Value")))
286 { printf ("\n\nLine %d: Can't find PEAK data.\n", __LINE__) ;
287 exit (1) ;
288 } ;
289
290 for (k = 0 ; k < channel_count ; k++)
291 { if (! (cptr = strchr (cptr, '\n')))
292 { printf ("\n\nLine %d: Got lost.\n", __LINE__) ;
293 exit (1) ;
294 } ;
295 if (sscanf (cptr, "%d %d %f", &chan, &position, &value) != 3)
296 { printf ("\n\nLine %d: sscanf failed.\n", __LINE__) ;
297 exit (1) ;
298 } ;
299 if (position == 0)
300 { printf ("\n\nLine %d: peak position for channel %d should not be at offset 0.\n", __LINE__, chan) ;
301 printf ("%s", buffer) ;
302 exit (1) ;
303 } ;
304 if (chan != k || fabs ((position) * 0.01 - value) > 1e-6)
305 { printf ("\n\nLine %d: Error : peak value incorrect!\n", __LINE__) ;
306 printf ("%s", buffer) ;
307 printf ("\n\nLine %d: %d %f %f\n", __LINE__, chan, position * 0.01, value) ;
308 exit (1) ;
309 } ;
310 cptr ++ ; /* Move past current newline. */
311 } ;
312
313 } /* check_logged_peaks */
314
315 static void
read_write_peak_test(const char * filename,int filetype)316 read_write_peak_test (const char *filename, int filetype)
317 { SNDFILE *file ;
318 SF_INFO sfinfo ;
319
320 double small_data [10], max_peak = 0.0 ;
321 unsigned k ;
322
323 print_test_name (__func__, filename) ;
324
325 for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++)
326 small_data [k] = 0.1 ;
327
328 sfinfo.samplerate = 44100 ;
329 sfinfo.channels = 2 ;
330 sfinfo.format = filetype ;
331 sfinfo.frames = 0 ;
332
333 /* Open the file, add peak chunk and write samples with value 0.1. */
334 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
335
336 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
337
338 test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ;
339
340 sf_close (file) ;
341
342 /* Open the fiel RDWR, write sample valied 1.25. */
343 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
344
345 for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++)
346 small_data [k] = 1.0 ;
347
348 test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ;
349
350 sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (max_peak)) ;
351
352 sf_close (file) ;
353
354 exit_if_true (max_peak < 0.1, "\n\nLine %d : max peak (%5.3f) should not be 0.1.\n\n", __LINE__, max_peak) ;
355
356 /* Open the file and test the values written to the PEAK chunk. */
357 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
358
359 exit_if_true (sfinfo.channels * sfinfo.frames != 2 * ARRAY_LEN (small_data),
360 "Line %d : frame count is %" PRId64 ", should be %zd\n", __LINE__, sfinfo.frames, 2 * ARRAY_LEN (small_data)) ;
361
362 sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (double)) ;
363
364 sf_close (file) ;
365
366 exit_if_true (max_peak < 1.0, "\n\nLine %d : max peak (%5.3f) should be 1.0.\n\n", __LINE__, max_peak) ;
367
368 unlink (filename) ;
369 puts ("ok") ;
370 } /* read_write_peak_test */
371
372