blob: f517e23e1d288e0df305e750ccfadbb2ee4ff0a3 [file] [log] [blame]
Alexandre Lision7c6f4a62013-09-05 13:27:01 -04001[+ AutoGen5 template h c +]
2/*
3** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18*/
19
20/*
21** Utility functions to make writing the test suite easier.
22**
23** The .c and .h files were generated automagically with Autogen from
24** the files utils.def and utils.tpl.
25*/
26
27[+ CASE (suffix) +]
28[+ == h +]
29
30#ifdef __cplusplus
31extern "C" {
32#endif /* __cplusplus */
33
34#include <stdint.h>
35#include <stdarg.h>
36
37#define SF_COUNT_TO_LONG(x) ((long) (x))
38#define ARRAY_LEN(x) ((int) (sizeof (x)) / (sizeof ((x) [0])))
39#define SIGNED_SIZEOF(x) ((int64_t) (sizeof (x)))
40#define NOT(x) (! (x))
41
42#define PIPE_INDEX(x) ((x) + 500)
43#define PIPE_TEST_LEN 12345
44
45
46[+ FOR float_type
47+]void gen_windowed_sine_[+ (get "name") +] ([+ (get "name") +] *data, int len, double maximum) ;
48[+ ENDFOR float_type
49+]
50
51void create_short_sndfile (const char *filename, int format, int channels) ;
52
53void check_file_hash_or_die (const char *filename, uint64_t target_hash, int line_num) ;
54
55void print_test_name (const char *test, const char *filename) ;
56
57void dump_data_to_file (const char *filename, const void *data, unsigned int datalen) ;
58
59void write_mono_file (const char * filename, int format, int srate, float * output, int len) ;
60
61static inline void
62exit_if_true (int test, const char *format, ...)
63{ if (test)
64 { va_list argptr ;
65 va_start (argptr, format) ;
66 vprintf (format, argptr) ;
67 va_end (argptr) ;
68 exit (1) ;
69 } ;
70} /* exit_if_true */
71
72/*
73** Functions for saving two vectors of data in an ascii text file which
74** can then be loaded into GNU octave for comparison.
75*/
76
77[+ FOR io_type
78+]int oct_save_[+ (get "io_element") +] (const [+ (get "io_element") +] *a, const [+ (get "io_element") +] *b, int len) ;
79[+ ENDFOR io_type
80+]
81
82void delete_file (int format, const char *filename) ;
83
84void count_open_files (void) ;
85void increment_open_file_count (void) ;
86void check_open_file_count_or_die (int lineno) ;
87
88#ifdef SNDFILE_H
89
90static inline void
91sf_info_clear (SF_INFO * info)
92{ memset (info, 0, sizeof (SF_INFO)) ;
93} /* sf_info_clear */
94
95static inline void
96sf_info_setup (SF_INFO * info, int format, int samplerate, int channels)
97{ sf_info_clear (info) ;
98
99 info->format = format ;
100 info->samplerate = samplerate ;
101 info->channels = channels ;
102} /* sf_info_setup */
103
104
105void dump_log_buffer (SNDFILE *file) ;
106void check_log_buffer_or_die (SNDFILE *file, int line_num) ;
107int string_in_log_buffer (SNDFILE *file, const char *s) ;
108void hexdump_file (const char * filename, sf_count_t offset, sf_count_t length) ;
109
110
111SNDFILE *test_open_file_or_die
112 (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num) ;
113
114void test_read_write_position_or_die
115 (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos) ;
116
117void test_seek_or_die
118 (SNDFILE *file, sf_count_t offset, int whence, sf_count_t new_pos, int channels, int line_num) ;
119
120[+ FOR read_op +]
121[+ FOR io_type
122+]void test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die
123 (SNDFILE *file, int pass, [+ (get "io_element") +] *test, sf_count_t [+ (get "count_name") +], int line_num) ;
124[+ ENDFOR io_type +][+ ENDFOR read_op +]
125
126void
127test_read_raw_or_die (SNDFILE *file, int pass, void *test, sf_count_t items, int line_num) ;
128
129[+ FOR write_op +]
130[+ FOR io_type
131+]void test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die
132 (SNDFILE *file, int pass, const [+ (get "io_element") +] *test, sf_count_t [+ (get "count_name") +], int line_num) ;
133[+ ENDFOR io_type +][+ ENDFOR write_op +]
134
135void
136test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num) ;
137
138[+ FOR io_type
139+]void compare_[+ (get "io_element") +]_or_die (const [+ (get "io_element") +] *left, const [+ (get "io_element") +] *right, unsigned count, int line_num) ;
140[+ ENDFOR io_type +]
141
142
143void gen_lowpass_noise_float (float *data, int len) ;
144
145sf_count_t file_length (const char * fname) ;
146sf_count_t file_length_fd (int fd) ;
147
148#endif
149
150#ifdef __cplusplus
151} /* extern "C" */
152#endif /* __cplusplus */
153
154[+ == c +]
155
156#include "sfconfig.h"
157
158#include <stdio.h>
159#include <stdlib.h>
160#include <inttypes.h>
161
162#if HAVE_UNISTD_H
163#include <unistd.h>
164#endif
165
166#if (HAVE_DECL_S_IRGRP == 0)
167#include <sf_unistd.h>
168#endif
169
170#include <errno.h>
171#include <string.h>
172#include <ctype.h>
173#include <math.h>
174#include <fcntl.h>
175#include <sys/stat.h>
176
177#include <sndfile.h>
178
179#include "utils.h"
180
181#ifndef M_PI
182#define M_PI 3.14159265358979323846264338
183#endif
184
185#define LOG_BUFFER_SIZE 2048
186
187/*
188** Neat solution to the Win32/OS2 binary file flage requirement.
189** If O_BINARY isn't already defined by the inclusion of the system
190** headers, set it to zero.
191*/
192#ifndef O_BINARY
193#define O_BINARY 0
194#endif
195
196[+ FOR float_type +]
197void
198gen_windowed_sine_[+ (get "name") +] ([+ (get "name") +] *data, int len, double maximum)
199{ int k ;
200
201 memset (data, 0, len * sizeof ([+ (get "name") +])) ;
202 /*
203 ** Choose a frequency of 1/32 so that it aligns perfectly with a DFT
204 ** bucket to minimise spreading of energy over more than one bucket.
205 ** Also do not want to make the frequency too high as some of the
206 ** codecs (ie gsm610) have a quite severe high frequency roll off.
207 */
208 len /= 2 ;
209
210 for (k = 0 ; k < len ; k++)
211 { data [k] = sin (2.0 * k * M_PI * 1.0 / 32.0 + 0.4) ;
212
213 /* Apply Hanning Window. */
214 data [k] *= maximum * (0.5 - 0.5 * cos (2.0 * M_PI * k / ((len) - 1))) ;
215 }
216
217 return ;
218} /* gen_windowed_sine_[+ (get "name") +] */
219[+ ENDFOR float_type +]
220
221void
222create_short_sndfile (const char *filename, int format, int channels)
223{ short data [2 * 3 * 4 * 5 * 6 * 7] = { 0, } ;
224 SNDFILE *file ;
225 SF_INFO sfinfo ;
226
227 sfinfo.samplerate = 44100 ;
228 sfinfo.channels = channels ;
229 sfinfo.format = format ;
230
231 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
232 { printf ("Error (%s, %d) : sf_open failed : %s\n", __FILE__, __LINE__, sf_strerror (file)) ;
233 exit (1) ;
234 } ;
235
236 sf_write_short (file, data, ARRAY_LEN (data)) ;
237
238 sf_close (file) ;
239} /* create_short_sndfile */
240
241void
242check_file_hash_or_die (const char *filename, uint64_t target_hash, int line_num)
243{ static unsigned char buf [4096] ;
244 uint64_t cksum ;
245 FILE *file ;
246 int k, read_count ;
247
248 memset (buf, 0, sizeof (buf)) ;
249
250 /* The 'b' in the mode string means binary for Win32. */
251 if ((file = fopen (filename, "rb")) == NULL)
252 { printf ("\n\nLine %d: could not open file '%s'\n\n", line_num, filename) ;
253 exit (1) ;
254 } ;
255
256 cksum = 0 ;
257
258 while ((read_count = fread (buf, 1, sizeof (buf), file)))
259 for (k = 0 ; k < read_count ; k++)
260 cksum = cksum * 511 + buf [k] ;
261
262 fclose (file) ;
263
264 if (target_hash == 0)
265 { printf (" 0x%016" PRIx64 "\n", cksum) ;
266 return ;
267 } ;
268
269 if (cksum != target_hash)
270 { printf ("\n\nLine %d: incorrect hash value 0x%016" PRIx64 " should be 0x%016" PRIx64 ".\n\n", line_num, cksum, target_hash) ;
271 exit (1) ;
272 } ;
273
274 return ;
275} /* check_file_hash_or_die */
276
277void
278print_test_name (const char *test, const char *filename)
279{ int count ;
280
281 if (test == NULL)
282 { printf (__FILE__ ": bad test of filename parameter.\n") ;
283 exit (1) ;
284 } ;
285
286 if (filename == NULL || strlen (filename) == 0)
287 { printf (" %-30s : ", test) ;
288 count = 25 ;
289 }
290 else
291 { printf (" %-30s : %s ", test, filename) ;
292 count = 24 - strlen (filename) ;
293 } ;
294
295 while (count -- > 0)
296 putchar ('.') ;
297 putchar (' ') ;
298
299 fflush (stdout) ;
300} /* print_test_name */
301
302void
303dump_data_to_file (const char *filename, const void *data, unsigned int datalen)
304{ FILE *file ;
305
306 if ((file = fopen (filename, "wb")) == NULL)
307 { printf ("\n\nLine %d : could not open file : %s\n\n", __LINE__, filename) ;
308 exit (1) ;
309 } ;
310
311 if (fwrite (data, 1, datalen, file) != datalen)
312 { printf ("\n\nLine %d : fwrite failed.\n\n", __LINE__) ;
313 exit (1) ;
314 } ;
315
316 fclose (file) ;
317
318} /* dump_data_to_file */
319
320/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
321*/
322
323static char octfilename [] = "error.dat" ;
324
325[+ FOR io_type
326+]int
327oct_save_[+ (get "io_element") +] (const [+ (get "io_element") +] *a, const [+ (get "io_element") +] *b, int len)
328{ FILE *file ;
329 int k ;
330
331 if (! (file = fopen (octfilename, "w")))
332 return 1 ;
333
334 fprintf (file, "# Not created by Octave\n") ;
335
336 fprintf (file, "# name: a\n") ;
337 fprintf (file, "# type: matrix\n") ;
338 fprintf (file, "# rows: %d\n", len) ;
339 fprintf (file, "# columns: 1\n") ;
340
341 for (k = 0 ; k < len ; k++)
342 fprintf (file, [+ (get "format_str") +] "\n", a [k]) ;
343
344 fprintf (file, "# name: b\n") ;
345 fprintf (file, "# type: matrix\n") ;
346 fprintf (file, "# rows: %d\n", len) ;
347 fprintf (file, "# columns: 1\n") ;
348
349 for (k = 0 ; k < len ; k++)
350 fprintf (file, [+ (get "format_str") +] "\n", b [k]) ;
351
352 fclose (file) ;
353 return 0 ;
354} /* oct_save_[+ (get "io_element") +] */
355[+ ENDFOR io_type
356+]
357
358void
359check_log_buffer_or_die (SNDFILE *file, int line_num)
360{ static char buffer [LOG_BUFFER_SIZE] ;
361 int count ;
362
363 memset (buffer, 0, sizeof (buffer)) ;
364
365 /* Get the log buffer data. */
366 count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
367
368 if (LOG_BUFFER_SIZE - count < 2)
369 { printf ("\n\nLine %d : Possible long log buffer.\n", line_num) ;
370 exit (1) ;
371 }
372
373 /* Look for "Should" */
374 if (strstr (buffer, "ould"))
375 { printf ("\n\nLine %d : Log buffer contains `ould'. Dumping.\n", line_num) ;
376 puts (buffer) ;
377 exit (1) ;
378 } ;
379
380 /* Look for "**" */
381 if (strstr (buffer, "*"))
382 { printf ("\n\nLine %d : Log buffer contains `*'. Dumping.\n", line_num) ;
383 puts (buffer) ;
384 exit (1) ;
385 } ;
386
387 /* Look for "Should" */
388 if (strstr (buffer, "nknown marker"))
389 { printf ("\n\nLine %d : Log buffer contains `nknown marker'. Dumping.\n", line_num) ;
390 puts (buffer) ;
391 exit (1) ;
392 } ;
393
394 return ;
395} /* check_log_buffer_or_die */
396
397int
398string_in_log_buffer (SNDFILE *file, const char *s)
399{ static char buffer [LOG_BUFFER_SIZE] ;
400 int count ;
401
402 memset (buffer, 0, sizeof (buffer)) ;
403
404 /* Get the log buffer data. */
405 count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
406
407 if (LOG_BUFFER_SIZE - count < 2)
408 { printf ("Possible long log buffer.\n") ;
409 exit (1) ;
410 }
411
412 /* Look for string */
413 return strstr (buffer, s) ? SF_TRUE : SF_FALSE ;
414} /* string_in_log_buffer */
415
416void
417hexdump_file (const char * filename, sf_count_t offset, sf_count_t length)
418{
419 FILE * file ;
420 char buffer [16] ;
421 int k, m, ch, readcount ;
422
423 if (length > 1000000)
424 { printf ("\n\nError : length (%ld) too long.\n\n", SF_COUNT_TO_LONG (offset)) ;
425 exit (1) ;
426 } ;
427
428 if ((file = fopen (filename, "r")) == NULL)
429 { printf ("\n\nError : hexdump_file (%s) could not open file for read.\n\n", filename) ;
430 exit (1) ;
431 } ;
432
433 if (fseek (file, offset, SEEK_SET) != 0)
434 { printf ("\n\nError : fseek(file, %ld, SEEK_SET) failed : %s\n\n", SF_COUNT_TO_LONG (offset), strerror (errno)) ;
435 exit (1) ;
436 } ;
437
438 puts ("\n\n") ;
439
440 for (k = 0 ; k < length ; k+= sizeof (buffer))
441 { readcount = fread (buffer, 1, sizeof (buffer), file) ;
442
443 printf ("%08lx : ", SF_COUNT_TO_LONG (offset + k)) ;
444
445 for (m = 0 ; m < readcount ; m++)
446 printf ("%02x ", buffer [m] & 0xFF) ;
447
448 for (m = readcount ; m < SIGNED_SIZEOF (buffer) ; m++)
449 printf (" ") ;
450
451 printf (" ") ;
452 for (m = 0 ; m < readcount ; m++)
453 { ch = isprint (buffer [m]) ? buffer [m] : '.' ;
454 putchar (ch) ;
455 } ;
456
457 if (readcount < SIGNED_SIZEOF (buffer))
458 break ;
459
460 putchar ('\n') ;
461 } ;
462
463 puts ("\n") ;
464
465 fclose (file) ;
466} /* hexdump_file */
467
468void
469dump_log_buffer (SNDFILE *file)
470{ static char buffer [LOG_BUFFER_SIZE] ;
471
472 memset (buffer, 0, sizeof (buffer)) ;
473
474 /* Get the log buffer data. */
475 sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
476
477 if (strlen (buffer) < 1)
478 puts ("Log buffer empty.\n") ;
479 else
480 puts (buffer) ;
481
482 return ;
483} /* dump_log_buffer */
484
485SNDFILE *
486test_open_file_or_die (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num)
487{ static int count = 0 ;
488
489 SNDFILE *file ;
490 const char *modestr, *func_name ;
491 int oflags = 0, omode = 0, err ;
492
493 /*
494 ** Need to test both sf_open() and sf_open_fd().
495 ** Do so alternately.
496 */
497 switch (mode)
498 { case SFM_READ :
499 modestr = "SFM_READ" ;
500 oflags = O_RDONLY | O_BINARY ;
501 omode = 0 ;
502 break ;
503
504 case SFM_WRITE :
505 modestr = "SFM_WRITE" ;
506 oflags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ;
507 omode = S_IRUSR | S_IWUSR | S_IRGRP ;
508 break ;
509
510 case SFM_RDWR :
511 modestr = "SFM_RDWR" ;
512 oflags = O_RDWR | O_CREAT | O_BINARY ;
513 omode = S_IRUSR | S_IWUSR | S_IRGRP ;
514 break ;
515 default :
516 printf ("\n\nLine %d: Bad mode.\n", line_num) ;
517 fflush (stdout) ;
518 exit (1) ;
519 } ;
520
521 if (OS_IS_WIN32)
522 { /* Windows does not understand and ignores the S_IRGRP flag, but Wine
523 ** gives a run time warning message, so just clear it.
524 */
525 omode &= ~S_IRGRP ;
526 } ;
527
528 if (allow_fd && ((++count) & 1) == 1)
529 { int fd ;
530
531 /* Only use the three argument open() function if omode != 0. */
532 fd = (omode == 0) ? open (filename, oflags) : open (filename, oflags, omode) ;
533
534 if (fd < 0)
535 { printf ("\n\n%s : open failed : %s\n", __func__, strerror (errno)) ;
536 exit (1) ;
537 } ;
538
539 func_name = "sf_open_fd" ;
540 file = sf_open_fd (fd, mode, sfinfo, SF_TRUE) ;
541 }
542 else
543 { func_name = "sf_open" ;
544 file = sf_open (filename, mode, sfinfo) ;
545 } ;
546
547 if (file == NULL)
548 { printf ("\n\nLine %d: %s (%s) failed : %s\n\n", line_num, func_name, modestr, sf_strerror (NULL)) ;
549 dump_log_buffer (file) ;
550 exit (1) ;
551 } ;
552
553 err = sf_error (file) ;
554 if (err != SF_ERR_NO_ERROR)
555 { printf ("\n\nLine %d : sf_error : %s\n\n", line_num, sf_error_number (err)) ;
556 dump_log_buffer (file) ;
557 exit (1) ;
558 } ;
559
560 return file ;
561} /* test_open_file_or_die */
562
563void
564test_read_write_position_or_die (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos)
565{ sf_count_t pos ;
566
567 /* Check the current read position. */
568 if (read_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_READ)) != read_pos)
569 { printf ("\n\nLine %d ", line_num) ;
570 if (pass > 0)
571 printf ("(pass %d): ", pass) ;
572 printf ("Read position (%ld) should be %ld.\n", SF_COUNT_TO_LONG (pos), SF_COUNT_TO_LONG (read_pos)) ;
573 exit (1) ;
574 } ;
575
576 /* Check the current write position. */
577 if (write_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_WRITE)) != write_pos)
578 { printf ("\n\nLine %d", line_num) ;
579 if (pass > 0)
580 printf (" (pass %d)", pass) ;
581 printf (" : Write position (%ld) should be %ld.\n",
582 SF_COUNT_TO_LONG (pos), SF_COUNT_TO_LONG (write_pos)) ;
583 exit (1) ;
584 } ;
585
586 return ;
587} /* test_read_write_position */
588
589void
590test_seek_or_die (SNDFILE *file, sf_count_t offset, int whence, sf_count_t new_pos, int channels, int line_num)
591{ sf_count_t position ;
592 const char *channel_name, *whence_name ;
593
594 switch (whence)
595 { case SEEK_SET :
596 whence_name = "SEEK_SET" ;
597 break ;
598 case SEEK_CUR :
599 whence_name = "SEEK_CUR" ;
600 break ;
601 case SEEK_END :
602 whence_name = "SEEK_END" ;
603 break ;
604
605 /* SFM_READ */
606 case SEEK_SET | SFM_READ :
607 whence_name = "SFM_READ | SEEK_SET" ;
608 break ;
609 case SEEK_CUR | SFM_READ :
610 whence_name = "SFM_READ | SEEK_CUR" ;
611 break ;
612 case SEEK_END | SFM_READ :
613 whence_name = "SFM_READ | SEEK_END" ;
614 break ;
615
616 /* SFM_WRITE */
617 case SEEK_SET | SFM_WRITE :
618 whence_name = "SFM_WRITE | SEEK_SET" ;
619 break ;
620 case SEEK_CUR | SFM_WRITE :
621 whence_name = "SFM_WRITE | SEEK_CUR" ;
622 break ;
623 case SEEK_END | SFM_WRITE :
624 whence_name = "SFM_WRITE | SEEK_END" ;
625 break ;
626
627 default :
628 printf ("\n\nLine %d: bad whence parameter.\n", line_num) ;
629 exit (1) ;
630 } ;
631
632 channel_name = (channels == 1) ? "Mono" : "Stereo" ;
633
634 if ((position = sf_seek (file, offset, whence)) != new_pos)
635 { printf ("\n\nLine %d : %s : sf_seek (file, %ld, %s) returned %ld (should be %ld).\n\n",
636 line_num, channel_name, SF_COUNT_TO_LONG (offset), whence_name,
637 SF_COUNT_TO_LONG (position), SF_COUNT_TO_LONG (new_pos)) ;
638 exit (1) ;
639 } ;
640
641} /* test_seek_or_die */
642
643[+ FOR read_op +]
644[+ FOR io_type +]
645void
646test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die (SNDFILE *file, int pass, [+ (get "io_element") +] *test, sf_count_t [+ (get "count_name") +], int line_num)
647{ sf_count_t count ;
648
649 if ((count = sf_[+ (get "op_element") +]_[+ (get "io_element") +] (file, test, [+ (get "count_name") +])) != [+ (get "count_name") +])
650 { printf ("\n\nLine %d", line_num) ;
651 if (pass > 0)
652 printf (" (pass %d)", pass) ;
653 printf (" : sf_[+ (get "op_element") +]_[+ (get "io_element") +] failed with short [+ (get "op_element") +] (%ld => %ld).\n",
654 SF_COUNT_TO_LONG ([+ (get "count_name") +]), SF_COUNT_TO_LONG (count)) ;
655 fflush (stdout) ;
656 puts (sf_strerror (file)) ;
657 exit (1) ;
658 } ;
659
660 return ;
661} /* test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die */
662[+ ENDFOR io_type +][+ ENDFOR read_op +]
663
664void
665test_read_raw_or_die (SNDFILE *file, int pass, void *test, sf_count_t items, int line_num)
666{ sf_count_t count ;
667
668 if ((count = sf_read_raw (file, test, items)) != items)
669 { printf ("\n\nLine %d", line_num) ;
670 if (pass > 0)
671 printf (" (pass %d)", pass) ;
672 printf (" : sf_read_raw failed with short read (%ld => %ld).\n",
673 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
674 fflush (stdout) ;
675 puts (sf_strerror (file)) ;
676 exit (1) ;
677 } ;
678
679 return ;
680} /* test_read_raw_or_die */
681
682[+ FOR write_op +]
683[+ FOR io_type +]
684void
685test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die (SNDFILE *file, int pass, const [+ (get "io_element") +] *test, sf_count_t [+ (get "count_name") +], int line_num)
686{ sf_count_t count ;
687
688 if ((count = sf_[+ (get "op_element") +]_[+ (get "io_element") +] (file, test, [+ (get "count_name") +])) != [+ (get "count_name") +])
689 { printf ("\n\nLine %d", line_num) ;
690 if (pass > 0)
691 printf (" (pass %d)", pass) ;
692 printf (" : sf_[+ (get "op_element") +]_[+ (get "io_element") +] failed with short [+ (get "op_element") +] (%ld => %ld).\n",
693 SF_COUNT_TO_LONG ([+ (get "count_name") +]), SF_COUNT_TO_LONG (count)) ;
694 fflush (stdout) ;
695 puts (sf_strerror (file)) ;
696 exit (1) ;
697 } ;
698
699 return ;
700} /* test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die */
701[+ ENDFOR io_type +][+ ENDFOR write_op +]
702
703void
704test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num)
705{ sf_count_t count ;
706
707 if ((count = sf_write_raw (file, test, items)) != items)
708 { printf ("\n\nLine %d", line_num) ;
709 if (pass > 0)
710 printf (" (pass %d)", pass) ;
711 printf (" : sf_write_raw failed with short write (%ld => %ld).\n",
712 SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
713 fflush (stdout) ;
714 puts (sf_strerror (file)) ;
715 exit (1) ;
716 } ;
717
718 return ;
719} /* test_write_raw_or_die */
720
721
722[+ FOR io_type
723+]void
724compare_[+ (get "io_element") +]_or_die (const [+ (get "io_element") +] *left, const [+ (get "io_element") +] *right, unsigned count, int line_num)
725{
726 unsigned k ;
727
728 for (k = 0 ; k < count ;k++)
729 if (left [k] != right [k])
730 { printf ("\n\nLine %d : Error at index %d, " [+ (get "format_str") +] " should be " [+ (get "format_str") +] ".\n\n", line_num, k, left [k], right [k]) ;
731 exit (1) ;
732 } ;
733
734 return ;
735} /* compare_[+ (get "io_element") +]_or_die */
736[+ ENDFOR io_type +]
737
738
739void
740delete_file (int format, const char *filename)
741{ char rsrc_name [512], *fname ;
742
743 unlink (filename) ;
744
745 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_SD2)
746 return ;
747
748 /*
749 ** Now try for a resource fork stored as a separate file.
750 ** Grab the un-adulterated filename again.
751 */
752 snprintf (rsrc_name, sizeof (rsrc_name), "%s", filename) ;
753
754 if ((fname = strrchr (rsrc_name, '/')) != NULL)
755 fname ++ ;
756 else if ((fname = strrchr (rsrc_name, '\\')) != NULL)
757 fname ++ ;
758 else
759 fname = rsrc_name ;
760
761 memmove (fname + 2, fname, strlen (fname) + 1) ;
762 fname [0] = '.' ;
763 fname [1] = '_' ;
764
765 unlink (rsrc_name) ;
766} /* delete_file */
767
768static int allowed_open_files = -1 ;
769
770void
771count_open_files (void)
772{
773#if OS_IS_WIN32
774 return ;
775#else
776 int k, count = 0 ;
777 struct stat statbuf ;
778
779 if (allowed_open_files > 0)
780 return ;
781
782 for (k = 0 ; k < 1024 ; k++)
783 if (fstat (k, &statbuf) == 0)
784 count ++ ;
785
786 allowed_open_files = count ;
787#endif
788} /* count_open_files */
789
790void
791increment_open_file_count (void)
792{ allowed_open_files ++ ;
793} /* increment_open_file_count */
794
795void
796check_open_file_count_or_die (int lineno)
797{
798#if OS_IS_WIN32
799 lineno = 0 ;
800 return ;
801#else
802 int k, count = 0 ;
803 struct stat statbuf ;
804
805 if (allowed_open_files < 0)
806 count_open_files () ;
807
808 for (k = 0 ; k < 1024 ; k++)
809 if (fstat (k, &statbuf) == 0)
810 count ++ ;
811
812 if (count > allowed_open_files)
813 { printf ("\nLine %d : number of open files (%d) > allowed (%d).\n\n", lineno, count, allowed_open_files) ;
814 exit (1) ;
815 } ;
816#endif
817} /* check_open_file_count_or_die */
818
819void
820write_mono_file (const char * filename, int format, int srate, float * output, int len)
821{ SNDFILE * file ;
822 SF_INFO sfinfo ;
823
824 memset (&sfinfo, 0, sizeof (sfinfo)) ;
825
826 sfinfo.samplerate = srate ;
827 sfinfo.channels = 1 ;
828 sfinfo.format = format ;
829
830 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
831 { printf ("sf_open (%s) : %s\n", filename, sf_strerror (NULL)) ;
832 exit (1) ;
833 } ;
834
835 sf_write_float (file, output, len) ;
836
837 sf_close (file) ;
838} /* write_mono_file */
839
840void
841gen_lowpass_noise_float (float *data, int len)
842{ int32_t value = 0x1243456 ;
843 double sample, last_val = 0.0 ;
844 int k ;
845
846 for (k = 0 ; k < len ; k++)
847 { /* Not a crypto quality RNG. */
848 value = 11117 * value + 211231 ;
849 value = 11117 * value + 211231 ;
850 value = 11117 * value + 211231 ;
851
852 sample = value / (0x7fffffff * 1.000001) ;
853 sample = 0.2 * sample - 0.9 * last_val ;
854
855 data [k] = last_val = sample ;
856 } ;
857
858} /* gen_lowpass_noise_float */
859
860
861/*
862** Windows is fucked.
863** If a file is opened R/W and data is written to it, then fstat will return
864** the correct file length, but stat will return zero.
865*/
866
867sf_count_t
868file_length (const char * fname)
869{ struct stat data ;
870
871 if (stat (fname, &data) != 0)
872 return 0 ;
873
874 return (sf_count_t) data.st_size ;
875} /* file_length */
876
877sf_count_t
878file_length_fd (int fd)
879{ struct stat data ;
880
881 memset (&data, 0, sizeof (data)) ;
882 if (fstat (fd, &data) != 0)
883 return 0 ;
884
885 return (sf_count_t) data.st_size ;
886} /* file_length_fd */
887
888
889[+ ESAC +]
890