annotate src/libsndfile-1.0.25/tests/command_test.c @ 85:545efbb81310

Import initial set of sources
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 18 Mar 2013 14:12:14 +0000
parents
children
rev   line source
cannam@85 1 /*
cannam@85 2 ** Copyright (C) 2001-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 #include "sfconfig.h"
cannam@85 20
cannam@85 21 #include <stdio.h>
cannam@85 22 #include <stdlib.h>
cannam@85 23 #include <string.h>
cannam@85 24 #include <time.h>
cannam@85 25
cannam@85 26 #if HAVE_UNISTD_H
cannam@85 27 #include <unistd.h>
cannam@85 28 #endif
cannam@85 29
cannam@85 30 #include <math.h>
cannam@85 31
cannam@85 32 #include <sndfile.h>
cannam@85 33
cannam@85 34 #include "utils.h"
cannam@85 35
cannam@85 36 #define BUFFER_LEN (1<<10)
cannam@85 37 #define LOG_BUFFER_SIZE 1024
cannam@85 38
cannam@85 39 static void float_norm_test (const char *filename) ;
cannam@85 40 static void double_norm_test (const char *filename) ;
cannam@85 41 static void format_tests (void) ;
cannam@85 42 static void calc_peak_test (int filetype, const char *filename) ;
cannam@85 43 static void truncate_test (const char *filename, int filetype) ;
cannam@85 44 static void instrument_test (const char *filename, int filetype) ;
cannam@85 45 static void channel_map_test (const char *filename, int filetype) ;
cannam@85 46 static void current_sf_info_test (const char *filename) ;
cannam@85 47 static void raw_needs_endswap_test (const char *filename, int filetype) ;
cannam@85 48
cannam@85 49 static void broadcast_test (const char *filename, int filetype) ;
cannam@85 50 static void broadcast_rdwr_test (const char *filename, int filetype) ;
cannam@85 51 static void broadcast_coding_history_test (const char *filename) ;
cannam@85 52 static void broadcast_coding_history_size (const char *filename) ;
cannam@85 53
cannam@85 54 /* Force the start of this buffer to be double aligned. Sparc-solaris will
cannam@85 55 ** choke if its not.
cannam@85 56 */
cannam@85 57
cannam@85 58 static int int_data [BUFFER_LEN] ;
cannam@85 59 static float float_data [BUFFER_LEN] ;
cannam@85 60 static double double_data [BUFFER_LEN] ;
cannam@85 61
cannam@85 62 int
cannam@85 63 main (int argc, char *argv [])
cannam@85 64 { int do_all = 0 ;
cannam@85 65 int test_count = 0 ;
cannam@85 66
cannam@85 67 if (argc != 2)
cannam@85 68 { printf ("Usage : %s <test>\n", argv [0]) ;
cannam@85 69 printf (" Where <test> is one of the following:\n") ;
cannam@85 70 printf (" ver - test sf_command (SFC_GETLIB_VERSION)\n") ;
cannam@85 71 printf (" norm - test floating point normalisation\n") ;
cannam@85 72 printf (" format - test format string commands\n") ;
cannam@85 73 printf (" peak - test peak calculation\n") ;
cannam@85 74 printf (" trunc - test file truncation\n") ;
cannam@85 75 printf (" inst - test set/get of SF_INSTRUMENT.\n") ;
cannam@85 76 printf (" chanmap - test set/get of channel map data..\n") ;
cannam@85 77 printf (" bext - test set/get of SF_BROADCAST_INFO.\n") ;
cannam@85 78 printf (" bextch - test set/get of SF_BROADCAST_INFO coding_history.\n") ;
cannam@85 79 printf (" rawend - test SFC_RAW_NEEDS_ENDSWAP.\n") ;
cannam@85 80 printf (" all - perform all tests\n") ;
cannam@85 81 exit (1) ;
cannam@85 82 } ;
cannam@85 83
cannam@85 84 do_all =! strcmp (argv [1], "all") ;
cannam@85 85
cannam@85 86 if (do_all || strcmp (argv [1], "ver") == 0)
cannam@85 87 { char buffer [128] ;
cannam@85 88
cannam@85 89 print_test_name ("version_test", "(none)") ;
cannam@85 90 buffer [0] = 0 ;
cannam@85 91 sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ;
cannam@85 92 if (strlen (buffer) < 1)
cannam@85 93 { printf ("Line %d: could not retrieve lib version.\n", __LINE__) ;
cannam@85 94 exit (1) ;
cannam@85 95 } ;
cannam@85 96 puts ("ok") ;
cannam@85 97 test_count ++ ;
cannam@85 98 } ;
cannam@85 99
cannam@85 100 if (do_all || strcmp (argv [1], "norm") == 0)
cannam@85 101 { /* Preliminary float/double normalisation tests. More testing
cannam@85 102 ** is done in the program 'floating_point_test'.
cannam@85 103 */
cannam@85 104 float_norm_test ("float.wav") ;
cannam@85 105 double_norm_test ("double.wav") ;
cannam@85 106 test_count ++ ;
cannam@85 107 } ;
cannam@85 108
cannam@85 109 if (do_all || strcmp (argv [1], "peak") == 0)
cannam@85 110 { calc_peak_test (SF_ENDIAN_BIG | SF_FORMAT_RAW, "be-peak.raw") ;
cannam@85 111 calc_peak_test (SF_ENDIAN_LITTLE | SF_FORMAT_RAW, "le-peak.raw") ;
cannam@85 112 test_count ++ ;
cannam@85 113 } ;
cannam@85 114
cannam@85 115 if (do_all || ! strcmp (argv [1], "format"))
cannam@85 116 { format_tests () ;
cannam@85 117 test_count ++ ;
cannam@85 118 } ;
cannam@85 119
cannam@85 120 if (do_all || strcmp (argv [1], "trunc") == 0)
cannam@85 121 { truncate_test ("truncate.raw", SF_FORMAT_RAW | SF_FORMAT_PCM_32) ;
cannam@85 122 truncate_test ("truncate.au" , SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
cannam@85 123 test_count ++ ;
cannam@85 124 } ;
cannam@85 125
cannam@85 126 if (do_all || strcmp (argv [1], "inst") == 0)
cannam@85 127 { instrument_test ("instrument.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@85 128 instrument_test ("instrument.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_24) ;
cannam@85 129 /*-instrument_test ("instrument.xi", SF_FORMAT_XI | SF_FORMAT_DPCM_16) ;-*/
cannam@85 130 test_count ++ ;
cannam@85 131 } ;
cannam@85 132
cannam@85 133 if (do_all || strcmp (argv [1], "current_sf_info") == 0)
cannam@85 134 { current_sf_info_test ("current.wav") ;
cannam@85 135 test_count ++ ;
cannam@85 136 } ;
cannam@85 137
cannam@85 138 if (do_all || strcmp (argv [1], "bext") == 0)
cannam@85 139 { broadcast_test ("broadcast.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@85 140 broadcast_rdwr_test ("broadcast.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@85 141
cannam@85 142 broadcast_test ("broadcast.wavex", SF_FORMAT_WAVEX | SF_FORMAT_PCM_16) ;
cannam@85 143 broadcast_rdwr_test ("broadcast.wavex", SF_FORMAT_WAVEX | SF_FORMAT_PCM_16) ;
cannam@85 144
cannam@85 145 broadcast_test ("broadcast.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
cannam@85 146 broadcast_rdwr_test ("broadcast.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
cannam@85 147 test_count ++ ;
cannam@85 148 } ;
cannam@85 149
cannam@85 150 if (do_all || strcmp (argv [1], "bextch") == 0)
cannam@85 151 { broadcast_coding_history_test ("coding_history.wav") ;
cannam@85 152 broadcast_coding_history_size ("coding_hist_size.wav") ;
cannam@85 153 test_count ++ ;
cannam@85 154 } ;
cannam@85 155
cannam@85 156 if (do_all || strcmp (argv [1], "chanmap") == 0)
cannam@85 157 { channel_map_test ("chanmap.wavex", SF_FORMAT_WAVEX | SF_FORMAT_PCM_16) ;
cannam@85 158 channel_map_test ("chanmap.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
cannam@85 159 channel_map_test ("chanmap.aifc" , SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
cannam@85 160 channel_map_test ("chanmap.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_16) ;
cannam@85 161 test_count ++ ;
cannam@85 162 } ;
cannam@85 163
cannam@85 164 if (do_all || strcmp (argv [1], "rawend") == 0)
cannam@85 165 { raw_needs_endswap_test ("raw_end.wav", SF_FORMAT_WAV) ;
cannam@85 166 raw_needs_endswap_test ("raw_end.wavex", SF_FORMAT_WAVEX) ;
cannam@85 167 raw_needs_endswap_test ("raw_end.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV) ;
cannam@85 168 raw_needs_endswap_test ("raw_end.aiff", SF_FORMAT_AIFF) ;
cannam@85 169 raw_needs_endswap_test ("raw_end.aiff_le", SF_ENDIAN_LITTLE | SF_FORMAT_AIFF) ;
cannam@85 170 test_count ++ ;
cannam@85 171 } ;
cannam@85 172
cannam@85 173 if (test_count == 0)
cannam@85 174 { printf ("Mono : ************************************\n") ;
cannam@85 175 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
cannam@85 176 printf ("Mono : ************************************\n") ;
cannam@85 177 return 1 ;
cannam@85 178 } ;
cannam@85 179
cannam@85 180 return 0 ;
cannam@85 181 } /* main */
cannam@85 182
cannam@85 183 /*============================================================================================
cannam@85 184 ** Here are the test functions.
cannam@85 185 */
cannam@85 186
cannam@85 187 static void
cannam@85 188 float_norm_test (const char *filename)
cannam@85 189 { SNDFILE *file ;
cannam@85 190 SF_INFO sfinfo ;
cannam@85 191 unsigned int k ;
cannam@85 192
cannam@85 193 print_test_name ("float_norm_test", filename) ;
cannam@85 194
cannam@85 195 sfinfo.samplerate = 44100 ;
cannam@85 196 sfinfo.format = (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;
cannam@85 197 sfinfo.channels = 1 ;
cannam@85 198 sfinfo.frames = BUFFER_LEN ;
cannam@85 199
cannam@85 200 /* Create float_data with all values being less than 1.0. */
cannam@85 201 for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
cannam@85 202 float_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;
cannam@85 203 for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)
cannam@85 204 float_data [k] = (k + 5) ;
cannam@85 205
cannam@85 206 if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
cannam@85 207 { printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
cannam@85 208 fflush (stdout) ;
cannam@85 209 puts (sf_strerror (NULL)) ;
cannam@85 210 exit (1) ;
cannam@85 211 } ;
cannam@85 212
cannam@85 213 /* Normalisation is on by default so no need to do anything here. */
cannam@85 214
cannam@85 215 if ((k = sf_write_float (file, float_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
cannam@85 216 { printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@85 217 exit (1) ;
cannam@85 218 } ;
cannam@85 219
cannam@85 220 /* Turn normalisation off. */
cannam@85 221 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
cannam@85 222
cannam@85 223 if ((k = sf_write_float (file, float_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
cannam@85 224 { printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@85 225 exit (1) ;
cannam@85 226 } ;
cannam@85 227
cannam@85 228 sf_close (file) ;
cannam@85 229
cannam@85 230 /* sfinfo struct should still contain correct data. */
cannam@85 231 if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
cannam@85 232 { printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
cannam@85 233 fflush (stdout) ;
cannam@85 234 puts (sf_strerror (NULL)) ;
cannam@85 235 exit (1) ;
cannam@85 236 } ;
cannam@85 237
cannam@85 238 if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))
cannam@85 239 { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;
cannam@85 240 exit (1) ;
cannam@85 241 } ;
cannam@85 242
cannam@85 243 if (sfinfo.frames != BUFFER_LEN)
cannam@85 244 { printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
cannam@85 245 exit (1) ;
cannam@85 246 } ;
cannam@85 247
cannam@85 248 if (sfinfo.channels != 1)
cannam@85 249 { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 250 exit (1) ;
cannam@85 251 } ;
cannam@85 252
cannam@85 253 /* Read float_data and check that it is normalised (ie default). */
cannam@85 254 if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@85 255 { printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@85 256 exit (1) ;
cannam@85 257 } ;
cannam@85 258
cannam@85 259 for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@85 260 if (float_data [k] >= 1.0)
cannam@85 261 { printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;
cannam@85 262 exit (1) ;
cannam@85 263 } ;
cannam@85 264
cannam@85 265 /* Seek to start of file, turn normalisation off, read float_data and check again. */
cannam@85 266 sf_seek (file, 0, SEEK_SET) ;
cannam@85 267 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
cannam@85 268
cannam@85 269 if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@85 270 { printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@85 271 exit (1) ;
cannam@85 272 } ;
cannam@85 273
cannam@85 274 for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@85 275 if (float_data [k] < 1.0)
cannam@85 276 { printf ("\n\nLine %d: float_data [%d] == %f which is less than 1.0\n", __LINE__, k, float_data [k]) ;
cannam@85 277 exit (1) ;
cannam@85 278 } ;
cannam@85 279
cannam@85 280 /* Seek to start of file, turn normalisation on, read float_data and do final check. */
cannam@85 281 sf_seek (file, 0, SEEK_SET) ;
cannam@85 282 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_TRUE) ;
cannam@85 283
cannam@85 284 if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@85 285 { printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@85 286 exit (1) ;
cannam@85 287 } ;
cannam@85 288
cannam@85 289 for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@85 290 if (float_data [k] > 1.0)
cannam@85 291 { printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;
cannam@85 292 exit (1) ;
cannam@85 293 } ;
cannam@85 294
cannam@85 295
cannam@85 296 sf_close (file) ;
cannam@85 297
cannam@85 298 unlink (filename) ;
cannam@85 299
cannam@85 300 printf ("ok\n") ;
cannam@85 301 } /* float_norm_test */
cannam@85 302
cannam@85 303 static void
cannam@85 304 double_norm_test (const char *filename)
cannam@85 305 { SNDFILE *file ;
cannam@85 306 SF_INFO sfinfo ;
cannam@85 307 unsigned int k ;
cannam@85 308
cannam@85 309 print_test_name ("double_norm_test", filename) ;
cannam@85 310
cannam@85 311 sfinfo.samplerate = 44100 ;
cannam@85 312 sfinfo.format = (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;
cannam@85 313 sfinfo.channels = 1 ;
cannam@85 314 sfinfo.frames = BUFFER_LEN ;
cannam@85 315
cannam@85 316 /* Create double_data with all values being less than 1.0. */
cannam@85 317 for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
cannam@85 318 double_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;
cannam@85 319 for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)
cannam@85 320 double_data [k] = (k + 5) ;
cannam@85 321
cannam@85 322 if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
cannam@85 323 { printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
cannam@85 324 fflush (stdout) ;
cannam@85 325 puts (sf_strerror (NULL)) ;
cannam@85 326 exit (1) ;
cannam@85 327 } ;
cannam@85 328
cannam@85 329 /* Normailsation is on by default so no need to do anything here. */
cannam@85 330 /*-sf_command (file, "set-norm-double", "true", 0) ;-*/
cannam@85 331
cannam@85 332 if ((k = sf_write_double (file, double_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
cannam@85 333 { printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@85 334 exit (1) ;
cannam@85 335 } ;
cannam@85 336
cannam@85 337 /* Turn normalisation off. */
cannam@85 338 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
cannam@85 339
cannam@85 340 if ((k = sf_write_double (file, double_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
cannam@85 341 { printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@85 342 exit (1) ;
cannam@85 343 } ;
cannam@85 344
cannam@85 345 sf_close (file) ;
cannam@85 346
cannam@85 347 if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
cannam@85 348 { printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
cannam@85 349 fflush (stdout) ;
cannam@85 350 puts (sf_strerror (NULL)) ;
cannam@85 351 exit (1) ;
cannam@85 352 } ;
cannam@85 353
cannam@85 354 if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))
cannam@85 355 { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;
cannam@85 356 exit (1) ;
cannam@85 357 } ;
cannam@85 358
cannam@85 359 if (sfinfo.frames != BUFFER_LEN)
cannam@85 360 { printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
cannam@85 361 exit (1) ;
cannam@85 362 } ;
cannam@85 363
cannam@85 364 if (sfinfo.channels != 1)
cannam@85 365 { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 366 exit (1) ;
cannam@85 367 } ;
cannam@85 368
cannam@85 369 /* Read double_data and check that it is normalised (ie default). */
cannam@85 370 if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@85 371 { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@85 372 exit (1) ;
cannam@85 373 } ;
cannam@85 374
cannam@85 375 for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@85 376 if (double_data [k] >= 1.0)
cannam@85 377 { printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ;
cannam@85 378 exit (1) ;
cannam@85 379 } ;
cannam@85 380
cannam@85 381 /* Seek to start of file, turn normalisation off, read double_data and check again. */
cannam@85 382 sf_seek (file, 0, SEEK_SET) ;
cannam@85 383 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
cannam@85 384
cannam@85 385 if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@85 386 { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@85 387 exit (1) ;
cannam@85 388 } ;
cannam@85 389
cannam@85 390 for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@85 391 if (double_data [k] < 1.0)
cannam@85 392 { printf ("\n\nLine %d: double_data [%d] == %f which is less than 1.0\n", __LINE__, k, double_data [k]) ;
cannam@85 393 exit (1) ;
cannam@85 394 } ;
cannam@85 395
cannam@85 396 /* Seek to start of file, turn normalisation on, read double_data and do final check. */
cannam@85 397 sf_seek (file, 0, SEEK_SET) ;
cannam@85 398 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_TRUE) ;
cannam@85 399
cannam@85 400 if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@85 401 { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@85 402 exit (1) ;
cannam@85 403 } ;
cannam@85 404
cannam@85 405 for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@85 406 if (double_data [k] > 1.0)
cannam@85 407 { printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ;
cannam@85 408 exit (1) ;
cannam@85 409 } ;
cannam@85 410
cannam@85 411
cannam@85 412 sf_close (file) ;
cannam@85 413
cannam@85 414 unlink (filename) ;
cannam@85 415
cannam@85 416 printf ("ok\n") ;
cannam@85 417 } /* double_norm_test */
cannam@85 418
cannam@85 419 static void
cannam@85 420 format_tests (void)
cannam@85 421 { SF_FORMAT_INFO format_info ;
cannam@85 422 SF_INFO sfinfo ;
cannam@85 423 const char *last_name ;
cannam@85 424 int k, count ;
cannam@85 425
cannam@85 426 print_test_name ("format_tests", "(null)") ;
cannam@85 427
cannam@85 428 /* Clear out SF_INFO struct and set channels > 0. */
cannam@85 429 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 430 sfinfo.channels = 1 ;
cannam@85 431
cannam@85 432 /* First test simple formats. */
cannam@85 433
cannam@85 434 sf_command (NULL, SFC_GET_SIMPLE_FORMAT_COUNT, &count, sizeof (int)) ;
cannam@85 435
cannam@85 436 if (count < 0 || count > 30)
cannam@85 437 { printf ("Line %d: Weird count.\n", __LINE__) ;
cannam@85 438 exit (1) ;
cannam@85 439 } ;
cannam@85 440
cannam@85 441 format_info.format = 0 ;
cannam@85 442 sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
cannam@85 443
cannam@85 444 last_name = format_info.name ;
cannam@85 445 for (k = 1 ; k < count ; k ++)
cannam@85 446 { format_info.format = k ;
cannam@85 447 sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
cannam@85 448 if (strcmp (last_name, format_info.name) >= 0)
cannam@85 449 { printf ("\n\nLine %d: format names out of sequence `%s' < `%s'.\n", __LINE__, last_name, format_info.name) ;
cannam@85 450 exit (1) ;
cannam@85 451 } ;
cannam@85 452 sfinfo.format = format_info.format ;
cannam@85 453
cannam@85 454 if (! sf_format_check (&sfinfo))
cannam@85 455 { printf ("\n\nLine %d: sf_format_check failed.\n", __LINE__) ;
cannam@85 456 printf (" Name : %s\n", format_info.name) ;
cannam@85 457 printf (" Format : 0x%X\n", sfinfo.format) ;
cannam@85 458 printf (" Channels : 0x%X\n", sfinfo.channels) ;
cannam@85 459 printf (" Sample Rate : 0x%X\n", sfinfo.samplerate) ;
cannam@85 460 exit (1) ;
cannam@85 461 } ;
cannam@85 462 last_name = format_info.name ;
cannam@85 463 } ;
cannam@85 464 format_info.format = 666 ;
cannam@85 465 sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
cannam@85 466
cannam@85 467 /* Now test major formats. */
cannam@85 468 sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int)) ;
cannam@85 469
cannam@85 470 if (count < 0 || count > 30)
cannam@85 471 { printf ("Line %d: Weird count.\n", __LINE__) ;
cannam@85 472 exit (1) ;
cannam@85 473 } ;
cannam@85 474
cannam@85 475 format_info.format = 0 ;
cannam@85 476 sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
cannam@85 477
cannam@85 478 last_name = format_info.name ;
cannam@85 479 for (k = 1 ; k < count ; k ++)
cannam@85 480 { format_info.format = k ;
cannam@85 481 sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
cannam@85 482 if (strcmp (last_name, format_info.name) >= 0)
cannam@85 483 { printf ("\n\nLine %d: format names out of sequence (%d) `%s' < `%s'.\n", __LINE__, k, last_name, format_info.name) ;
cannam@85 484 exit (1) ;
cannam@85 485 } ;
cannam@85 486
cannam@85 487 last_name = format_info.name ;
cannam@85 488 } ;
cannam@85 489 format_info.format = 666 ;
cannam@85 490 sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
cannam@85 491
cannam@85 492 /* Now test subtype formats. */
cannam@85 493 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int)) ;
cannam@85 494
cannam@85 495 if (count < 0 || count > 30)
cannam@85 496 { printf ("Line %d: Weird count.\n", __LINE__) ;
cannam@85 497 exit (1) ;
cannam@85 498 } ;
cannam@85 499
cannam@85 500 format_info.format = 0 ;
cannam@85 501 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
cannam@85 502
cannam@85 503 last_name = format_info.name ;
cannam@85 504 for (k = 1 ; k < count ; k ++)
cannam@85 505 { format_info.format = k ;
cannam@85 506 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
cannam@85 507 } ;
cannam@85 508 format_info.format = 666 ;
cannam@85 509 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
cannam@85 510
cannam@85 511
cannam@85 512 printf ("ok\n") ;
cannam@85 513 } /* format_tests */
cannam@85 514
cannam@85 515 static void
cannam@85 516 calc_peak_test (int filetype, const char *filename)
cannam@85 517 { SNDFILE *file ;
cannam@85 518 SF_INFO sfinfo ;
cannam@85 519 int k, format ;
cannam@85 520 double peak ;
cannam@85 521
cannam@85 522 print_test_name ("calc_peak_test", filename) ;
cannam@85 523
cannam@85 524 format = (filetype | SF_FORMAT_PCM_16) ;
cannam@85 525
cannam@85 526 sfinfo.samplerate = 44100 ;
cannam@85 527 sfinfo.format = format ;
cannam@85 528 sfinfo.channels = 1 ;
cannam@85 529 sfinfo.frames = BUFFER_LEN ;
cannam@85 530
cannam@85 531 /* Create double_data with max value of 0.5. */
cannam@85 532 for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@85 533 double_data [k] = (k + 1) / (2.0 * BUFFER_LEN) ;
cannam@85 534
cannam@85 535 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 536
cannam@85 537 test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 538
cannam@85 539 sf_close (file) ;
cannam@85 540
cannam@85 541 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 542
cannam@85 543 if (sfinfo.format != format)
cannam@85 544 { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 545 exit (1) ;
cannam@85 546 } ;
cannam@85 547
cannam@85 548 if (sfinfo.frames != BUFFER_LEN)
cannam@85 549 { printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
cannam@85 550 exit (1) ;
cannam@85 551 } ;
cannam@85 552
cannam@85 553 if (sfinfo.channels != 1)
cannam@85 554 { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 555 exit (1) ;
cannam@85 556 } ;
cannam@85 557
cannam@85 558 sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ;
cannam@85 559 if (fabs (peak - (1 << 14)) > 1.0)
cannam@85 560 { printf ("Line %d : Peak value should be %d (is %f).\n", __LINE__, (1 << 14), peak) ;
cannam@85 561 exit (1) ;
cannam@85 562 } ;
cannam@85 563
cannam@85 564 sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ;
cannam@85 565 if (fabs (peak - 0.5) > 4e-5)
cannam@85 566 { printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
cannam@85 567 exit (1) ;
cannam@85 568 } ;
cannam@85 569
cannam@85 570 sf_close (file) ;
cannam@85 571
cannam@85 572 format = (filetype | SF_FORMAT_FLOAT) ;
cannam@85 573 sfinfo.samplerate = 44100 ;
cannam@85 574 sfinfo.format = format ;
cannam@85 575 sfinfo.channels = 1 ;
cannam@85 576 sfinfo.frames = BUFFER_LEN ;
cannam@85 577
cannam@85 578 /* Create double_data with max value of 0.5. */
cannam@85 579 for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@85 580 double_data [k] = (k + 1) / (2.0 * BUFFER_LEN) ;
cannam@85 581
cannam@85 582 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 583
cannam@85 584 test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 585
cannam@85 586 sf_close (file) ;
cannam@85 587
cannam@85 588 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 589
cannam@85 590 if (sfinfo.format != format)
cannam@85 591 { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@85 592 exit (1) ;
cannam@85 593 } ;
cannam@85 594
cannam@85 595 if (sfinfo.frames != BUFFER_LEN)
cannam@85 596 { printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
cannam@85 597 exit (1) ;
cannam@85 598 } ;
cannam@85 599
cannam@85 600 if (sfinfo.channels != 1)
cannam@85 601 { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 602 exit (1) ;
cannam@85 603 } ;
cannam@85 604
cannam@85 605 sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ;
cannam@85 606 if (fabs (peak - 0.5) > 1e-5)
cannam@85 607 { printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
cannam@85 608 exit (1) ;
cannam@85 609 } ;
cannam@85 610
cannam@85 611 sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ;
cannam@85 612 if (fabs (peak - 0.5) > 1e-5)
cannam@85 613 { printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
cannam@85 614 exit (1) ;
cannam@85 615 } ;
cannam@85 616
cannam@85 617 sf_close (file) ;
cannam@85 618
cannam@85 619 unlink (filename) ;
cannam@85 620
cannam@85 621 printf ("ok\n") ;
cannam@85 622 } /* calc_peak_test */
cannam@85 623
cannam@85 624 static void
cannam@85 625 truncate_test (const char *filename, int filetype)
cannam@85 626 { SNDFILE *file ;
cannam@85 627 SF_INFO sfinfo ;
cannam@85 628 sf_count_t len ;
cannam@85 629
cannam@85 630 print_test_name ("truncate_test", filename) ;
cannam@85 631
cannam@85 632 sfinfo.samplerate = 11025 ;
cannam@85 633 sfinfo.format = filetype ;
cannam@85 634 sfinfo.channels = 2 ;
cannam@85 635
cannam@85 636 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 637
cannam@85 638 test_write_int_or_die (file, 0, int_data, BUFFER_LEN, __LINE__) ;
cannam@85 639
cannam@85 640 len = 100 ;
cannam@85 641 if (sf_command (file, SFC_FILE_TRUNCATE, &len, sizeof (len)))
cannam@85 642 { printf ("Line %d: sf_command (SFC_FILE_TRUNCATE) returned error.\n", __LINE__) ;
cannam@85 643 exit (1) ;
cannam@85 644 } ;
cannam@85 645
cannam@85 646 test_seek_or_die (file, 0, SEEK_CUR, len, 2, __LINE__) ;
cannam@85 647 test_seek_or_die (file, 0, SEEK_END, len, 2, __LINE__) ;
cannam@85 648
cannam@85 649 sf_close (file) ;
cannam@85 650
cannam@85 651 unlink (filename) ;
cannam@85 652 puts ("ok") ;
cannam@85 653 } /* truncate_test */
cannam@85 654
cannam@85 655 /*------------------------------------------------------------------------------
cannam@85 656 */
cannam@85 657
cannam@85 658 static void
cannam@85 659 instrumet_rw_test (const char *filename)
cannam@85 660 { SNDFILE *sndfile ;
cannam@85 661 SF_INFO sfinfo ;
cannam@85 662 SF_INSTRUMENT inst ;
cannam@85 663 memset (&sfinfo, 0, sizeof (SF_INFO)) ;
cannam@85 664
cannam@85 665 sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
cannam@85 666
cannam@85 667 if (sf_command (sndfile, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE)
cannam@85 668 { inst.basenote = 22 ;
cannam@85 669
cannam@85 670 if (sf_command (sndfile, SFC_SET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE)
cannam@85 671 printf ("Sucess: [%s] updated\n", filename) ;
cannam@85 672 else
cannam@85 673 printf ("Error: SFC_SET_INSTRUMENT on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
cannam@85 674 }
cannam@85 675 else
cannam@85 676 printf ("Error: SFC_GET_INSTRUMENT on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
cannam@85 677
cannam@85 678
cannam@85 679 if (sf_command (sndfile, SFC_UPDATE_HEADER_NOW, NULL, 0) != 0)
cannam@85 680 printf ("Error: SFC_UPDATE_HEADER_NOW on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
cannam@85 681
cannam@85 682 sf_write_sync (sndfile) ;
cannam@85 683 sf_close (sndfile) ;
cannam@85 684
cannam@85 685 return ;
cannam@85 686 } /* instrumet_rw_test */
cannam@85 687
cannam@85 688 static void
cannam@85 689 instrument_test (const char *filename, int filetype)
cannam@85 690 { static SF_INSTRUMENT write_inst =
cannam@85 691 { 2, /* gain */
cannam@85 692 3, /* detune */
cannam@85 693 4, /* basenote */
cannam@85 694 5, 6, /* key low and high */
cannam@85 695 7, 8, /* velocity low and high */
cannam@85 696 2, /* loop_count */
cannam@85 697 { { 801, 2, 3, 0 },
cannam@85 698 { 801, 3, 4, 0 },
cannam@85 699 }
cannam@85 700 } ;
cannam@85 701 SF_INSTRUMENT read_inst ;
cannam@85 702 SNDFILE *file ;
cannam@85 703 SF_INFO sfinfo ;
cannam@85 704
cannam@85 705 print_test_name ("instrument_test", filename) ;
cannam@85 706
cannam@85 707 sfinfo.samplerate = 11025 ;
cannam@85 708 sfinfo.format = filetype ;
cannam@85 709 sfinfo.channels = 1 ;
cannam@85 710
cannam@85 711 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 712 if (sf_command (file, SFC_SET_INSTRUMENT, &write_inst, sizeof (write_inst)) == SF_FALSE)
cannam@85 713 { printf ("\n\nLine %d : sf_command (SFC_SET_INSTRUMENT) failed.\n\n", __LINE__) ;
cannam@85 714 exit (1) ;
cannam@85 715 } ;
cannam@85 716 test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 717 sf_close (file) ;
cannam@85 718
cannam@85 719 memset (&read_inst, 0, sizeof (read_inst)) ;
cannam@85 720
cannam@85 721 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 722 if (sf_command (file, SFC_GET_INSTRUMENT, &read_inst, sizeof (read_inst)) == SF_FALSE)
cannam@85 723 { printf ("\n\nLine %d : sf_command (SFC_GET_INSTRUMENT) failed.\n\n", __LINE__) ;
cannam@85 724 exit (1) ;
cannam@85 725 return ;
cannam@85 726 } ;
cannam@85 727 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 728 sf_close (file) ;
cannam@85 729
cannam@85 730 if ((filetype & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV)
cannam@85 731 { /*
cannam@85 732 ** For all the fields that WAV doesn't support, modify the
cannam@85 733 ** write_inst struct to hold the default value that the WAV
cannam@85 734 ** module should hold.
cannam@85 735 */
cannam@85 736 write_inst.detune = 0 ;
cannam@85 737 write_inst.key_lo = write_inst.velocity_lo = 0 ;
cannam@85 738 write_inst.key_hi = write_inst.velocity_hi = 127 ;
cannam@85 739 write_inst.gain = 1 ;
cannam@85 740 } ;
cannam@85 741
cannam@85 742 if ((filetype & SF_FORMAT_TYPEMASK) == SF_FORMAT_XI)
cannam@85 743 { /*
cannam@85 744 ** For all the fields that XI doesn't support, modify the
cannam@85 745 ** write_inst struct to hold the default value that the XI
cannam@85 746 ** module should hold.
cannam@85 747 */
cannam@85 748 write_inst.basenote = 0 ;
cannam@85 749 write_inst.detune = 0 ;
cannam@85 750 write_inst.key_lo = write_inst.velocity_lo = 0 ;
cannam@85 751 write_inst.key_hi = write_inst.velocity_hi = 127 ;
cannam@85 752 write_inst.gain = 1 ;
cannam@85 753 } ;
cannam@85 754
cannam@85 755 if (memcmp (&write_inst, &read_inst, sizeof (write_inst)) != 0)
cannam@85 756 { printf ("\n\nLine %d : instrument comparison failed.\n\n", __LINE__) ;
cannam@85 757 printf ("W Base Note : %u\n"
cannam@85 758 " Detune : %u\n"
cannam@85 759 " Low Note : %u\tHigh Note : %u\n"
cannam@85 760 " Low Vel. : %u\tHigh Vel. : %u\n"
cannam@85 761 " Gain : %d\tCount : %d\n"
cannam@85 762 " mode : %d\n"
cannam@85 763 " start : %d\tend : %d\tcount :%d\n"
cannam@85 764 " mode : %d\n"
cannam@85 765 " start : %d\tend : %d\tcount :%d\n\n",
cannam@85 766 write_inst.basenote,
cannam@85 767 write_inst.detune,
cannam@85 768 write_inst.key_lo, write_inst.key_hi,
cannam@85 769 write_inst.velocity_lo, write_inst.velocity_hi,
cannam@85 770 write_inst.gain, write_inst.loop_count,
cannam@85 771 write_inst.loops [0].mode, write_inst.loops [0].start,
cannam@85 772 write_inst.loops [0].end, write_inst.loops [0].count,
cannam@85 773 write_inst.loops [1].mode, write_inst.loops [1].start,
cannam@85 774 write_inst.loops [1].end, write_inst.loops [1].count) ;
cannam@85 775 printf ("R Base Note : %u\n"
cannam@85 776 " Detune : %u\n"
cannam@85 777 " Low Note : %u\tHigh Note : %u\n"
cannam@85 778 " Low Vel. : %u\tHigh Vel. : %u\n"
cannam@85 779 " Gain : %d\tCount : %d\n"
cannam@85 780 " mode : %d\n"
cannam@85 781 " start : %d\tend : %d\tcount :%d\n"
cannam@85 782 " mode : %d\n"
cannam@85 783 " start : %d\tend : %d\tcount :%d\n\n",
cannam@85 784 read_inst.basenote,
cannam@85 785 read_inst.detune,
cannam@85 786 read_inst.key_lo, read_inst.key_hi,
cannam@85 787 read_inst.velocity_lo, read_inst.velocity_hi,
cannam@85 788 read_inst.gain, read_inst.loop_count,
cannam@85 789 read_inst.loops [0].mode, read_inst.loops [0].start,
cannam@85 790 read_inst.loops [0].end, read_inst.loops [0].count,
cannam@85 791 read_inst.loops [1].mode, read_inst.loops [1].start,
cannam@85 792 read_inst.loops [1].end, read_inst.loops [1].count) ;
cannam@85 793
cannam@85 794 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_XI)
cannam@85 795 exit (1) ;
cannam@85 796 } ;
cannam@85 797
cannam@85 798 if (0) instrumet_rw_test (filename) ;
cannam@85 799
cannam@85 800 unlink (filename) ;
cannam@85 801 puts ("ok") ;
cannam@85 802 } /* instrument_test */
cannam@85 803
cannam@85 804 static void
cannam@85 805 current_sf_info_test (const char *filename)
cannam@85 806 { SNDFILE *outfile, *infile ;
cannam@85 807 SF_INFO outinfo, ininfo ;
cannam@85 808
cannam@85 809 print_test_name ("current_sf_info_test", filename) ;
cannam@85 810
cannam@85 811 outinfo.samplerate = 44100 ;
cannam@85 812 outinfo.format = (SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@85 813 outinfo.channels = 1 ;
cannam@85 814 outinfo.frames = 0 ;
cannam@85 815
cannam@85 816 outfile = test_open_file_or_die (filename, SFM_WRITE, &outinfo, SF_TRUE, __LINE__) ;
cannam@85 817 sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, 0) ;
cannam@85 818
cannam@85 819 exit_if_true (outinfo.frames != 0,
cannam@85 820 "\n\nLine %d : Initial sfinfo.frames is not zero.\n\n", __LINE__
cannam@85 821 ) ;
cannam@85 822
cannam@85 823 test_write_double_or_die (outfile, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 824 sf_command (outfile, SFC_GET_CURRENT_SF_INFO, &outinfo, sizeof (outinfo)) ;
cannam@85 825
cannam@85 826 exit_if_true (outinfo.frames != BUFFER_LEN,
cannam@85 827 "\n\nLine %d : Initial sfinfo.frames (%ld) should be %d.\n\n", __LINE__,
cannam@85 828 SF_COUNT_TO_LONG (outinfo.frames), BUFFER_LEN
cannam@85 829 ) ;
cannam@85 830
cannam@85 831 /* Read file making sure no channel map exists. */
cannam@85 832 memset (&ininfo, 0, sizeof (ininfo)) ;
cannam@85 833 infile = test_open_file_or_die (filename, SFM_READ, &ininfo, SF_TRUE, __LINE__) ;
cannam@85 834
cannam@85 835 test_write_double_or_die (outfile, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 836
cannam@85 837 sf_command (infile, SFC_GET_CURRENT_SF_INFO, &ininfo, sizeof (ininfo)) ;
cannam@85 838
cannam@85 839 exit_if_true (ininfo.frames != BUFFER_LEN,
cannam@85 840 "\n\nLine %d : Initial sfinfo.frames (%ld) should be %d.\n\n", __LINE__,
cannam@85 841 SF_COUNT_TO_LONG (ininfo.frames), BUFFER_LEN
cannam@85 842 ) ;
cannam@85 843
cannam@85 844 sf_close (outfile) ;
cannam@85 845 sf_close (infile) ;
cannam@85 846
cannam@85 847 unlink (filename) ;
cannam@85 848 puts ("ok") ;
cannam@85 849 } /* current_sf_info_test */
cannam@85 850
cannam@85 851 static void
cannam@85 852 broadcast_test (const char *filename, int filetype)
cannam@85 853 { static SF_BROADCAST_INFO bc_write, bc_read ;
cannam@85 854 SNDFILE *file ;
cannam@85 855 SF_INFO sfinfo ;
cannam@85 856 int errors = 0 ;
cannam@85 857
cannam@85 858 print_test_name ("broadcast_test", filename) ;
cannam@85 859
cannam@85 860 sfinfo.samplerate = 11025 ;
cannam@85 861 sfinfo.format = filetype ;
cannam@85 862 sfinfo.channels = 1 ;
cannam@85 863
cannam@85 864 memset (&bc_write, 0, sizeof (bc_write)) ;
cannam@85 865
cannam@85 866 snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
cannam@85 867 snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
cannam@85 868 snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
cannam@85 869 snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@85 870 snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@85 871 snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
cannam@85 872 bc_write.coding_history_size = 0 ;
cannam@85 873
cannam@85 874 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 875 if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
cannam@85 876 { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@85 877 exit (1) ;
cannam@85 878 } ;
cannam@85 879 test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 880 sf_close (file) ;
cannam@85 881
cannam@85 882 memset (&bc_read, 0, sizeof (bc_read)) ;
cannam@85 883
cannam@85 884 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 885 if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
cannam@85 886 { printf ("\n\nLine %d : sf_command (SFC_GET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@85 887 exit (1) ;
cannam@85 888 return ;
cannam@85 889 } ;
cannam@85 890 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 891 sf_close (file) ;
cannam@85 892
cannam@85 893 if (bc_read.version != 1)
cannam@85 894 { printf ("\n\nLine %d : Read bad version number %d.\n\n", __LINE__, bc_read.version) ;
cannam@85 895 exit (1) ;
cannam@85 896 return ;
cannam@85 897 } ;
cannam@85 898
cannam@85 899 bc_read.version = bc_write.version = 0 ;
cannam@85 900
cannam@85 901 if (memcmp (bc_write.description, bc_read.description, sizeof (bc_write.description)) != 0)
cannam@85 902 { printf ("\n\nLine %d : description mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.description, bc_read.description) ;
cannam@85 903 errors ++ ;
cannam@85 904 } ;
cannam@85 905
cannam@85 906 if (memcmp (bc_write.originator, bc_read.originator, sizeof (bc_write.originator)) != 0)
cannam@85 907 { printf ("\n\nLine %d : originator mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.originator, bc_read.originator) ;
cannam@85 908 errors ++ ;
cannam@85 909 } ;
cannam@85 910
cannam@85 911 if (memcmp (bc_write.originator_reference, bc_read.originator_reference, sizeof (bc_write.originator_reference)) != 0)
cannam@85 912 { printf ("\n\nLine %d : originator_reference mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.originator_reference, bc_read.originator_reference) ;
cannam@85 913 errors ++ ;
cannam@85 914 } ;
cannam@85 915
cannam@85 916 if (memcmp (bc_write.origination_date, bc_read.origination_date, sizeof (bc_write.origination_date)) != 0)
cannam@85 917 { printf ("\n\nLine %d : origination_date mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.origination_date, bc_read.origination_date) ;
cannam@85 918 errors ++ ;
cannam@85 919 } ;
cannam@85 920
cannam@85 921 if (memcmp (bc_write.origination_time, bc_read.origination_time, sizeof (bc_write.origination_time)) != 0)
cannam@85 922 { printf ("\n\nLine %d : origination_time mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.origination_time, bc_read.origination_time) ;
cannam@85 923 errors ++ ;
cannam@85 924 } ;
cannam@85 925
cannam@85 926 if (memcmp (bc_write.umid, bc_read.umid, sizeof (bc_write.umid)) != 0)
cannam@85 927 { printf ("\n\nLine %d : umid mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.umid, bc_read.umid) ;
cannam@85 928 errors ++ ;
cannam@85 929 } ;
cannam@85 930
cannam@85 931 if (errors)
cannam@85 932 exit (1) ;
cannam@85 933
cannam@85 934 unlink (filename) ;
cannam@85 935 puts ("ok") ;
cannam@85 936 } /* broadcast_test */
cannam@85 937
cannam@85 938 static void
cannam@85 939 broadcast_rdwr_test (const char *filename, int filetype)
cannam@85 940 { SF_BROADCAST_INFO binfo ;
cannam@85 941 SNDFILE *file ;
cannam@85 942 SF_INFO sfinfo ;
cannam@85 943 sf_count_t frames ;
cannam@85 944
cannam@85 945 print_test_name (__func__, filename) ;
cannam@85 946
cannam@85 947 create_short_sndfile (filename, filetype, 2) ;
cannam@85 948
cannam@85 949 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 950 memset (&binfo, 0, sizeof (binfo)) ;
cannam@85 951
cannam@85 952 snprintf (binfo.description, sizeof (binfo.description), "Test description") ;
cannam@85 953 snprintf (binfo.originator, sizeof (binfo.originator), "Test originator") ;
cannam@85 954 snprintf (binfo.originator_reference, sizeof (binfo.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
cannam@85 955 snprintf (binfo.origination_date, sizeof (binfo.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@85 956 snprintf (binfo.origination_time, sizeof (binfo.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@85 957 snprintf (binfo.umid, sizeof (binfo.umid), "Some umid") ;
cannam@85 958 binfo.coding_history_size = 0 ;
cannam@85 959
cannam@85 960 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 961 frames = sfinfo.frames ;
cannam@85 962 if (sf_command (file, SFC_SET_BROADCAST_INFO, &binfo, sizeof (binfo)) != SF_FALSE)
cannam@85 963 { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) should have failed but didn't.\n\n", __LINE__) ;
cannam@85 964 exit (1) ;
cannam@85 965 } ;
cannam@85 966 sf_close (file) ;
cannam@85 967
cannam@85 968 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 969 sf_close (file) ;
cannam@85 970 exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %lld should be %lld.\n", __LINE__, sfinfo.frames, frames) ;
cannam@85 971
cannam@85 972 unlink (filename) ;
cannam@85 973 puts ("ok") ;
cannam@85 974 } /* broadcast_rdwr_test */
cannam@85 975
cannam@85 976 static void
cannam@85 977 check_coding_history_newlines (const char *filename)
cannam@85 978 { static SF_BROADCAST_INFO bc_write, bc_read ;
cannam@85 979 SNDFILE *file ;
cannam@85 980 SF_INFO sfinfo ;
cannam@85 981 unsigned k ;
cannam@85 982
cannam@85 983 sfinfo.samplerate = 22050 ;
cannam@85 984 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
cannam@85 985 sfinfo.channels = 1 ;
cannam@85 986
cannam@85 987 memset (&bc_write, 0, sizeof (bc_write)) ;
cannam@85 988
cannam@85 989 snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
cannam@85 990 snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
cannam@85 991 snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
cannam@85 992 snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@85 993 snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@85 994 snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
cannam@85 995 bc_write.coding_history_size = snprintf (bc_write.coding_history, sizeof (bc_write.coding_history), "This has\nUnix\nand\rMac OS9\rline endings.\nLast line") ; ;
cannam@85 996
cannam@85 997 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 998 if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
cannam@85 999 { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@85 1000 exit (1) ;
cannam@85 1001 } ;
cannam@85 1002
cannam@85 1003 test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 1004 sf_close (file) ;
cannam@85 1005
cannam@85 1006 memset (&bc_read, 0, sizeof (bc_read)) ;
cannam@85 1007
cannam@85 1008 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1009 if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
cannam@85 1010 { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@85 1011 exit (1) ;
cannam@85 1012 } ;
cannam@85 1013 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 1014 sf_close (file) ;
cannam@85 1015
cannam@85 1016 if (bc_read.coding_history_size == 0)
cannam@85 1017 { printf ("\n\nLine %d : missing coding history.\n\n", __LINE__) ;
cannam@85 1018 exit (1) ;
cannam@85 1019 } ;
cannam@85 1020
cannam@85 1021 if (strstr (bc_read.coding_history, "Last line") == NULL)
cannam@85 1022 { printf ("\n\nLine %d : coding history truncated.\n\n", __LINE__) ;
cannam@85 1023 exit (1) ;
cannam@85 1024 } ;
cannam@85 1025
cannam@85 1026 for (k = 1 ; k < bc_read.coding_history_size ; k++)
cannam@85 1027 { if (bc_read.coding_history [k] == '\n' && bc_read.coding_history [k - 1] != '\r')
cannam@85 1028 { printf ("\n\nLine %d : '\\n' without '\\r' before.\n\n", __LINE__) ;
cannam@85 1029 exit (1) ;
cannam@85 1030 } ;
cannam@85 1031
cannam@85 1032 if (bc_read.coding_history [k] == '\r' && bc_read.coding_history [k + 1] != '\n')
cannam@85 1033 { printf ("\n\nLine %d : '\\r' without '\\n' after.\n\n", __LINE__) ;
cannam@85 1034 exit (1) ;
cannam@85 1035 } ;
cannam@85 1036
cannam@85 1037 if (bc_read.coding_history [k] == 0 && k < bc_read.coding_history_size - 1)
cannam@85 1038 { printf ("\n\nLine %d : '\\0' within coding history at index %d of %d.\n\n", __LINE__, k, bc_read.coding_history_size) ;
cannam@85 1039 exit (1) ;
cannam@85 1040 } ;
cannam@85 1041 } ;
cannam@85 1042
cannam@85 1043 return ;
cannam@85 1044 } /* check_coding_history_newlines */
cannam@85 1045
cannam@85 1046 static void
cannam@85 1047 broadcast_coding_history_test (const char *filename)
cannam@85 1048 { static SF_BROADCAST_INFO bc_write, bc_read ;
cannam@85 1049 SNDFILE *file ;
cannam@85 1050 SF_INFO sfinfo ;
cannam@85 1051 const char *default_history = "A=PCM,F=22050,W=16,M=mono" ;
cannam@85 1052 const char *supplied_history =
cannam@85 1053 "A=PCM,F=44100,W=24,M=mono,T=other\r\n"
cannam@85 1054 "A=PCM,F=22050,W=16,M=mono,T=yet_another\r\n" ;
cannam@85 1055
cannam@85 1056 print_test_name ("broadcast_coding_history_test", filename) ;
cannam@85 1057
cannam@85 1058 sfinfo.samplerate = 22050 ;
cannam@85 1059 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
cannam@85 1060 sfinfo.channels = 1 ;
cannam@85 1061
cannam@85 1062 memset (&bc_write, 0, sizeof (bc_write)) ;
cannam@85 1063
cannam@85 1064 snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
cannam@85 1065 snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
cannam@85 1066 snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
cannam@85 1067 snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@85 1068 snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@85 1069 snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
cannam@85 1070 /* Coding history will be filled in by the library. */
cannam@85 1071 bc_write.coding_history_size = 0 ;
cannam@85 1072
cannam@85 1073 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1074 if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
cannam@85 1075 { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@85 1076 exit (1) ;
cannam@85 1077 } ;
cannam@85 1078
cannam@85 1079 test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 1080 sf_close (file) ;
cannam@85 1081
cannam@85 1082 memset (&bc_read, 0, sizeof (bc_read)) ;
cannam@85 1083
cannam@85 1084 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1085 if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
cannam@85 1086 { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@85 1087 exit (1) ;
cannam@85 1088 } ;
cannam@85 1089 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 1090 sf_close (file) ;
cannam@85 1091
cannam@85 1092 if (bc_read.coding_history_size == 0)
cannam@85 1093 { printf ("\n\nLine %d : missing coding history.\n\n", __LINE__) ;
cannam@85 1094 exit (1) ;
cannam@85 1095 } ;
cannam@85 1096
cannam@85 1097 if (bc_read.coding_history_size < strlen (default_history) || memcmp (bc_read.coding_history, default_history, strlen (default_history)) != 0)
cannam@85 1098 { printf ("\n\n"
cannam@85 1099 "Line %d : unexpected coding history '%.*s',\n"
cannam@85 1100 " should be '%s'\n\n", __LINE__, bc_read.coding_history_size, bc_read.coding_history, default_history) ;
cannam@85 1101 exit (1) ;
cannam@85 1102 } ;
cannam@85 1103
cannam@85 1104 bc_write.coding_history_size = strlen (supplied_history) ;
cannam@85 1105 bc_write.coding_history_size = snprintf (bc_write.coding_history, sizeof (bc_write.coding_history), "%s", supplied_history) ;
cannam@85 1106
cannam@85 1107 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1108 if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
cannam@85 1109 { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@85 1110 exit (1) ;
cannam@85 1111 } ;
cannam@85 1112
cannam@85 1113 test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 1114 sf_close (file) ;
cannam@85 1115
cannam@85 1116 memset (&bc_read, 0, sizeof (bc_read)) ;
cannam@85 1117
cannam@85 1118 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1119 if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
cannam@85 1120 { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@85 1121 exit (1) ;
cannam@85 1122 } ;
cannam@85 1123
cannam@85 1124 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 1125 sf_close (file) ;
cannam@85 1126
cannam@85 1127 if (strstr (bc_read.coding_history, supplied_history) != bc_read.coding_history)
cannam@85 1128 { printf ("\n\nLine %d : unexpected coding history :\n"
cannam@85 1129 "----------------------------------------------------\n%s"
cannam@85 1130 "----------------------------------------------------\n"
cannam@85 1131 "should be this :\n"
cannam@85 1132 "----------------------------------------------------\n%s"
cannam@85 1133 "----------------------------------------------------\n"
cannam@85 1134 "with one more line at the end.\n\n",
cannam@85 1135 __LINE__, bc_read.coding_history, supplied_history) ;
cannam@85 1136 exit (1) ;
cannam@85 1137 } ;
cannam@85 1138
cannam@85 1139 check_coding_history_newlines (filename) ;
cannam@85 1140
cannam@85 1141 unlink (filename) ;
cannam@85 1142 puts ("ok") ;
cannam@85 1143 } /* broadcast_coding_history_test */
cannam@85 1144
cannam@85 1145 /*==============================================================================
cannam@85 1146 */
cannam@85 1147
cannam@85 1148 static void
cannam@85 1149 broadcast_coding_history_size (const char *filename)
cannam@85 1150 { /* SF_BROADCAST_INFO struct with coding_history field of 1024 bytes. */
cannam@85 1151 static SF_BROADCAST_INFO_VAR (1024) bc_write ;
cannam@85 1152 static SF_BROADCAST_INFO_VAR (1024) bc_read ;
cannam@85 1153 SNDFILE *file ;
cannam@85 1154 SF_INFO sfinfo ;
cannam@85 1155 int k ;
cannam@85 1156
cannam@85 1157 print_test_name (__func__, filename) ;
cannam@85 1158
cannam@85 1159 sfinfo.samplerate = 22050 ;
cannam@85 1160 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
cannam@85 1161 sfinfo.channels = 1 ;
cannam@85 1162
cannam@85 1163 memset (&bc_write, 0, sizeof (bc_write)) ;
cannam@85 1164
cannam@85 1165 snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
cannam@85 1166 snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
cannam@85 1167 snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
cannam@85 1168 snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@85 1169 snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@85 1170 snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
cannam@85 1171 bc_write.coding_history_size = 0 ;
cannam@85 1172
cannam@85 1173 for (k = 0 ; bc_write.coding_history_size < 512 ; k++)
cannam@85 1174 { snprintf (bc_write.coding_history + bc_write.coding_history_size,
cannam@85 1175 sizeof (bc_write.coding_history) - bc_write.coding_history_size, "line %4d\n", k) ;
cannam@85 1176 bc_write.coding_history_size = strlen (bc_write.coding_history) ;
cannam@85 1177 } ;
cannam@85 1178
cannam@85 1179 exit_if_true (bc_write.coding_history_size < 512,
cannam@85 1180 "\n\nLine %d : bc_write.coding_history_size (%d) should be > 512.\n\n", __LINE__, bc_write.coding_history_size) ;
cannam@85 1181
cannam@85 1182 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1183 if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
cannam@85 1184 { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@85 1185 exit (1) ;
cannam@85 1186 } ;
cannam@85 1187
cannam@85 1188 test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 1189 sf_close (file) ;
cannam@85 1190
cannam@85 1191 memset (&bc_read, 0, sizeof (bc_read)) ;
cannam@85 1192
cannam@85 1193 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1194 if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
cannam@85 1195 { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@85 1196 exit (1) ;
cannam@85 1197 } ;
cannam@85 1198 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 1199 sf_close (file) ;
cannam@85 1200
cannam@85 1201 exit_if_true (bc_read.coding_history_size < 512,
cannam@85 1202 "\n\nLine %d : unexpected coding history size %d (should be > 512).\n\n", __LINE__, bc_read.coding_history_size) ;
cannam@85 1203
cannam@85 1204 exit_if_true (strstr (bc_read.coding_history, "libsndfile") == NULL,
cannam@85 1205 "\n\nLine %d : coding history incomplete (should contain 'libsndfile').\n\n", __LINE__) ;
cannam@85 1206
cannam@85 1207 unlink (filename) ;
cannam@85 1208 puts ("ok") ;
cannam@85 1209 } /* broadcast_coding_history_size */
cannam@85 1210
cannam@85 1211 /*==============================================================================
cannam@85 1212 */
cannam@85 1213
cannam@85 1214 static void
cannam@85 1215 channel_map_test (const char *filename, int filetype)
cannam@85 1216 { SNDFILE *file ;
cannam@85 1217 SF_INFO sfinfo ;
cannam@85 1218 int channel_map_read [4], channel_map_write [4] =
cannam@85 1219 { SF_CHANNEL_MAP_LEFT, SF_CHANNEL_MAP_RIGHT, SF_CHANNEL_MAP_LFE,
cannam@85 1220 SF_CHANNEL_MAP_REAR_CENTER
cannam@85 1221 } ;
cannam@85 1222
cannam@85 1223 print_test_name ("channel_map_test", filename) ;
cannam@85 1224
cannam@85 1225 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 1226 sfinfo.samplerate = 11025 ;
cannam@85 1227 sfinfo.format = filetype ;
cannam@85 1228 sfinfo.channels = ARRAY_LEN (channel_map_read) ;
cannam@85 1229
cannam@85 1230 switch (filetype & SF_FORMAT_TYPEMASK)
cannam@85 1231 { /* WAVEX and RF64 have a default channel map, even if you don't specify one. */
cannam@85 1232 case SF_FORMAT_WAVEX :
cannam@85 1233 case SF_FORMAT_RF64 :
cannam@85 1234 /* Write file without channel map. */
cannam@85 1235 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1236 test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 1237 sf_close (file) ;
cannam@85 1238
cannam@85 1239 /* Read file making default channel map exists. */
cannam@85 1240 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1241 exit_if_true (
cannam@85 1242 sf_command (file, SFC_GET_CHANNEL_MAP_INFO, channel_map_read, sizeof (channel_map_read)) == SF_FALSE,
cannam@85 1243 "\n\nLine %d : sf_command (SFC_GET_CHANNEL_MAP_INFO) should not have failed.\n\n", __LINE__
cannam@85 1244 ) ;
cannam@85 1245 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 1246 sf_close (file) ;
cannam@85 1247 break ;
cannam@85 1248
cannam@85 1249 default :
cannam@85 1250 break ;
cannam@85 1251 } ;
cannam@85 1252
cannam@85 1253 /* Write file with a channel map. */
cannam@85 1254 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1255 exit_if_true (
cannam@85 1256 sf_command (file, SFC_SET_CHANNEL_MAP_INFO, channel_map_write, sizeof (channel_map_write)) == SF_FALSE,
cannam@85 1257 "\n\nLine %d : sf_command (SFC_SET_CHANNEL_MAP_INFO) failed.\n\n", __LINE__
cannam@85 1258 ) ;
cannam@85 1259 test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 1260 sf_close (file) ;
cannam@85 1261
cannam@85 1262 /* Read file making sure no channel map exists. */
cannam@85 1263 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1264 exit_if_true (
cannam@85 1265 sf_command (file, SFC_GET_CHANNEL_MAP_INFO, channel_map_read, sizeof (channel_map_read)) != SF_TRUE,
cannam@85 1266 "\n\nLine %d : sf_command (SFC_GET_CHANNEL_MAP_INFO) failed.\n\n", __LINE__
cannam@85 1267 ) ;
cannam@85 1268 check_log_buffer_or_die (file, __LINE__) ;
cannam@85 1269 sf_close (file) ;
cannam@85 1270
cannam@85 1271 exit_if_true (
cannam@85 1272 memcmp (channel_map_read, channel_map_write, sizeof (channel_map_read)) != 0,
cannam@85 1273 "\n\nLine %d : Channel map read does not match channel map written.\n\n", __LINE__
cannam@85 1274 ) ;
cannam@85 1275
cannam@85 1276 unlink (filename) ;
cannam@85 1277 puts ("ok") ;
cannam@85 1278 } /* channel_map_test */
cannam@85 1279
cannam@85 1280 static void
cannam@85 1281 raw_needs_endswap_test (const char *filename, int filetype)
cannam@85 1282 { static int subtypes [] =
cannam@85 1283 { SF_FORMAT_FLOAT, SF_FORMAT_DOUBLE,
cannam@85 1284 SF_FORMAT_PCM_16, SF_FORMAT_PCM_24, SF_FORMAT_PCM_32
cannam@85 1285 } ;
cannam@85 1286 SNDFILE *file ;
cannam@85 1287 SF_INFO sfinfo ;
cannam@85 1288 unsigned k ;
cannam@85 1289 int needs_endswap ;
cannam@85 1290
cannam@85 1291 print_test_name (__func__, filename) ;
cannam@85 1292
cannam@85 1293 for (k = 0 ; k < ARRAY_LEN (subtypes) ; k++)
cannam@85 1294 {
cannam@85 1295 if (filetype == (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF))
cannam@85 1296 switch (subtypes [k])
cannam@85 1297 { /* Little endian AIFF does not AFAIK support fl32 and fl64. */
cannam@85 1298 case SF_FORMAT_FLOAT :
cannam@85 1299 case SF_FORMAT_DOUBLE :
cannam@85 1300 continue ;
cannam@85 1301 default :
cannam@85 1302 break ;
cannam@85 1303 } ;
cannam@85 1304
cannam@85 1305 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 1306 sfinfo.samplerate = 11025 ;
cannam@85 1307 sfinfo.format = filetype | subtypes [k] ;
cannam@85 1308 sfinfo.channels = 1 ;
cannam@85 1309
cannam@85 1310 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1311 test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@85 1312 sf_close (file) ;
cannam@85 1313
cannam@85 1314 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 1315 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@85 1316
cannam@85 1317 needs_endswap = sf_command (file, SFC_RAW_DATA_NEEDS_ENDSWAP, NULL, 0) ;
cannam@85 1318
cannam@85 1319 switch (filetype)
cannam@85 1320 { case SF_FORMAT_WAV :
cannam@85 1321 case SF_FORMAT_WAVEX :
cannam@85 1322 case SF_FORMAT_AIFF | SF_ENDIAN_LITTLE :
cannam@85 1323 exit_if_true (needs_endswap != CPU_IS_BIG_ENDIAN,
cannam@85 1324 "\n\nLine %d : SFC_RAW_DATA_NEEDS_ENDSWAP failed for (%d | %d).\n\n", __LINE__, filetype, k) ;
cannam@85 1325 break ;
cannam@85 1326
cannam@85 1327 case SF_FORMAT_AIFF :
cannam@85 1328 case SF_FORMAT_WAV | SF_ENDIAN_BIG :
cannam@85 1329 exit_if_true (needs_endswap != CPU_IS_LITTLE_ENDIAN,
cannam@85 1330 "\n\nLine %d : SFC_RAW_DATA_NEEDS_ENDSWAP failed for (%d | %d).\n\n", __LINE__, filetype, k) ;
cannam@85 1331 break ;
cannam@85 1332
cannam@85 1333 default :
cannam@85 1334 printf ("\n\nLine %d : bad format value %d.\n\n", __LINE__, filetype) ;
cannam@85 1335 exit (1) ;
cannam@85 1336 break ;
cannam@85 1337 } ;
cannam@85 1338
cannam@85 1339 sf_close (file) ;
cannam@85 1340 } ;
cannam@85 1341
cannam@85 1342 unlink (filename) ;
cannam@85 1343 puts ("ok") ;
cannam@85 1344 } /* raw_needs_endswap_test */