annotate src/libsndfile-1.0.25/tests/scale_clip_test.tpl @ 36:55ece8862b6d

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