annotate src/libsndfile-1.0.27/tests/scale_clip_test.c @ 84:08ae793730bd

Add null config files
author Chris Cannam
date Mon, 02 Mar 2020 14:03:47 +0000
parents 1df64224f5ac
children
rev   line source
Chris@40 1 /*
Chris@40 2 ** Copyright (C) 1999-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@40 3 **
Chris@40 4 ** This program is free software; you can redistribute it and/or modify
Chris@40 5 ** it under the terms of the GNU General Public License as published by
Chris@40 6 ** the Free Software Foundation; either version 2 of the License, or
Chris@40 7 ** (at your option) any later version.
Chris@40 8 **
Chris@40 9 ** This program is distributed in the hope that it will be useful,
Chris@40 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@40 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@40 12 ** GNU General Public License for more details.
Chris@40 13 **
Chris@40 14 ** You should have received a copy of the GNU General Public License
Chris@40 15 ** along with this program; if not, write to the Free Software
Chris@40 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Chris@40 17 */
Chris@40 18
Chris@40 19 #include "sfconfig.h"
Chris@40 20
Chris@40 21 #include <stdio.h>
Chris@40 22 #include <stdlib.h>
Chris@40 23 #include <string.h>
Chris@40 24 #include <math.h>
Chris@40 25 #include <inttypes.h>
Chris@40 26
Chris@40 27 #if HAVE_UNISTD_H
Chris@40 28 #include <unistd.h>
Chris@40 29 #endif
Chris@40 30
Chris@40 31 #include <sndfile.h>
Chris@40 32
Chris@40 33 #include "utils.h"
Chris@40 34
Chris@40 35 #ifndef M_PI
Chris@40 36 #define M_PI 3.14159265358979323846264338
Chris@40 37 #endif
Chris@40 38
Chris@40 39 #define HALF_BUFFER_SIZE (1 << 12)
Chris@40 40 #define BUFFER_SIZE (2 * HALF_BUFFER_SIZE)
Chris@40 41
Chris@40 42 #define SINE_AMP 1.1
Chris@40 43 #define MAX_ERROR 0.0202
Chris@40 44
Chris@40 45
Chris@40 46 static void flt_scale_clip_test_16 (const char *filename, int filetype, float maxval) ;
Chris@40 47 static void flt_scale_clip_test_24 (const char *filename, int filetype, float maxval) ;
Chris@40 48 static void flt_scale_clip_test_32 (const char *filename, int filetype, float maxval) ;
Chris@40 49 static void flt_scale_clip_test_08 (const char *filename, int filetype, float maxval) ;
Chris@40 50
Chris@40 51 static void dbl_scale_clip_test_16 (const char *filename, int filetype, float maxval) ;
Chris@40 52 static void dbl_scale_clip_test_24 (const char *filename, int filetype, float maxval) ;
Chris@40 53 static void dbl_scale_clip_test_32 (const char *filename, int filetype, float maxval) ;
Chris@40 54 static void dbl_scale_clip_test_08 (const char *filename, int filetype, float maxval) ;
Chris@40 55
Chris@40 56
Chris@40 57
Chris@40 58 static void flt_short_clip_read_test (const char *filename, int filetype) ;
Chris@40 59 static void flt_int_clip_read_test (const char *filename, int filetype) ;
Chris@40 60
Chris@40 61 static void dbl_short_clip_read_test (const char *filename, int filetype) ;
Chris@40 62 static void dbl_int_clip_read_test (const char *filename, int filetype) ;
Chris@40 63
Chris@40 64
Chris@40 65
Chris@40 66 static void short_flt_scale_write_test (const char *filename, int filetype) ;
Chris@40 67 static void short_dbl_scale_write_test (const char *filename, int filetype) ;
Chris@40 68
Chris@40 69 static void int_flt_scale_write_test (const char *filename, int filetype) ;
Chris@40 70 static void int_dbl_scale_write_test (const char *filename, int filetype) ;
Chris@40 71
Chris@40 72
Chris@40 73 typedef union
Chris@40 74 { double dbl [BUFFER_SIZE] ;
Chris@40 75 float flt [BUFFER_SIZE] ;
Chris@40 76 int i [BUFFER_SIZE] ;
Chris@40 77 short s [BUFFER_SIZE] ;
Chris@40 78 } BUFFER ;
Chris@40 79
Chris@40 80 /* Data buffer. */
Chris@40 81 static BUFFER buffer_out ;
Chris@40 82 static BUFFER buffer_in ;
Chris@40 83
Chris@40 84 int
Chris@40 85 main (void)
Chris@40 86 {
Chris@40 87 flt_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
Chris@40 88 flt_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
Chris@40 89
Chris@40 90 dbl_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
Chris@40 91 dbl_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
Chris@40 92
Chris@40 93 /*
Chris@40 94 ** Now use SF_FORMAT_AU where possible because it allows both
Chris@40 95 ** big and little endian files.
Chris@40 96 */
Chris@40 97
Chris@40 98 flt_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
Chris@40 99 flt_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
Chris@40 100 flt_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
Chris@40 101 flt_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
Chris@40 102 flt_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
Chris@40 103 flt_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
Chris@40 104
Chris@40 105 dbl_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
Chris@40 106 dbl_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
Chris@40 107 dbl_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
Chris@40 108 dbl_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
Chris@40 109 dbl_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
Chris@40 110 dbl_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
Chris@40 111
Chris@40 112 flt_short_clip_read_test ("flt_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
Chris@40 113 flt_int_clip_read_test ("flt_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
Chris@40 114 dbl_short_clip_read_test ("dbl_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
Chris@40 115 dbl_int_clip_read_test ("dbl_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
Chris@40 116
Chris@40 117 short_flt_scale_write_test ("short_flt.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
Chris@40 118 int_flt_scale_write_test ("int_flt.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
Chris@40 119 short_dbl_scale_write_test ("short_dbl.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
Chris@40 120 int_dbl_scale_write_test ("int_dbl.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
Chris@40 121
Chris@40 122 return 0 ;
Chris@40 123 } /* main */
Chris@40 124
Chris@40 125 /*============================================================================================
Chris@40 126 ** Here are the test functions.
Chris@40 127 */
Chris@40 128
Chris@40 129
Chris@40 130 static void
Chris@40 131 flt_scale_clip_test_16 (const char *filename, int filetype, float maxval)
Chris@40 132 { SNDFILE *file ;
Chris@40 133 SF_INFO sfinfo ;
Chris@40 134 int k ;
Chris@40 135 float *data_out, *data_in ;
Chris@40 136 double diff, clip_max_diff ;
Chris@40 137
Chris@40 138 print_test_name ("flt_scale_clip_test_16", filename) ;
Chris@40 139
Chris@40 140 data_out = buffer_out.flt ;
Chris@40 141 data_in = buffer_in.flt ;
Chris@40 142
Chris@40 143 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 144 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
Chris@40 145 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
Chris@40 146 } ;
Chris@40 147
Chris@40 148 sfinfo.samplerate = 44100 ;
Chris@40 149 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 150 sfinfo.channels = 1 ;
Chris@40 151 sfinfo.format = filetype ;
Chris@40 152
Chris@40 153 /*
Chris@40 154 ** Write two versions of the data:
Chris@40 155 ** normalized and clipped
Chris@40 156 ** un-normalized and clipped.
Chris@40 157 */
Chris@40 158
Chris@40 159 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 160 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 161 test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 162 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@40 163 test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 164 sf_close (file) ;
Chris@40 165
Chris@40 166 memset (&buffer_in, 0, sizeof (buffer_in)) ;
Chris@40 167
Chris@40 168 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 169
Chris@40 170 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 171
Chris@40 172 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 173 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 174 exit (1) ;
Chris@40 175 } ;
Chris@40 176
Chris@40 177 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 178 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 179 exit (1) ;
Chris@40 180 } ;
Chris@40 181
Chris@40 182 if (sfinfo.channels != 1)
Chris@40 183 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 184 exit (1) ;
Chris@40 185 } ;
Chris@40 186
Chris@40 187 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 188
Chris@40 189 test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 190 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@40 191 test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 192 sf_close (file) ;
Chris@40 193
Chris@40 194 /* Check normalized version. */
Chris@40 195 clip_max_diff = 0.0 ;
Chris@40 196 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 197 { if (fabs (data_in [k]) > 1.0)
Chris@40 198 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 199 exit (1) ;
Chris@40 200 } ;
Chris@40 201
Chris@40 202 if (data_out [k] * data_in [k] < 0.0)
Chris@40 203 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 204 exit (1) ;
Chris@40 205 } ;
Chris@40 206
Chris@40 207 if (fabs (data_out [k]) > 1.0)
Chris@40 208 continue ;
Chris@40 209
Chris@40 210 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 211 if (diff > clip_max_diff)
Chris@40 212 clip_max_diff = diff ;
Chris@40 213 } ;
Chris@40 214
Chris@40 215 if (clip_max_diff < 1e-20)
Chris@40 216 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 217 exit (1) ;
Chris@40 218 } ;
Chris@40 219
Chris@40 220 if (clip_max_diff > 1.0 / 0x8000)
Chris@40 221 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 222 exit (1) ;
Chris@40 223 } ;
Chris@40 224
Chris@40 225 /* Check the un-normalized data. */
Chris@40 226 clip_max_diff = 0.0 ;
Chris@40 227 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
Chris@40 228 { if (fabs (data_in [k]) > maxval)
Chris@40 229 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 230 exit (1) ;
Chris@40 231 } ;
Chris@40 232
Chris@40 233 if (data_out [k] * data_in [k] < 0.0)
Chris@40 234 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 235 exit (1) ;
Chris@40 236 } ;
Chris@40 237
Chris@40 238 if (fabs (data_out [k]) > maxval)
Chris@40 239 continue ;
Chris@40 240
Chris@40 241 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 242 if (diff > clip_max_diff)
Chris@40 243 clip_max_diff = diff ;
Chris@40 244 } ;
Chris@40 245
Chris@40 246 if (clip_max_diff < 1e-20)
Chris@40 247 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 248 exit (1) ;
Chris@40 249 } ;
Chris@40 250
Chris@40 251 if (clip_max_diff > 1.0)
Chris@40 252 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
Chris@40 253 exit (1) ;
Chris@40 254 } ;
Chris@40 255
Chris@40 256 printf ("ok\n") ;
Chris@40 257 unlink (filename) ;
Chris@40 258 } /* flt_scale_clip_test_16 */
Chris@40 259
Chris@40 260 static void
Chris@40 261 flt_scale_clip_test_24 (const char *filename, int filetype, float maxval)
Chris@40 262 { SNDFILE *file ;
Chris@40 263 SF_INFO sfinfo ;
Chris@40 264 int k ;
Chris@40 265 float *data_out, *data_in ;
Chris@40 266 double diff, clip_max_diff ;
Chris@40 267
Chris@40 268 print_test_name ("flt_scale_clip_test_24", filename) ;
Chris@40 269
Chris@40 270 data_out = buffer_out.flt ;
Chris@40 271 data_in = buffer_in.flt ;
Chris@40 272
Chris@40 273 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 274 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
Chris@40 275 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
Chris@40 276 } ;
Chris@40 277
Chris@40 278 sfinfo.samplerate = 44100 ;
Chris@40 279 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 280 sfinfo.channels = 1 ;
Chris@40 281 sfinfo.format = filetype ;
Chris@40 282
Chris@40 283 /*
Chris@40 284 ** Write two versions of the data:
Chris@40 285 ** normalized and clipped
Chris@40 286 ** un-normalized and clipped.
Chris@40 287 */
Chris@40 288
Chris@40 289 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 290 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 291 test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 292 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@40 293 test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 294 sf_close (file) ;
Chris@40 295
Chris@40 296 memset (&buffer_in, 0, sizeof (buffer_in)) ;
Chris@40 297
Chris@40 298 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 299
Chris@40 300 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 301
Chris@40 302 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 303 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 304 exit (1) ;
Chris@40 305 } ;
Chris@40 306
Chris@40 307 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 308 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 309 exit (1) ;
Chris@40 310 } ;
Chris@40 311
Chris@40 312 if (sfinfo.channels != 1)
Chris@40 313 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 314 exit (1) ;
Chris@40 315 } ;
Chris@40 316
Chris@40 317 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 318
Chris@40 319 test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 320 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@40 321 test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 322 sf_close (file) ;
Chris@40 323
Chris@40 324 /* Check normalized version. */
Chris@40 325 clip_max_diff = 0.0 ;
Chris@40 326 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 327 { if (fabs (data_in [k]) > 1.0)
Chris@40 328 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 329 exit (1) ;
Chris@40 330 } ;
Chris@40 331
Chris@40 332 if (data_out [k] * data_in [k] < 0.0)
Chris@40 333 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 334 exit (1) ;
Chris@40 335 } ;
Chris@40 336
Chris@40 337 if (fabs (data_out [k]) > 1.0)
Chris@40 338 continue ;
Chris@40 339
Chris@40 340 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 341 if (diff > clip_max_diff)
Chris@40 342 clip_max_diff = diff ;
Chris@40 343 } ;
Chris@40 344
Chris@40 345 if (clip_max_diff < 1e-20)
Chris@40 346 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 347 exit (1) ;
Chris@40 348 } ;
Chris@40 349
Chris@40 350 if (clip_max_diff > 1.0 / 0x800000)
Chris@40 351 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 352 exit (1) ;
Chris@40 353 } ;
Chris@40 354
Chris@40 355 /* Check the un-normalized data. */
Chris@40 356 clip_max_diff = 0.0 ;
Chris@40 357 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
Chris@40 358 { if (fabs (data_in [k]) > maxval)
Chris@40 359 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 360 exit (1) ;
Chris@40 361 } ;
Chris@40 362
Chris@40 363 if (data_out [k] * data_in [k] < 0.0)
Chris@40 364 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 365 exit (1) ;
Chris@40 366 } ;
Chris@40 367
Chris@40 368 if (fabs (data_out [k]) > maxval)
Chris@40 369 continue ;
Chris@40 370
Chris@40 371 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 372 if (diff > clip_max_diff)
Chris@40 373 clip_max_diff = diff ;
Chris@40 374 } ;
Chris@40 375
Chris@40 376 if (clip_max_diff < 1e-20)
Chris@40 377 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 378 exit (1) ;
Chris@40 379 } ;
Chris@40 380
Chris@40 381 if (clip_max_diff > 1.0)
Chris@40 382 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
Chris@40 383 exit (1) ;
Chris@40 384 } ;
Chris@40 385
Chris@40 386 printf ("ok\n") ;
Chris@40 387 unlink (filename) ;
Chris@40 388 } /* flt_scale_clip_test_24 */
Chris@40 389
Chris@40 390 static void
Chris@40 391 flt_scale_clip_test_32 (const char *filename, int filetype, float maxval)
Chris@40 392 { SNDFILE *file ;
Chris@40 393 SF_INFO sfinfo ;
Chris@40 394 int k ;
Chris@40 395 float *data_out, *data_in ;
Chris@40 396 double diff, clip_max_diff ;
Chris@40 397
Chris@40 398 print_test_name ("flt_scale_clip_test_32", filename) ;
Chris@40 399
Chris@40 400 data_out = buffer_out.flt ;
Chris@40 401 data_in = buffer_in.flt ;
Chris@40 402
Chris@40 403 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 404 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
Chris@40 405 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
Chris@40 406 } ;
Chris@40 407
Chris@40 408 sfinfo.samplerate = 44100 ;
Chris@40 409 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 410 sfinfo.channels = 1 ;
Chris@40 411 sfinfo.format = filetype ;
Chris@40 412
Chris@40 413 /*
Chris@40 414 ** Write two versions of the data:
Chris@40 415 ** normalized and clipped
Chris@40 416 ** un-normalized and clipped.
Chris@40 417 */
Chris@40 418
Chris@40 419 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 420 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 421 test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 422 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@40 423 test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 424 sf_close (file) ;
Chris@40 425
Chris@40 426 memset (&buffer_in, 0, sizeof (buffer_in)) ;
Chris@40 427
Chris@40 428 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 429
Chris@40 430 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 431
Chris@40 432 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 433 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 434 exit (1) ;
Chris@40 435 } ;
Chris@40 436
Chris@40 437 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 438 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 439 exit (1) ;
Chris@40 440 } ;
Chris@40 441
Chris@40 442 if (sfinfo.channels != 1)
Chris@40 443 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 444 exit (1) ;
Chris@40 445 } ;
Chris@40 446
Chris@40 447 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 448
Chris@40 449 test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 450 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@40 451 test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 452 sf_close (file) ;
Chris@40 453
Chris@40 454 /* Check normalized version. */
Chris@40 455 clip_max_diff = 0.0 ;
Chris@40 456 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 457 { if (fabs (data_in [k]) > 1.0)
Chris@40 458 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 459 exit (1) ;
Chris@40 460 } ;
Chris@40 461
Chris@40 462 if (data_out [k] * data_in [k] < 0.0)
Chris@40 463 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 464 exit (1) ;
Chris@40 465 } ;
Chris@40 466
Chris@40 467 if (fabs (data_out [k]) > 1.0)
Chris@40 468 continue ;
Chris@40 469
Chris@40 470 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 471 if (diff > clip_max_diff)
Chris@40 472 clip_max_diff = diff ;
Chris@40 473 } ;
Chris@40 474
Chris@40 475 if (clip_max_diff < 1e-20)
Chris@40 476 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 477 exit (1) ;
Chris@40 478 } ;
Chris@40 479
Chris@40 480 if (clip_max_diff > 1.0 / 0x80000000)
Chris@40 481 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 482 exit (1) ;
Chris@40 483 } ;
Chris@40 484
Chris@40 485 /* Check the un-normalized data. */
Chris@40 486 clip_max_diff = 0.0 ;
Chris@40 487 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
Chris@40 488 { if (fabs (data_in [k]) > maxval)
Chris@40 489 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 490 exit (1) ;
Chris@40 491 } ;
Chris@40 492
Chris@40 493 if (data_out [k] * data_in [k] < 0.0)
Chris@40 494 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 495 exit (1) ;
Chris@40 496 } ;
Chris@40 497
Chris@40 498 if (fabs (data_out [k]) > maxval)
Chris@40 499 continue ;
Chris@40 500
Chris@40 501 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 502 if (diff > clip_max_diff)
Chris@40 503 clip_max_diff = diff ;
Chris@40 504 } ;
Chris@40 505
Chris@40 506 if (clip_max_diff < 1e-20)
Chris@40 507 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 508 exit (1) ;
Chris@40 509 } ;
Chris@40 510
Chris@40 511 if (clip_max_diff > 1.0)
Chris@40 512 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
Chris@40 513 exit (1) ;
Chris@40 514 } ;
Chris@40 515
Chris@40 516 printf ("ok\n") ;
Chris@40 517 unlink (filename) ;
Chris@40 518 } /* flt_scale_clip_test_32 */
Chris@40 519
Chris@40 520 static void
Chris@40 521 flt_scale_clip_test_08 (const char *filename, int filetype, float maxval)
Chris@40 522 { SNDFILE *file ;
Chris@40 523 SF_INFO sfinfo ;
Chris@40 524 int k ;
Chris@40 525 float *data_out, *data_in ;
Chris@40 526 double diff, clip_max_diff ;
Chris@40 527
Chris@40 528 print_test_name ("flt_scale_clip_test_08", filename) ;
Chris@40 529
Chris@40 530 data_out = buffer_out.flt ;
Chris@40 531 data_in = buffer_in.flt ;
Chris@40 532
Chris@40 533 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 534 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
Chris@40 535 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
Chris@40 536 } ;
Chris@40 537
Chris@40 538 sfinfo.samplerate = 44100 ;
Chris@40 539 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 540 sfinfo.channels = 1 ;
Chris@40 541 sfinfo.format = filetype ;
Chris@40 542
Chris@40 543 /*
Chris@40 544 ** Write two versions of the data:
Chris@40 545 ** normalized and clipped
Chris@40 546 ** un-normalized and clipped.
Chris@40 547 */
Chris@40 548
Chris@40 549 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 550 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 551 test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 552 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@40 553 test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 554 sf_close (file) ;
Chris@40 555
Chris@40 556 memset (&buffer_in, 0, sizeof (buffer_in)) ;
Chris@40 557
Chris@40 558 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 559
Chris@40 560 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 561
Chris@40 562 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 563 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 564 exit (1) ;
Chris@40 565 } ;
Chris@40 566
Chris@40 567 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 568 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 569 exit (1) ;
Chris@40 570 } ;
Chris@40 571
Chris@40 572 if (sfinfo.channels != 1)
Chris@40 573 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 574 exit (1) ;
Chris@40 575 } ;
Chris@40 576
Chris@40 577 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 578
Chris@40 579 test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 580 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@40 581 test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 582 sf_close (file) ;
Chris@40 583
Chris@40 584 /* Check normalized version. */
Chris@40 585 clip_max_diff = 0.0 ;
Chris@40 586 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 587 { if (fabs (data_in [k]) > 1.0)
Chris@40 588 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 589 exit (1) ;
Chris@40 590 } ;
Chris@40 591
Chris@40 592 if (data_out [k] * data_in [k] < 0.0)
Chris@40 593 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 594 exit (1) ;
Chris@40 595 } ;
Chris@40 596
Chris@40 597 if (fabs (data_out [k]) > 1.0)
Chris@40 598 continue ;
Chris@40 599
Chris@40 600 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 601 if (diff > clip_max_diff)
Chris@40 602 clip_max_diff = diff ;
Chris@40 603 } ;
Chris@40 604
Chris@40 605 if (clip_max_diff < 1e-20)
Chris@40 606 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 607 exit (1) ;
Chris@40 608 } ;
Chris@40 609
Chris@40 610 if (clip_max_diff > 1.0 / 0x80)
Chris@40 611 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 612 exit (1) ;
Chris@40 613 } ;
Chris@40 614
Chris@40 615 /* Check the un-normalized data. */
Chris@40 616 clip_max_diff = 0.0 ;
Chris@40 617 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
Chris@40 618 { if (fabs (data_in [k]) > maxval)
Chris@40 619 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 620 exit (1) ;
Chris@40 621 } ;
Chris@40 622
Chris@40 623 if (data_out [k] * data_in [k] < 0.0)
Chris@40 624 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 625 exit (1) ;
Chris@40 626 } ;
Chris@40 627
Chris@40 628 if (fabs (data_out [k]) > maxval)
Chris@40 629 continue ;
Chris@40 630
Chris@40 631 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 632 if (diff > clip_max_diff)
Chris@40 633 clip_max_diff = diff ;
Chris@40 634 } ;
Chris@40 635
Chris@40 636 if (clip_max_diff < 1e-20)
Chris@40 637 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 638 exit (1) ;
Chris@40 639 } ;
Chris@40 640
Chris@40 641 if (clip_max_diff > 1.0)
Chris@40 642 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
Chris@40 643 exit (1) ;
Chris@40 644 } ;
Chris@40 645
Chris@40 646 printf ("ok\n") ;
Chris@40 647 unlink (filename) ;
Chris@40 648 } /* flt_scale_clip_test_08 */
Chris@40 649
Chris@40 650
Chris@40 651
Chris@40 652 static void
Chris@40 653 dbl_scale_clip_test_16 (const char *filename, int filetype, float maxval)
Chris@40 654 { SNDFILE *file ;
Chris@40 655 SF_INFO sfinfo ;
Chris@40 656 int k ;
Chris@40 657 double *data_out, *data_in ;
Chris@40 658 double diff, clip_max_diff ;
Chris@40 659
Chris@40 660 print_test_name ("dbl_scale_clip_test_16", filename) ;
Chris@40 661
Chris@40 662 data_out = buffer_out.dbl ;
Chris@40 663 data_in = buffer_in.dbl ;
Chris@40 664
Chris@40 665 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 666 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
Chris@40 667 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
Chris@40 668 } ;
Chris@40 669
Chris@40 670 sfinfo.samplerate = 44100 ;
Chris@40 671 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 672 sfinfo.channels = 1 ;
Chris@40 673 sfinfo.format = filetype ;
Chris@40 674
Chris@40 675 /*
Chris@40 676 ** Write two versions of the data:
Chris@40 677 ** normalized and clipped
Chris@40 678 ** un-normalized and clipped.
Chris@40 679 */
Chris@40 680
Chris@40 681 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 682 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 683 test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 684 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@40 685 test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 686 sf_close (file) ;
Chris@40 687
Chris@40 688 memset (&buffer_in, 0, sizeof (buffer_in)) ;
Chris@40 689
Chris@40 690 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 691
Chris@40 692 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 693
Chris@40 694 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 695 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 696 exit (1) ;
Chris@40 697 } ;
Chris@40 698
Chris@40 699 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 700 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 701 exit (1) ;
Chris@40 702 } ;
Chris@40 703
Chris@40 704 if (sfinfo.channels != 1)
Chris@40 705 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 706 exit (1) ;
Chris@40 707 } ;
Chris@40 708
Chris@40 709 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 710
Chris@40 711 test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 712 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@40 713 test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 714 sf_close (file) ;
Chris@40 715
Chris@40 716 /* Check normalized version. */
Chris@40 717 clip_max_diff = 0.0 ;
Chris@40 718 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 719 { if (fabs (data_in [k]) > 1.0)
Chris@40 720 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 721 exit (1) ;
Chris@40 722 } ;
Chris@40 723
Chris@40 724 if (data_out [k] * data_in [k] < 0.0)
Chris@40 725 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 726 exit (1) ;
Chris@40 727 } ;
Chris@40 728
Chris@40 729 if (fabs (data_out [k]) > 1.0)
Chris@40 730 continue ;
Chris@40 731
Chris@40 732 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 733 if (diff > clip_max_diff)
Chris@40 734 clip_max_diff = diff ;
Chris@40 735 } ;
Chris@40 736
Chris@40 737 if (clip_max_diff < 1e-20)
Chris@40 738 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 739 exit (1) ;
Chris@40 740 } ;
Chris@40 741
Chris@40 742 if (clip_max_diff > 1.0 / 0x8000)
Chris@40 743 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 744 exit (1) ;
Chris@40 745 } ;
Chris@40 746
Chris@40 747 /* Check the un-normalized data. */
Chris@40 748 clip_max_diff = 0.0 ;
Chris@40 749 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
Chris@40 750 { if (fabs (data_in [k]) > maxval)
Chris@40 751 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 752 exit (1) ;
Chris@40 753 } ;
Chris@40 754
Chris@40 755 if (data_out [k] * data_in [k] < 0.0)
Chris@40 756 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 757 exit (1) ;
Chris@40 758 } ;
Chris@40 759
Chris@40 760 if (fabs (data_out [k]) > maxval)
Chris@40 761 continue ;
Chris@40 762
Chris@40 763 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 764 if (diff > clip_max_diff)
Chris@40 765 clip_max_diff = diff ;
Chris@40 766 } ;
Chris@40 767
Chris@40 768 if (clip_max_diff < 1e-20)
Chris@40 769 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 770 exit (1) ;
Chris@40 771 } ;
Chris@40 772
Chris@40 773 if (clip_max_diff > 1.0)
Chris@40 774 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
Chris@40 775 exit (1) ;
Chris@40 776 } ;
Chris@40 777
Chris@40 778 printf ("ok\n") ;
Chris@40 779 unlink (filename) ;
Chris@40 780 } /* dbl_scale_clip_test_16 */
Chris@40 781
Chris@40 782 static void
Chris@40 783 dbl_scale_clip_test_24 (const char *filename, int filetype, float maxval)
Chris@40 784 { SNDFILE *file ;
Chris@40 785 SF_INFO sfinfo ;
Chris@40 786 int k ;
Chris@40 787 double *data_out, *data_in ;
Chris@40 788 double diff, clip_max_diff ;
Chris@40 789
Chris@40 790 print_test_name ("dbl_scale_clip_test_24", filename) ;
Chris@40 791
Chris@40 792 data_out = buffer_out.dbl ;
Chris@40 793 data_in = buffer_in.dbl ;
Chris@40 794
Chris@40 795 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 796 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
Chris@40 797 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
Chris@40 798 } ;
Chris@40 799
Chris@40 800 sfinfo.samplerate = 44100 ;
Chris@40 801 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 802 sfinfo.channels = 1 ;
Chris@40 803 sfinfo.format = filetype ;
Chris@40 804
Chris@40 805 /*
Chris@40 806 ** Write two versions of the data:
Chris@40 807 ** normalized and clipped
Chris@40 808 ** un-normalized and clipped.
Chris@40 809 */
Chris@40 810
Chris@40 811 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 812 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 813 test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 814 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@40 815 test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 816 sf_close (file) ;
Chris@40 817
Chris@40 818 memset (&buffer_in, 0, sizeof (buffer_in)) ;
Chris@40 819
Chris@40 820 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 821
Chris@40 822 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 823
Chris@40 824 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 825 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 826 exit (1) ;
Chris@40 827 } ;
Chris@40 828
Chris@40 829 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 830 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 831 exit (1) ;
Chris@40 832 } ;
Chris@40 833
Chris@40 834 if (sfinfo.channels != 1)
Chris@40 835 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 836 exit (1) ;
Chris@40 837 } ;
Chris@40 838
Chris@40 839 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 840
Chris@40 841 test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 842 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@40 843 test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 844 sf_close (file) ;
Chris@40 845
Chris@40 846 /* Check normalized version. */
Chris@40 847 clip_max_diff = 0.0 ;
Chris@40 848 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 849 { if (fabs (data_in [k]) > 1.0)
Chris@40 850 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 851 exit (1) ;
Chris@40 852 } ;
Chris@40 853
Chris@40 854 if (data_out [k] * data_in [k] < 0.0)
Chris@40 855 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 856 exit (1) ;
Chris@40 857 } ;
Chris@40 858
Chris@40 859 if (fabs (data_out [k]) > 1.0)
Chris@40 860 continue ;
Chris@40 861
Chris@40 862 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 863 if (diff > clip_max_diff)
Chris@40 864 clip_max_diff = diff ;
Chris@40 865 } ;
Chris@40 866
Chris@40 867 if (clip_max_diff < 1e-20)
Chris@40 868 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 869 exit (1) ;
Chris@40 870 } ;
Chris@40 871
Chris@40 872 if (clip_max_diff > 1.0 / 0x800000)
Chris@40 873 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 874 exit (1) ;
Chris@40 875 } ;
Chris@40 876
Chris@40 877 /* Check the un-normalized data. */
Chris@40 878 clip_max_diff = 0.0 ;
Chris@40 879 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
Chris@40 880 { if (fabs (data_in [k]) > maxval)
Chris@40 881 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 882 exit (1) ;
Chris@40 883 } ;
Chris@40 884
Chris@40 885 if (data_out [k] * data_in [k] < 0.0)
Chris@40 886 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 887 exit (1) ;
Chris@40 888 } ;
Chris@40 889
Chris@40 890 if (fabs (data_out [k]) > maxval)
Chris@40 891 continue ;
Chris@40 892
Chris@40 893 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 894 if (diff > clip_max_diff)
Chris@40 895 clip_max_diff = diff ;
Chris@40 896 } ;
Chris@40 897
Chris@40 898 if (clip_max_diff < 1e-20)
Chris@40 899 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 900 exit (1) ;
Chris@40 901 } ;
Chris@40 902
Chris@40 903 if (clip_max_diff > 1.0)
Chris@40 904 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
Chris@40 905 exit (1) ;
Chris@40 906 } ;
Chris@40 907
Chris@40 908 printf ("ok\n") ;
Chris@40 909 unlink (filename) ;
Chris@40 910 } /* dbl_scale_clip_test_24 */
Chris@40 911
Chris@40 912 static void
Chris@40 913 dbl_scale_clip_test_32 (const char *filename, int filetype, float maxval)
Chris@40 914 { SNDFILE *file ;
Chris@40 915 SF_INFO sfinfo ;
Chris@40 916 int k ;
Chris@40 917 double *data_out, *data_in ;
Chris@40 918 double diff, clip_max_diff ;
Chris@40 919
Chris@40 920 print_test_name ("dbl_scale_clip_test_32", filename) ;
Chris@40 921
Chris@40 922 data_out = buffer_out.dbl ;
Chris@40 923 data_in = buffer_in.dbl ;
Chris@40 924
Chris@40 925 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 926 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
Chris@40 927 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
Chris@40 928 } ;
Chris@40 929
Chris@40 930 sfinfo.samplerate = 44100 ;
Chris@40 931 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 932 sfinfo.channels = 1 ;
Chris@40 933 sfinfo.format = filetype ;
Chris@40 934
Chris@40 935 /*
Chris@40 936 ** Write two versions of the data:
Chris@40 937 ** normalized and clipped
Chris@40 938 ** un-normalized and clipped.
Chris@40 939 */
Chris@40 940
Chris@40 941 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 942 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 943 test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 944 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@40 945 test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 946 sf_close (file) ;
Chris@40 947
Chris@40 948 memset (&buffer_in, 0, sizeof (buffer_in)) ;
Chris@40 949
Chris@40 950 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 951
Chris@40 952 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 953
Chris@40 954 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 955 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 956 exit (1) ;
Chris@40 957 } ;
Chris@40 958
Chris@40 959 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 960 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 961 exit (1) ;
Chris@40 962 } ;
Chris@40 963
Chris@40 964 if (sfinfo.channels != 1)
Chris@40 965 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 966 exit (1) ;
Chris@40 967 } ;
Chris@40 968
Chris@40 969 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 970
Chris@40 971 test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 972 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@40 973 test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 974 sf_close (file) ;
Chris@40 975
Chris@40 976 /* Check normalized version. */
Chris@40 977 clip_max_diff = 0.0 ;
Chris@40 978 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 979 { if (fabs (data_in [k]) > 1.0)
Chris@40 980 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 981 exit (1) ;
Chris@40 982 } ;
Chris@40 983
Chris@40 984 if (data_out [k] * data_in [k] < 0.0)
Chris@40 985 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 986 exit (1) ;
Chris@40 987 } ;
Chris@40 988
Chris@40 989 if (fabs (data_out [k]) > 1.0)
Chris@40 990 continue ;
Chris@40 991
Chris@40 992 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 993 if (diff > clip_max_diff)
Chris@40 994 clip_max_diff = diff ;
Chris@40 995 } ;
Chris@40 996
Chris@40 997 if (clip_max_diff < 1e-20)
Chris@40 998 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 999 exit (1) ;
Chris@40 1000 } ;
Chris@40 1001
Chris@40 1002 if (clip_max_diff > 1.0 / 0x80000000)
Chris@40 1003 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 1004 exit (1) ;
Chris@40 1005 } ;
Chris@40 1006
Chris@40 1007 /* Check the un-normalized data. */
Chris@40 1008 clip_max_diff = 0.0 ;
Chris@40 1009 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
Chris@40 1010 { if (fabs (data_in [k]) > maxval)
Chris@40 1011 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 1012 exit (1) ;
Chris@40 1013 } ;
Chris@40 1014
Chris@40 1015 if (data_out [k] * data_in [k] < 0.0)
Chris@40 1016 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 1017 exit (1) ;
Chris@40 1018 } ;
Chris@40 1019
Chris@40 1020 if (fabs (data_out [k]) > maxval)
Chris@40 1021 continue ;
Chris@40 1022
Chris@40 1023 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 1024 if (diff > clip_max_diff)
Chris@40 1025 clip_max_diff = diff ;
Chris@40 1026 } ;
Chris@40 1027
Chris@40 1028 if (clip_max_diff < 1e-20)
Chris@40 1029 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 1030 exit (1) ;
Chris@40 1031 } ;
Chris@40 1032
Chris@40 1033 if (clip_max_diff > 1.0)
Chris@40 1034 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
Chris@40 1035 exit (1) ;
Chris@40 1036 } ;
Chris@40 1037
Chris@40 1038 printf ("ok\n") ;
Chris@40 1039 unlink (filename) ;
Chris@40 1040 } /* dbl_scale_clip_test_32 */
Chris@40 1041
Chris@40 1042 static void
Chris@40 1043 dbl_scale_clip_test_08 (const char *filename, int filetype, float maxval)
Chris@40 1044 { SNDFILE *file ;
Chris@40 1045 SF_INFO sfinfo ;
Chris@40 1046 int k ;
Chris@40 1047 double *data_out, *data_in ;
Chris@40 1048 double diff, clip_max_diff ;
Chris@40 1049
Chris@40 1050 print_test_name ("dbl_scale_clip_test_08", filename) ;
Chris@40 1051
Chris@40 1052 data_out = buffer_out.dbl ;
Chris@40 1053 data_in = buffer_in.dbl ;
Chris@40 1054
Chris@40 1055 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 1056 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
Chris@40 1057 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
Chris@40 1058 } ;
Chris@40 1059
Chris@40 1060 sfinfo.samplerate = 44100 ;
Chris@40 1061 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 1062 sfinfo.channels = 1 ;
Chris@40 1063 sfinfo.format = filetype ;
Chris@40 1064
Chris@40 1065 /*
Chris@40 1066 ** Write two versions of the data:
Chris@40 1067 ** normalized and clipped
Chris@40 1068 ** un-normalized and clipped.
Chris@40 1069 */
Chris@40 1070
Chris@40 1071 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1072 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 1073 test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 1074 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@40 1075 test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 1076 sf_close (file) ;
Chris@40 1077
Chris@40 1078 memset (&buffer_in, 0, sizeof (buffer_in)) ;
Chris@40 1079
Chris@40 1080 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1081
Chris@40 1082 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 1083
Chris@40 1084 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 1085 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 1086 exit (1) ;
Chris@40 1087 } ;
Chris@40 1088
Chris@40 1089 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 1090 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 1091 exit (1) ;
Chris@40 1092 } ;
Chris@40 1093
Chris@40 1094 if (sfinfo.channels != 1)
Chris@40 1095 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 1096 exit (1) ;
Chris@40 1097 } ;
Chris@40 1098
Chris@40 1099 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 1100
Chris@40 1101 test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 1102 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@40 1103 test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 1104 sf_close (file) ;
Chris@40 1105
Chris@40 1106 /* Check normalized version. */
Chris@40 1107 clip_max_diff = 0.0 ;
Chris@40 1108 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 1109 { if (fabs (data_in [k]) > 1.0)
Chris@40 1110 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 1111 exit (1) ;
Chris@40 1112 } ;
Chris@40 1113
Chris@40 1114 if (data_out [k] * data_in [k] < 0.0)
Chris@40 1115 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 1116 exit (1) ;
Chris@40 1117 } ;
Chris@40 1118
Chris@40 1119 if (fabs (data_out [k]) > 1.0)
Chris@40 1120 continue ;
Chris@40 1121
Chris@40 1122 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 1123 if (diff > clip_max_diff)
Chris@40 1124 clip_max_diff = diff ;
Chris@40 1125 } ;
Chris@40 1126
Chris@40 1127 if (clip_max_diff < 1e-20)
Chris@40 1128 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 1129 exit (1) ;
Chris@40 1130 } ;
Chris@40 1131
Chris@40 1132 if (clip_max_diff > 1.0 / 0x80)
Chris@40 1133 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 1134 exit (1) ;
Chris@40 1135 } ;
Chris@40 1136
Chris@40 1137 /* Check the un-normalized data. */
Chris@40 1138 clip_max_diff = 0.0 ;
Chris@40 1139 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
Chris@40 1140 { if (fabs (data_in [k]) > maxval)
Chris@40 1141 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 1142 exit (1) ;
Chris@40 1143 } ;
Chris@40 1144
Chris@40 1145 if (data_out [k] * data_in [k] < 0.0)
Chris@40 1146 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 1147 exit (1) ;
Chris@40 1148 } ;
Chris@40 1149
Chris@40 1150 if (fabs (data_out [k]) > maxval)
Chris@40 1151 continue ;
Chris@40 1152
Chris@40 1153 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 1154 if (diff > clip_max_diff)
Chris@40 1155 clip_max_diff = diff ;
Chris@40 1156 } ;
Chris@40 1157
Chris@40 1158 if (clip_max_diff < 1e-20)
Chris@40 1159 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 1160 exit (1) ;
Chris@40 1161 } ;
Chris@40 1162
Chris@40 1163 if (clip_max_diff > 1.0)
Chris@40 1164 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
Chris@40 1165 exit (1) ;
Chris@40 1166 } ;
Chris@40 1167
Chris@40 1168 printf ("ok\n") ;
Chris@40 1169 unlink (filename) ;
Chris@40 1170 } /* dbl_scale_clip_test_08 */
Chris@40 1171
Chris@40 1172
Chris@40 1173
Chris@40 1174
Chris@40 1175 /*==============================================================================
Chris@40 1176 */
Chris@40 1177
Chris@40 1178
Chris@40 1179 static void flt_short_clip_read_test (const char *filename, int filetype)
Chris@40 1180 { SNDFILE *file ;
Chris@40 1181 SF_INFO sfinfo ;
Chris@40 1182 float *data_out ;
Chris@40 1183 short *data_in, max_value ;
Chris@40 1184 int k ;
Chris@40 1185
Chris@40 1186 print_test_name ("flt_short_clip_read_test", filename) ;
Chris@40 1187
Chris@40 1188 data_out = buffer_out.flt ;
Chris@40 1189 data_in = buffer_in.s ;
Chris@40 1190
Chris@40 1191 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1192 data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
Chris@40 1193 data_out [BUFFER_SIZE / 8] = 1.0 ;
Chris@40 1194 data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
Chris@40 1195 data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
Chris@40 1196 data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
Chris@40 1197
Chris@40 1198 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1199 sfinfo.samplerate = 44100 ;
Chris@40 1200 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 1201 sfinfo.channels = 1 ;
Chris@40 1202 sfinfo.format = filetype ;
Chris@40 1203
Chris@40 1204 /* Save unclipped data to the file. */
Chris@40 1205 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1206 test_write_float_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1207 sf_close (file) ;
Chris@40 1208
Chris@40 1209 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1210
Chris@40 1211 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1212 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
Chris@40 1213
Chris@40 1214 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 1215
Chris@40 1216 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 1217 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 1218 exit (1) ;
Chris@40 1219 } ;
Chris@40 1220
Chris@40 1221 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 1222 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 1223 exit (1) ;
Chris@40 1224 } ;
Chris@40 1225
Chris@40 1226 if (sfinfo.channels != 1)
Chris@40 1227 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 1228 exit (1) ;
Chris@40 1229 } ;
Chris@40 1230
Chris@40 1231 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 1232
Chris@40 1233 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 1234 test_read_short_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1235 /*-sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;-*/
Chris@40 1236 sf_close (file) ;
Chris@40 1237
Chris@40 1238 /* Check the first half. */
Chris@40 1239 max_value = 0 ;
Chris@40 1240 for (k = 0 ; k < sfinfo.frames ; k++)
Chris@40 1241 { /* Check if data_out has different sign from data_in. */
Chris@40 1242 if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
Chris@40 1243 { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ;
Chris@40 1244 exit (1) ;
Chris@40 1245 } ;
Chris@40 1246 max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
Chris@40 1247 } ;
Chris@40 1248
Chris@40 1249 unlink (filename) ;
Chris@40 1250 puts ("ok") ;
Chris@40 1251 } /* flt_short_clip_read_test */
Chris@40 1252 static void flt_int_clip_read_test (const char *filename, int filetype)
Chris@40 1253 { SNDFILE *file ;
Chris@40 1254 SF_INFO sfinfo ;
Chris@40 1255 float *data_out ;
Chris@40 1256 int *data_in, max_value ;
Chris@40 1257 int k ;
Chris@40 1258
Chris@40 1259 print_test_name ("flt_int_clip_read_test", filename) ;
Chris@40 1260
Chris@40 1261 data_out = buffer_out.flt ;
Chris@40 1262 data_in = buffer_in.i ;
Chris@40 1263
Chris@40 1264 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1265 data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
Chris@40 1266 data_out [BUFFER_SIZE / 8] = 1.0 ;
Chris@40 1267 data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
Chris@40 1268 data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
Chris@40 1269 data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
Chris@40 1270
Chris@40 1271 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1272 sfinfo.samplerate = 44100 ;
Chris@40 1273 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 1274 sfinfo.channels = 1 ;
Chris@40 1275 sfinfo.format = filetype ;
Chris@40 1276
Chris@40 1277 /* Save unclipped data to the file. */
Chris@40 1278 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1279 test_write_float_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1280 sf_close (file) ;
Chris@40 1281
Chris@40 1282 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1283
Chris@40 1284 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1285 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
Chris@40 1286
Chris@40 1287 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 1288
Chris@40 1289 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 1290 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 1291 exit (1) ;
Chris@40 1292 } ;
Chris@40 1293
Chris@40 1294 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 1295 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 1296 exit (1) ;
Chris@40 1297 } ;
Chris@40 1298
Chris@40 1299 if (sfinfo.channels != 1)
Chris@40 1300 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 1301 exit (1) ;
Chris@40 1302 } ;
Chris@40 1303
Chris@40 1304 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 1305
Chris@40 1306 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 1307 test_read_int_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1308 /*-sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;-*/
Chris@40 1309 sf_close (file) ;
Chris@40 1310
Chris@40 1311 /* Check the first half. */
Chris@40 1312 max_value = 0 ;
Chris@40 1313 for (k = 0 ; k < sfinfo.frames ; k++)
Chris@40 1314 { /* Check if data_out has different sign from data_in. */
Chris@40 1315 if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
Chris@40 1316 { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ;
Chris@40 1317 exit (1) ;
Chris@40 1318 } ;
Chris@40 1319 max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
Chris@40 1320 } ;
Chris@40 1321
Chris@40 1322 unlink (filename) ;
Chris@40 1323 puts ("ok") ;
Chris@40 1324 } /* flt_int_clip_read_test */
Chris@40 1325
Chris@40 1326 static void dbl_short_clip_read_test (const char *filename, int filetype)
Chris@40 1327 { SNDFILE *file ;
Chris@40 1328 SF_INFO sfinfo ;
Chris@40 1329 double *data_out ;
Chris@40 1330 short *data_in, max_value ;
Chris@40 1331 int k ;
Chris@40 1332
Chris@40 1333 print_test_name ("dbl_short_clip_read_test", filename) ;
Chris@40 1334
Chris@40 1335 data_out = buffer_out.dbl ;
Chris@40 1336 data_in = buffer_in.s ;
Chris@40 1337
Chris@40 1338 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1339 data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
Chris@40 1340 data_out [BUFFER_SIZE / 8] = 1.0 ;
Chris@40 1341 data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
Chris@40 1342 data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
Chris@40 1343 data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
Chris@40 1344
Chris@40 1345 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1346 sfinfo.samplerate = 44100 ;
Chris@40 1347 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 1348 sfinfo.channels = 1 ;
Chris@40 1349 sfinfo.format = filetype ;
Chris@40 1350
Chris@40 1351 /* Save unclipped data to the file. */
Chris@40 1352 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1353 test_write_double_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1354 sf_close (file) ;
Chris@40 1355
Chris@40 1356 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1357
Chris@40 1358 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1359 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
Chris@40 1360
Chris@40 1361 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 1362
Chris@40 1363 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 1364 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 1365 exit (1) ;
Chris@40 1366 } ;
Chris@40 1367
Chris@40 1368 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 1369 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 1370 exit (1) ;
Chris@40 1371 } ;
Chris@40 1372
Chris@40 1373 if (sfinfo.channels != 1)
Chris@40 1374 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 1375 exit (1) ;
Chris@40 1376 } ;
Chris@40 1377
Chris@40 1378 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 1379
Chris@40 1380 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 1381 test_read_short_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1382 /*-sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;-*/
Chris@40 1383 sf_close (file) ;
Chris@40 1384
Chris@40 1385 /* Check the first half. */
Chris@40 1386 max_value = 0 ;
Chris@40 1387 for (k = 0 ; k < sfinfo.frames ; k++)
Chris@40 1388 { /* Check if data_out has different sign from data_in. */
Chris@40 1389 if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
Chris@40 1390 { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ;
Chris@40 1391 exit (1) ;
Chris@40 1392 } ;
Chris@40 1393 max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
Chris@40 1394 } ;
Chris@40 1395
Chris@40 1396 unlink (filename) ;
Chris@40 1397 puts ("ok") ;
Chris@40 1398 } /* dbl_short_clip_read_test */
Chris@40 1399 static void dbl_int_clip_read_test (const char *filename, int filetype)
Chris@40 1400 { SNDFILE *file ;
Chris@40 1401 SF_INFO sfinfo ;
Chris@40 1402 double *data_out ;
Chris@40 1403 int *data_in, max_value ;
Chris@40 1404 int k ;
Chris@40 1405
Chris@40 1406 print_test_name ("dbl_int_clip_read_test", filename) ;
Chris@40 1407
Chris@40 1408 data_out = buffer_out.dbl ;
Chris@40 1409 data_in = buffer_in.i ;
Chris@40 1410
Chris@40 1411 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1412 data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
Chris@40 1413 data_out [BUFFER_SIZE / 8] = 1.0 ;
Chris@40 1414 data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
Chris@40 1415 data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
Chris@40 1416 data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
Chris@40 1417
Chris@40 1418 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1419 sfinfo.samplerate = 44100 ;
Chris@40 1420 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 1421 sfinfo.channels = 1 ;
Chris@40 1422 sfinfo.format = filetype ;
Chris@40 1423
Chris@40 1424 /* Save unclipped data to the file. */
Chris@40 1425 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1426 test_write_double_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1427 sf_close (file) ;
Chris@40 1428
Chris@40 1429 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1430
Chris@40 1431 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1432 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
Chris@40 1433
Chris@40 1434 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 1435
Chris@40 1436 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 1437 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 1438 exit (1) ;
Chris@40 1439 } ;
Chris@40 1440
Chris@40 1441 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 1442 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 1443 exit (1) ;
Chris@40 1444 } ;
Chris@40 1445
Chris@40 1446 if (sfinfo.channels != 1)
Chris@40 1447 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 1448 exit (1) ;
Chris@40 1449 } ;
Chris@40 1450
Chris@40 1451 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 1452
Chris@40 1453 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 1454 test_read_int_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1455 /*-sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;-*/
Chris@40 1456 sf_close (file) ;
Chris@40 1457
Chris@40 1458 /* Check the first half. */
Chris@40 1459 max_value = 0 ;
Chris@40 1460 for (k = 0 ; k < sfinfo.frames ; k++)
Chris@40 1461 { /* Check if data_out has different sign from data_in. */
Chris@40 1462 if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
Chris@40 1463 { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ;
Chris@40 1464 exit (1) ;
Chris@40 1465 } ;
Chris@40 1466 max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
Chris@40 1467 } ;
Chris@40 1468
Chris@40 1469 unlink (filename) ;
Chris@40 1470 puts ("ok") ;
Chris@40 1471 } /* dbl_int_clip_read_test */
Chris@40 1472
Chris@40 1473
Chris@40 1474 /*==============================================================================
Chris@40 1475 */
Chris@40 1476
Chris@40 1477
Chris@40 1478 static void short_flt_scale_write_test (const char *filename, int filetype)
Chris@40 1479 { SNDFILE *file ;
Chris@40 1480 SF_INFO sfinfo ;
Chris@40 1481 short *data_out ;
Chris@40 1482 float *data_in, max_value ;
Chris@40 1483 int k ;
Chris@40 1484
Chris@40 1485 print_test_name ("short_flt_clip_write_test", filename) ;
Chris@40 1486
Chris@40 1487 data_out = buffer_out.s ;
Chris@40 1488 data_in = buffer_in.flt ;
Chris@40 1489
Chris@40 1490 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1491 data_out [k] = lrintf (0x7FFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
Chris@40 1492
Chris@40 1493 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1494 sfinfo.samplerate = 44100 ;
Chris@40 1495 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 1496 sfinfo.channels = 1 ;
Chris@40 1497 sfinfo.format = filetype ;
Chris@40 1498
Chris@40 1499 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1500 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1501 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
Chris@40 1502 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1503 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
Chris@40 1504 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1505 sf_close (file) ;
Chris@40 1506
Chris@40 1507 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1508
Chris@40 1509 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1510
Chris@40 1511 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 1512
Chris@40 1513 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 1514 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 1515 exit (1) ;
Chris@40 1516 } ;
Chris@40 1517
Chris@40 1518 if (sfinfo.frames != 3 * BUFFER_SIZE)
Chris@40 1519 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, 3 * BUFFER_SIZE, sfinfo.frames) ;
Chris@40 1520 exit (1) ;
Chris@40 1521 } ;
Chris@40 1522
Chris@40 1523 if (sfinfo.channels != 1)
Chris@40 1524 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 1525 exit (1) ;
Chris@40 1526 } ;
Chris@40 1527
Chris@40 1528 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 1529
Chris@40 1530 /* Check the first section. */
Chris@40 1531 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1532
Chris@40 1533 max_value = 0.0 ;
Chris@40 1534 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1535 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1536
Chris@40 1537 if (max_value < 1000.0)
Chris@40 1538 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
Chris@40 1539 exit (1) ;
Chris@40 1540 } ;
Chris@40 1541
Chris@40 1542 /* Check the second section. */
Chris@40 1543 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1544
Chris@40 1545 max_value = 0.0 ;
Chris@40 1546 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1547 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1548
Chris@40 1549 if (max_value > 1.0)
Chris@40 1550 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
Chris@40 1551 exit (1) ;
Chris@40 1552 } ;
Chris@40 1553
Chris@40 1554 /* Check the third section. */
Chris@40 1555 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1556
Chris@40 1557 max_value = 0.0 ;
Chris@40 1558 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1559 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1560
Chris@40 1561 if (max_value < 1000.0)
Chris@40 1562 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
Chris@40 1563 exit (1) ;
Chris@40 1564 } ;
Chris@40 1565
Chris@40 1566 sf_close (file) ;
Chris@40 1567
Chris@40 1568 unlink (filename) ;
Chris@40 1569 puts ("ok") ;
Chris@40 1570 } /* short_flt_scale_write_test */
Chris@40 1571 static void short_dbl_scale_write_test (const char *filename, int filetype)
Chris@40 1572 { SNDFILE *file ;
Chris@40 1573 SF_INFO sfinfo ;
Chris@40 1574 short *data_out ;
Chris@40 1575 double *data_in, max_value ;
Chris@40 1576 int k ;
Chris@40 1577
Chris@40 1578 print_test_name ("short_dbl_clip_write_test", filename) ;
Chris@40 1579
Chris@40 1580 data_out = buffer_out.s ;
Chris@40 1581 data_in = buffer_in.dbl ;
Chris@40 1582
Chris@40 1583 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1584 data_out [k] = lrint (0x7FFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
Chris@40 1585
Chris@40 1586 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1587 sfinfo.samplerate = 44100 ;
Chris@40 1588 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 1589 sfinfo.channels = 1 ;
Chris@40 1590 sfinfo.format = filetype ;
Chris@40 1591
Chris@40 1592 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1593 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1594 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
Chris@40 1595 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1596 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
Chris@40 1597 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1598 sf_close (file) ;
Chris@40 1599
Chris@40 1600 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1601
Chris@40 1602 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1603
Chris@40 1604 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 1605
Chris@40 1606 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 1607 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 1608 exit (1) ;
Chris@40 1609 } ;
Chris@40 1610
Chris@40 1611 if (sfinfo.frames != 3 * BUFFER_SIZE)
Chris@40 1612 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, 3 * BUFFER_SIZE, sfinfo.frames) ;
Chris@40 1613 exit (1) ;
Chris@40 1614 } ;
Chris@40 1615
Chris@40 1616 if (sfinfo.channels != 1)
Chris@40 1617 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 1618 exit (1) ;
Chris@40 1619 } ;
Chris@40 1620
Chris@40 1621 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 1622
Chris@40 1623 /* Check the first section. */
Chris@40 1624 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1625
Chris@40 1626 max_value = 0.0 ;
Chris@40 1627 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1628 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1629
Chris@40 1630 if (max_value < 1000.0)
Chris@40 1631 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
Chris@40 1632 exit (1) ;
Chris@40 1633 } ;
Chris@40 1634
Chris@40 1635 /* Check the second section. */
Chris@40 1636 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1637
Chris@40 1638 max_value = 0.0 ;
Chris@40 1639 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1640 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1641
Chris@40 1642 if (max_value > 1.0)
Chris@40 1643 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
Chris@40 1644 exit (1) ;
Chris@40 1645 } ;
Chris@40 1646
Chris@40 1647 /* Check the third section. */
Chris@40 1648 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1649
Chris@40 1650 max_value = 0.0 ;
Chris@40 1651 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1652 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1653
Chris@40 1654 if (max_value < 1000.0)
Chris@40 1655 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
Chris@40 1656 exit (1) ;
Chris@40 1657 } ;
Chris@40 1658
Chris@40 1659 sf_close (file) ;
Chris@40 1660
Chris@40 1661 unlink (filename) ;
Chris@40 1662 puts ("ok") ;
Chris@40 1663 } /* short_dbl_scale_write_test */
Chris@40 1664
Chris@40 1665 static void int_flt_scale_write_test (const char *filename, int filetype)
Chris@40 1666 { SNDFILE *file ;
Chris@40 1667 SF_INFO sfinfo ;
Chris@40 1668 int *data_out ;
Chris@40 1669 float *data_in, max_value ;
Chris@40 1670 int k ;
Chris@40 1671
Chris@40 1672 print_test_name ("int_flt_clip_write_test", filename) ;
Chris@40 1673
Chris@40 1674 data_out = buffer_out.i ;
Chris@40 1675 data_in = buffer_in.flt ;
Chris@40 1676
Chris@40 1677 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1678 data_out [k] = lrintf (0x7FFFFFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
Chris@40 1679
Chris@40 1680 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1681 sfinfo.samplerate = 44100 ;
Chris@40 1682 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 1683 sfinfo.channels = 1 ;
Chris@40 1684 sfinfo.format = filetype ;
Chris@40 1685
Chris@40 1686 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1687 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1688 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
Chris@40 1689 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1690 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
Chris@40 1691 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1692 sf_close (file) ;
Chris@40 1693
Chris@40 1694 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1695
Chris@40 1696 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1697
Chris@40 1698 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 1699
Chris@40 1700 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 1701 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 1702 exit (1) ;
Chris@40 1703 } ;
Chris@40 1704
Chris@40 1705 if (sfinfo.frames != 3 * BUFFER_SIZE)
Chris@40 1706 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, 3 * BUFFER_SIZE, sfinfo.frames) ;
Chris@40 1707 exit (1) ;
Chris@40 1708 } ;
Chris@40 1709
Chris@40 1710 if (sfinfo.channels != 1)
Chris@40 1711 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 1712 exit (1) ;
Chris@40 1713 } ;
Chris@40 1714
Chris@40 1715 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 1716
Chris@40 1717 /* Check the first section. */
Chris@40 1718 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1719
Chris@40 1720 max_value = 0.0 ;
Chris@40 1721 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1722 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1723
Chris@40 1724 if (max_value < 1000.0)
Chris@40 1725 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
Chris@40 1726 exit (1) ;
Chris@40 1727 } ;
Chris@40 1728
Chris@40 1729 /* Check the second section. */
Chris@40 1730 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1731
Chris@40 1732 max_value = 0.0 ;
Chris@40 1733 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1734 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1735
Chris@40 1736 if (max_value > 1.0)
Chris@40 1737 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
Chris@40 1738 exit (1) ;
Chris@40 1739 } ;
Chris@40 1740
Chris@40 1741 /* Check the third section. */
Chris@40 1742 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1743
Chris@40 1744 max_value = 0.0 ;
Chris@40 1745 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1746 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1747
Chris@40 1748 if (max_value < 1000.0)
Chris@40 1749 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
Chris@40 1750 exit (1) ;
Chris@40 1751 } ;
Chris@40 1752
Chris@40 1753 sf_close (file) ;
Chris@40 1754
Chris@40 1755 unlink (filename) ;
Chris@40 1756 puts ("ok") ;
Chris@40 1757 } /* int_flt_scale_write_test */
Chris@40 1758 static void int_dbl_scale_write_test (const char *filename, int filetype)
Chris@40 1759 { SNDFILE *file ;
Chris@40 1760 SF_INFO sfinfo ;
Chris@40 1761 int *data_out ;
Chris@40 1762 double *data_in, max_value ;
Chris@40 1763 int k ;
Chris@40 1764
Chris@40 1765 print_test_name ("int_dbl_clip_write_test", filename) ;
Chris@40 1766
Chris@40 1767 data_out = buffer_out.i ;
Chris@40 1768 data_in = buffer_in.dbl ;
Chris@40 1769
Chris@40 1770 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1771 data_out [k] = lrint (0x7FFFFFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
Chris@40 1772
Chris@40 1773 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1774 sfinfo.samplerate = 44100 ;
Chris@40 1775 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 1776 sfinfo.channels = 1 ;
Chris@40 1777 sfinfo.format = filetype ;
Chris@40 1778
Chris@40 1779 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1780 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1781 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
Chris@40 1782 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1783 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
Chris@40 1784 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 1785 sf_close (file) ;
Chris@40 1786
Chris@40 1787 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 1788
Chris@40 1789 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 1790
Chris@40 1791 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 1792
Chris@40 1793 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 1794 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 1795 exit (1) ;
Chris@40 1796 } ;
Chris@40 1797
Chris@40 1798 if (sfinfo.frames != 3 * BUFFER_SIZE)
Chris@40 1799 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, 3 * BUFFER_SIZE, sfinfo.frames) ;
Chris@40 1800 exit (1) ;
Chris@40 1801 } ;
Chris@40 1802
Chris@40 1803 if (sfinfo.channels != 1)
Chris@40 1804 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 1805 exit (1) ;
Chris@40 1806 } ;
Chris@40 1807
Chris@40 1808 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 1809
Chris@40 1810 /* Check the first section. */
Chris@40 1811 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1812
Chris@40 1813 max_value = 0.0 ;
Chris@40 1814 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1815 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1816
Chris@40 1817 if (max_value < 1000.0)
Chris@40 1818 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
Chris@40 1819 exit (1) ;
Chris@40 1820 } ;
Chris@40 1821
Chris@40 1822 /* Check the second section. */
Chris@40 1823 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1824
Chris@40 1825 max_value = 0.0 ;
Chris@40 1826 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1827 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1828
Chris@40 1829 if (max_value > 1.0)
Chris@40 1830 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
Chris@40 1831 exit (1) ;
Chris@40 1832 } ;
Chris@40 1833
Chris@40 1834 /* Check the third section. */
Chris@40 1835 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 1836
Chris@40 1837 max_value = 0.0 ;
Chris@40 1838 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 1839 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 1840
Chris@40 1841 if (max_value < 1000.0)
Chris@40 1842 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
Chris@40 1843 exit (1) ;
Chris@40 1844 } ;
Chris@40 1845
Chris@40 1846 sf_close (file) ;
Chris@40 1847
Chris@40 1848 unlink (filename) ;
Chris@40 1849 puts ("ok") ;
Chris@40 1850 } /* int_dbl_scale_write_test */
Chris@40 1851
Chris@40 1852
Chris@40 1853