1 [+ AutoGen5 template h c +]
3 ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
21 ** Utility functions to make writing the test suite easier.
23 ** The .c and .h files were generated automagically with Autogen from
24 ** the files utils.def and utils.tpl.
32 #endif /* __cplusplus */
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))
42 #define PIPE_INDEX(x) ((x) + 500)
43 #define PIPE_TEST_LEN 12345
47 +]void gen_windowed_sine_[+ (get "name") +] ([+ (get "name") +] *data, int len, double maximum) ;
51 void create_short_sndfile (const char *filename, int format, int channels) ;
53 void check_file_hash_or_die (const char *filename, uint64_t target_hash, int line_num) ;
55 void print_test_name (const char *test, const char *filename) ;
57 void dump_data_to_file (const char *filename, const void *data, unsigned int datalen) ;
59 void write_mono_file (const char * filename, int format, int srate, float * output, int len) ;
62 exit_if_true (int test, const char *format, ...)
65 va_start (argptr, format) ;
66 vprintf (format, argptr) ;
73 ** Functions for saving two vectors of data in an ascii text file which
74 ** can then be loaded into GNU octave for comparison.
78 +]int oct_save_[+ (get "io_element") +] (const [+ (get "io_element") +] *a, const [+ (get "io_element") +] *b, int len) ;
82 void delete_file (int format, const char *filename) ;
84 void count_open_files (void) ;
85 void increment_open_file_count (void) ;
86 void check_open_file_count_or_die (int lineno) ;
91 sf_info_clear (SF_INFO * info)
92 { memset (info, 0, sizeof (SF_INFO)) ;
96 sf_info_setup (SF_INFO * info, int format, int samplerate, int channels)
97 { sf_info_clear (info) ;
99 info->format = format ;
100 info->samplerate = samplerate ;
101 info->channels = channels ;
102 } /* sf_info_setup */
105 void dump_log_buffer (SNDFILE *file) ;
106 void check_log_buffer_or_die (SNDFILE *file, int line_num) ;
107 int string_in_log_buffer (SNDFILE *file, const char *s) ;
108 void hexdump_file (const char * filename, sf_count_t offset, sf_count_t length) ;
111 SNDFILE *test_open_file_or_die
112 (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num) ;
114 void test_read_write_position_or_die
115 (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos) ;
117 void test_seek_or_die
118 (SNDFILE *file, sf_count_t offset, int whence, sf_count_t new_pos, int channels, int line_num) ;
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 +]
127 test_read_raw_or_die (SNDFILE *file, int pass, void *test, sf_count_t items, int line_num) ;
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 +]
136 test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num) ;
139 +]void compare_[+ (get "io_element") +]_or_die (const [+ (get "io_element") +] *left, const [+ (get "io_element") +] *right, unsigned count, int line_num) ;
143 void gen_lowpass_noise_float (float *data, int len) ;
145 sf_count_t file_length (const char * fname) ;
146 sf_count_t file_length_fd (int fd) ;
152 #endif /* __cplusplus */
156 #include "sfconfig.h"
160 #include <inttypes.h>
166 #if (HAVE_DECL_S_IRGRP == 0)
167 #include <sf_unistd.h>
175 #include <sys/stat.h>
182 #define M_PI 3.14159265358979323846264338
185 #define LOG_BUFFER_SIZE 2048
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.
198 gen_windowed_sine_[+ (get "name") +] ([+ (get "name") +] *data, int len, double maximum)
201 memset (data, 0, len * sizeof ([+ (get "name") +])) ;
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.
210 for (k = 0 ; k < len ; k++)
211 { data [k] = sin (2.0 * k * M_PI * 1.0 / 32.0 + 0.4) ;
213 /* Apply Hanning Window. */
214 data [k] *= maximum * (0.5 - 0.5 * cos (2.0 * M_PI * k / ((len) - 1))) ;
218 } /* gen_windowed_sine_[+ (get "name") +] */
219 [+ ENDFOR float_type +]
222 create_short_sndfile (const char *filename, int format, int channels)
223 { short data [2 * 3 * 4 * 5 * 6 * 7] = { 0, } ;
227 sfinfo.samplerate = 44100 ;
228 sfinfo.channels = channels ;
229 sfinfo.format = format ;
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)) ;
236 sf_write_short (file, data, ARRAY_LEN (data)) ;
239 } /* create_short_sndfile */
242 check_file_hash_or_die (const char *filename, uint64_t target_hash, int line_num)
243 { static unsigned char buf [4096] ;
248 memset (buf, 0, sizeof (buf)) ;
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) ;
258 while ((read_count = fread (buf, 1, sizeof (buf), file)))
259 for (k = 0 ; k < read_count ; k++)
260 cksum = cksum * 511 + buf [k] ;
264 if (target_hash == 0)
265 { printf (" 0x%016" PRIx64 "\n", cksum) ;
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) ;
275 } /* check_file_hash_or_die */
278 print_test_name (const char *test, const char *filename)
282 { printf (__FILE__ ": bad test of filename parameter.\n") ;
286 if (filename == NULL || strlen (filename) == 0)
287 { printf (" %-30s : ", test) ;
291 { printf (" %-30s : %s ", test, filename) ;
292 count = 24 - strlen (filename) ;
300 } /* print_test_name */
303 dump_data_to_file (const char *filename, const void *data, unsigned int datalen)
306 if ((file = fopen (filename, "wb")) == NULL)
307 { printf ("\n\nLine %d : could not open file : %s\n\n", __LINE__, filename) ;
311 if (fwrite (data, 1, datalen, file) != datalen)
312 { printf ("\n\nLine %d : fwrite failed.\n\n", __LINE__) ;
318 } /* dump_data_to_file */
320 /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
323 static char octfilename [] = "error.dat" ;
327 oct_save_[+ (get "io_element") +] (const [+ (get "io_element") +] *a, const [+ (get "io_element") +] *b, int len)
331 if (! (file = fopen (octfilename, "w")))
334 fprintf (file, "# Not created by Octave\n") ;
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") ;
341 for (k = 0 ; k < len ; k++)
342 fprintf (file, [+ (get "format_str") +] "\n", a [k]) ;
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") ;
349 for (k = 0 ; k < len ; k++)
350 fprintf (file, [+ (get "format_str") +] "\n", b [k]) ;
354 } /* oct_save_[+ (get "io_element") +] */
359 check_log_buffer_or_die (SNDFILE *file, int line_num)
360 { static char buffer [LOG_BUFFER_SIZE] ;
363 memset (buffer, 0, sizeof (buffer)) ;
365 /* Get the log buffer data. */
366 count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
368 if (LOG_BUFFER_SIZE - count < 2)
369 { printf ("\n\nLine %d : Possible long log buffer.\n", line_num) ;
373 /* Look for "Should" */
374 if (strstr (buffer, "ould"))
375 { printf ("\n\nLine %d : Log buffer contains `ould'. Dumping.\n", line_num) ;
381 if (strstr (buffer, "*"))
382 { printf ("\n\nLine %d : Log buffer contains `*'. Dumping.\n", line_num) ;
387 /* Look for "Should" */
388 if (strstr (buffer, "nknown marker"))
389 { printf ("\n\nLine %d : Log buffer contains `nknown marker'. Dumping.\n", line_num) ;
395 } /* check_log_buffer_or_die */
398 string_in_log_buffer (SNDFILE *file, const char *s)
399 { static char buffer [LOG_BUFFER_SIZE] ;
402 memset (buffer, 0, sizeof (buffer)) ;
404 /* Get the log buffer data. */
405 count = sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
407 if (LOG_BUFFER_SIZE - count < 2)
408 { printf ("Possible long log buffer.\n") ;
412 /* Look for string */
413 return strstr (buffer, s) ? SF_TRUE : SF_FALSE ;
414 } /* string_in_log_buffer */
417 hexdump_file (const char * filename, sf_count_t offset, sf_count_t length)
421 int k, m, ch, readcount ;
423 if (length > 1000000)
424 { printf ("\n\nError : length (%ld) too long.\n\n", SF_COUNT_TO_LONG (offset)) ;
428 if ((file = fopen (filename, "r")) == NULL)
429 { printf ("\n\nError : hexdump_file (%s) could not open file for read.\n\n", filename) ;
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)) ;
440 for (k = 0 ; k < length ; k+= sizeof (buffer))
441 { readcount = fread (buffer, 1, sizeof (buffer), file) ;
443 printf ("%08lx : ", SF_COUNT_TO_LONG (offset + k)) ;
445 for (m = 0 ; m < readcount ; m++)
446 printf ("%02x ", buffer [m] & 0xFF) ;
448 for (m = readcount ; m < SIGNED_SIZEOF (buffer) ; m++)
452 for (m = 0 ; m < readcount ; m++)
453 { ch = isprint (buffer [m]) ? buffer [m] : '.' ;
457 if (readcount < SIGNED_SIZEOF (buffer))
469 dump_log_buffer (SNDFILE *file)
470 { static char buffer [LOG_BUFFER_SIZE] ;
472 memset (buffer, 0, sizeof (buffer)) ;
474 /* Get the log buffer data. */
475 sf_command (file, SFC_GET_LOG_INFO, buffer, LOG_BUFFER_SIZE) ;
477 if (strlen (buffer) < 1)
478 puts ("Log buffer empty.\n") ;
483 } /* dump_log_buffer */
486 test_open_file_or_die (const char *filename, int mode, SF_INFO *sfinfo, int allow_fd, int line_num)
487 { static int count = 0 ;
490 const char *modestr, *func_name ;
491 int oflags = 0, omode = 0, err ;
494 ** Need to test both sf_open() and sf_open_fd().
495 ** Do so alternately.
499 modestr = "SFM_READ" ;
500 oflags = O_RDONLY | O_BINARY ;
505 modestr = "SFM_WRITE" ;
506 oflags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ;
507 omode = S_IRUSR | S_IWUSR | S_IRGRP ;
511 modestr = "SFM_RDWR" ;
512 oflags = O_RDWR | O_CREAT | O_BINARY ;
513 omode = S_IRUSR | S_IWUSR | S_IRGRP ;
516 printf ("\n\nLine %d: Bad mode.\n", line_num) ;
522 { /* Windows does not understand and ignores the S_IRGRP flag, but Wine
523 ** gives a run time warning message, so just clear it.
528 if (allow_fd && ((++count) & 1) == 1)
531 /* Only use the three argument open() function if omode != 0. */
532 fd = (omode == 0) ? open (filename, oflags) : open (filename, oflags, omode) ;
535 { printf ("\n\n%s : open failed : %s\n", __func__, strerror (errno)) ;
539 func_name = "sf_open_fd" ;
540 file = sf_open_fd (fd, mode, sfinfo, SF_TRUE) ;
543 { func_name = "sf_open" ;
544 file = sf_open (filename, mode, sfinfo) ;
548 { printf ("\n\nLine %d: %s (%s) failed : %s\n\n", line_num, func_name, modestr, sf_strerror (NULL)) ;
549 dump_log_buffer (file) ;
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) ;
561 } /* test_open_file_or_die */
564 test_read_write_position_or_die (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos)
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) ;
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)) ;
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) ;
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)) ;
587 } /* test_read_write_position */
590 test_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 ;
596 whence_name = "SEEK_SET" ;
599 whence_name = "SEEK_CUR" ;
602 whence_name = "SEEK_END" ;
606 case SEEK_SET | SFM_READ :
607 whence_name = "SFM_READ | SEEK_SET" ;
609 case SEEK_CUR | SFM_READ :
610 whence_name = "SFM_READ | SEEK_CUR" ;
612 case SEEK_END | SFM_READ :
613 whence_name = "SFM_READ | SEEK_END" ;
617 case SEEK_SET | SFM_WRITE :
618 whence_name = "SFM_WRITE | SEEK_SET" ;
620 case SEEK_CUR | SFM_WRITE :
621 whence_name = "SFM_WRITE | SEEK_CUR" ;
623 case SEEK_END | SFM_WRITE :
624 whence_name = "SFM_WRITE | SEEK_END" ;
628 printf ("\n\nLine %d: bad whence parameter.\n", line_num) ;
632 channel_name = (channels == 1) ? "Mono" : "Stereo" ;
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)) ;
641 } /* test_seek_or_die */
646 test_[+ (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)
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) ;
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)) ;
656 puts (sf_strerror (file)) ;
661 } /* test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die */
662 [+ ENDFOR io_type +][+ ENDFOR read_op +]
665 test_read_raw_or_die (SNDFILE *file, int pass, void *test, sf_count_t items, int line_num)
668 if ((count = sf_read_raw (file, test, items)) != items)
669 { printf ("\n\nLine %d", line_num) ;
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)) ;
675 puts (sf_strerror (file)) ;
680 } /* test_read_raw_or_die */
685 test_[+ (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)
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) ;
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)) ;
695 puts (sf_strerror (file)) ;
700 } /* test_[+ (get "op_element") +]_[+ (get "io_element") +]_or_die */
701 [+ ENDFOR io_type +][+ ENDFOR write_op +]
704 test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num)
707 if ((count = sf_write_raw (file, test, items)) != items)
708 { printf ("\n\nLine %d", line_num) ;
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)) ;
714 puts (sf_strerror (file)) ;
719 } /* test_write_raw_or_die */
724 compare_[+ (get "io_element") +]_or_die (const [+ (get "io_element") +] *left, const [+ (get "io_element") +] *right, unsigned count, int line_num)
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]) ;
735 } /* compare_[+ (get "io_element") +]_or_die */
740 delete_file (int format, const char *filename)
741 { char rsrc_name [512], *fname ;
745 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_SD2)
749 ** Now try for a resource fork stored as a separate file.
750 ** Grab the un-adulterated filename again.
752 snprintf (rsrc_name, sizeof (rsrc_name), "%s", filename) ;
754 if ((fname = strrchr (rsrc_name, '/')) != NULL)
756 else if ((fname = strrchr (rsrc_name, '\\')) != NULL)
761 memmove (fname + 2, fname, strlen (fname) + 1) ;
768 static int allowed_open_files = -1 ;
771 count_open_files (void)
777 struct stat statbuf ;
779 if (allowed_open_files > 0)
782 for (k = 0 ; k < 1024 ; k++)
783 if (fstat (k, &statbuf) == 0)
786 allowed_open_files = count ;
788 } /* count_open_files */
791 increment_open_file_count (void)
792 { allowed_open_files ++ ;
793 } /* increment_open_file_count */
796 check_open_file_count_or_die (int lineno)
803 struct stat statbuf ;
805 if (allowed_open_files < 0)
806 count_open_files () ;
808 for (k = 0 ; k < 1024 ; k++)
809 if (fstat (k, &statbuf) == 0)
812 if (count > allowed_open_files)
813 { printf ("\nLine %d : number of open files (%d) > allowed (%d).\n\n", lineno, count, allowed_open_files) ;
817 } /* check_open_file_count_or_die */
820 write_mono_file (const char * filename, int format, int srate, float * output, int len)
824 memset (&sfinfo, 0, sizeof (sfinfo)) ;
826 sfinfo.samplerate = srate ;
827 sfinfo.channels = 1 ;
828 sfinfo.format = format ;
830 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
831 { printf ("sf_open (%s) : %s\n", filename, sf_strerror (NULL)) ;
835 sf_write_float (file, output, len) ;
838 } /* write_mono_file */
841 gen_lowpass_noise_float (float *data, int len)
842 { int32_t value = 0x1243456 ;
843 double sample, last_val = 0.0 ;
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 ;
852 sample = value / (0x7fffffff * 1.000001) ;
853 sample = 0.2 * sample - 0.9 * last_val ;
855 data [k] = last_val = sample ;
858 } /* gen_lowpass_noise_float */
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.
868 file_length (const char * fname)
871 if (stat (fname, &data) != 0)
874 return (sf_count_t) data.st_size ;
878 file_length_fd (int fd)
881 memset (&data, 0, sizeof (data)) ;
882 if (fstat (fd, &data) != 0)
885 return (sf_count_t) data.st_size ;
886 } /* file_length_fd */