annotate src/libsndfile-1.0.25/tests/utils.c @ 169:223a55898ab9 tip default

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