2 ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
20 ** Utility functions to make writing the test suite easier.
22 ** The .c and .h files were generated automagically with Autogen from
23 ** the files utils.def and utils.tpl.
38 #if (HAVE_DECL_S_IRGRP == 0)
39 #include <sf_unistd.h>
54 #define M_PI 3.14159265358979323846264338
57 #define LOG_BUFFER_SIZE 2048
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.
70 gen_windowed_sine_float (float *data
, int len
, double maximum
)
73 memset (data
, 0, len
* sizeof (float)) ;
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.
82 for (k
= 0 ; k
< len
; k
++)
83 { data
[k
] = sin (2.0 * k
* M_PI
* 1.0 / 32.0 + 0.4) ;
85 /* Apply Hanning Window. */
86 data
[k
] *= maximum
* (0.5 - 0.5 * cos (2.0 * M_PI
* k
/ ((len
) - 1))) ;
90 } /* gen_windowed_sine_float */
93 gen_windowed_sine_double (double *data
, int len
, double maximum
)
96 memset (data
, 0, len
* sizeof (double)) ;
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.
105 for (k
= 0 ; k
< len
; k
++)
106 { data
[k
] = sin (2.0 * k
* M_PI
* 1.0 / 32.0 + 0.4) ;
108 /* Apply Hanning Window. */
109 data
[k
] *= maximum
* (0.5 - 0.5 * cos (2.0 * M_PI
* k
/ ((len
) - 1))) ;
113 } /* gen_windowed_sine_double */
117 create_short_sndfile (const char *filename
, int format
, int channels
)
118 { short data
[2 * 3 * 4 * 5 * 6 * 7] = { 0, } ;
122 sfinfo
.samplerate
= 44100 ;
123 sfinfo
.channels
= channels
;
124 sfinfo
.format
= format
;
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
)) ;
131 sf_write_short (file
, data
, ARRAY_LEN (data
)) ;
134 } /* create_short_sndfile */
137 check_file_hash_or_die (const char *filename
, uint64_t target_hash
, int line_num
)
138 { static unsigned char buf
[4096] ;
143 memset (buf
, 0, sizeof (buf
)) ;
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
) ;
153 while ((read_count
= fread (buf
, 1, sizeof (buf
), file
)))
154 for (k
= 0 ; k
< read_count
; k
++)
155 cksum
= cksum
* 511 + buf
[k
] ;
159 if (target_hash
== 0)
160 { printf (" 0x%016" PRIx64
"\n", cksum
) ;
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
) ;
170 } /* check_file_hash_or_die */
173 print_test_name (const char *test
, const char *filename
)
177 { printf (__FILE__
": bad test of filename parameter.\n") ;
181 if (filename
== NULL
|| strlen (filename
) == 0)
182 { printf (" %-30s : ", test
) ;
186 { printf (" %-30s : %s ", test
, filename
) ;
187 count
= 24 - strlen (filename
) ;
195 } /* print_test_name */
198 dump_data_to_file (const char *filename
, const void *data
, unsigned int datalen
)
201 if ((file
= fopen (filename
, "wb")) == NULL
)
202 { printf ("\n\nLine %d : could not open file : %s\n\n", __LINE__
, filename
) ;
206 if (fwrite (data
, 1, datalen
, file
) != datalen
)
207 { printf ("\n\nLine %d : fwrite failed.\n\n", __LINE__
) ;
213 } /* dump_data_to_file */
215 /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
218 static char octfilename
[] = "error.dat" ;
221 oct_save_short (const short *a
, const short *b
, int len
)
225 if (! (file
= fopen (octfilename
, "w")))
228 fprintf (file
, "# Not created by Octave\n") ;
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") ;
235 for (k
= 0 ; k
< len
; k
++)
236 fprintf (file
, "% d" "\n", a
[k
]) ;
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") ;
243 for (k
= 0 ; k
< len
; k
++)
244 fprintf (file
, "% d" "\n", b
[k
]) ;
248 } /* oct_save_short */
250 oct_save_int (const int *a
, const int *b
, int len
)
254 if (! (file
= fopen (octfilename
, "w")))
257 fprintf (file
, "# Not created by Octave\n") ;
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") ;
264 for (k
= 0 ; k
< len
; k
++)
265 fprintf (file
, "% d" "\n", a
[k
]) ;
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") ;
272 for (k
= 0 ; k
< len
; k
++)
273 fprintf (file
, "% d" "\n", b
[k
]) ;
279 oct_save_float (const float *a
, const float *b
, int len
)
283 if (! (file
= fopen (octfilename
, "w")))
286 fprintf (file
, "# Not created by Octave\n") ;
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") ;
293 for (k
= 0 ; k
< len
; k
++)
294 fprintf (file
, "% g" "\n", a
[k
]) ;
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") ;
301 for (k
= 0 ; k
< len
; k
++)
302 fprintf (file
, "% g" "\n", b
[k
]) ;
306 } /* oct_save_float */
308 oct_save_double (const double *a
, const double *b
, int len
)
312 if (! (file
= fopen (octfilename
, "w")))
315 fprintf (file
, "# Not created by Octave\n") ;
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") ;
322 for (k
= 0 ; k
< len
; k
++)
323 fprintf (file
, "% g" "\n", a
[k
]) ;
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") ;
330 for (k
= 0 ; k
< len
; k
++)
331 fprintf (file
, "% g" "\n", b
[k
]) ;
335 } /* oct_save_double */
339 check_log_buffer_or_die (SNDFILE
*file
, int line_num
)
340 { static char buffer
[LOG_BUFFER_SIZE
] ;
343 memset (buffer
, 0, sizeof (buffer
)) ;
345 /* Get the log buffer data. */
346 count
= sf_command (file
, SFC_GET_LOG_INFO
, buffer
, LOG_BUFFER_SIZE
) ;
348 if (LOG_BUFFER_SIZE
- count
< 2)
349 { printf ("\n\nLine %d : Possible long log buffer.\n", line_num
) ;
353 /* Look for "Should" */
354 if (strstr (buffer
, "ould"))
355 { printf ("\n\nLine %d : Log buffer contains `ould'. Dumping.\n", line_num
) ;
361 if (strstr (buffer
, "*"))
362 { printf ("\n\nLine %d : Log buffer contains `*'. Dumping.\n", line_num
) ;
367 /* Look for "Should" */
368 if (strstr (buffer
, "nknown marker"))
369 { printf ("\n\nLine %d : Log buffer contains `nknown marker'. Dumping.\n", line_num
) ;
375 } /* check_log_buffer_or_die */
378 string_in_log_buffer (SNDFILE
*file
, const char *s
)
379 { static char buffer
[LOG_BUFFER_SIZE
] ;
382 memset (buffer
, 0, sizeof (buffer
)) ;
384 /* Get the log buffer data. */
385 count
= sf_command (file
, SFC_GET_LOG_INFO
, buffer
, LOG_BUFFER_SIZE
) ;
387 if (LOG_BUFFER_SIZE
- count
< 2)
388 { printf ("Possible long log buffer.\n") ;
392 /* Look for string */
393 return strstr (buffer
, s
) ? SF_TRUE
: SF_FALSE
;
394 } /* string_in_log_buffer */
397 hexdump_file (const char * filename
, sf_count_t offset
, sf_count_t length
)
401 int k
, m
, ch
, readcount
;
403 if (length
> 1000000)
404 { printf ("\n\nError : length (%ld) too long.\n\n", SF_COUNT_TO_LONG (offset
)) ;
408 if ((file
= fopen (filename
, "r")) == NULL
)
409 { printf ("\n\nError : hexdump_file (%s) could not open file for read.\n\n", filename
) ;
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
)) ;
420 for (k
= 0 ; k
< length
; k
+= sizeof (buffer
))
421 { readcount
= fread (buffer
, 1, sizeof (buffer
), file
) ;
423 printf ("%08lx : ", SF_COUNT_TO_LONG (offset
+ k
)) ;
425 for (m
= 0 ; m
< readcount
; m
++)
426 printf ("%02x ", buffer
[m
] & 0xFF) ;
428 for (m
= readcount
; m
< SIGNED_SIZEOF (buffer
) ; m
++)
432 for (m
= 0 ; m
< readcount
; m
++)
433 { ch
= isprint (buffer
[m
]) ? buffer
[m
] : '.' ;
437 if (readcount
< SIGNED_SIZEOF (buffer
))
449 dump_log_buffer (SNDFILE
*file
)
450 { static char buffer
[LOG_BUFFER_SIZE
] ;
452 memset (buffer
, 0, sizeof (buffer
)) ;
454 /* Get the log buffer data. */
455 sf_command (file
, SFC_GET_LOG_INFO
, buffer
, LOG_BUFFER_SIZE
) ;
457 if (strlen (buffer
) < 1)
458 puts ("Log buffer empty.\n") ;
463 } /* dump_log_buffer */
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 ;
470 const char *modestr
, *func_name
;
471 int oflags
= 0, omode
= 0, err
;
474 ** Need to test both sf_open() and sf_open_fd().
475 ** Do so alternately.
479 modestr
= "SFM_READ" ;
480 oflags
= O_RDONLY
| O_BINARY
;
485 modestr
= "SFM_WRITE" ;
486 oflags
= O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
;
487 omode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
491 modestr
= "SFM_RDWR" ;
492 oflags
= O_RDWR
| O_CREAT
| O_BINARY
;
493 omode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
496 printf ("\n\nLine %d: Bad mode.\n", line_num
) ;
502 { /* Windows does not understand and ignores the S_IRGRP flag, but Wine
503 ** gives a run time warning message, so just clear it.
508 if (allow_fd
&& ((++count
) & 1) == 1)
511 /* Only use the three argument open() function if omode != 0. */
512 fd
= (omode
== 0) ? open (filename
, oflags
) : open (filename
, oflags
, omode
) ;
515 { printf ("\n\n%s : open failed : %s\n", __func__
, strerror (errno
)) ;
519 func_name
= "sf_open_fd" ;
520 file
= sf_open_fd (fd
, mode
, sfinfo
, SF_TRUE
) ;
523 { func_name
= "sf_open" ;
524 file
= sf_open (filename
, mode
, sfinfo
) ;
528 { printf ("\n\nLine %d: %s (%s) failed : %s\n\n", line_num
, func_name
, modestr
, sf_strerror (NULL
)) ;
529 dump_log_buffer (file
) ;
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
) ;
541 } /* test_open_file_or_die */
544 test_read_write_position_or_die (SNDFILE
*file
, int line_num
, int pass
, sf_count_t read_pos
, sf_count_t write_pos
)
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
) ;
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
)) ;
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
) ;
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
)) ;
567 } /* test_read_write_position */
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
;
576 whence_name
= "SEEK_SET" ;
579 whence_name
= "SEEK_CUR" ;
582 whence_name
= "SEEK_END" ;
586 case SEEK_SET
| SFM_READ
:
587 whence_name
= "SFM_READ | SEEK_SET" ;
589 case SEEK_CUR
| SFM_READ
:
590 whence_name
= "SFM_READ | SEEK_CUR" ;
592 case SEEK_END
| SFM_READ
:
593 whence_name
= "SFM_READ | SEEK_END" ;
597 case SEEK_SET
| SFM_WRITE
:
598 whence_name
= "SFM_WRITE | SEEK_SET" ;
600 case SEEK_CUR
| SFM_WRITE
:
601 whence_name
= "SFM_WRITE | SEEK_CUR" ;
603 case SEEK_END
| SFM_WRITE
:
604 whence_name
= "SFM_WRITE | SEEK_END" ;
608 printf ("\n\nLine %d: bad whence parameter.\n", line_num
) ;
612 channel_name
= (channels
== 1) ? "Mono" : "Stereo" ;
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
)) ;
621 } /* test_seek_or_die */
626 test_read_short_or_die (SNDFILE
*file
, int pass
, short *test
, sf_count_t items
, int line_num
)
629 if ((count
= sf_read_short (file
, test
, items
)) != items
)
630 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
636 puts (sf_strerror (file
)) ;
641 } /* test_read_short_or_die */
644 test_read_int_or_die (SNDFILE
*file
, int pass
, int *test
, sf_count_t items
, int line_num
)
647 if ((count
= sf_read_int (file
, test
, items
)) != items
)
648 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
654 puts (sf_strerror (file
)) ;
659 } /* test_read_int_or_die */
662 test_read_float_or_die (SNDFILE
*file
, int pass
, float *test
, sf_count_t items
, int line_num
)
665 if ((count
= sf_read_float (file
, test
, items
)) != items
)
666 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
672 puts (sf_strerror (file
)) ;
677 } /* test_read_float_or_die */
680 test_read_double_or_die (SNDFILE
*file
, int pass
, double *test
, sf_count_t items
, int line_num
)
683 if ((count
= sf_read_double (file
, test
, items
)) != items
)
684 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
690 puts (sf_strerror (file
)) ;
695 } /* test_read_double_or_die */
699 test_readf_short_or_die (SNDFILE
*file
, int pass
, short *test
, sf_count_t frames
, int line_num
)
702 if ((count
= sf_readf_short (file
, test
, frames
)) != frames
)
703 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
709 puts (sf_strerror (file
)) ;
714 } /* test_readf_short_or_die */
717 test_readf_int_or_die (SNDFILE
*file
, int pass
, int *test
, sf_count_t frames
, int line_num
)
720 if ((count
= sf_readf_int (file
, test
, frames
)) != frames
)
721 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
727 puts (sf_strerror (file
)) ;
732 } /* test_readf_int_or_die */
735 test_readf_float_or_die (SNDFILE
*file
, int pass
, float *test
, sf_count_t frames
, int line_num
)
738 if ((count
= sf_readf_float (file
, test
, frames
)) != frames
)
739 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
745 puts (sf_strerror (file
)) ;
750 } /* test_readf_float_or_die */
753 test_readf_double_or_die (SNDFILE
*file
, int pass
, double *test
, sf_count_t frames
, int line_num
)
756 if ((count
= sf_readf_double (file
, test
, frames
)) != frames
)
757 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
763 puts (sf_strerror (file
)) ;
768 } /* test_readf_double_or_die */
772 test_read_raw_or_die (SNDFILE
*file
, int pass
, void *test
, sf_count_t items
, int line_num
)
775 if ((count
= sf_read_raw (file
, test
, items
)) != items
)
776 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
782 puts (sf_strerror (file
)) ;
787 } /* test_read_raw_or_die */
792 test_write_short_or_die (SNDFILE
*file
, int pass
, const short *test
, sf_count_t items
, int line_num
)
795 if ((count
= sf_write_short (file
, test
, items
)) != items
)
796 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
802 puts (sf_strerror (file
)) ;
807 } /* test_write_short_or_die */
810 test_write_int_or_die (SNDFILE
*file
, int pass
, const int *test
, sf_count_t items
, int line_num
)
813 if ((count
= sf_write_int (file
, test
, items
)) != items
)
814 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
820 puts (sf_strerror (file
)) ;
825 } /* test_write_int_or_die */
828 test_write_float_or_die (SNDFILE
*file
, int pass
, const float *test
, sf_count_t items
, int line_num
)
831 if ((count
= sf_write_float (file
, test
, items
)) != items
)
832 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
838 puts (sf_strerror (file
)) ;
843 } /* test_write_float_or_die */
846 test_write_double_or_die (SNDFILE
*file
, int pass
, const double *test
, sf_count_t items
, int line_num
)
849 if ((count
= sf_write_double (file
, test
, items
)) != items
)
850 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
856 puts (sf_strerror (file
)) ;
861 } /* test_write_double_or_die */
865 test_writef_short_or_die (SNDFILE
*file
, int pass
, const short *test
, sf_count_t frames
, int line_num
)
868 if ((count
= sf_writef_short (file
, test
, frames
)) != frames
)
869 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
875 puts (sf_strerror (file
)) ;
880 } /* test_writef_short_or_die */
883 test_writef_int_or_die (SNDFILE
*file
, int pass
, const int *test
, sf_count_t frames
, int line_num
)
886 if ((count
= sf_writef_int (file
, test
, frames
)) != frames
)
887 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
893 puts (sf_strerror (file
)) ;
898 } /* test_writef_int_or_die */
901 test_writef_float_or_die (SNDFILE
*file
, int pass
, const float *test
, sf_count_t frames
, int line_num
)
904 if ((count
= sf_writef_float (file
, test
, frames
)) != frames
)
905 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
911 puts (sf_strerror (file
)) ;
916 } /* test_writef_float_or_die */
919 test_writef_double_or_die (SNDFILE
*file
, int pass
, const double *test
, sf_count_t frames
, int line_num
)
922 if ((count
= sf_writef_double (file
, test
, frames
)) != frames
)
923 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
929 puts (sf_strerror (file
)) ;
934 } /* test_writef_double_or_die */
938 test_write_raw_or_die (SNDFILE
*file
, int pass
, const void *test
, sf_count_t items
, int line_num
)
941 if ((count
= sf_write_raw (file
, test
, items
)) != items
)
942 { printf ("\n\nLine %d", line_num
) ;
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
)) ;
948 puts (sf_strerror (file
)) ;
953 } /* test_write_raw_or_die */
957 compare_short_or_die (const short *left
, const short *right
, unsigned count
, int line_num
)
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
]) ;
968 } /* compare_short_or_die */
970 compare_int_or_die (const int *left
, const int *right
, unsigned count
, int line_num
)
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
]) ;
981 } /* compare_int_or_die */
983 compare_float_or_die (const float *left
, const float *right
, unsigned count
, int line_num
)
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
]) ;
994 } /* compare_float_or_die */
996 compare_double_or_die (const double *left
, const double *right
, unsigned count
, int line_num
)
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
]) ;
1007 } /* compare_double_or_die */
1012 delete_file (int format
, const char *filename
)
1013 { char rsrc_name
[512], *fname
;
1017 if ((format
& SF_FORMAT_TYPEMASK
) != SF_FORMAT_SD2
)
1021 ** Now try for a resource fork stored as a separate file.
1022 ** Grab the un-adulterated filename again.
1024 snprintf (rsrc_name
, sizeof (rsrc_name
), "%s", filename
) ;
1026 if ((fname
= strrchr (rsrc_name
, '/')) != NULL
)
1028 else if ((fname
= strrchr (rsrc_name
, '\\')) != NULL
)
1033 memmove (fname
+ 2, fname
, strlen (fname
) + 1) ;
1037 unlink (rsrc_name
) ;
1040 static int allowed_open_files
= -1 ;
1043 count_open_files (void)
1049 struct stat statbuf
;
1051 if (allowed_open_files
> 0)
1054 for (k
= 0 ; k
< 1024 ; k
++)
1055 if (fstat (k
, &statbuf
) == 0)
1058 allowed_open_files
= count
;
1060 } /* count_open_files */
1063 increment_open_file_count (void)
1064 { allowed_open_files
++ ;
1065 } /* increment_open_file_count */
1068 check_open_file_count_or_die (int lineno
)
1075 struct stat statbuf
;
1077 if (allowed_open_files
< 0)
1078 count_open_files () ;
1080 for (k
= 0 ; k
< 1024 ; k
++)
1081 if (fstat (k
, &statbuf
) == 0)
1084 if (count
> allowed_open_files
)
1085 { printf ("\nLine %d : number of open files (%d) > allowed (%d).\n\n", lineno
, count
, allowed_open_files
) ;
1089 } /* check_open_file_count_or_die */
1092 write_mono_file (const char * filename
, int format
, int srate
, float * output
, int len
)
1096 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
1098 sfinfo
.samplerate
= srate
;
1099 sfinfo
.channels
= 1 ;
1100 sfinfo
.format
= format
;
1102 if ((file
= sf_open (filename
, SFM_WRITE
, &sfinfo
)) == NULL
)
1103 { printf ("sf_open (%s) : %s\n", filename
, sf_strerror (NULL
)) ;
1107 sf_write_float (file
, output
, len
) ;
1110 } /* write_mono_file */
1113 gen_lowpass_noise_float (float *data
, int len
)
1114 { int32_t value
= 0x1243456 ;
1115 double sample
, last_val
= 0.0 ;
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 ;
1124 sample
= value
/ (0x7fffffff * 1.000001) ;
1125 sample
= 0.2 * sample
- 0.9 * last_val
;
1127 data
[k
] = last_val
= sample
;
1130 } /* gen_lowpass_noise_float */
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.
1140 file_length (const char * fname
)
1141 { struct stat data
;
1143 if (stat (fname
, &data
) != 0)
1146 return (sf_count_t
) data
.st_size
;
1150 file_length_fd (int fd
)
1151 { struct stat data
;
1153 memset (&data
, 0, sizeof (data
)) ;
1154 if (fstat (fd
, &data
) != 0)
1157 return (sf_count_t
) data
.st_size
;
1158 } /* file_length_fd */