Revert "Examples Makefile."
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / utils.c
1 /*
2 ** Copyright (C) 2002-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 /*
20 ** Utility functions to make writing the test suite easier.
21 **
22 ** The .c and .h files were generated automagically with Autogen from
23 ** the files utils.def and utils.tpl.
24 */
25
26
27
28 #include "sfconfig.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <inttypes.h>
33
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #if (HAVE_DECL_S_IRGRP == 0)
39 #include <sf_unistd.h>
40 #endif
41
42 #include <errno.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <math.h>
46 #include <fcntl.h>
47 #include <sys/stat.h>
48
49 #include <sndfile.h>
50
51 #include "utils.h"
52
53 #ifndef M_PI
54 #define M_PI 3.14159265358979323846264338
55 #endif
56
57 #define LOG_BUFFER_SIZE 2048
58
59 /*
60 ** Neat solution to the Win32/OS2 binary file flage requirement.
61 ** If O_BINARY isn't already defined by the inclusion of the system
62 ** headers, set it to zero.
63 */
64 #ifndef O_BINARY
65 #define O_BINARY 0
66 #endif
67
68
69 void
70 gen_windowed_sine_float (float *data, int len, double maximum)
71 { int k ;
72
73 memset (data, 0, len * sizeof (float)) ;
74 /*
75 ** Choose a frequency of 1/32 so that it aligns perfectly with a DFT
76 ** bucket to minimise spreading of energy over more than one bucket.
77 ** Also do not want to make the frequency too high as some of the
78 ** codecs (ie gsm610) have a quite severe high frequency roll off.
79 */
80 len /= 2 ;
81
82 for (k = 0 ; k < len ; k++)
83 { data [k] = sin (2.0 * k * M_PI * 1.0 / 32.0 + 0.4) ;
84
85 /* Apply Hanning Window. */
86 data [k] *= maximum * (0.5 - 0.5 * cos (2.0 * M_PI * k / ((len) - 1))) ;
87 }
88
89 return ;
90 } /* gen_windowed_sine_float */
91
92 void
93 gen_windowed_sine_double (double *data, int len, double maximum)
94 { int k ;
95
96 memset (data, 0, len * sizeof (double)) ;
97 /*
98 ** Choose a frequency of 1/32 so that it aligns perfectly with a DFT
99 ** bucket to minimise spreading of energy over more than one bucket.
100 ** Also do not want to make the frequency too high as some of the
101 ** codecs (ie gsm610) have a quite severe high frequency roll off.
102 */
103 len /= 2 ;
104
105 for (k = 0 ; k < len ; k++)
106 { data [k] = sin (2.0 * k * M_PI * 1.0 / 32.0 + 0.4) ;
107
108 /* Apply Hanning Window. */
109 data [k] *= maximum * (0.5 - 0.5 * cos (2.0 * M_PI * k / ((len) - 1))) ;
110 }
111
112 return ;
113 } /* gen_windowed_sine_double */
114
115
116 void
117 create_short_sndfile (const char *filename, int format, int channels)
118 { short data [2 * 3 * 4 * 5 * 6 * 7] = { 0, } ;
119 SNDFILE *file ;
120 SF_INFO sfinfo ;
121
122 sfinfo.samplerate = 44100 ;
123 sfinfo.channels = channels ;
124 sfinfo.format = format ;
125
126 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
127 { printf ("Error (%s, %d) : sf_open failed : %s\n", __FILE__, __LINE__, sf_strerror (file)) ;
128 exit (1) ;
129 } ;
130
131 sf_write_short (file, data, ARRAY_LEN (data)) ;
132
133 sf_close (file) ;
134 } /* create_short_sndfile */
135
136 void
137 check_file_hash_or_die (const char *filename, uint64_t target_hash, int line_num)
138 { static unsigned char buf [4096] ;
139 uint64_t cksum ;
140 FILE *file ;
141 int k, read_count ;
142
143 memset (buf, 0, sizeof (buf)) ;
144
145 /* The 'b' in the mode string means binary for Win32. */
146 if ((file = fopen (filename, "rb")) == NULL)
147 { printf ("\n\nLine %d: could not open file '%s'\n\n", line_num, filename) ;
148 exit (1) ;
149 } ;
150
151 cksum = 0 ;
152
153 while ((read_count = fread (buf, 1, sizeof (buf), file)))
154 for (k = 0 ; k < read_count ; k++)
155 cksum = cksum * 511 + buf [k] ;
156
157 fclose (file) ;
158
159 if (target_hash == 0)
160 { printf (" 0x%016" PRIx64 "\n", cksum) ;
161 return ;
162 } ;
163
164 if (cksum != target_hash)
165 { printf ("\n\nLine %d: incorrect hash value 0x%016" PRIx64 " should be 0x%016" PRIx64 ".\n\n", line_num, cksum, target_hash) ;
166 exit (1) ;
167 } ;
168
169 return ;
170 } /* check_file_hash_or_die */
171
172 void
173 print_test_name (const char *test, const char *filename)
174 { int count ;
175
176 if (test == NULL)
177 { printf (__FILE__ ": bad test of filename parameter.\n") ;
178 exit (1) ;
179 } ;
180
181 if (filename == NULL || strlen (filename) == 0)
182 { printf (" %-30s : ", test) ;
183 count = 25 ;
184 }
185 else
186 { printf (" %-30s : %s ", test, filename) ;
187 count = 24 - strlen (filename) ;
188 } ;
189
190 while (count -- > 0)
191 putchar ('.') ;
192 putchar (' ') ;
193
194 fflush (stdout) ;
195 } /* print_test_name */
196
197 void
198 dump_data_to_file (const char *filename, const void *data, unsigned int datalen)
199 { FILE *file ;
200
201 if ((file = fopen (filename, "wb")) == NULL)
202 { printf ("\n\nLine %d : could not open file : %s\n\n", __LINE__, filename) ;
203 exit (1) ;
204 } ;
205
206 if (fwrite (data, 1, datalen, file) != datalen)
207 { printf ("\n\nLine %d : fwrite failed.\n\n", __LINE__) ;
208 exit (1) ;
209 } ;
210
211 fclose (file) ;
212
213 } /* dump_data_to_file */
214
215 /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
216 */
217
218 static char octfilename [] = "error.dat" ;
219
220 int
221 oct_save_short (const short *a, const short *b, int len)
222 { FILE *file ;
223 int k ;
224
225 if (! (file = fopen (octfilename, "w")))
226 return 1 ;
227
228 fprintf (file, "# Not created by Octave\n") ;
229
230 fprintf (file, "# name: a\n") ;
231 fprintf (file, "# type: matrix\n") ;
232 fprintf (file, "# rows: %d\n", len) ;
233 fprintf (file, "# columns: 1\n") ;
234
235 for (k = 0 ; k < len ; k++)
236 fprintf (file, "% d" "\n", a [k]) ;
237
238 fprintf (file, "# name: b\n") ;
239 fprintf (file, "# type: matrix\n") ;
240 fprintf (file, "# rows: %d\n", len) ;
241 fprintf (file, "# columns: 1\n") ;
242
243 for (k = 0 ; k < len ; k++)
244 fprintf (file, "% d" "\n", b [k]) ;
245
246 fclose (file) ;
247 return 0 ;
248 } /* oct_save_short */
249 int
250 oct_save_int (const int *a, const int *b, int len)
251 { FILE *file ;
252 int k ;
253
254 if (! (file = fopen (octfilename, "w")))
255 return 1 ;
256
257 fprintf (file, "# Not created by Octave\n") ;
258
259 fprintf (file, "# name: a\n") ;
260 fprintf (file, "# type: matrix\n") ;
261 fprintf (file, "# rows: %d\n", len) ;
262 fprintf (file, "# columns: 1\n") ;
263
264 for (k = 0 ; k < len ; k++)
265 fprintf (file, "% d" "\n", a [k]) ;
266
267 fprintf (file, "# name: b\n") ;
268 fprintf (file, "# type: matrix\n") ;
269 fprintf (file, "# rows: %d\n", len) ;
270 fprintf (file, "# columns: 1\n") ;
271
272 for (k = 0 ; k < len ; k++)
273 fprintf (file, "% d" "\n", b [k]) ;
274
275 fclose (file) ;
276 return 0 ;
277 } /* oct_save_int */
278 int
279 oct_save_float (const float *a, const float *b, int len)
280 { FILE *file ;
281 int k ;
282
283 if (! (file = fopen (octfilename, "w")))
284 return 1 ;
285
286 fprintf (file, "# Not created by Octave\n") ;
287
288 fprintf (file, "# name: a\n") ;
289 fprintf (file, "# type: matrix\n") ;
290 fprintf (file, "# rows: %d\n", len) ;
291 fprintf (file, "# columns: 1\n") ;
292
293 for (k = 0 ; k < len ; k++)
294 fprintf (file, "% g" "\n", a [k]) ;
295
296 fprintf (file, "# name: b\n") ;
297 fprintf (file, "# type: matrix\n") ;
298 fprintf (file, "# rows: %d\n", len) ;
299 fprintf (file, "# columns: 1\n") ;
300
301 for (k = 0 ; k < len ; k++)
302 fprintf (file, "% g" "\n", b [k]) ;
303
304 fclose (file) ;
305 return 0 ;
306 } /* oct_save_float */
307 int
308 oct_save_double (const double *a, const double *b, int len)
309 { FILE *file ;
310 int k ;
311
312 if (! (file = fopen (octfilename, "w")))
313 return 1 ;
314
315 fprintf (file, "# Not created by Octave\n") ;
316
317 fprintf (file, "# name: a\n") ;
318 fprintf (file, "# type: matrix\n") ;
319 fprintf (file, "# rows: %d\n", len) ;
320 fprintf (file, "# columns: 1\n") ;
321
322 for (k = 0 ; k < len ; k++)
323 fprintf (file, "% g" "\n", a [k]) ;
324
325 fprintf (file, "# name: b\n") ;
326 fprintf (file, "# type: matrix\n") ;
327 fprintf (file, "# rows: %d\n", len) ;
328 fprintf (file, "# columns: 1\n") ;
329
330 for (k = 0 ; k < len ; k++)
331 fprintf (file, "% g" "\n", b [k]) ;
332
333 fclose (file) ;
334 return 0 ;
335 } /* oct_save_double */
336
337
338 void
339 check_log_buffer_or_die (SNDFILE *file, int line_num)
340 { static char buffer [LOG_BUFFER_SIZE] ;
341 int count ;
342
343 memset (buffer, 0, sizeof (buffer)) ;
344
345 /* Get the log buffer data. */
346 count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
347
348 if (LOG_BUFFER_SIZE - count < 2)
349 { printf ("\n\nLine %d : Possible long log buffer.\n", line_num) ;
350 exit (1) ;
351 }
352
353 /* Look for "Should" */
354 if (strstr (buffer, "ould"))
355 { printf ("\n\nLine %d : Log buffer contains `ould'. Dumping.\n", line_num) ;
356 puts (buffer) ;
357 exit (1) ;
358 } ;
359
360 /* Look for "**" */
361 if (strstr (buffer, "*"))
362 { printf ("\n\nLine %d : Log buffer contains `*'. Dumping.\n", line_num) ;
363 puts (buffer) ;
364 exit (1) ;
365 } ;
366
367 /* Look for "Should" */
368 if (strstr (buffer, "nknown marker"))
369 { printf ("\n\nLine %d : Log buffer contains `nknown marker'. Dumping.\n", line_num) ;
370 puts (buffer) ;
371 exit (1) ;
372 } ;
373
374 return ;
375 } /* check_log_buffer_or_die */
376
377 int
378 string_in_log_buffer (SNDFILE *file, const char *s)
379 { static char buffer [LOG_BUFFER_SIZE] ;
380 int count ;
381
382 memset (buffer, 0, sizeof (buffer)) ;
383
384 /* Get the log buffer data. */
385 count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
386
387 if (LOG_BUFFER_SIZE - count < 2)
388 { printf ("Possible long log buffer.\n") ;
389 exit (1) ;
390 }
391
392 /* Look for string */
393 return strstr (buffer, s) ? SF_TRUE : SF_FALSE ;
394 } /* string_in_log_buffer */
395
396 void
397 hexdump_file (const char * filename, sf_count_t offset, sf_count_t length)
398 {
399 FILE * file ;
400 char buffer [16] ;
401 int k, m, ch, readcount ;
402
403 if (length > 1000000)
404 { printf ("\n\nError : length (%ld) too long.\n\n", SF_COUNT_TO_LONG (offset)) ;
405 exit (1) ;
406 } ;
407
408 if ((file = fopen (filename, "r")) == NULL)
409 { printf ("\n\nError : hexdump_file (%s) could not open file for read.\n\n", filename) ;
410 exit (1) ;
411 } ;
412
413 if (fseek (file, offset, SEEK_SET) != 0)
414 { printf ("\n\nError : fseek(file, %ld, SEEK_SET) failed : %s\n\n", SF_COUNT_TO_LONG (offset), strerror (errno)) ;
415 exit (1) ;
416 } ;
417
418 puts ("\n\n") ;
419
420 for (k = 0 ; k < length ; k+= sizeof (buffer))
421 { readcount = fread (buffer, 1, sizeof (buffer), file) ;
422
423 printf ("%08lx : ", SF_COUNT_TO_LONG (offset + k)) ;
424
425 for (m = 0 ; m < readcount ; m++)
426 printf ("%02x ", buffer [m] & 0xFF) ;
427
428 for (m = readcount ; m < SIGNED_SIZEOF (buffer) ; m++)
429 printf (" ") ;
430
431 printf (" ") ;
432 for (m = 0 ; m < readcount ; m++)
433 { ch = isprint (buffer [m]) ? buffer [m] : '.' ;
434 putchar (ch) ;
435 } ;
436
437 if (readcount < SIGNED_SIZEOF (buffer))
438 break ;
439
440 putchar ('\n') ;
441 } ;
442
443 puts ("\n") ;
444
445 fclose (file) ;
446 } /* hexdump_file */
447
448 void
449 dump_log_buffer (SNDFILE *file)
450 { static char buffer [LOG_BUFFER_SIZE] ;
451
452 memset (buffer, 0, sizeof (buffer)) ;
453
454 /* Get the log buffer data. */
455 sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
456
457 if (strlen (buffer) < 1)
458 puts ("Log buffer empty.\n") ;
459 else
460 puts (buffer) ;
461
462 return ;
463 } /* dump_log_buffer */
464
465 SNDFILE *
466 test_open_file_or_die (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num)
467 { static int count = 0 ;
468
469 SNDFILE *file ;
470 const char *modestr, *func_name ;
471 int oflags = 0, omode = 0, err ;
472
473 /*
474 ** Need to test both sf_open() and sf_open_fd().
475 ** Do so alternately.
476 */
477 switch (mode)
478 { case SFM_READ :
479 modestr = "SFM_READ" ;
480 oflags = O_RDONLY | O_BINARY ;
481 omode = 0 ;
482 break ;
483
484 case SFM_WRITE :
485 modestr = "SFM_WRITE" ;
486 oflags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ;
487 omode = S_IRUSR | S_IWUSR | S_IRGRP ;
488 break ;
489
490 case SFM_RDWR :
491 modestr = "SFM_RDWR" ;
492 oflags = O_RDWR | O_CREAT | O_BINARY ;
493 omode = S_IRUSR | S_IWUSR | S_IRGRP ;
494 break ;
495 default :
496 printf ("\n\nLine %d: Bad mode.\n", line_num) ;
497 fflush (stdout) ;
498 exit (1) ;
499 } ;
500
501 if (OS_IS_WIN32)
502 { /* Windows does not understand and ignores the S_IRGRP flag, but Wine
503 ** gives a run time warning message, so just clear it.
504 */
505 omode &= ~S_IRGRP ;
506 } ;
507
508 if (allow_fd && ((++count) & 1) == 1)
509 { int fd ;
510
511 /* Only use the three argument open() function if omode != 0. */
512 fd = (omode == 0) ? open (filename, oflags) : open (filename, oflags, omode) ;
513
514 if (fd < 0)
515 { printf ("\n\n%s : open failed : %s\n", __func__, strerror (errno)) ;
516 exit (1) ;
517 } ;
518
519 func_name = "sf_open_fd" ;
520 file = sf_open_fd (fd, mode, sfinfo, SF_TRUE) ;
521 }
522 else
523 { func_name = "sf_open" ;
524 file = sf_open (filename, mode, sfinfo) ;
525 } ;
526
527 if (file == NULL)
528 { printf ("\n\nLine %d: %s (%s) failed : %s\n\n", line_num, func_name, modestr, sf_strerror (NULL)) ;
529 dump_log_buffer (file) ;
530 exit (1) ;
531 } ;
532
533 err = sf_error (file) ;
534 if (err != SF_ERR_NO_ERROR)
535 { printf ("\n\nLine %d : sf_error : %s\n\n", line_num, sf_error_number (err)) ;
536 dump_log_buffer (file) ;
537 exit (1) ;
538 } ;
539
540 return file ;
541 } /* test_open_file_or_die */
542
543 void
544 test_read_write_position_or_die (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos)
545 { sf_count_t pos ;
546
547 /* Check the current read position. */
548 if (read_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_READ)) != read_pos)
549 { printf ("\n\nLine %d ", line_num) ;
550 if (pass > 0)
551 printf ("(pass %d): ", pass) ;
552 printf ("Read position (%ld) should be %ld.\n", SF_COUNT_TO_LONG (pos), SF_COUNT_TO_LONG (read_pos)) ;
553 exit (1) ;
554 } ;
555
556 /* Check the current write position. */
557 if (write_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_WRITE)) != write_pos)
558 { printf ("\n\nLine %d", line_num) ;
559 if (pass > 0)
560 printf (" (pass %d)", pass) ;
561 printf (" : Write position (%ld) should be %ld.\n",
562 SF_COUNT_TO_LONG (pos), SF_COUNT_TO_LONG (write_pos)) ;
563 exit (1) ;
564 } ;
565
566 return ;
567 } /* test_read_write_position */
568
569 void
570 test_seek_or_die (SNDFILE *file, sf_count_t offset, int whence, sf_count_t new_pos, int channels, int line_num)
571 { sf_count_t position ;
572 const char *channel_name, *whence_name ;
573
574 switch (whence)
575 { case SEEK_SET :
576 whence_name = "SEEK_SET" ;
577 break ;
578 case SEEK_CUR :
579 whence_name = "SEEK_CUR" ;
580 break ;
581 case SEEK_END :
582 whence_name = "SEEK_END" ;
583 break ;
584
585 /* SFM_READ */
586 case SEEK_SET | SFM_READ :
587 whence_name = "SFM_READ | SEEK_SET" ;
588 break ;
589 case SEEK_CUR | SFM_READ :
590 whence_name = "SFM_READ | SEEK_CUR" ;
591 break ;
592 case SEEK_END | SFM_READ :
593 whence_name = "SFM_READ | SEEK_END" ;
594 break ;
595
596 /* SFM_WRITE */
597 case SEEK_SET | SFM_WRITE :
598 whence_name = "SFM_WRITE | SEEK_SET" ;
599 break ;
600 case SEEK_CUR | SFM_WRITE :
601 whence_name = "SFM_WRITE | SEEK_CUR" ;
602 break ;
603 case SEEK_END | SFM_WRITE :
604 whence_name = "SFM_WRITE | SEEK_END" ;
605 break ;
606
607 default :
608 printf ("\n\nLine %d: bad whence parameter.\n", line_num) ;
609 exit (1) ;
610 } ;
611
612 channel_name = (channels == 1) ? "Mono" : "Stereo" ;
613
614 if ((position = sf_seek (file, offset, whence)) != new_pos)
615 { printf ("\n\nLine %d : %s : sf_seek (file, %ld, %s) returned %ld (should be %ld).\n\n",
616 line_num, channel_name, SF_COUNT_TO_LONG (offset), whence_name,
617 SF_COUNT_TO_LONG (position), SF_COUNT_TO_LONG (new_pos)) ;
618 exit (1) ;
619 } ;
620
621 } /* test_seek_or_die */
622
623
624
625 void
626 test_read_short_or_die (SNDFILE *file, int pass, short *test, sf_count_t items, int line_num)
627 { sf_count_t count ;
628
629 if ((count = sf_read_short (file, test, items)) != items)
630 { printf ("\n\nLine %d", line_num) ;
631 if (pass > 0)
632 printf (" (pass %d)", pass) ;
633 printf (" : sf_read_short failed with short read (%ld => %ld).\n",
634 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
635 fflush (stdout) ;
636 puts (sf_strerror (file)) ;
637 exit (1) ;
638 } ;
639
640 return ;
641 } /* test_read_short_or_die */
642
643 void
644 test_read_int_or_die (SNDFILE *file, int pass, int *test, sf_count_t items, int line_num)
645 { sf_count_t count ;
646
647 if ((count = sf_read_int (file, test, items)) != items)
648 { printf ("\n\nLine %d", line_num) ;
649 if (pass > 0)
650 printf (" (pass %d)", pass) ;
651 printf (" : sf_read_int failed with short read (%ld => %ld).\n",
652 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
653 fflush (stdout) ;
654 puts (sf_strerror (file)) ;
655 exit (1) ;
656 } ;
657
658 return ;
659 } /* test_read_int_or_die */
660
661 void
662 test_read_float_or_die (SNDFILE *file, int pass, float *test, sf_count_t items, int line_num)
663 { sf_count_t count ;
664
665 if ((count = sf_read_float (file, test, items)) != items)
666 { printf ("\n\nLine %d", line_num) ;
667 if (pass > 0)
668 printf (" (pass %d)", pass) ;
669 printf (" : sf_read_float failed with short read (%ld => %ld).\n",
670 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
671 fflush (stdout) ;
672 puts (sf_strerror (file)) ;
673 exit (1) ;
674 } ;
675
676 return ;
677 } /* test_read_float_or_die */
678
679 void
680 test_read_double_or_die (SNDFILE *file, int pass, double *test, sf_count_t items, int line_num)
681 { sf_count_t count ;
682
683 if ((count = sf_read_double (file, test, items)) != items)
684 { printf ("\n\nLine %d", line_num) ;
685 if (pass > 0)
686 printf (" (pass %d)", pass) ;
687 printf (" : sf_read_double failed with short read (%ld => %ld).\n",
688 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
689 fflush (stdout) ;
690 puts (sf_strerror (file)) ;
691 exit (1) ;
692 } ;
693
694 return ;
695 } /* test_read_double_or_die */
696
697
698 void
699 test_readf_short_or_die (SNDFILE *file, int pass, short *test, sf_count_t frames, int line_num)
700 { sf_count_t count ;
701
702 if ((count = sf_readf_short (file, test, frames)) != frames)
703 { printf ("\n\nLine %d", line_num) ;
704 if (pass > 0)
705 printf (" (pass %d)", pass) ;
706 printf (" : sf_readf_short failed with short readf (%ld => %ld).\n",
707 SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ;
708 fflush (stdout) ;
709 puts (sf_strerror (file)) ;
710 exit (1) ;
711 } ;
712
713 return ;
714 } /* test_readf_short_or_die */
715
716 void
717 test_readf_int_or_die (SNDFILE *file, int pass, int *test, sf_count_t frames, int line_num)
718 { sf_count_t count ;
719
720 if ((count = sf_readf_int (file, test, frames)) != frames)
721 { printf ("\n\nLine %d", line_num) ;
722 if (pass > 0)
723 printf (" (pass %d)", pass) ;
724 printf (" : sf_readf_int failed with short readf (%ld => %ld).\n",
725 SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ;
726 fflush (stdout) ;
727 puts (sf_strerror (file)) ;
728 exit (1) ;
729 } ;
730
731 return ;
732 } /* test_readf_int_or_die */
733
734 void
735 test_readf_float_or_die (SNDFILE *file, int pass, float *test, sf_count_t frames, int line_num)
736 { sf_count_t count ;
737
738 if ((count = sf_readf_float (file, test, frames)) != frames)
739 { printf ("\n\nLine %d", line_num) ;
740 if (pass > 0)
741 printf (" (pass %d)", pass) ;
742 printf (" : sf_readf_float failed with short readf (%ld => %ld).\n",
743 SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ;
744 fflush (stdout) ;
745 puts (sf_strerror (file)) ;
746 exit (1) ;
747 } ;
748
749 return ;
750 } /* test_readf_float_or_die */
751
752 void
753 test_readf_double_or_die (SNDFILE *file, int pass, double *test, sf_count_t frames, int line_num)
754 { sf_count_t count ;
755
756 if ((count = sf_readf_double (file, test, frames)) != frames)
757 { printf ("\n\nLine %d", line_num) ;
758 if (pass > 0)
759 printf (" (pass %d)", pass) ;
760 printf (" : sf_readf_double failed with short readf (%ld => %ld).\n",
761 SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ;
762 fflush (stdout) ;
763 puts (sf_strerror (file)) ;
764 exit (1) ;
765 } ;
766
767 return ;
768 } /* test_readf_double_or_die */
769
770
771 void
772 test_read_raw_or_die (SNDFILE *file, int pass, void *test, sf_count_t items, int line_num)
773 { sf_count_t count ;
774
775 if ((count = sf_read_raw (file, test, items)) != items)
776 { printf ("\n\nLine %d", line_num) ;
777 if (pass > 0)
778 printf (" (pass %d)", pass) ;
779 printf (" : sf_read_raw failed with short read (%ld => %ld).\n",
780 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
781 fflush (stdout) ;
782 puts (sf_strerror (file)) ;
783 exit (1) ;
784 } ;
785
786 return ;
787 } /* test_read_raw_or_die */
788
789
790
791 void
792 test_write_short_or_die (SNDFILE *file, int pass, const short *test, sf_count_t items, int line_num)
793 { sf_count_t count ;
794
795 if ((count = sf_write_short (file, test, items)) != items)
796 { printf ("\n\nLine %d", line_num) ;
797 if (pass > 0)
798 printf (" (pass %d)", pass) ;
799 printf (" : sf_write_short failed with short write (%ld => %ld).\n",
800 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
801 fflush (stdout) ;
802 puts (sf_strerror (file)) ;
803 exit (1) ;
804 } ;
805
806 return ;
807 } /* test_write_short_or_die */
808
809 void
810 test_write_int_or_die (SNDFILE *file, int pass, const int *test, sf_count_t items, int line_num)
811 { sf_count_t count ;
812
813 if ((count = sf_write_int (file, test, items)) != items)
814 { printf ("\n\nLine %d", line_num) ;
815 if (pass > 0)
816 printf (" (pass %d)", pass) ;
817 printf (" : sf_write_int failed with short write (%ld => %ld).\n",
818 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
819 fflush (stdout) ;
820 puts (sf_strerror (file)) ;
821 exit (1) ;
822 } ;
823
824 return ;
825 } /* test_write_int_or_die */
826
827 void
828 test_write_float_or_die (SNDFILE *file, int pass, const float *test, sf_count_t items, int line_num)
829 { sf_count_t count ;
830
831 if ((count = sf_write_float (file, test, items)) != items)
832 { printf ("\n\nLine %d", line_num) ;
833 if (pass > 0)
834 printf (" (pass %d)", pass) ;
835 printf (" : sf_write_float failed with short write (%ld => %ld).\n",
836 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
837 fflush (stdout) ;
838 puts (sf_strerror (file)) ;
839 exit (1) ;
840 } ;
841
842 return ;
843 } /* test_write_float_or_die */
844
845 void
846 test_write_double_or_die (SNDFILE *file, int pass, const double *test, sf_count_t items, int line_num)
847 { sf_count_t count ;
848
849 if ((count = sf_write_double (file, test, items)) != items)
850 { printf ("\n\nLine %d", line_num) ;
851 if (pass > 0)
852 printf (" (pass %d)", pass) ;
853 printf (" : sf_write_double failed with short write (%ld => %ld).\n",
854 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
855 fflush (stdout) ;
856 puts (sf_strerror (file)) ;
857 exit (1) ;
858 } ;
859
860 return ;
861 } /* test_write_double_or_die */
862
863
864 void
865 test_writef_short_or_die (SNDFILE *file, int pass, const short *test, sf_count_t frames, int line_num)
866 { sf_count_t count ;
867
868 if ((count = sf_writef_short (file, test, frames)) != frames)
869 { printf ("\n\nLine %d", line_num) ;
870 if (pass > 0)
871 printf (" (pass %d)", pass) ;
872 printf (" : sf_writef_short failed with short writef (%ld => %ld).\n",
873 SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ;
874 fflush (stdout) ;
875 puts (sf_strerror (file)) ;
876 exit (1) ;
877 } ;
878
879 return ;
880 } /* test_writef_short_or_die */
881
882 void
883 test_writef_int_or_die (SNDFILE *file, int pass, const int *test, sf_count_t frames, int line_num)
884 { sf_count_t count ;
885
886 if ((count = sf_writef_int (file, test, frames)) != frames)
887 { printf ("\n\nLine %d", line_num) ;
888 if (pass > 0)
889 printf (" (pass %d)", pass) ;
890 printf (" : sf_writef_int failed with short writef (%ld => %ld).\n",
891 SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ;
892 fflush (stdout) ;
893 puts (sf_strerror (file)) ;
894 exit (1) ;
895 } ;
896
897 return ;
898 } /* test_writef_int_or_die */
899
900 void
901 test_writef_float_or_die (SNDFILE *file, int pass, const float *test, sf_count_t frames, int line_num)
902 { sf_count_t count ;
903
904 if ((count = sf_writef_float (file, test, frames)) != frames)
905 { printf ("\n\nLine %d", line_num) ;
906 if (pass > 0)
907 printf (" (pass %d)", pass) ;
908 printf (" : sf_writef_float failed with short writef (%ld => %ld).\n",
909 SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ;
910 fflush (stdout) ;
911 puts (sf_strerror (file)) ;
912 exit (1) ;
913 } ;
914
915 return ;
916 } /* test_writef_float_or_die */
917
918 void
919 test_writef_double_or_die (SNDFILE *file, int pass, const double *test, sf_count_t frames, int line_num)
920 { sf_count_t count ;
921
922 if ((count = sf_writef_double (file, test, frames)) != frames)
923 { printf ("\n\nLine %d", line_num) ;
924 if (pass > 0)
925 printf (" (pass %d)", pass) ;
926 printf (" : sf_writef_double failed with short writef (%ld => %ld).\n",
927 SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ;
928 fflush (stdout) ;
929 puts (sf_strerror (file)) ;
930 exit (1) ;
931 } ;
932
933 return ;
934 } /* test_writef_double_or_die */
935
936
937 void
938 test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num)
939 { sf_count_t count ;
940
941 if ((count = sf_write_raw (file, test, items)) != items)
942 { printf ("\n\nLine %d", line_num) ;
943 if (pass > 0)
944 printf (" (pass %d)", pass) ;
945 printf (" : sf_write_raw failed with short write (%ld => %ld).\n",
946 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
947 fflush (stdout) ;
948 puts (sf_strerror (file)) ;
949 exit (1) ;
950 } ;
951
952 return ;
953 } /* test_write_raw_or_die */
954
955
956 void
957 compare_short_or_die (const short *left, const short *right, unsigned count, int line_num)
958 {
959 unsigned k ;
960
961 for (k = 0 ; k < count ;k++)
962 if (left [k] != right [k])
963 { printf ("\n\nLine %d : Error at index %d, " "% d" " should be " "% d" ".\n\n", line_num, k, left [k], right [k]) ;
964 exit (1) ;
965 } ;
966
967 return ;
968 } /* compare_short_or_die */
969 void
970 compare_int_or_die (const int *left, const int *right, unsigned count, int line_num)
971 {
972 unsigned k ;
973
974 for (k = 0 ; k < count ;k++)
975 if (left [k] != right [k])
976 { printf ("\n\nLine %d : Error at index %d, " "% d" " should be " "% d" ".\n\n", line_num, k, left [k], right [k]) ;
977 exit (1) ;
978 } ;
979
980 return ;
981 } /* compare_int_or_die */
982 void
983 compare_float_or_die (const float *left, const float *right, unsigned count, int line_num)
984 {
985 unsigned k ;
986
987 for (k = 0 ; k < count ;k++)
988 if (left [k] != right [k])
989 { printf ("\n\nLine %d : Error at index %d, " "% g" " should be " "% g" ".\n\n", line_num, k, left [k], right [k]) ;
990 exit (1) ;
991 } ;
992
993 return ;
994 } /* compare_float_or_die */
995 void
996 compare_double_or_die (const double *left, const double *right, unsigned count, int line_num)
997 {
998 unsigned k ;
999
1000 for (k = 0 ; k < count ;k++)
1001 if (left [k] != right [k])
1002 { printf ("\n\nLine %d : Error at index %d, " "% g" " should be " "% g" ".\n\n", line_num, k, left [k], right [k]) ;
1003 exit (1) ;
1004 } ;
1005
1006 return ;
1007 } /* compare_double_or_die */
1008
1009
1010
1011 void
1012 delete_file (int format, const char *filename)
1013 { char rsrc_name [512], *fname ;
1014
1015 unlink (filename) ;
1016
1017 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_SD2)
1018 return ;
1019
1020 /*
1021 ** Now try for a resource fork stored as a separate file.
1022 ** Grab the un-adulterated filename again.
1023 */
1024 snprintf (rsrc_name, sizeof (rsrc_name), "%s", filename) ;
1025
1026 if ((fname = strrchr (rsrc_name, '/')) != NULL)
1027 fname ++ ;
1028 else if ((fname = strrchr (rsrc_name, '\\')) != NULL)
1029 fname ++ ;
1030 else
1031 fname = rsrc_name ;
1032
1033 memmove (fname + 2, fname, strlen (fname) + 1) ;
1034 fname [0] = '.' ;
1035 fname [1] = '_' ;
1036
1037 unlink (rsrc_name) ;
1038 } /* delete_file */
1039
1040 static int allowed_open_files = -1 ;
1041
1042 void
1043 count_open_files (void)
1044 {
1045 #if OS_IS_WIN32
1046 return ;
1047 #else
1048 int k, count = 0 ;
1049 struct stat statbuf ;
1050
1051 if (allowed_open_files > 0)
1052 return ;
1053
1054 for (k = 0 ; k < 1024 ; k++)
1055 if (fstat (k, &statbuf) == 0)
1056 count ++ ;
1057
1058 allowed_open_files = count ;
1059 #endif
1060 } /* count_open_files */
1061
1062 void
1063 increment_open_file_count (void)
1064 { allowed_open_files ++ ;
1065 } /* increment_open_file_count */
1066
1067 void
1068 check_open_file_count_or_die (int lineno)
1069 {
1070 #if OS_IS_WIN32
1071 lineno = 0 ;
1072 return ;
1073 #else
1074 int k, count = 0 ;
1075 struct stat statbuf ;
1076
1077 if (allowed_open_files < 0)
1078 count_open_files () ;
1079
1080 for (k = 0 ; k < 1024 ; k++)
1081 if (fstat (k, &statbuf) == 0)
1082 count ++ ;
1083
1084 if (count > allowed_open_files)
1085 { printf ("\nLine %d : number of open files (%d) > allowed (%d).\n\n", lineno, count, allowed_open_files) ;
1086 exit (1) ;
1087 } ;
1088 #endif
1089 } /* check_open_file_count_or_die */
1090
1091 void
1092 write_mono_file (const char * filename, int format, int srate, float * output, int len)
1093 { SNDFILE * file ;
1094 SF_INFO sfinfo ;
1095
1096 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1097
1098 sfinfo.samplerate = srate ;
1099 sfinfo.channels = 1 ;
1100 sfinfo.format = format ;
1101
1102 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
1103 { printf ("sf_open (%s) : %s\n", filename, sf_strerror (NULL)) ;
1104 exit (1) ;
1105 } ;
1106
1107 sf_write_float (file, output, len) ;
1108
1109 sf_close (file) ;
1110 } /* write_mono_file */
1111
1112 void
1113 gen_lowpass_noise_float (float *data, int len)
1114 { int32_t value = 0x1243456 ;
1115 double sample, last_val = 0.0 ;
1116 int k ;
1117
1118 for (k = 0 ; k < len ; k++)
1119 { /* Not a crypto quality RNG. */
1120 value = 11117 * value + 211231 ;
1121 value = 11117 * value + 211231 ;
1122 value = 11117 * value + 211231 ;
1123
1124 sample = value / (0x7fffffff * 1.000001) ;
1125 sample = 0.2 * sample - 0.9 * last_val ;
1126
1127 data [k] = last_val = sample ;
1128 } ;
1129
1130 } /* gen_lowpass_noise_float */
1131
1132
1133 /*
1134 ** Windows is fucked.
1135 ** If a file is opened R/W and data is written to it, then fstat will return
1136 ** the correct file length, but stat will return zero.
1137 */
1138
1139 sf_count_t
1140 file_length (const char * fname)
1141 { struct stat data ;
1142
1143 if (stat (fname, &data) != 0)
1144 return 0 ;
1145
1146 return (sf_count_t) data.st_size ;
1147 } /* file_length */
1148
1149 sf_count_t
1150 file_length_fd (int fd)
1151 { struct stat data ;
1152
1153 memset (&data, 0, sizeof (data)) ;
1154 if (fstat (fd, &data) != 0)
1155 return 0 ;
1156
1157 return (sf_count_t) data.st_size ;
1158 } /* file_length_fd */
1159
1160
1161
1162