annotate src/libsndfile-1.0.27/tests/scale_clip_test.tpl @ 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 [+ AutoGen5 template c +]
Chris@40 2 /*
Chris@40 3 ** Copyright (C) 1999-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@40 4 **
Chris@40 5 ** This program is free software; you can redistribute it and/or modify
Chris@40 6 ** it under the terms of the GNU General Public License as published by
Chris@40 7 ** the Free Software Foundation; either version 2 of the License, or
Chris@40 8 ** (at your option) any later version.
Chris@40 9 **
Chris@40 10 ** This program is distributed in the hope that it will be useful,
Chris@40 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@40 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@40 13 ** GNU General Public License for more details.
Chris@40 14 **
Chris@40 15 ** You should have received a copy of the GNU General Public License
Chris@40 16 ** along with this program; if not, write to the Free Software
Chris@40 17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Chris@40 18 */
Chris@40 19
Chris@40 20 #include "sfconfig.h"
Chris@40 21
Chris@40 22 #include <stdio.h>
Chris@40 23 #include <stdlib.h>
Chris@40 24 #include <string.h>
Chris@40 25 #include <math.h>
Chris@40 26 #include <inttypes.h>
Chris@40 27
Chris@40 28 #if HAVE_UNISTD_H
Chris@40 29 #include <unistd.h>
Chris@40 30 #endif
Chris@40 31
Chris@40 32 #include <sndfile.h>
Chris@40 33
Chris@40 34 #include "utils.h"
Chris@40 35
Chris@40 36 #ifndef M_PI
Chris@40 37 #define M_PI 3.14159265358979323846264338
Chris@40 38 #endif
Chris@40 39
Chris@40 40 #define HALF_BUFFER_SIZE (1 << 12)
Chris@40 41 #define BUFFER_SIZE (2 * HALF_BUFFER_SIZE)
Chris@40 42
Chris@40 43 #define SINE_AMP 1.1
Chris@40 44 #define MAX_ERROR 0.0202
Chris@40 45
Chris@40 46 [+ FOR float_type +]
Chris@40 47 [+ FOR data_type
Chris@40 48 +]static void [+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +] (const char *filename, int filetype, float maxval) ;
Chris@40 49 [+ ENDFOR data_type
Chris@40 50 +][+ ENDFOR float_type +]
Chris@40 51
Chris@40 52 [+ FOR float_type +]
Chris@40 53 [+ FOR int_type
Chris@40 54 +]static void [+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test (const char *filename, int filetype) ;
Chris@40 55 [+ ENDFOR int_type
Chris@40 56 +][+ ENDFOR float_type +]
Chris@40 57
Chris@40 58 [+ FOR int_type +]
Chris@40 59 [+ FOR float_type
Chris@40 60 +]static void [+ (get "int_type_name") +]_[+ (get "float_short_name") +]_scale_write_test (const char *filename, int filetype) ;
Chris@40 61 [+ ENDFOR float_type
Chris@40 62 +][+ ENDFOR int_type +]
Chris@40 63
Chris@40 64 typedef union
Chris@40 65 { double dbl [BUFFER_SIZE] ;
Chris@40 66 float flt [BUFFER_SIZE] ;
Chris@40 67 int i [BUFFER_SIZE] ;
Chris@40 68 short s [BUFFER_SIZE] ;
Chris@40 69 } BUFFER ;
Chris@40 70
Chris@40 71 /* Data buffer. */
Chris@40 72 static BUFFER buffer_out ;
Chris@40 73 static BUFFER buffer_in ;
Chris@40 74
Chris@40 75 int
Chris@40 76 main (void)
Chris@40 77 {
Chris@40 78 flt_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
Chris@40 79 flt_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
Chris@40 80
Chris@40 81 dbl_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
Chris@40 82 dbl_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
Chris@40 83
Chris@40 84 /*
Chris@40 85 ** Now use SF_FORMAT_AU where possible because it allows both
Chris@40 86 ** big and little endian files.
Chris@40 87 */
Chris@40 88
Chris@40 89 flt_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
Chris@40 90 flt_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
Chris@40 91 flt_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
Chris@40 92 flt_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
Chris@40 93 flt_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
Chris@40 94 flt_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
Chris@40 95
Chris@40 96 dbl_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
Chris@40 97 dbl_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
Chris@40 98 dbl_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
Chris@40 99 dbl_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
Chris@40 100 dbl_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
Chris@40 101 dbl_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
Chris@40 102
Chris@40 103 flt_short_clip_read_test ("flt_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
Chris@40 104 flt_int_clip_read_test ("flt_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
Chris@40 105 dbl_short_clip_read_test ("dbl_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
Chris@40 106 dbl_int_clip_read_test ("dbl_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
Chris@40 107
Chris@40 108 short_flt_scale_write_test ("short_flt.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
Chris@40 109 int_flt_scale_write_test ("int_flt.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
Chris@40 110 short_dbl_scale_write_test ("short_dbl.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
Chris@40 111 int_dbl_scale_write_test ("int_dbl.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
Chris@40 112
Chris@40 113 return 0 ;
Chris@40 114 } /* main */
Chris@40 115
Chris@40 116 /*============================================================================================
Chris@40 117 ** Here are the test functions.
Chris@40 118 */
Chris@40 119
Chris@40 120 [+ FOR float_type +]
Chris@40 121 [+ FOR data_type
Chris@40 122 +]static void
Chris@40 123 [+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +] (const char *filename, int filetype, float maxval)
Chris@40 124 { SNDFILE *file ;
Chris@40 125 SF_INFO sfinfo ;
Chris@40 126 int k ;
Chris@40 127 [+ (get "float_type_name") +] *data_out, *data_in ;
Chris@40 128 double diff, clip_max_diff ;
Chris@40 129
Chris@40 130 print_test_name ("[+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +]", filename) ;
Chris@40 131
Chris@40 132 data_out = buffer_out.[+ (get "float_short_name") +] ;
Chris@40 133 data_in = buffer_in.[+ (get "float_short_name") +] ;
Chris@40 134
Chris@40 135 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 136 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
Chris@40 137 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
Chris@40 138 } ;
Chris@40 139
Chris@40 140 sfinfo.samplerate = 44100 ;
Chris@40 141 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 142 sfinfo.channels = 1 ;
Chris@40 143 sfinfo.format = filetype ;
Chris@40 144
Chris@40 145 /*
Chris@40 146 ** Write two versions of the data:
Chris@40 147 ** normalized and clipped
Chris@40 148 ** un-normalized and clipped.
Chris@40 149 */
Chris@40 150
Chris@40 151 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 152 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 153 test_write_[+ (get "float_type_name") +]_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 154 sf_command (file, SFC_SET_NORM_[+ (get "float_upper_name") +], NULL, SF_FALSE) ;
Chris@40 155 test_write_[+ (get "float_type_name") +]_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 156 sf_close (file) ;
Chris@40 157
Chris@40 158 memset (&buffer_in, 0, sizeof (buffer_in)) ;
Chris@40 159
Chris@40 160 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 161
Chris@40 162 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 163
Chris@40 164 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 165 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 166 exit (1) ;
Chris@40 167 } ;
Chris@40 168
Chris@40 169 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 170 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 171 exit (1) ;
Chris@40 172 } ;
Chris@40 173
Chris@40 174 if (sfinfo.channels != 1)
Chris@40 175 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 176 exit (1) ;
Chris@40 177 } ;
Chris@40 178
Chris@40 179 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 180
Chris@40 181 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 182 sf_command (file, SFC_SET_NORM_[+ (get "float_upper_name") +], NULL, SF_FALSE) ;
Chris@40 183 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
Chris@40 184 sf_close (file) ;
Chris@40 185
Chris@40 186 /* Check normalized version. */
Chris@40 187 clip_max_diff = 0.0 ;
Chris@40 188 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
Chris@40 189 { if (fabs (data_in [k]) > 1.0)
Chris@40 190 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 191 exit (1) ;
Chris@40 192 } ;
Chris@40 193
Chris@40 194 if (data_out [k] * data_in [k] < 0.0)
Chris@40 195 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 196 exit (1) ;
Chris@40 197 } ;
Chris@40 198
Chris@40 199 if (fabs (data_out [k]) > 1.0)
Chris@40 200 continue ;
Chris@40 201
Chris@40 202 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 203 if (diff > clip_max_diff)
Chris@40 204 clip_max_diff = diff ;
Chris@40 205 } ;
Chris@40 206
Chris@40 207 if (clip_max_diff < 1e-20)
Chris@40 208 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 209 exit (1) ;
Chris@40 210 } ;
Chris@40 211
Chris@40 212 if (clip_max_diff > [+ (get "error_val") +])
Chris@40 213 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 214 exit (1) ;
Chris@40 215 } ;
Chris@40 216
Chris@40 217 /* Check the un-normalized data. */
Chris@40 218 clip_max_diff = 0.0 ;
Chris@40 219 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
Chris@40 220 { if (fabs (data_in [k]) > maxval)
Chris@40 221 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
Chris@40 222 exit (1) ;
Chris@40 223 } ;
Chris@40 224
Chris@40 225 if (data_out [k] * data_in [k] < 0.0)
Chris@40 226 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
Chris@40 227 exit (1) ;
Chris@40 228 } ;
Chris@40 229
Chris@40 230 if (fabs (data_out [k]) > maxval)
Chris@40 231 continue ;
Chris@40 232
Chris@40 233 diff = fabs (data_out [k] - data_in [k]) ;
Chris@40 234 if (diff > clip_max_diff)
Chris@40 235 clip_max_diff = diff ;
Chris@40 236 } ;
Chris@40 237
Chris@40 238 if (clip_max_diff < 1e-20)
Chris@40 239 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
Chris@40 240 exit (1) ;
Chris@40 241 } ;
Chris@40 242
Chris@40 243 if (clip_max_diff > 1.0)
Chris@40 244 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
Chris@40 245 exit (1) ;
Chris@40 246 } ;
Chris@40 247
Chris@40 248 printf ("ok\n") ;
Chris@40 249 unlink (filename) ;
Chris@40 250 } /* [+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +] */
Chris@40 251
Chris@40 252 [+ ENDFOR data_type
Chris@40 253 +]
Chris@40 254 [+ ENDFOR float_type +]
Chris@40 255
Chris@40 256 /*==============================================================================
Chris@40 257 */
Chris@40 258
Chris@40 259 [+ FOR float_type +]
Chris@40 260 [+ FOR int_type
Chris@40 261 +]static void [+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test (const char *filename, int filetype)
Chris@40 262 { SNDFILE *file ;
Chris@40 263 SF_INFO sfinfo ;
Chris@40 264 [+ (get "float_type_name") +] *data_out ;
Chris@40 265 [+ (get "int_type_name") +] *data_in, max_value ;
Chris@40 266 int k ;
Chris@40 267
Chris@40 268 print_test_name ("[+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test", filename) ;
Chris@40 269
Chris@40 270 data_out = buffer_out.[+ (get "float_short_name") +] ;
Chris@40 271 data_in = buffer_in.[+ (get "int_short_name") +] ;
Chris@40 272
Chris@40 273 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 274 data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
Chris@40 275 data_out [BUFFER_SIZE / 8] = 1.0 ;
Chris@40 276 data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
Chris@40 277 data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
Chris@40 278 data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
Chris@40 279
Chris@40 280 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 281 sfinfo.samplerate = 44100 ;
Chris@40 282 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 283 sfinfo.channels = 1 ;
Chris@40 284 sfinfo.format = filetype ;
Chris@40 285
Chris@40 286 /* Save unclipped data to the file. */
Chris@40 287 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 288 test_write_[+ (get "float_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 289 sf_close (file) ;
Chris@40 290
Chris@40 291 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 292
Chris@40 293 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 294 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
Chris@40 295
Chris@40 296 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 297
Chris@40 298 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 299 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 300 exit (1) ;
Chris@40 301 } ;
Chris@40 302
Chris@40 303 if (sfinfo.frames != BUFFER_SIZE)
Chris@40 304 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 305 exit (1) ;
Chris@40 306 } ;
Chris@40 307
Chris@40 308 if (sfinfo.channels != 1)
Chris@40 309 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 310 exit (1) ;
Chris@40 311 } ;
Chris@40 312
Chris@40 313 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 314
Chris@40 315 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
Chris@40 316 test_read_[+ (get "int_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 317 /*-sf_command (file, SFC_SET_NORM_[+ (get "float_upper_name") +], NULL, SF_FALSE) ;-*/
Chris@40 318 sf_close (file) ;
Chris@40 319
Chris@40 320 /* Check the first half. */
Chris@40 321 max_value = 0 ;
Chris@40 322 for (k = 0 ; k < sfinfo.frames ; k++)
Chris@40 323 { /* Check if data_out has different sign from data_in. */
Chris@40 324 if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
Chris@40 325 { 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 326 exit (1) ;
Chris@40 327 } ;
Chris@40 328 max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
Chris@40 329 } ;
Chris@40 330
Chris@40 331 unlink (filename) ;
Chris@40 332 puts ("ok") ;
Chris@40 333 } /* [+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test */
Chris@40 334 [+ ENDFOR int_type
Chris@40 335 +][+ ENDFOR float_type +]
Chris@40 336
Chris@40 337 /*==============================================================================
Chris@40 338 */
Chris@40 339
Chris@40 340 [+ FOR int_type +]
Chris@40 341 [+ FOR float_type
Chris@40 342 +]static void [+ (get "int_type_name") +]_[+ (get "float_short_name") +]_scale_write_test (const char *filename, int filetype)
Chris@40 343 { SNDFILE *file ;
Chris@40 344 SF_INFO sfinfo ;
Chris@40 345 [+ (get "int_type_name") +] *data_out ;
Chris@40 346 [+ (get "float_type_name") +] *data_in, max_value ;
Chris@40 347 int k ;
Chris@40 348
Chris@40 349 print_test_name ("[+ (get "int_type_name") +]_[+ (get "float_short_name") +]_clip_write_test", filename) ;
Chris@40 350
Chris@40 351 data_out = buffer_out.[+ (get "int_short_name") +] ;
Chris@40 352 data_in = buffer_in.[+ (get "float_short_name") +] ;
Chris@40 353
Chris@40 354 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 355 data_out [k] = [+ (get "float_to_int") +] ([+ (get "int_max_value") +] * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
Chris@40 356
Chris@40 357 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 358 sfinfo.samplerate = 44100 ;
Chris@40 359 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 360 sfinfo.channels = 1 ;
Chris@40 361 sfinfo.format = filetype ;
Chris@40 362
Chris@40 363 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 364 test_write_[+ (get "int_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 365 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
Chris@40 366 test_write_[+ (get "int_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 367 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
Chris@40 368 test_write_[+ (get "int_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
Chris@40 369 sf_close (file) ;
Chris@40 370
Chris@40 371 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 372
Chris@40 373 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 374
Chris@40 375 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
Chris@40 376
Chris@40 377 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@40 378 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 379 exit (1) ;
Chris@40 380 } ;
Chris@40 381
Chris@40 382 if (sfinfo.frames != 3 * BUFFER_SIZE)
Chris@40 383 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %" PRId64 ").\n\n", __LINE__, 3 * BUFFER_SIZE, sfinfo.frames) ;
Chris@40 384 exit (1) ;
Chris@40 385 } ;
Chris@40 386
Chris@40 387 if (sfinfo.channels != 1)
Chris@40 388 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
Chris@40 389 exit (1) ;
Chris@40 390 } ;
Chris@40 391
Chris@40 392 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 393
Chris@40 394 /* Check the first section. */
Chris@40 395 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 396
Chris@40 397 max_value = 0.0 ;
Chris@40 398 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 399 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 400
Chris@40 401 if (max_value < 1000.0)
Chris@40 402 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
Chris@40 403 exit (1) ;
Chris@40 404 } ;
Chris@40 405
Chris@40 406 /* Check the second section. */
Chris@40 407 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 408
Chris@40 409 max_value = 0.0 ;
Chris@40 410 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 411 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 412
Chris@40 413 if (max_value > 1.0)
Chris@40 414 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
Chris@40 415 exit (1) ;
Chris@40 416 } ;
Chris@40 417
Chris@40 418 /* Check the third section. */
Chris@40 419 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
Chris@40 420
Chris@40 421 max_value = 0.0 ;
Chris@40 422 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 423 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
Chris@40 424
Chris@40 425 if (max_value < 1000.0)
Chris@40 426 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
Chris@40 427 exit (1) ;
Chris@40 428 } ;
Chris@40 429
Chris@40 430 sf_close (file) ;
Chris@40 431
Chris@40 432 unlink (filename) ;
Chris@40 433 puts ("ok") ;
Chris@40 434 } /* [+ (get "int_type_name") +]_[+ (get "float_short_name") +]_scale_write_test */
Chris@40 435 [+ ENDFOR float_type
Chris@40 436 +][+ ENDFOR int_type +]
Chris@40 437
Chris@40 438