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