1[+ AutoGen5 template c +] 2/* 3** Copyright (C) 1999-2017 Erik de Castro Lopo <erikd@mega-nerd.com> 4** 5** This program is free software; you can redistribute it and/or modify 6** it under the terms of the GNU General Public License as published by 7** the Free Software Foundation; either version 2 of the License, or 8** (at your option) any later version. 9** 10** This program is distributed in the hope that it will be useful, 11** but WITHOUT ANY WARRANTY; without even the implied warranty of 12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13** GNU General Public License for more details. 14** 15** You should have received a copy of the GNU General Public License 16** along with this program; if not, write to the Free Software 17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18*/ 19 20#include "sfconfig.h" 21 22#include <stdio.h> 23#include <stdlib.h> 24#include <string.h> 25#include <math.h> 26#include <inttypes.h> 27 28 29#if HAVE_UNISTD_H 30#include <unistd.h> 31#else 32#include "sf_unistd.h" 33#endif 34 35#include <sndfile.h> 36 37#include "utils.h" 38#include "generate.h" 39 40#define SAMPLE_RATE 11025 41#define DATA_LENGTH (1 << 12) 42 43#define SILLY_WRITE_COUNT (234) 44 45static const char WRT_TEST_PREFIX[] = "wrt" ; 46 47[+ FOR data_type 48+]static void pcm_test_[+ (get "type_name") +] (const char *str, int format, int long_file_ok) ; 49[+ ENDFOR data_type 50+] 51static void empty_file_test (const char *filename, int format) ; 52 53typedef union 54{ double d [DATA_LENGTH] ; 55 float f [DATA_LENGTH] ; 56 int i [DATA_LENGTH] ; 57 short s [DATA_LENGTH] ; 58 char c [DATA_LENGTH] ; 59} BUFFER ; 60 61static BUFFER orig_data ; 62static BUFFER test_data ; 63 64int 65main (int argc, char **argv) 66{ int do_all = 0 ; 67 int test_count = 0 ; 68 69 count_open_files () ; 70 71 if (argc != 2) 72 { printf ("Usage : %s <test>\n", argv [0]) ; 73 printf (" Where <test> is one of the following:\n") ; 74 printf (" wav - test WAV file functions (little endian)\n") ; 75 printf (" aiff - test AIFF file functions (big endian)\n") ; 76 printf (" au - test AU file functions\n") ; 77 printf (" avr - test AVR file functions\n") ; 78 printf (" caf - test CAF file functions\n") ; 79 printf (" raw - test RAW header-less PCM file functions\n") ; 80 printf (" paf - test PAF file functions\n") ; 81 printf (" svx - test 8SVX/16SV file functions\n") ; 82 printf (" nist - test NIST Sphere file functions\n") ; 83 printf (" ircam - test IRCAM file functions\n") ; 84 printf (" voc - Create Voice file functions\n") ; 85 printf (" w64 - Sonic Foundry's W64 file functions\n") ; 86 printf (" flac - test FLAC file functions\n") ; 87 printf (" mpc2k - test MPC 2000 file functions\n") ; 88 printf (" rf64 - test RF64 file functions\n") ; 89 printf (" all - perform all tests\n") ; 90 exit (1) ; 91 } ; 92 93 do_all = !strcmp (argv [1], "all") ; 94 95 if (do_all || ! strcmp (argv [1], "wav")) 96 { pcm_test_char ("char.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ; 97 pcm_test_short ("short.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ; 98 pcm_test_24bit ("24bit.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ; 99 pcm_test_int ("int.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ; 100 101 pcm_test_char ("char.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ; 102 pcm_test_short ("short.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ; 103 pcm_test_24bit ("24bit.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ; 104 pcm_test_int ("int.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ; 105 106 pcm_test_24bit ("24bit.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_PCM_24, SF_FALSE) ; 107 pcm_test_int ("int.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_PCM_32, SF_FALSE) ; 108 109 /* Lite remove start */ 110 pcm_test_float ("float.wav" , SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ; 111 pcm_test_double ("double.wav" , SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ; 112 113 pcm_test_float ("float.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ; 114 pcm_test_double ("double.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ; 115 116 pcm_test_float ("float.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_FLOAT , SF_FALSE) ; 117 pcm_test_double ("double.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_DOUBLE, SF_FALSE) ; 118 /* Lite remove end */ 119 120 empty_file_test ("empty_char.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ; 121 empty_file_test ("empty_short.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ; 122 empty_file_test ("empty_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ; 123 124 test_count++ ; 125 } ; 126 127 if (do_all || ! strcmp (argv [1], "aiff")) 128 { pcm_test_char ("char_u8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_U8, SF_FALSE) ; 129 pcm_test_char ("char_s8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_S8, SF_FALSE) ; 130 pcm_test_short ("short.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ; 131 pcm_test_24bit ("24bit.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ; 132 pcm_test_int ("int.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ; 133 134 pcm_test_short ("short_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ; 135 pcm_test_24bit ("24bit_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ; 136 pcm_test_int ("int_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ; 137 138 pcm_test_short ("short_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ; 139 pcm_test_24bit ("24bit_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ; 140 pcm_test_int ("int_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ; 141 142 /* Lite remove start */ 143 pcm_test_short ("dwvw16.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_16, SF_TRUE) ; 144 pcm_test_24bit ("dwvw24.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_24, SF_TRUE) ; 145 146 pcm_test_float ("float.aifc" , SF_FORMAT_AIFF | SF_FORMAT_FLOAT , SF_FALSE) ; 147 pcm_test_double ("double.aifc" , SF_FORMAT_AIFF | SF_FORMAT_DOUBLE, SF_FALSE) ; 148 /* Lite remove end */ 149 150 empty_file_test ("empty_char.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ; 151 empty_file_test ("empty_short.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ; 152 empty_file_test ("empty_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ; 153 154 test_count++ ; 155 } ; 156 157 if (do_all || ! strcmp (argv [1], "au")) 158 { pcm_test_char ("char.au" , SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ; 159 pcm_test_short ("short.au" , SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ; 160 pcm_test_24bit ("24bit.au" , SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ; 161 pcm_test_int ("int.au" , SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ; 162 /* Lite remove start */ 163 pcm_test_float ("float.au" , SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ; 164 pcm_test_double ("double.au", SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ; 165 /* Lite remove end */ 166 167 pcm_test_char ("char_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ; 168 pcm_test_short ("short_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ; 169 pcm_test_24bit ("24bit_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ; 170 pcm_test_int ("int_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ; 171 /* Lite remove start */ 172 pcm_test_float ("float_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ; 173 pcm_test_double ("double_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ; 174 /* Lite remove end */ 175 test_count++ ; 176 } ; 177 178 if (do_all || ! strcmp (argv [1], "caf")) 179 { pcm_test_char ("char.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_S8, SF_FALSE) ; 180 pcm_test_short ("short.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ; 181 pcm_test_24bit ("24bit.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ; 182 pcm_test_int ("int.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ; 183 /* Lite remove start */ 184 pcm_test_float ("float.caf" , SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ; 185 pcm_test_double ("double.caf" , SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ; 186 /* Lite remove end */ 187 188 pcm_test_short ("short_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ; 189 pcm_test_24bit ("24bit_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ; 190 pcm_test_int ("int_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ; 191 /* Lite remove start */ 192 pcm_test_float ("float_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ; 193 pcm_test_double ("double_le.caf", SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ; 194 195 pcm_test_short ("alac16.caf" , SF_FORMAT_CAF | SF_FORMAT_ALAC_16, SF_FALSE) ; 196 pcm_test_20bit ("alac20.caf" , SF_FORMAT_CAF | SF_FORMAT_ALAC_20, SF_FALSE) ; 197 pcm_test_24bit ("alac24.caf" , SF_FORMAT_CAF | SF_FORMAT_ALAC_24, SF_FALSE) ; 198 pcm_test_int ("alac32.caf" , SF_FORMAT_CAF | SF_FORMAT_ALAC_32, SF_FALSE) ; 199 200 /* Lite remove end */ 201 test_count++ ; 202 } ; 203 204 if (do_all || ! strcmp (argv [1], "raw")) 205 { pcm_test_char ("char_s8.raw" , SF_FORMAT_RAW | SF_FORMAT_PCM_S8, SF_FALSE) ; 206 pcm_test_char ("char_u8.raw" , SF_FORMAT_RAW | SF_FORMAT_PCM_U8, SF_FALSE) ; 207 208 pcm_test_short ("short_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ; 209 pcm_test_short ("short_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ; 210 pcm_test_24bit ("24bit_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ; 211 pcm_test_24bit ("24bit_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ; 212 pcm_test_int ("int_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ; 213 pcm_test_int ("int_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ; 214 215 /* Lite remove start */ 216 pcm_test_float ("float_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ; 217 pcm_test_float ("float_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ; 218 219 pcm_test_double ("double_le.raw", SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ; 220 pcm_test_double ("double_be.raw", SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ; 221 /* Lite remove end */ 222 test_count++ ; 223 } ; 224 225 /* Lite remove start */ 226 if (do_all || ! strcmp (argv [1], "paf")) 227 { pcm_test_char ("char_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ; 228 pcm_test_char ("char_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ; 229 pcm_test_short ("short_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ; 230 pcm_test_short ("short_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ; 231 pcm_test_24bit ("24bit_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ; 232 pcm_test_24bit ("24bit_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ; 233 test_count++ ; 234 } ; 235 236 if (do_all || ! strcmp (argv [1], "svx")) 237 { pcm_test_char ("char.svx" , SF_FORMAT_SVX | SF_FORMAT_PCM_S8, SF_FALSE) ; 238 pcm_test_short ("short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16, SF_FALSE) ; 239 240 empty_file_test ("empty_char.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_S8) ; 241 empty_file_test ("empty_short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16) ; 242 243 test_count++ ; 244 } ; 245 246 if (do_all || ! strcmp (argv [1], "nist")) 247 { pcm_test_short ("short_le.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ; 248 pcm_test_short ("short_be.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ; 249 pcm_test_24bit ("24bit_le.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ; 250 pcm_test_24bit ("24bit_be.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ; 251 pcm_test_int ("int_le.nist" , SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ; 252 pcm_test_int ("int_be.nist" , SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ; 253 254 test_count++ ; 255 } ; 256 257 if (do_all || ! strcmp (argv [1], "ircam")) 258 { pcm_test_short ("short_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ; 259 pcm_test_short ("short_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ; 260 pcm_test_int ("int_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ; 261 pcm_test_int ("int_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ; 262 pcm_test_float ("float_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ; 263 pcm_test_float ("float_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ; 264 265 test_count++ ; 266 } ; 267 268 if (do_all || ! strcmp (argv [1], "voc")) 269 { pcm_test_char ("char.voc" , SF_FORMAT_VOC | SF_FORMAT_PCM_U8, SF_FALSE) ; 270 pcm_test_short ("short.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_16, SF_FALSE) ; 271 272 test_count++ ; 273 } ; 274 275 if (do_all || ! strcmp (argv [1], "mat4")) 276 { pcm_test_short ("short_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ; 277 pcm_test_short ("short_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ; 278 pcm_test_int ("int_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ; 279 pcm_test_int ("int_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ; 280 pcm_test_float ("float_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ; 281 pcm_test_float ("float_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ; 282 pcm_test_double ("double_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ; 283 pcm_test_double ("double_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ; 284 285 empty_file_test ("empty_short.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ; 286 empty_file_test ("empty_float.mat4", SF_FORMAT_MAT4 | SF_FORMAT_FLOAT) ; 287 test_count++ ; 288 } ; 289 290 if (do_all || ! strcmp (argv [1], "mat5")) 291 { pcm_test_char ("char_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ; 292 pcm_test_char ("char_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ; 293 pcm_test_short ("short_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ; 294 pcm_test_short ("short_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ; 295 pcm_test_int ("int_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ; 296 pcm_test_int ("int_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ; 297 pcm_test_float ("float_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ; 298 pcm_test_float ("float_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ; 299 pcm_test_double ("double_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ; 300 pcm_test_double ("double_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ; 301 302 increment_open_file_count () ; 303 304 empty_file_test ("empty_char.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8) ; 305 empty_file_test ("empty_short.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ; 306 empty_file_test ("empty_float.mat5", SF_FORMAT_MAT5 | SF_FORMAT_FLOAT) ; 307 308 test_count++ ; 309 } ; 310 311 if (do_all || ! strcmp (argv [1], "pvf")) 312 { pcm_test_char ("char.pvf" , SF_FORMAT_PVF | SF_FORMAT_PCM_S8, SF_FALSE) ; 313 pcm_test_short ("short.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_16, SF_FALSE) ; 314 pcm_test_int ("int.pvf" , SF_FORMAT_PVF | SF_FORMAT_PCM_32, SF_FALSE) ; 315 test_count++ ; 316 } ; 317 318 if (do_all || ! strcmp (argv [1], "htk")) 319 { pcm_test_short ("short.htk", SF_FORMAT_HTK | SF_FORMAT_PCM_16, SF_FALSE) ; 320 test_count++ ; 321 } ; 322 323 if (do_all || ! strcmp (argv [1], "mpc2k")) 324 { pcm_test_short ("short.mpc", SF_FORMAT_MPC2K | SF_FORMAT_PCM_16, SF_FALSE) ; 325 test_count++ ; 326 } ; 327 328 if (do_all || ! strcmp (argv [1], "avr")) 329 { pcm_test_char ("char_u8.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_U8, SF_FALSE) ; 330 pcm_test_char ("char_s8.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_S8, SF_FALSE) ; 331 pcm_test_short ("short.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_16, SF_FALSE) ; 332 test_count++ ; 333 } ; 334 /* Lite remove end */ 335 336 if (do_all || ! strcmp (argv [1], "w64")) 337 { pcm_test_char ("char.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_U8, SF_FALSE) ; 338 pcm_test_short ("short.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_16, SF_FALSE) ; 339 pcm_test_24bit ("24bit.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_24, SF_FALSE) ; 340 pcm_test_int ("int.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_32, SF_FALSE) ; 341 /* Lite remove start */ 342 pcm_test_float ("float.w64" , SF_FORMAT_W64 | SF_FORMAT_FLOAT , SF_FALSE) ; 343 pcm_test_double ("double.w64" , SF_FORMAT_W64 | SF_FORMAT_DOUBLE, SF_FALSE) ; 344 /* Lite remove end */ 345 346 empty_file_test ("empty_char.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ; 347 empty_file_test ("empty_short.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ; 348 empty_file_test ("empty_float.w64", SF_FORMAT_W64 | SF_FORMAT_FLOAT) ; 349 350 test_count++ ; 351 } ; 352 353 if (do_all || ! strcmp (argv [1], "sds")) 354 { pcm_test_char ("char.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_S8, SF_FALSE) ; 355 pcm_test_short ("short.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_16, SF_FALSE) ; 356 pcm_test_24bit ("24bit.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_24, SF_FALSE) ; 357 358 empty_file_test ("empty_char.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_S8) ; 359 empty_file_test ("empty_short.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_16) ; 360 361 test_count++ ; 362 } ; 363 364 if (do_all || ! strcmp (argv [1], "sd2")) 365 { pcm_test_char ("char.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_S8, SF_TRUE) ; 366 pcm_test_short ("short.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_16, SF_TRUE) ; 367 pcm_test_24bit ("24bit.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_24, SF_TRUE) ; 368 pcm_test_int ("32bit.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_32, SF_TRUE) ; 369 test_count++ ; 370 } ; 371 372 if (do_all || ! strcmp (argv [1], "flac")) 373 { if (HAVE_EXTERNAL_XIPH_LIBS) 374 { pcm_test_char ("char.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, SF_TRUE) ; 375 pcm_test_short ("short.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_16, SF_TRUE) ; 376 pcm_test_24bit ("24bit.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_24, SF_TRUE) ; 377 } 378 else 379 puts (" No FLAC tests because FLAC support was not compiled in.") ; 380 test_count++ ; 381 } ; 382 383 if (do_all || ! strcmp (argv [1], "rf64")) 384 { pcm_test_char ("char.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_U8, SF_FALSE) ; 385 pcm_test_short ("short.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_16, SF_FALSE) ; 386 pcm_test_24bit ("24bit.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_24, SF_FALSE) ; 387 pcm_test_int ("int.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_32, SF_FALSE) ; 388 389 /* Lite remove start */ 390 pcm_test_float ("float.rf64" , SF_FORMAT_RF64 | SF_FORMAT_FLOAT , SF_FALSE) ; 391 pcm_test_double ("double.rf64" , SF_FORMAT_RF64 | SF_FORMAT_DOUBLE, SF_FALSE) ; 392 empty_file_test ("empty_char.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_U8) ; 393 empty_file_test ("empty_short.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ; 394 empty_file_test ("empty_float.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ; 395 /* Lite remove end */ 396 397 test_count++ ; 398 } ; 399 400 if (test_count == 0) 401 { printf ("Mono : ************************************\n") ; 402 printf ("Mono : * No '%s' test defined.\n", argv [1]) ; 403 printf ("Mono : ************************************\n") ; 404 return 1 ; 405 } ; 406 407 /* Only open file descriptors should be stdin, stdout and stderr. */ 408 check_open_file_count_or_die (__LINE__) ; 409 410 return 0 ; 411} /* main */ 412 413/*============================================================================================ 414** Helper functions and macros. 415*/ 416 417static void create_short_file (const char *filename) ; 418 419#define CHAR_ERROR(x, y) (abs ((x) - (y)) > 255) 420#define INT_ERROR(x, y) (((x) - (y)) != 0) 421#define BIT_20_ERROR(x, y) (abs ((x) - (y)) > 4095) 422#define TRIBYTE_ERROR(x, y) (abs ((x) - (y)) > 255) 423#define FLOAT_ERROR(x, y) (fabs ((x) - (y)) > 1e-5) 424 425#define CONVERT_DATA(k, len, new, orig) \ 426 { for ((k) = 0 ; (k) < (len) ; (k) ++) \ 427 (new) [k] = (orig) [k] ; \ 428 } 429 430[+ FOR data_type 431+] 432/*====================================================================================== 433*/ 434 435static void mono_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) ; 436static void stereo_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) ; 437static void mono_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) ; 438static void new_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int allow_fd) ; 439static void multi_seek_test (const char * filename, int format) ; 440static void write_seek_extend_test (const char * filename, int format) ; 441 442static void 443pcm_test_[+ (get "type_name") +] (const char *filename, int format, int long_file_ok) 444{ SF_INFO sfinfo ; 445 [+ (get "data_type") +] *orig ; 446 int k, allow_fd ; 447 448 /* Sd2 files cannot be opened from an existing file descriptor. */ 449 allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ; 450 451 get_unique_test_name (&filename, WRT_TEST_PREFIX) ; 452 print_test_name ("pcm_test_[+ (get "type_name") +]", filename) ; 453 454 sfinfo.samplerate = 44100 ; 455 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */ 456 sfinfo.channels = 1 ; 457 sfinfo.format = format ; 458 459 test_sf_format_or_die (&sfinfo, __LINE__) ; 460 461 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, [+ (get "max_val") +]) ; 462 463 orig = orig_data.[+ (get "data_field") +] ; 464 465 /* Make this a macro so gdb steps over it in one go. */ 466 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ; 467 468 /* Some test broken out here. */ 469 470 mono_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ; 471 472 /* Sub format DWVW does not allow seeking. */ 473 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 || 474 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24) 475 { unlink (filename) ; 476 printf ("no seek : ok\n") ; 477 return ; 478 } ; 479 480 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC 481 && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_16 482 && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_20 483 && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_24 484 && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_32 485 ) 486 mono_rdwr_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ; 487 488 /* If the format doesn't support stereo we're done. */ 489 sfinfo.channels = 2 ; 490 if (sf_format_check (&sfinfo) == 0) 491 { unlink (filename) ; 492 puts ("no stereo : ok") ; 493 return ; 494 } ; 495 496 stereo_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ; 497 498 /* New read/write test. Not sure if this is needed yet. */ 499 500 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF 501 && (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC 502 && (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC 503 && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_16 504 && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_20 505 && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_24 506 && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_32 507 ) 508 new_rdwr_[+ (get "type_name") +]_test (filename, format, allow_fd) ; 509 510 delete_file (format, filename) ; 511 512 puts ("ok") ; 513 return ; 514} /* pcm_test_[+ (get "type_name") +] */ 515 516static void 517mono_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) 518{ SNDFILE *file ; 519 SF_INFO sfinfo ; 520 [+ (get "data_type") +] *orig, *test ; 521 sf_count_t count ; 522 int k, items, total ; 523 524 sfinfo.samplerate = 44100 ; 525 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */ 526 sfinfo.channels = 1 ; 527 sfinfo.format = format ; 528 529 orig = orig_data.[+ (get "data_field") +] ; 530 test = test_data.[+ (get "data_field") +] ; 531 532 items = DATA_LENGTH ; 533 534 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ; 535 536 if (sfinfo.frames || sfinfo.sections || sfinfo.seekable) 537 { printf ("\n\nLine %d : Weird SF_INFO fields.\n", __LINE__) ; 538 exit (1) ; 539 } ; 540 541 sf_set_string (file, SF_STR_ARTIST, "Your name here") ; 542 543 test_write_[+ (get "data_type") +]_or_die (file, 0, orig, items, __LINE__) ; 544 sf_write_sync (file) ; 545 test_write_[+ (get "data_type") +]_or_die (file, 0, orig, items, __LINE__) ; 546 sf_write_sync (file) ; 547 548 /* Add non-audio data after the audio. */ 549 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ; 550 551 sf_close (file) ; 552 553 memset (test, 0, items * sizeof ([+ (get "data_type") +])) ; 554 555 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW) 556 memset (&sfinfo, 0, sizeof (sfinfo)) ; 557 558 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ; 559 560 if (sfinfo.format != format) 561 { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ; 562 exit (1) ; 563 } ; 564 565 if (sfinfo.frames < 2 * items) 566 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, items) ; 567 exit (1) ; 568 } ; 569 570 if (! long_file_ok && sfinfo.frames > 2 * items) 571 { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, items) ; 572 exit (1) ; 573 } ; 574 575 if (sfinfo.channels != 1) 576 { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ; 577 exit (1) ; 578 } ; 579 580 if (sfinfo.seekable != 1) 581 { printf ("\n\nLine %d : File should be seekable.\n", __LINE__) ; 582 exit (1) ; 583 } ; 584 585 check_log_buffer_or_die (file, __LINE__) ; 586 587 test_read_[+ (get "data_type") +]_or_die (file, 0, test, items, __LINE__) ; 588 for (k = 0 ; k < items ; k++) 589 if ([+ (get "error_func") +] (orig [k], test [k])) 590 { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ; 591 oct_save_[+ (get "data_type") +] (orig, test, items) ; 592 exit (1) ; 593 } ; 594 595 /* Test multiple short reads. */ 596 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; 597 598 total = 0 ; 599 for (k = 1 ; k <= 32 ; k++) 600 { int ik ; 601 602 test_read_[+ (get "data_type") +]_or_die (file, 0, test + total, k, __LINE__) ; 603 total += k ; 604 605 for (ik = 0 ; ik < total ; ik++) 606 if ([+ (get "error_func") +] (orig [ik], test [ik])) 607 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, ik, orig [ik], test [ik]) ; 608 exit (1) ; 609 } ; 610 } ; 611 612 /* Seek to start of file. */ 613 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; 614 615 test_read_[+ (get "data_type") +]_or_die (file, 0, test, 4, __LINE__) ; 616 for (k = 0 ; k < 4 ; k++) 617 if ([+ (get "error_func") +] (orig [k], test [k])) 618 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ; 619 exit (1) ; 620 } ; 621 622 /* For some codecs we can't go past here. */ 623 if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 || 624 (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24) 625 { sf_close (file) ; 626 unlink (filename) ; 627 printf ("no seek : ") ; 628 return ; 629 } ; 630 631 /* Seek to offset from start of file. */ 632 test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ; 633 634 test_read_[+ (get "data_type") +]_or_die (file, 0, test + 10, 4, __LINE__) ; 635 for (k = 10 ; k < 14 ; k++) 636 if ([+ (get "error_func") +] (orig [k], test [k])) 637 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ; 638 exit (1) ; 639 } ; 640 641 /* Seek to offset from current position. */ 642 test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ; 643 644 test_read_[+ (get "data_type") +]_or_die (file, 0, test + 20, 4, __LINE__) ; 645 for (k = 20 ; k < 24 ; k++) 646 if ([+ (get "error_func") +] (orig [k], test [k])) 647 { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ; 648 exit (1) ; 649 } ; 650 651 /* Seek to offset from end of file. */ 652 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ; 653 654 test_read_[+ (get "data_type") +]_or_die (file, 0, test + 10, 4, __LINE__) ; 655 for (k = 10 ; k < 14 ; k++) 656 if ([+ (get "error_func") +] (orig [k], test [k])) 657 { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ; 658 exit (1) ; 659 } ; 660 661 /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */ 662 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; 663 664 count = 0 ; 665 while (count < sfinfo.frames) 666 count += sf_read_[+ (get "data_type") +] (file, test, 311) ; 667 668 /* Check that no error has occurred. */ 669 if (sf_error (file)) 670 { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ; 671 puts (sf_strerror (file)) ; 672 exit (1) ; 673 } ; 674 675 /* Check that we haven't read beyond EOF. */ 676 if (count > sfinfo.frames) 677 { printf ("\n\nLines %d : read past end of file (%" PRId64 " should be %" PRId64 ")\n", __LINE__, count, sfinfo.frames) ; 678 exit (1) ; 679 } ; 680 681 test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ; 682 683 sf_close (file) ; 684 685 multi_seek_test (filename, format) ; 686 write_seek_extend_test (filename, format) ; 687 688} /* mono_[+ (get "type_name") +]_test */ 689 690static void 691stereo_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) 692{ SNDFILE *file ; 693 SF_INFO sfinfo ; 694 [+ (get "data_type") +] *orig, *test ; 695 int k, items, frames ; 696 697 sfinfo.samplerate = 44100 ; 698 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */ 699 sfinfo.channels = 2 ; 700 sfinfo.format = format ; 701 702 gen_windowed_sine_double (orig_data.d, DATA_LENGTH, [+ (get "max_val") +]) ; 703 704 orig = orig_data.[+ (get "data_field") +] ; 705 test = test_data.[+ (get "data_field") +] ; 706 707 /* Make this a macro so gdb steps over it in one go. */ 708 CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ; 709 710 items = DATA_LENGTH ; 711 frames = items / sfinfo.channels ; 712 713 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ; 714 715 sf_set_string (file, SF_STR_ARTIST, "Your name here") ; 716 717 test_writef_[+ (get "data_type") +]_or_die (file, 0, orig, frames, __LINE__) ; 718 719 sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ; 720 721 sf_close (file) ; 722 723 memset (test, 0, items * sizeof ([+ (get "data_type") +])) ; 724 725 if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW) 726 memset (&sfinfo, 0, sizeof (sfinfo)) ; 727 728 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ; 729 730 if (sfinfo.format != format) 731 { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n", 732 __LINE__, format, sfinfo.format) ; 733 exit (1) ; 734 } ; 735 736 if (sfinfo.frames < frames) 737 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", 738 __LINE__, sfinfo.frames, frames) ; 739 exit (1) ; 740 } ; 741 742 if (! long_file_ok && sfinfo.frames > frames) 743 { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%" PRId64 " should be %d)\n", 744 __LINE__, sfinfo.frames, frames) ; 745 exit (1) ; 746 } ; 747 748 if (sfinfo.channels != 2) 749 { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ; 750 exit (1) ; 751 } ; 752 753 check_log_buffer_or_die (file, __LINE__) ; 754 755 test_readf_[+ (get "data_type") +]_or_die (file, 0, test, frames, __LINE__) ; 756 for (k = 0 ; k < items ; k++) 757 if ([+ (get "error_func") +] (test [k], orig [k])) 758 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ; 759 exit (1) ; 760 } ; 761 762 /* Seek to start of file. */ 763 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; 764 765 test_readf_[+ (get "data_type") +]_or_die (file, 0, test, 2, __LINE__) ; 766 for (k = 0 ; k < 4 ; k++) 767 if ([+ (get "error_func") +] (test [k], orig [k])) 768 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ; 769 exit (1) ; 770 } ; 771 772 /* Seek to offset from start of file. */ 773 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; 774 775 /* Check for errors here. */ 776 if (sf_error (file)) 777 { printf ("Line %d: Should NOT return an error.\n", __LINE__) ; 778 puts (sf_strerror (file)) ; 779 exit (1) ; 780 } ; 781 782 if (sf_read_[+ (get "data_type") +] (file, test, 1) > 0) 783 { printf ("Line %d: Should return 0.\n", __LINE__) ; 784 exit (1) ; 785 } ; 786 787 if (! sf_error (file)) 788 { printf ("Line %d: Should return an error.\n", __LINE__) ; 789 exit (1) ; 790 } ; 791 /*-----------------------*/ 792 793 test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 10, 2, __LINE__) ; 794 for (k = 20 ; k < 24 ; k++) 795 if ([+ (get "error_func") +] (test [k], orig [k])) 796 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ; 797 exit (1) ; 798 } ; 799 800 /* Seek to offset from current position. */ 801 test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ; 802 803 test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 20, 2, __LINE__) ; 804 for (k = 40 ; k < 44 ; k++) 805 if ([+ (get "error_func") +] (test [k], orig [k])) 806 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ; 807 exit (1) ; 808 } ; 809 810 /* Seek to offset from end of file. */ 811 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ; 812 813 test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 20, 2, __LINE__) ; 814 for (k = 20 ; k < 24 ; k++) 815 if ([+ (get "error_func") +] (test [k], orig [k])) 816 { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ; 817 exit (1) ; 818 } ; 819 820 sf_close (file) ; 821} /* stereo_[+ (get "type_name") +]_test */ 822 823static void 824mono_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) 825{ SNDFILE *file ; 826 SF_INFO sfinfo ; 827 [+ (get "data_type") +] *orig, *test ; 828 int k, pass ; 829 830 switch (format & SF_FORMAT_SUBMASK) 831 { case SF_FORMAT_ALAC_16 : 832 case SF_FORMAT_ALAC_20 : 833 case SF_FORMAT_ALAC_24 : 834 case SF_FORMAT_ALAC_32 : 835 allow_fd = 0 ; 836 break ; 837 838 default : 839 break ; 840 } ; 841 842 orig = orig_data.[+ (get "data_field") +] ; 843 test = test_data.[+ (get "data_field") +] ; 844 845 sfinfo.samplerate = SAMPLE_RATE ; 846 sfinfo.frames = DATA_LENGTH ; 847 sfinfo.channels = 1 ; 848 sfinfo.format = format ; 849 850 if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW 851 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU 852 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) 853 unlink (filename) ; 854 else 855 { /* Create a short file. */ 856 create_short_file (filename) ; 857 858 /* Opening a already existing short file (ie invalid header) RDWR is disallowed. 859 ** If this returns a valif pointer sf_open() screwed up. 860 */ 861 if ((file = sf_open (filename, SFM_RDWR, &sfinfo))) 862 { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ; 863 exit (1) ; 864 } ; 865 866 /* Truncate the file to zero bytes. */ 867 if (truncate_file_to_zero (filename) < 0) 868 { printf ("\n\nLine %d: truncate_file_to_zero (%s) failed", __LINE__, filename) ; 869 perror (NULL) ; 870 exit (1) ; 871 } ; 872 } ; 873 874 /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain 875 ** all the usual data required when opening the file in WRITE mode. 876 */ 877 sfinfo.samplerate = SAMPLE_RATE ; 878 sfinfo.frames = DATA_LENGTH ; 879 sfinfo.channels = 1 ; 880 sfinfo.format = format ; 881 882 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ; 883 884 /* Do 3 writes followed by reads. After each, check the data and the current 885 ** read and write offsets. 886 */ 887 for (pass = 1 ; pass <= 3 ; pass ++) 888 { orig [20] = pass * 2 ; 889 890 /* Write some data. */ 891 test_write_[+ (get "data_type") +]_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ; 892 893 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ; 894 895 /* Read what we just wrote. */ 896 test_read_[+ (get "data_type") +]_or_die (file, 0, test, DATA_LENGTH, __LINE__) ; 897 898 /* Check the data. */ 899 for (k = 0 ; k < DATA_LENGTH ; k++) 900 if ([+ (get "error_func") +] (orig [k], test [k])) 901 { printf ("\n\nLine %d (pass %d) A : Error at sample %d ([+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, pass, k, orig [k], test [k]) ; 902 oct_save_[+ (get "data_type") +] (orig, test, DATA_LENGTH) ; 903 exit (1) ; 904 } ; 905 906 test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ; 907 } ; /* for (pass ...) */ 908 909 sf_close (file) ; 910 911 /* Open the file again to check the data. */ 912 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ; 913 914 if (sfinfo.format != format) 915 { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ; 916 exit (1) ; 917 } ; 918 919 if (sfinfo.frames < 3 * DATA_LENGTH) 920 { printf ("\n\nLine %d : Not enough frames in file. (%" PRId64 " < %d)\n", __LINE__, sfinfo.frames, 3 * DATA_LENGTH) ; 921 exit (1) ; 922 } 923 924 if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH) 925 { printf ("\n\nLine %d : Incorrect number of frames in file. (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, 3 * DATA_LENGTH) ; 926 exit (1) ; 927 } ; 928 929 if (sfinfo.channels != 1) 930 { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ; 931 exit (1) ; 932 } ; 933 934 if (! long_file_ok) 935 test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ; 936 else 937 test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ; 938 939 for (pass = 1 ; pass <= 3 ; pass ++) 940 { orig [20] = pass * 2 ; 941 942 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ; 943 944 /* Read what we just wrote. */ 945 test_read_[+ (get "data_type") +]_or_die (file, pass, test, DATA_LENGTH, __LINE__) ; 946 947 /* Check the data. */ 948 for (k = 0 ; k < DATA_LENGTH ; k++) 949 if ([+ (get "error_func") +] (orig [k], test [k])) 950 { printf ("\n\nLine %d (pass %d) B : Error at sample %d ([+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, pass, k, orig [k], test [k]) ; 951 oct_save_[+ (get "data_type") +] (orig, test, DATA_LENGTH) ; 952 exit (1) ; 953 } ; 954 955 } ; /* for (pass ...) */ 956 957 sf_close (file) ; 958} /* mono_rdwr_[+ (get "data_type") +]_test */ 959 960static void 961new_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int allow_fd) 962{ SNDFILE *wfile, *rwfile ; 963 SF_INFO sfinfo ; 964 [+ (get "data_type") +] *orig, *test ; 965 int items, frames ; 966 967 orig = orig_data.[+ (get "data_field") +] ; 968 test = test_data.[+ (get "data_field") +] ; 969 970 sfinfo.samplerate = 44100 ; 971 sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */ 972 sfinfo.channels = 2 ; 973 sfinfo.format = format ; 974 975 items = DATA_LENGTH ; 976 frames = items / sfinfo.channels ; 977 978 wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ; 979 sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ; 980 test_writef_[+ (get "data_type") +]_or_die (wfile, 1, orig, frames, __LINE__) ; 981 sf_write_sync (wfile) ; 982 test_writef_[+ (get "data_type") +]_or_die (wfile, 2, orig, frames, __LINE__) ; 983 sf_write_sync (wfile) ; 984 985 rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ; 986 if (sfinfo.frames != 2 * frames) 987 { printf ("\n\nLine %d : incorrect number of frames in file (%" PRId64 " should be %d)\n\n", __LINE__, sfinfo.frames, 2 * frames) ; 988 exit (1) ; 989 } ; 990 991 test_writef_[+ (get "data_type") +]_or_die (wfile, 3, orig, frames, __LINE__) ; 992 993 test_readf_[+ (get "data_type") +]_or_die (rwfile, 1, test, frames, __LINE__) ; 994 test_readf_[+ (get "data_type") +]_or_die (rwfile, 2, test, frames, __LINE__) ; 995 996 sf_close (wfile) ; 997 sf_close (rwfile) ; 998} /* new_rdwr_[+ (get "type_name") +]_test */ 999 1000[+ ENDFOR data_type +] 1001 1002/*---------------------------------------------------------------------------------------- 1003*/ 1004 1005static void 1006empty_file_test (const char *filename, int format) 1007{ SNDFILE *file ; 1008 SF_INFO info ; 1009 int allow_fd ; 1010 1011 /* Sd2 files cannot be opened from an existing file descriptor. */ 1012 allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ; 1013 1014 get_unique_test_name (&filename, WRT_TEST_PREFIX) ; 1015 print_test_name ("empty_file_test", filename) ; 1016 1017 unlink (filename) ; 1018 1019 info.samplerate = 48000 ; 1020 info.channels = 2 ; 1021 info.format = format ; 1022 info.frames = 0 ; 1023 1024 if (sf_format_check (&info) == SF_FALSE) 1025 { info.channels = 1 ; 1026 if (sf_format_check (&info) == SF_FALSE) 1027 { puts ("invalid file format") ; 1028 return ; 1029 } ; 1030 } ; 1031 1032 /* Create an empty file. */ 1033 file = test_open_file_or_die (filename, SFM_WRITE, &info, allow_fd, __LINE__) ; 1034 sf_close (file) ; 1035 1036 /* Open for read and check the length. */ 1037 file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ; 1038 1039 if (info.frames != 0) 1040 { printf ("\n\nError : frame count (%" PRId64 ") should be zero.\n", info.frames) ; 1041 exit (1) ; 1042 } ; 1043 1044 sf_close (file) ; 1045 1046 /* Open for read/write and check the length. */ 1047 file = test_open_file_or_die (filename, SFM_RDWR, &info, allow_fd, __LINE__) ; 1048 1049 if (info.frames != 0) 1050 { printf ("\n\nError : frame count (%" PRId64 ") should be zero.\n", info.frames) ; 1051 exit (1) ; 1052 } ; 1053 1054 sf_close (file) ; 1055 1056 /* Open for read and check the length. */ 1057 file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ; 1058 1059 if (info.frames != 0) 1060 { printf ("\n\nError : frame count (%" PRId64 ") should be zero.\n", info.frames) ; 1061 exit (1) ; 1062 } ; 1063 1064 sf_close (file) ; 1065 1066 check_open_file_count_or_die (__LINE__) ; 1067 1068 unlink (filename) ; 1069 puts ("ok") ; 1070 1071 return ; 1072} /* empty_file_test */ 1073 1074 1075/*---------------------------------------------------------------------------------------- 1076*/ 1077 1078static void 1079create_short_file (const char *filename) 1080{ FILE *file ; 1081 1082 if (! (file = fopen (filename, "w"))) 1083 { printf ("create_short_file : fopen (%s, \"w\") failed.", filename) ; 1084 fflush (stdout) ; 1085 perror (NULL) ; 1086 exit (1) ; 1087 } ; 1088 1089 fprintf (file, "This is the file data.\n") ; 1090 1091 fclose (file) ; 1092} /* create_short_file */ 1093 1094 1095static void 1096multi_seek_test (const char * filename, int format) 1097{ SNDFILE * file ; 1098 SF_INFO info ; 1099 sf_count_t pos ; 1100 int k ; 1101 1102 /* This test doesn't work on the following. */ 1103 switch (format & SF_FORMAT_TYPEMASK) 1104 { case SF_FORMAT_RAW : 1105 return ; 1106 1107 default : 1108 break ; 1109 } ; 1110 1111 memset (&info, 0, sizeof (info)) ; 1112 1113 generate_file (filename, format, 88200) ; 1114 1115 file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ; 1116 1117 for (k = 0 ; k < 10 ; k++) 1118 { pos = info.frames / (k + 2) ; 1119 test_seek_or_die (file, pos, SEEK_SET, pos, info.channels, __LINE__) ; 1120 } ; 1121 1122 sf_close (file) ; 1123} /* multi_seek_test */ 1124 1125static void 1126write_seek_extend_test (const char * filename, int format) 1127{ SNDFILE * file ; 1128 SF_INFO info ; 1129 short *orig, *test ; 1130 unsigned items, k ; 1131 1132 /* This test doesn't work on the following container formats. */ 1133 switch (format & SF_FORMAT_TYPEMASK) 1134 { case SF_FORMAT_FLAC : 1135 case SF_FORMAT_HTK : 1136 case SF_FORMAT_PAF : 1137 case SF_FORMAT_SDS : 1138 case SF_FORMAT_SVX : 1139 return ; 1140 1141 default : 1142 break ; 1143 } ; 1144 1145 /* This test doesn't work on the following codec formats. */ 1146 switch (format & SF_FORMAT_SUBMASK) 1147 { case SF_FORMAT_ALAC_16 : 1148 case SF_FORMAT_ALAC_20 : 1149 case SF_FORMAT_ALAC_24 : 1150 case SF_FORMAT_ALAC_32 : 1151 return ; 1152 1153 default : 1154 break ; 1155 } ; 1156 1157 memset (&info, 0, sizeof (info)) ; 1158 1159 info.samplerate = 48000 ; 1160 info.channels = 1 ; 1161 info.format = format ; 1162 1163 items = 512 ; 1164 exit_if_true (items > ARRAY_LEN (orig_data.s), "Line %d : Bad assumption.\n", __LINE__) ; 1165 1166 orig = orig_data.s ; 1167 test = test_data.s ; 1168 1169 for (k = 0 ; k < ARRAY_LEN (orig_data.s) ; k++) 1170 orig [k] = 0x3fff ; 1171 1172 file = test_open_file_or_die (filename, SFM_WRITE, &info, SF_FALSE, __LINE__) ; 1173 test_write_short_or_die (file, 0, orig, items, __LINE__) ; 1174 1175 /* Extend the file using a seek. */ 1176 test_seek_or_die (file, 2 * items, SEEK_SET, 2 * items, info.channels, __LINE__) ; 1177 1178 test_writef_short_or_die (file, 0, orig, items, __LINE__) ; 1179 sf_close (file) ; 1180 1181 file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ; 1182 test_read_short_or_die (file, 0, test, 3 * items, __LINE__) ; 1183 sf_close (file) ; 1184 1185 if (info.frames < 3 * items) 1186 { printf ("\n\nLine %d : Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, info.frames, 3 * items) ; 1187 exit (1) ; 1188 } ; 1189 1190 /* Can't do these formats due to scaling. */ 1191 switch (format & SF_FORMAT_SUBMASK) 1192 { case SF_FORMAT_PCM_S8 : 1193 case SF_FORMAT_PCM_U8 : 1194 return ; 1195 default : 1196 break ; 1197 } ; 1198 1199 for (k = 0 ; k < items ; k++) 1200 { exit_if_true (test [k] != 0x3fff, "Line %d : test [%d] == %d, should be 0x3fff.\n", __LINE__, k, test [k]) ; 1201 exit_if_true (test [items + k] != 0, "Line %d : test [%d] == %d, should be 0.\n", __LINE__, items + k, test [items + k]) ; 1202 exit_if_true (test [2 * items + k] != 0x3fff, "Line %d : test [%d] == %d, should be 0x3fff.\n", __LINE__, 2 * items + k, test [2 * items + k]) ; 1203 } ; 1204 1205 return ; 1206} /* write_seek_extend_test */ 1207 1208 1209