annotate src/libsndfile-1.0.25/tests/lossy_comp_test.c @ 23:619f715526df sv_v2.1

Update Vamp plugin SDK to 2.5
author Chris Cannam
date Thu, 09 May 2013 10:52:46 +0100
parents c7265573341e
children
rev   line source
Chris@0 1 /*
Chris@0 2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@0 3 **
Chris@0 4 ** This program is free software; you can redistribute it and/or modify
Chris@0 5 ** it under the terms of the GNU General Public License as published by
Chris@0 6 ** the Free Software Foundation; either version 2 of the License, or
Chris@0 7 ** (at your option) any later version.
Chris@0 8 **
Chris@0 9 ** This program is distributed in the hope that it will be useful,
Chris@0 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 ** GNU General Public License for more details.
Chris@0 13 **
Chris@0 14 ** You should have received a copy of the GNU General Public License
Chris@0 15 ** along with this program; if not, write to the Free Software
Chris@0 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Chris@0 17 */
Chris@0 18
Chris@0 19 #include "sfconfig.h"
Chris@0 20
Chris@0 21 #include <stdio.h>
Chris@0 22 #include <stdlib.h>
Chris@0 23 #include <string.h>
Chris@0 24 #include <math.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 <sndfile.h>
Chris@0 31
Chris@0 32 #include "utils.h"
Chris@0 33
Chris@0 34 #define BUFFER_SIZE (1<<14) /* Should be (1<<14) */
Chris@0 35 #define SAMPLE_RATE 11025
Chris@0 36
Chris@0 37 #ifndef M_PI
Chris@0 38 #define M_PI 3.14159265358979323846264338
Chris@0 39 #endif
Chris@0 40
Chris@0 41 #define LCT_MAX(x,y) ((x) > (y) ? (x) : (y))
Chris@0 42
Chris@0 43 static void lcomp_test_short (const char *filename, int filetype, int chan, double margin) ;
Chris@0 44 static void lcomp_test_int (const char *filename, int filetype, int chan, double margin) ;
Chris@0 45 static void lcomp_test_float (const char *filename, int filetype, int chan, double margin) ;
Chris@0 46 static void lcomp_test_double (const char *filename, int filetype, int chan, double margin) ;
Chris@0 47
Chris@0 48 static void sdlcomp_test_short (const char *filename, int filetype, int chan, double margin) ;
Chris@0 49 static void sdlcomp_test_int (const char *filename, int filetype, int chan, double margin) ;
Chris@0 50 static void sdlcomp_test_float (const char *filename, int filetype, int chan, double margin) ;
Chris@0 51 static void sdlcomp_test_double (const char *filename, int filetype, int chan, double margin) ;
Chris@0 52
Chris@0 53 static void read_raw_test (const char *filename, int filetype, int chan) ;
Chris@0 54
Chris@0 55 static int error_function (double data, double orig, double margin) ;
Chris@0 56 static int decay_response (int k) ;
Chris@0 57
Chris@0 58 static void gen_signal_double (double *data, double scale, int channels, int datalen) ;
Chris@0 59
Chris@0 60 static void smoothed_diff_short (short *data, unsigned int datalen) ;
Chris@0 61 static void smoothed_diff_int (int *data, unsigned int datalen) ;
Chris@0 62 static void smoothed_diff_float (float *data, unsigned int datalen) ;
Chris@0 63 static void smoothed_diff_double (double *data, unsigned int datalen) ;
Chris@0 64
Chris@0 65 static void check_comment (SNDFILE * file, int format, int lineno) ;
Chris@0 66
Chris@0 67 static int is_lossy (int filetype) ;
Chris@0 68
Chris@0 69 /*
Chris@0 70 ** Force the start of these buffers to be double aligned. Sparc-solaris will
Chris@0 71 ** choke if they are not.
Chris@0 72 */
Chris@0 73 typedef union
Chris@0 74 { double d [BUFFER_SIZE + 1] ;
Chris@0 75 float f [BUFFER_SIZE + 1] ;
Chris@0 76 int i [BUFFER_SIZE + 1] ;
Chris@0 77 short s [BUFFER_SIZE + 1] ;
Chris@0 78 char c [BUFFER_SIZE + 1] ;
Chris@0 79 } BUFFER ;
Chris@0 80
Chris@0 81 static BUFFER data_buffer ;
Chris@0 82 static BUFFER orig_buffer ;
Chris@0 83 static BUFFER smooth_buffer ;
Chris@0 84
Chris@0 85 static const char *long_comment =
Chris@0 86 "This is really quite a long comment. It is designed to be long enough "
Chris@0 87 "to screw up the encoders and decoders if the file container format does "
Chris@0 88 "not handle things correctly. If everything is working correctly, the "
Chris@0 89 "decoder will only decode the actual audio data, and not this string at "
Chris@0 90 "the end of the file." ;
Chris@0 91
Chris@0 92 int
Chris@0 93 main (int argc, char *argv [])
Chris@0 94 { int do_all = 0 ;
Chris@0 95 int test_count = 0 ;
Chris@0 96
Chris@0 97 if (argc != 2)
Chris@0 98 { printf ("Usage : %s <test>\n", argv [0]) ;
Chris@0 99 printf (" Where <test> is one of the following:\n") ;
Chris@0 100 printf (" wav_ima - test IMA ADPCM WAV file functions\n") ;
Chris@0 101 printf (" wav_msadpcm - test MS ADPCM WAV file functions\n") ;
Chris@0 102 printf (" wav_gsm610 - test GSM 6.10 WAV file functions\n") ;
Chris@0 103 printf (" wav_ulaw - test u-law WAV file functions\n") ;
Chris@0 104 printf (" wav_alaw - test A-law WAV file functions\n") ;
Chris@0 105 printf (" wve - test Psion WVE file functions\n") ;
Chris@0 106 printf (" all - perform all tests\n") ;
Chris@0 107 exit (1) ;
Chris@0 108 } ;
Chris@0 109
Chris@0 110 do_all = ! strcmp (argv [1], "all") ;
Chris@0 111
Chris@0 112 if (do_all || strcmp (argv [1], "wav_pcm") == 0)
Chris@0 113 { /* This is just a sanity test for PCM encoding. */
Chris@0 114 lcomp_test_short ("pcm.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16, 2, 1e-50) ;
Chris@0 115 lcomp_test_int ("pcm.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_32, 2, 1e-50) ;
Chris@0 116 lcomp_test_short ("pcm.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_16, 2, 1e-50) ;
Chris@0 117 lcomp_test_int ("pcm.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_32, 2, 1e-50) ;
Chris@0 118 /* Lite remove start */
Chris@0 119 lcomp_test_float ("pcm.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT, 2, 1e-50) ;
Chris@0 120 lcomp_test_double ("pcm.wav", SF_FORMAT_WAV | SF_FORMAT_DOUBLE, 2, 1e-50) ;
Chris@0 121 /* Lite remove end */
Chris@0 122
Chris@0 123 read_raw_test ("pcm.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 2) ;
Chris@0 124 test_count++ ;
Chris@0 125 } ;
Chris@0 126
Chris@0 127 /* For all the rest, if the file format supports more than 1 channel, use stereo. */
Chris@0 128 /* Lite remove start */
Chris@0 129 if (do_all || strcmp (argv [1], "wav_ima") == 0)
Chris@0 130 { lcomp_test_short ("ima.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 131 lcomp_test_int ("ima.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.65) ;
Chris@0 132 lcomp_test_float ("ima.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 133 lcomp_test_double ("ima.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 134
Chris@0 135 lcomp_test_short ("ima.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 136 lcomp_test_int ("ima.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 137 lcomp_test_float ("ima.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 138 lcomp_test_double ("ima.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 139
Chris@0 140 sdlcomp_test_short ("ima.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 141 sdlcomp_test_int ("ima.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 142 sdlcomp_test_float ("ima.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 143 sdlcomp_test_double ("ima.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 144 test_count++ ;
Chris@0 145 } ;
Chris@0 146
Chris@0 147 if (do_all || strcmp (argv [1], "wav_msadpcm") == 0)
Chris@0 148 { lcomp_test_short ("msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 149 lcomp_test_int ("msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 150 lcomp_test_float ("msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 151 lcomp_test_double ("msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 152
Chris@0 153 lcomp_test_short ("msadpcm.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 154 lcomp_test_int ("msadpcm.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 155 lcomp_test_float ("msadpcm.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 156 lcomp_test_double ("msadpcm.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 157
Chris@0 158 sdlcomp_test_short ("msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 159 sdlcomp_test_int ("msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 160 sdlcomp_test_float ("msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 161 sdlcomp_test_double ("msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 162
Chris@0 163 test_count++ ;
Chris@0 164 } ;
Chris@0 165
Chris@0 166 if (do_all || strcmp (argv [1], "wav_g721") == 0)
Chris@0 167 { printf ("**** Fix this later : error bound should be 0.06 ****\n") ;
Chris@0 168 lcomp_test_short ("g721.wav", SF_FORMAT_WAV | SF_FORMAT_G721_32, 1, 0.7) ;
Chris@0 169 lcomp_test_int ("g721.wav", SF_FORMAT_WAV | SF_FORMAT_G721_32, 1, 0.7) ;
Chris@0 170
Chris@0 171 lcomp_test_short ("g721.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_G721_32, 1, 0.7) ;
Chris@0 172 lcomp_test_int ("g721.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_G721_32, 1, 0.7) ;
Chris@0 173
Chris@0 174 test_count++ ;
Chris@0 175 } ;
Chris@0 176 /* Lite remove end */
Chris@0 177
Chris@0 178 if (do_all || strcmp (argv [1], "wav_ulaw") == 0)
Chris@0 179 { lcomp_test_short ("ulaw.wav", SF_FORMAT_WAV | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 180 lcomp_test_int ("ulaw.wav", SF_FORMAT_WAV | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 181
Chris@0 182 lcomp_test_short ("ulaw.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 183 lcomp_test_int ("ulaw.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 184
Chris@0 185 /* Lite remove start */
Chris@0 186 lcomp_test_float ("ulaw.wav", SF_FORMAT_WAV | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 187 lcomp_test_double ("ulaw.wav", SF_FORMAT_WAV | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 188 /* Lite remove end */
Chris@0 189
Chris@0 190 read_raw_test ("ulaw.wav", SF_FORMAT_WAV | SF_FORMAT_ULAW, 2) ;
Chris@0 191 test_count++ ;
Chris@0 192 } ;
Chris@0 193
Chris@0 194 if (do_all || strcmp (argv [1], "wav_alaw") == 0)
Chris@0 195 { lcomp_test_short ("alaw.wav", SF_FORMAT_WAV | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 196 lcomp_test_int ("alaw.wav", SF_FORMAT_WAV | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 197 /* Lite remove start */
Chris@0 198 lcomp_test_float ("alaw.wav", SF_FORMAT_WAV | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 199 lcomp_test_double ("alaw.wav", SF_FORMAT_WAV | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 200 /* Lite remove end */
Chris@0 201
Chris@0 202 read_raw_test ("alaw.wav", SF_FORMAT_WAV | SF_FORMAT_ALAW, 2) ;
Chris@0 203 test_count++ ;
Chris@0 204 } ;
Chris@0 205
Chris@0 206 if (do_all || strcmp (argv [1], "wav_gsm610") == 0)
Chris@0 207 { /* Don't do lcomp_test_XXX as the errors are too big. */
Chris@0 208 sdlcomp_test_short ("gsm610.wav", SF_FORMAT_WAV | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 209 sdlcomp_test_int ("gsm610.wav", SF_FORMAT_WAV | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 210
Chris@0 211 sdlcomp_test_short ("gsm610.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 212 sdlcomp_test_int ("gsm610.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 213
Chris@0 214 /* Lite remove start */
Chris@0 215 sdlcomp_test_float ("gsm610.wav", SF_FORMAT_WAV | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 216 sdlcomp_test_double ("gsm610.wav", SF_FORMAT_WAV | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 217 /* Lite remove end */
Chris@0 218 test_count++ ;
Chris@0 219 } ;
Chris@0 220
Chris@0 221 if (do_all || strcmp (argv [1], "aiff_ulaw") == 0)
Chris@0 222 { lcomp_test_short ("ulaw.aiff", SF_FORMAT_AIFF | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 223 lcomp_test_int ("ulaw.aiff", SF_FORMAT_AIFF | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 224 /* Lite remove start */
Chris@0 225 lcomp_test_float ("ulaw.aiff", SF_FORMAT_AIFF | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 226 lcomp_test_double ("ulaw.aiff", SF_FORMAT_AIFF | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 227 /* Lite remove end */
Chris@0 228
Chris@0 229 read_raw_test ("ulaw.aiff", SF_FORMAT_AIFF | SF_FORMAT_ULAW, 2) ;
Chris@0 230 test_count++ ;
Chris@0 231 } ;
Chris@0 232
Chris@0 233 if (do_all || strcmp (argv [1], "aiff_alaw") == 0)
Chris@0 234 { lcomp_test_short ("alaw.aiff", SF_FORMAT_AIFF | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 235 lcomp_test_int ("alaw.aiff", SF_FORMAT_AIFF | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 236 /* Lite remove start */
Chris@0 237 lcomp_test_float ("alaw.aiff", SF_FORMAT_AIFF | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 238 lcomp_test_double ("alaw.aiff", SF_FORMAT_AIFF | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 239 /* Lite remove end */
Chris@0 240
Chris@0 241 read_raw_test ("alaw.aiff", SF_FORMAT_AIFF | SF_FORMAT_ALAW, 2) ;
Chris@0 242 test_count++ ;
Chris@0 243 } ;
Chris@0 244
Chris@0 245 if (do_all || strcmp (argv [1], "aiff_gsm610") == 0)
Chris@0 246 { /* Don't do lcomp_test_XXX as the errors are too big. */
Chris@0 247 sdlcomp_test_short ("gsm610.aiff", SF_FORMAT_AIFF | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 248 sdlcomp_test_int ("gsm610.aiff", SF_FORMAT_AIFF | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 249 /* Lite remove start */
Chris@0 250 sdlcomp_test_float ("gsm610.aiff", SF_FORMAT_AIFF | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 251 sdlcomp_test_double ("gsm610.aiff", SF_FORMAT_AIFF | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 252 /* Lite remove end */
Chris@0 253 test_count++ ;
Chris@0 254 } ;
Chris@0 255
Chris@0 256 if (strcmp (argv [1], "aiff_ima") == 0)
Chris@0 257 { lcomp_test_short ("ima.aiff", SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 258 lcomp_test_int ("ima.aiff", SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 259 /* Lite remove start */
Chris@0 260 lcomp_test_float ("ima.aiff", SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 261 lcomp_test_double ("ima.aiff", SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 262 /* Lite remove end */
Chris@0 263 } ;
Chris@0 264
Chris@0 265 if (do_all || strcmp (argv [1], "au_ulaw") == 0)
Chris@0 266 { lcomp_test_short ("ulaw.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 267 lcomp_test_int ("ulaw.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 268 /* Lite remove start */
Chris@0 269 lcomp_test_float ("ulaw.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 270 lcomp_test_double ("ulaw.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 271 /* Lite remove end */
Chris@0 272 test_count++ ;
Chris@0 273 } ;
Chris@0 274
Chris@0 275 if (do_all || strcmp (argv [1], "au_alaw") == 0)
Chris@0 276 { lcomp_test_short ("alaw.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 277 lcomp_test_int ("alaw.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 278 /* Lite remove start */
Chris@0 279 lcomp_test_float ("alaw.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 280 lcomp_test_double ("alaw.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 281 /* Lite remove end */
Chris@0 282 test_count++ ;
Chris@0 283 } ;
Chris@0 284
Chris@0 285 /* Lite remove start */
Chris@0 286 if (do_all || strcmp (argv [1], "au_g721") == 0)
Chris@0 287 { printf ("**** Fix this later : error bound should be 0.06 ****\n") ;
Chris@0 288 lcomp_test_short ("g721.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_G721_32, 1, 0.7) ;
Chris@0 289 lcomp_test_int ("g721.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G721_32, 1, 0.7) ;
Chris@0 290 lcomp_test_float ("g721.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_G721_32, 1, 0.7) ;
Chris@0 291 lcomp_test_double ("g721.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G721_32, 1, 0.7) ;
Chris@0 292
Chris@0 293 /*- sdlcomp_test_short ("g721.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G721_32, 1, 0.07) ;
Chris@0 294 sdlcomp_test_int ("g721.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_G721_32, 1, 0.07) ;
Chris@0 295 sdlcomp_test_float ("g721.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G721_32, 1, 0.07) ;
Chris@0 296 sdlcomp_test_double ("g721.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_G721_32, 1, 0.12) ;
Chris@0 297 -*/
Chris@0 298 test_count++ ;
Chris@0 299 } ;
Chris@0 300
Chris@0 301 if (do_all || strcmp (argv [1], "au_g723") == 0)
Chris@0 302 { printf ("**** Fix this later : error bound should be 0.16 ****\n") ;
Chris@0 303 lcomp_test_short ("g723_24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_G723_24, 1, 0.7) ;
Chris@0 304 lcomp_test_int ("g723_24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G723_24, 1, 0.7) ;
Chris@0 305 lcomp_test_float ("g723_24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_G723_24, 1, 0.7) ;
Chris@0 306 lcomp_test_double ("g723_24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G723_24, 1, 0.7) ;
Chris@0 307
Chris@0 308 lcomp_test_short ("g723_40.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_G723_40, 1, 0.85) ;
Chris@0 309 lcomp_test_int ("g723_40.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G723_40, 1, 0.84) ;
Chris@0 310 lcomp_test_float ("g723_40.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_G723_40, 1, 0.86) ;
Chris@0 311 lcomp_test_double ("g723_40.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G723_40, 1, 0.86) ;
Chris@0 312
Chris@0 313 /*- sdlcomp_test_short ("g723.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G723_24, 1, 0.15) ;
Chris@0 314 sdlcomp_test_int ("g723.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_G723_24, 1, 0.15) ;
Chris@0 315 sdlcomp_test_float ("g723.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G723_24, 1, 0.15) ;
Chris@0 316 sdlcomp_test_double ("g723.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_G723_24, 1, 0.15) ;
Chris@0 317 -*/
Chris@0 318 test_count++ ;
Chris@0 319 } ;
Chris@0 320 /* Lite remove end */
Chris@0 321
Chris@0 322 if (do_all || strcmp (argv [1], "caf_ulaw") == 0)
Chris@0 323 { lcomp_test_short ("ulaw.caf", SF_FORMAT_CAF | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 324 lcomp_test_int ("ulaw.caf", SF_FORMAT_CAF | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 325 /* Lite remove start */
Chris@0 326 lcomp_test_float ("ulaw.caf", SF_FORMAT_CAF | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 327 lcomp_test_double ("ulaw.caf", SF_FORMAT_CAF | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 328 /* Lite remove end */
Chris@0 329
Chris@0 330 read_raw_test ("ulaw.caf", SF_FORMAT_CAF | SF_FORMAT_ULAW, 2) ;
Chris@0 331 test_count++ ;
Chris@0 332 } ;
Chris@0 333
Chris@0 334 if (do_all || strcmp (argv [1], "caf_alaw") == 0)
Chris@0 335 { lcomp_test_short ("alaw.caf", SF_FORMAT_CAF | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 336 lcomp_test_int ("alaw.caf", SF_FORMAT_CAF | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 337 /* Lite remove start */
Chris@0 338 lcomp_test_float ("alaw.caf", SF_FORMAT_CAF | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 339 lcomp_test_double ("alaw.caf", SF_FORMAT_CAF | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 340 /* Lite remove end */
Chris@0 341
Chris@0 342 read_raw_test ("alaw.caf", SF_FORMAT_CAF | SF_FORMAT_ALAW, 2) ;
Chris@0 343 test_count++ ;
Chris@0 344 } ;
Chris@0 345
Chris@0 346
Chris@0 347 if (do_all || strcmp (argv [1], "raw_ulaw") == 0)
Chris@0 348 { lcomp_test_short ("ulaw.raw", SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 349 lcomp_test_int ("ulaw.raw", SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 350 /* Lite remove start */
Chris@0 351 lcomp_test_float ("ulaw.raw", SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 352 lcomp_test_double ("ulaw.raw", SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 353 /* Lite remove end */
Chris@0 354 test_count++ ;
Chris@0 355 } ;
Chris@0 356
Chris@0 357 if (do_all || strcmp (argv [1], "raw_alaw") == 0)
Chris@0 358 { lcomp_test_short ("alaw.raw", SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 359 lcomp_test_int ("alaw.raw", SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 360 /* Lite remove start */
Chris@0 361 lcomp_test_float ("alaw.raw", SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 362 lcomp_test_double ("alaw.raw", SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 363 /* Lite remove end */
Chris@0 364 test_count++ ;
Chris@0 365 } ;
Chris@0 366
Chris@0 367 if (do_all || strcmp (argv [1], "raw_gsm610") == 0)
Chris@0 368 { /* Don't do lcomp_test_XXX as the errors are too big. */
Chris@0 369 sdlcomp_test_short ("raw.gsm", SF_FORMAT_RAW | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 370 sdlcomp_test_int ("raw.gsm", SF_FORMAT_RAW | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 371 sdlcomp_test_float ("raw.gsm", SF_FORMAT_RAW | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 372 sdlcomp_test_double ("raw.gsm", SF_FORMAT_RAW | SF_FORMAT_GSM610, 1, 0.24) ;
Chris@0 373 test_count++ ;
Chris@0 374 } ;
Chris@0 375
Chris@0 376 if (do_all || strcmp (argv [1], "ogg_vorbis") == 0)
Chris@0 377 { if (HAVE_EXTERNAL_LIBS)
Chris@0 378 { /* Don't do lcomp_test_XXX as the errors are too big. */
Chris@0 379 sdlcomp_test_short ("vorbis.oga", SF_FORMAT_OGG | SF_FORMAT_VORBIS, 1, 0.30) ;
Chris@0 380 sdlcomp_test_int ("vorbis.oga", SF_FORMAT_OGG | SF_FORMAT_VORBIS, 1, 0.30) ;
Chris@0 381 sdlcomp_test_float ("vorbis.oga", SF_FORMAT_OGG | SF_FORMAT_VORBIS, 1, 0.30) ;
Chris@0 382 sdlcomp_test_double ("vorbis.oga", SF_FORMAT_OGG | SF_FORMAT_VORBIS, 1, 0.30) ;
Chris@0 383 }
Chris@0 384 else
Chris@0 385 puts (" No Ogg/Vorbis tests because Ogg/Vorbis support was not compiled in.") ;
Chris@0 386
Chris@0 387 test_count++ ;
Chris@0 388 } ;
Chris@0 389
Chris@0 390 /* Lite remove start */
Chris@0 391 if (do_all || strcmp (argv [1], "ircam_ulaw") == 0)
Chris@0 392 { lcomp_test_short ("ulaw.ircam", SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 393 lcomp_test_int ("ulaw.ircam", SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 394 lcomp_test_float ("ulaw.ircam", SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 395 lcomp_test_double ("ulaw.ircam", SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 396 test_count++ ;
Chris@0 397 } ;
Chris@0 398
Chris@0 399 if (do_all || strcmp (argv [1], "ircam_alaw") == 0)
Chris@0 400 { lcomp_test_short ("alaw.ircam", SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 401 lcomp_test_int ("alaw.ircam", SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 402 lcomp_test_float ("alaw.ircam", SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 403 lcomp_test_double ("alaw.ircam", SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 404 test_count++ ;
Chris@0 405 } ;
Chris@0 406
Chris@0 407 if (do_all || strcmp (argv [1], "nist_ulaw") == 0)
Chris@0 408 { lcomp_test_short ("ulaw.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 409 lcomp_test_int ("ulaw.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 410 lcomp_test_float ("ulaw.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 411 lcomp_test_double ("ulaw.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 412 test_count++ ;
Chris@0 413 } ;
Chris@0 414
Chris@0 415 if (do_all || strcmp (argv [1], "nist_alaw") == 0)
Chris@0 416 { lcomp_test_short ("alaw.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 417 lcomp_test_int ("alaw.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 418 lcomp_test_float ("alaw.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 419 lcomp_test_double ("alaw.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 420 test_count++ ;
Chris@0 421 } ;
Chris@0 422
Chris@0 423 if (do_all || strcmp (argv [1], "voc_ulaw") == 0)
Chris@0 424 { lcomp_test_short ("ulaw.voc", SF_FORMAT_VOC | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 425 lcomp_test_int ("ulaw.voc", SF_FORMAT_VOC | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 426 lcomp_test_float ("ulaw.voc", SF_FORMAT_VOC | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 427 lcomp_test_double ("ulaw.voc", SF_FORMAT_VOC | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 428 test_count++ ;
Chris@0 429 } ;
Chris@0 430
Chris@0 431 if (do_all || strcmp (argv [1], "voc_alaw") == 0)
Chris@0 432 { lcomp_test_short ("alaw.voc", SF_FORMAT_VOC | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 433 lcomp_test_int ("alaw.voc", SF_FORMAT_VOC | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 434 lcomp_test_float ("alaw.voc", SF_FORMAT_VOC | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 435 lcomp_test_double ("alaw.voc", SF_FORMAT_VOC | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 436 test_count++ ;
Chris@0 437 } ;
Chris@0 438 /* Lite remove end */
Chris@0 439
Chris@0 440 if (do_all || strcmp (argv [1], "w64_ulaw") == 0)
Chris@0 441 { lcomp_test_short ("ulaw.w64", SF_FORMAT_W64 | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 442 lcomp_test_int ("ulaw.w64", SF_FORMAT_W64 | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 443 /* Lite remove start */
Chris@0 444 lcomp_test_float ("ulaw.w64", SF_FORMAT_W64 | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 445 lcomp_test_double ("ulaw.w64", SF_FORMAT_W64 | SF_FORMAT_ULAW, 2, 0.04) ;
Chris@0 446 /* Lite remove end */
Chris@0 447
Chris@0 448 read_raw_test ("ulaw.w64", SF_FORMAT_W64 | SF_FORMAT_ULAW, 2) ;
Chris@0 449 test_count++ ;
Chris@0 450 } ;
Chris@0 451
Chris@0 452 if (do_all || strcmp (argv [1], "w64_alaw") == 0)
Chris@0 453 { lcomp_test_short ("alaw.w64", SF_FORMAT_W64 | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 454 lcomp_test_int ("alaw.w64", SF_FORMAT_W64 | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 455 /* Lite remove start */
Chris@0 456 lcomp_test_float ("alaw.w64", SF_FORMAT_W64 | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 457 lcomp_test_double ("alaw.w64", SF_FORMAT_W64 | SF_FORMAT_ALAW, 2, 0.04) ;
Chris@0 458 /* Lite remove end */
Chris@0 459
Chris@0 460 read_raw_test ("alaw.w64", SF_FORMAT_W64 | SF_FORMAT_ALAW, 2) ;
Chris@0 461 test_count++ ;
Chris@0 462 } ;
Chris@0 463
Chris@0 464 /* Lite remove start */
Chris@0 465 if (do_all || strcmp (argv [1], "w64_ima") == 0)
Chris@0 466 { lcomp_test_short ("ima.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 467 lcomp_test_int ("ima.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 468 lcomp_test_float ("ima.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 469 lcomp_test_double ("ima.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 470
Chris@0 471 sdlcomp_test_short ("ima.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 472 sdlcomp_test_int ("ima.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 473 sdlcomp_test_float ("ima.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 474 sdlcomp_test_double ("ima.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM, 2, 0.18) ;
Chris@0 475 test_count++ ;
Chris@0 476 } ;
Chris@0 477
Chris@0 478 if (do_all || strcmp (argv [1], "w64_msadpcm") == 0)
Chris@0 479 { lcomp_test_short ("msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 480 lcomp_test_int ("msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 481 lcomp_test_float ("msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 482 lcomp_test_double ("msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 483
Chris@0 484 sdlcomp_test_short ("msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 485 sdlcomp_test_int ("msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 486 sdlcomp_test_float ("msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 487 sdlcomp_test_double ("msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM, 2, 0.36) ;
Chris@0 488 test_count++ ;
Chris@0 489 } ;
Chris@0 490
Chris@0 491 if (do_all || strcmp (argv [1], "wve") == 0)
Chris@0 492 { lcomp_test_short ("psion.wve", SF_FORMAT_WVE | SF_FORMAT_ALAW, 1, 0.04) ;
Chris@0 493 lcomp_test_int ("psion.wve", SF_FORMAT_WVE | SF_FORMAT_ALAW, 1, 0.04) ;
Chris@0 494 /* Lite remove start */
Chris@0 495 lcomp_test_float ("psion.wve", SF_FORMAT_WVE | SF_FORMAT_ALAW, 1, 0.04) ;
Chris@0 496 lcomp_test_double ("psion.wve", SF_FORMAT_WVE | SF_FORMAT_ALAW, 1, 0.04) ;
Chris@0 497 /* Lite remove end */
Chris@0 498 test_count++ ;
Chris@0 499 } ;
Chris@0 500
Chris@0 501 /* Lite remove end */
Chris@0 502
Chris@0 503 if (do_all || strcmp (argv [1], "w64_gsm610") == 0)
Chris@0 504 { /* Don't do lcomp_test_XXX as the errors are too big. */
Chris@0 505 sdlcomp_test_short ("gsm610.w64", SF_FORMAT_W64 | SF_FORMAT_GSM610, 1, 0.2) ;
Chris@0 506 sdlcomp_test_int ("gsm610.w64", SF_FORMAT_W64 | SF_FORMAT_GSM610, 1, 0.2) ;
Chris@0 507 /* Lite remove start */
Chris@0 508 sdlcomp_test_float ("gsm610.w64", SF_FORMAT_W64 | SF_FORMAT_GSM610, 1, 0.2) ;
Chris@0 509 sdlcomp_test_double ("gsm610.w64", SF_FORMAT_W64 | SF_FORMAT_GSM610, 1, 0.2) ;
Chris@0 510 /* Lite remove end */
Chris@0 511 test_count++ ;
Chris@0 512 } ;
Chris@0 513
Chris@0 514 /* Lite remove start */
Chris@0 515 if (do_all || strcmp (argv [1], "vox_adpcm") == 0)
Chris@0 516 { lcomp_test_short ("adpcm.vox", SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, 1, 0.17) ;
Chris@0 517 lcomp_test_int ("adpcm.vox", SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, 1, 0.17) ;
Chris@0 518 lcomp_test_float ("adpcm.vox", SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, 1, 0.17) ;
Chris@0 519 lcomp_test_double ("adpcm.vox", SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, 1, 0.17) ;
Chris@0 520
Chris@0 521 sdlcomp_test_short ("adpcm.vox", SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, 1, 0.072) ;
Chris@0 522 sdlcomp_test_int ("adpcm.vox", SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, 1, 0.072) ;
Chris@0 523 sdlcomp_test_float ("adpcm.vox", SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, 1, 0.072) ;
Chris@0 524 sdlcomp_test_double ("adpcm.vox", SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, 1, 0.072) ;
Chris@0 525 test_count++ ;
Chris@0 526 } ;
Chris@0 527
Chris@0 528 if (do_all || strcmp (argv [1], "xi_dpcm") == 0)
Chris@0 529 { lcomp_test_short ("8bit.xi", SF_FORMAT_XI | SF_FORMAT_DPCM_8, 1, 0.25) ;
Chris@0 530 lcomp_test_int ("8bit.xi", SF_FORMAT_XI | SF_FORMAT_DPCM_8, 1, 0.25) ;
Chris@0 531
Chris@0 532 lcomp_test_short ("16bit.xi", SF_FORMAT_XI | SF_FORMAT_DPCM_16, 1, 0.002) ;
Chris@0 533 lcomp_test_int ("16bit.xi", SF_FORMAT_XI | SF_FORMAT_DPCM_16, 1, 0.002) ;
Chris@0 534 lcomp_test_float ("16bit.xi", SF_FORMAT_XI | SF_FORMAT_DPCM_16, 1, 0.002) ;
Chris@0 535 lcomp_test_double ("16bit.xi", SF_FORMAT_XI | SF_FORMAT_DPCM_16, 1, 0.002) ;
Chris@0 536 test_count++ ;
Chris@0 537 } ;
Chris@0 538 /* Lite remove end */
Chris@0 539
Chris@0 540 if (test_count == 0)
Chris@0 541 { printf ("************************************\n") ;
Chris@0 542 printf ("* No '%s' test defined.\n", argv [1]) ;
Chris@0 543 printf ("************************************\n") ;
Chris@0 544 return 1 ;
Chris@0 545 } ;
Chris@0 546
Chris@0 547 return 0 ;
Chris@0 548 } /* main */
Chris@0 549
Chris@0 550 /*============================================================================================
Chris@0 551 ** Here are the test functions.
Chris@0 552 */
Chris@0 553
Chris@0 554 static void
Chris@0 555 lcomp_test_short (const char *filename, int filetype, int channels, double margin)
Chris@0 556 { SNDFILE *file ;
Chris@0 557 SF_INFO sfinfo ;
Chris@0 558 int k, m, seekpos, half_max_abs ;
Chris@0 559 long datalen ;
Chris@0 560 short *orig, *data ;
Chris@0 561
Chris@0 562 print_test_name ("lcomp_test_short", filename) ;
Chris@0 563
Chris@0 564 datalen = BUFFER_SIZE / channels ;
Chris@0 565
Chris@0 566 data = data_buffer.s ;
Chris@0 567 orig = orig_buffer.s ;
Chris@0 568
Chris@0 569 gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ;
Chris@0 570 for (k = 0 ; k < channels * datalen ; k++)
Chris@0 571 orig [k] = (short) (orig_buffer.d [k]) ;
Chris@0 572
Chris@0 573 sfinfo.samplerate = SAMPLE_RATE ;
Chris@0 574 sfinfo.frames = 123456789 ; /* Ridiculous value. */
Chris@0 575 sfinfo.channels = channels ;
Chris@0 576 sfinfo.format = filetype ;
Chris@0 577
Chris@0 578 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 579 test_writef_short_or_die (file, 0, orig, datalen, __LINE__) ;
Chris@0 580 sf_set_string (file, SF_STR_COMMENT, long_comment) ;
Chris@0 581 sf_close (file) ;
Chris@0 582
Chris@0 583 memset (data, 0, datalen * sizeof (short)) ;
Chris@0 584
Chris@0 585 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
Chris@0 586 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@0 587
Chris@0 588 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 589
Chris@0 590 if ((sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@0 591 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
Chris@0 592 exit (1) ;
Chris@0 593 } ;
Chris@0 594
Chris@0 595 if (sfinfo.frames < datalen / channels)
Chris@0 596 { printf ("Too few frames in file. (%ld should be a little more than %ld)\n", SF_COUNT_TO_LONG (sfinfo.frames), datalen) ;
Chris@0 597 exit (1) ;
Chris@0 598 } ;
Chris@0 599
Chris@0 600 if (sfinfo.frames > (datalen + datalen / 20))
Chris@0 601 { printf ("Too many frames in file. (%ld should be a little more than %ld)\n", SF_COUNT_TO_LONG (sfinfo.frames), datalen) ;
Chris@0 602 exit (1) ;
Chris@0 603 } ;
Chris@0 604
Chris@0 605 if (sfinfo.channels != channels)
Chris@0 606 { printf ("Incorrect number of channels in file.\n") ;
Chris@0 607 exit (1) ;
Chris@0 608 } ;
Chris@0 609
Chris@0 610 check_log_buffer_or_die (file, __LINE__) ;
Chris@0 611
Chris@0 612 check_comment (file, filetype, __LINE__) ;
Chris@0 613
Chris@0 614 test_readf_short_or_die (file, 0, data, datalen, __LINE__) ;
Chris@0 615
Chris@0 616 half_max_abs = 0 ;
Chris@0 617 for (k = 0 ; k < datalen ; k++)
Chris@0 618 { if (error_function (data [k], orig [k], margin))
Chris@0 619 { printf ("\n\nLine %d: Incorrect sample A (#%d : %d should be %d).\n", __LINE__, k, data [k], orig [k]) ;
Chris@0 620 oct_save_short (orig, data, datalen) ;
Chris@0 621 exit (1) ;
Chris@0 622 } ;
Chris@0 623 half_max_abs = LCT_MAX (half_max_abs, abs (data [k] / 2)) ;
Chris@0 624 } ;
Chris@0 625
Chris@0 626 if (half_max_abs < 1.0)
Chris@0 627 { printf ("\n\nLine %d: Signal is all zeros.\n", __LINE__) ;
Chris@0 628 exit (1) ;
Chris@0 629 } ;
Chris@0 630
Chris@0 631 if ((k = sf_readf_short (file, data, datalen)) != sfinfo.frames - datalen)
Chris@0 632 { printf ("\n\nLine %d: Incorrect read length (%ld should be %d).\n", __LINE__,
Chris@0 633 SF_COUNT_TO_LONG (channels * sfinfo.frames - datalen), k) ;
Chris@0 634 exit (1) ;
Chris@0 635 } ;
Chris@0 636
Chris@0 637 /* This check is only for block based encoders which must append silence
Chris@0 638 ** to the end of a file so as to fill out a block.
Chris@0 639 */
Chris@0 640 for (k = 0 ; k < sfinfo.frames - datalen ; k++)
Chris@0 641 if (abs (data [channels * k]) > decay_response (channels * k))
Chris@0 642 { printf ("\n\nLine %d : Incorrect sample B (#%d : abs (%d) should be < %d).\n", __LINE__, channels * k, data [channels * k], decay_response (channels * k)) ;
Chris@0 643 exit (1) ;
Chris@0 644 } ;
Chris@0 645
Chris@0 646 if (! sfinfo.seekable)
Chris@0 647 { sf_close (file) ;
Chris@0 648 unlink (filename) ;
Chris@0 649 printf ("ok\n") ;
Chris@0 650 return ;
Chris@0 651 } ;
Chris@0 652
Chris@0 653 /* Now test sf_seek function. */
Chris@0 654
Chris@0 655 if ((k = sf_seek (file, 0, SEEK_SET)) != 0)
Chris@0 656 { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ;
Chris@0 657 exit (1) ;
Chris@0 658 } ;
Chris@0 659
Chris@0 660 for (m = 0 ; m < 3 ; m++)
Chris@0 661 { test_readf_short_or_die (file, m, data, 11, __LINE__) ;
Chris@0 662
Chris@0 663 for (k = 0 ; k < channels * 11 ; k++)
Chris@0 664 if (error_function (1.0 * data [k], 1.0 * orig [k + channels * m * 11], margin))
Chris@0 665 { printf ("\n\nLine %d: Incorrect sample (m = %d) (#%d : %d => %d).\n", __LINE__, m, k + channels * m * 11, orig [k + channels * m * 11], data [k]) ;
Chris@0 666 for (m = 0 ; m < channels ; m++)
Chris@0 667 printf ("%d ", data [m]) ;
Chris@0 668 printf ("\n") ;
Chris@0 669 exit (1) ;
Chris@0 670 } ;
Chris@0 671 } ;
Chris@0 672
Chris@0 673 seekpos = BUFFER_SIZE / 10 ;
Chris@0 674
Chris@0 675 /* Check seek from start of file. */
Chris@0 676 if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos)
Chris@0 677 { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ;
Chris@0 678 exit (1) ;
Chris@0 679 } ;
Chris@0 680
Chris@0 681 test_readf_short_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 682
Chris@0 683 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin))
Chris@0 684 { printf ("\n\nLine %d: sf_seek (SEEK_SET) followed by sf_readf_short failed (%d, %d).\n", __LINE__, orig [1], data [0]) ;
Chris@0 685 exit (1) ;
Chris@0 686 } ;
Chris@0 687
Chris@0 688 if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1)
Chris@0 689 { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ;
Chris@0 690 exit (1) ;
Chris@0 691 } ;
Chris@0 692
Chris@0 693 seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ;
Chris@0 694 k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ;
Chris@0 695 test_readf_short_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 696 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos)
Chris@0 697 { printf ("\n\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_short failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ;
Chris@0 698 oct_save_short (orig, data, datalen) ;
Chris@0 699 exit (1) ;
Chris@0 700 } ;
Chris@0 701
Chris@0 702 seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ;
Chris@0 703 /* Check seek backward from current position. */
Chris@0 704 k = sf_seek (file, -20, SEEK_CUR) ;
Chris@0 705 test_readf_short_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 706 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos)
Chris@0 707 { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_readf_short failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ;
Chris@0 708 exit (1) ;
Chris@0 709 } ;
Chris@0 710
Chris@0 711 /* Check that read past end of file returns number of items. */
Chris@0 712 sf_seek (file, sfinfo.frames, SEEK_SET) ;
Chris@0 713
Chris@0 714 if ((k = sf_readf_short (file, data, datalen)) != 0)
Chris@0 715 { printf ("\n\nLine %d: Return value from sf_readf_short past end of file incorrect (%d).\n", __LINE__, k) ;
Chris@0 716 exit (1) ;
Chris@0 717 } ;
Chris@0 718
Chris@0 719 /* Check seek backward from end. */
Chris@0 720 if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5)
Chris@0 721 { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ;
Chris@0 722 exit (1) ;
Chris@0 723 } ;
Chris@0 724
Chris@0 725 test_readf_short_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 726 if (error_function (1.0 * data [0], 1.0 * orig [5 * channels], margin))
Chris@0 727 { printf ("\nLine %d: sf_seek (SEEK_END) followed by sf_readf_short failed (%d should be %d).\n", __LINE__, data [0], orig [5 * channels]) ;
Chris@0 728 exit (1) ;
Chris@0 729 } ;
Chris@0 730
Chris@0 731 sf_close (file) ;
Chris@0 732
Chris@0 733 unlink (filename) ;
Chris@0 734 printf ("ok\n") ;
Chris@0 735 } /* lcomp_test_short */
Chris@0 736
Chris@0 737 /*--------------------------------------------------------------------------------------------
Chris@0 738 */
Chris@0 739
Chris@0 740 static void
Chris@0 741 lcomp_test_int (const char *filename, int filetype, int channels, double margin)
Chris@0 742 { SNDFILE *file ;
Chris@0 743 SF_INFO sfinfo ;
Chris@0 744 int k, m, half_max_abs ;
Chris@0 745 long datalen, seekpos ;
Chris@0 746 double scale, max_val ;
Chris@0 747 int *orig, *data ;
Chris@0 748
Chris@0 749 print_test_name ("lcomp_test_int", filename) ;
Chris@0 750
Chris@0 751 datalen = BUFFER_SIZE / channels ;
Chris@0 752
Chris@0 753 if (is_lossy (filetype))
Chris@0 754 { scale = 1.0 * 0x10000 ;
Chris@0 755 max_val = 32000.0 * scale ;
Chris@0 756 }
Chris@0 757 else
Chris@0 758 { scale = 1.0 ;
Chris@0 759 max_val = 0x7fffffff * scale ;
Chris@0 760 } ;
Chris@0 761
Chris@0 762 data = data_buffer.i ;
Chris@0 763 orig = orig_buffer.i ;
Chris@0 764
Chris@0 765 gen_signal_double (orig_buffer.d, max_val, channels, datalen) ;
Chris@0 766
Chris@0 767 for (k = 0 ; k < channels * datalen ; k++)
Chris@0 768 orig [k] = lrint (orig_buffer.d [k]) ;
Chris@0 769
Chris@0 770 sfinfo.samplerate = SAMPLE_RATE ;
Chris@0 771 sfinfo.frames = 123456789 ; /* Ridiculous value. */
Chris@0 772 sfinfo.channels = channels ;
Chris@0 773 sfinfo.format = filetype ;
Chris@0 774
Chris@0 775 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 776 test_writef_int_or_die (file, 0, orig, datalen, __LINE__) ;
Chris@0 777 sf_set_string (file, SF_STR_COMMENT, long_comment) ;
Chris@0 778 sf_close (file) ;
Chris@0 779
Chris@0 780 memset (data, 0, datalen * sizeof (int)) ;
Chris@0 781
Chris@0 782 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
Chris@0 783 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@0 784
Chris@0 785 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 786
Chris@0 787 if ((sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@0 788 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
Chris@0 789 exit (1) ;
Chris@0 790 } ;
Chris@0 791
Chris@0 792 if (sfinfo.frames < datalen / channels)
Chris@0 793 { printf ("Too few.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 794 exit (1) ;
Chris@0 795 } ;
Chris@0 796
Chris@0 797 if (sfinfo.frames > (datalen + datalen / 20))
Chris@0 798 { printf ("Too many.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 799 exit (1) ;
Chris@0 800 } ;
Chris@0 801
Chris@0 802 if (sfinfo.channels != channels)
Chris@0 803 { printf ("Incorrect number of channels in file.\n") ;
Chris@0 804 exit (1) ;
Chris@0 805 } ;
Chris@0 806
Chris@0 807 check_log_buffer_or_die (file, __LINE__) ;
Chris@0 808
Chris@0 809 check_comment (file, filetype, __LINE__) ;
Chris@0 810
Chris@0 811 test_readf_int_or_die (file, 0, data, datalen, __LINE__) ;
Chris@0 812
Chris@0 813 half_max_abs = 0 ;
Chris@0 814 for (k = 0 ; k < datalen ; k++)
Chris@0 815 { if (error_function (data [k] / scale, orig [k] / scale, margin))
Chris@0 816 { printf ("\nLine %d: Incorrect sample (#%d : %f should be %f).\n", __LINE__, k, data [k] / scale, orig [k] / scale) ;
Chris@0 817 oct_save_int (orig, data, datalen) ;
Chris@0 818 exit (1) ;
Chris@0 819 } ;
Chris@0 820 half_max_abs = LCT_MAX (half_max_abs, abs (data [k] / 2)) ;
Chris@0 821 } ;
Chris@0 822
Chris@0 823 if (half_max_abs < 1.0)
Chris@0 824 { printf ("\n\nLine %d: Signal is all zeros (%d, 0x%X).\n", __LINE__, half_max_abs, half_max_abs) ;
Chris@0 825 exit (1) ;
Chris@0 826 } ;
Chris@0 827
Chris@0 828 if ((k = sf_readf_int (file, data, datalen)) != sfinfo.frames - datalen)
Chris@0 829 { printf ("\n\nLine %d: Incorrect read length (%ld should be %d).\n", __LINE__,
Chris@0 830 SF_COUNT_TO_LONG (channels * sfinfo.frames - datalen), k) ;
Chris@0 831 exit (1) ;
Chris@0 832 } ;
Chris@0 833
Chris@0 834 /* This check is only for block based encoders which must append silence
Chris@0 835 ** to the end of a file so as to fill out a block.
Chris@0 836 */
Chris@0 837 if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM)
Chris@0 838 for (k = 0 ; k < sfinfo.frames - datalen ; k++)
Chris@0 839 if (abs (data [channels * k] / scale) > decay_response (channels * k))
Chris@0 840 { printf ("\n\nLine %d : Incorrect sample B (#%d : abs (%d) should be < %d).\n", __LINE__, channels * k, data [channels * k], decay_response (channels * k)) ;
Chris@0 841 exit (1) ;
Chris@0 842 } ;
Chris@0 843
Chris@0 844 if (! sfinfo.seekable)
Chris@0 845 { sf_close (file) ;
Chris@0 846 unlink (filename) ;
Chris@0 847 printf ("ok\n") ;
Chris@0 848 return ;
Chris@0 849 } ;
Chris@0 850
Chris@0 851 /* Now test sf_seek function. */
Chris@0 852
Chris@0 853 if ((k = sf_seek (file, 0, SEEK_SET)) != 0)
Chris@0 854 { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ;
Chris@0 855 exit (1) ;
Chris@0 856 } ;
Chris@0 857
Chris@0 858 for (m = 0 ; m < 3 ; m++)
Chris@0 859 { test_readf_int_or_die (file, m, data, 11, __LINE__) ;
Chris@0 860
Chris@0 861 for (k = 0 ; k < channels * 11 ; k++)
Chris@0 862 if (error_function (data [k] / scale, orig [k + channels * m * 11] / scale, margin))
Chris@0 863 { printf ("\nLine %d: Incorrect sample (m = %d) (#%d : %d => %d).\n", __LINE__, m, k + channels * m * 11, orig [k + channels * m * 11], data [k]) ;
Chris@0 864 for (m = 0 ; m < channels ; m++)
Chris@0 865 printf ("%d ", data [m]) ;
Chris@0 866 printf ("\n") ;
Chris@0 867 exit (1) ;
Chris@0 868 } ;
Chris@0 869 } ;
Chris@0 870
Chris@0 871 seekpos = BUFFER_SIZE / 10 ;
Chris@0 872
Chris@0 873 /* Check seek from start of file. */
Chris@0 874 if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos)
Chris@0 875 { printf ("Seek to start of file + %ld failed (%d).\n", seekpos, k) ;
Chris@0 876 exit (1) ;
Chris@0 877 } ;
Chris@0 878
Chris@0 879 test_readf_int_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 880
Chris@0 881 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin))
Chris@0 882 { printf ("\nLine %d: sf_seek (SEEK_SET) followed by sf_readf_int failed (%d, %d).\n", __LINE__, orig [1], data [0]) ;
Chris@0 883 exit (1) ;
Chris@0 884 } ;
Chris@0 885
Chris@0 886 if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1)
Chris@0 887 { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %ld)\n", __LINE__, k, seekpos + 1) ;
Chris@0 888 exit (1) ;
Chris@0 889 } ;
Chris@0 890
Chris@0 891 seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ;
Chris@0 892 k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ;
Chris@0 893 test_readf_int_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 894 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos)
Chris@0 895 { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %ld).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ;
Chris@0 896 exit (1) ;
Chris@0 897 } ;
Chris@0 898
Chris@0 899 seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ;
Chris@0 900 /* Check seek backward from current position. */
Chris@0 901 k = sf_seek (file, -20, SEEK_CUR) ;
Chris@0 902 test_readf_int_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 903 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos)
Chris@0 904 { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %ld).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ;
Chris@0 905 exit (1) ;
Chris@0 906 } ;
Chris@0 907
Chris@0 908 /* Check that read past end of file returns number of items. */
Chris@0 909 sf_seek (file, sfinfo.frames, SEEK_SET) ;
Chris@0 910
Chris@0 911 if ((k = sf_readf_int (file, data, datalen)) != 0)
Chris@0 912 { printf ("\n\nLine %d: Return value from sf_readf_int past end of file incorrect (%d).\n", __LINE__, k) ;
Chris@0 913 exit (1) ;
Chris@0 914 } ;
Chris@0 915
Chris@0 916 /* Check seek backward from end. */
Chris@0 917 if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5)
Chris@0 918 { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ;
Chris@0 919 exit (1) ;
Chris@0 920 } ;
Chris@0 921
Chris@0 922 test_readf_int_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 923 if (error_function (data [0] / scale, orig [5 * channels] / scale, margin))
Chris@0 924 { printf ("\nLine %d: sf_seek (SEEK_END) followed by sf_readf_short failed (%d should be %d).\n", __LINE__, data [0], orig [5]) ;
Chris@0 925 exit (1) ;
Chris@0 926 } ;
Chris@0 927
Chris@0 928 sf_close (file) ;
Chris@0 929
Chris@0 930 unlink (filename) ;
Chris@0 931 printf ("ok\n") ;
Chris@0 932 } /* lcomp_test_int */
Chris@0 933
Chris@0 934 /*--------------------------------------------------------------------------------------------
Chris@0 935 */
Chris@0 936
Chris@0 937 static void
Chris@0 938 lcomp_test_float (const char *filename, int filetype, int channels, double margin)
Chris@0 939 { SNDFILE *file ;
Chris@0 940 SF_INFO sfinfo ;
Chris@0 941 int k, m, seekpos ;
Chris@0 942 long datalen ;
Chris@0 943 float *orig, *data ;
Chris@0 944 double half_max_abs ;
Chris@0 945
Chris@0 946 print_test_name ("lcomp_test_float", filename) ;
Chris@0 947
Chris@0 948 datalen = BUFFER_SIZE / channels ;
Chris@0 949
Chris@0 950 data = data_buffer.f ;
Chris@0 951 orig = orig_buffer.f ;
Chris@0 952
Chris@0 953 gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ;
Chris@0 954 for (k = 0 ; k < channels * datalen ; k++)
Chris@0 955 orig [k] = orig_buffer.d [k] ;
Chris@0 956
Chris@0 957 sfinfo.samplerate = SAMPLE_RATE ;
Chris@0 958 sfinfo.frames = 123456789 ; /* Ridiculous value. */
Chris@0 959 sfinfo.channels = channels ;
Chris@0 960 sfinfo.format = filetype ;
Chris@0 961
Chris@0 962 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 963 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@0 964 test_writef_float_or_die (file, 0, orig, datalen, __LINE__) ;
Chris@0 965 sf_set_string (file, SF_STR_COMMENT, long_comment) ;
Chris@0 966 sf_close (file) ;
Chris@0 967
Chris@0 968 memset (data, 0, datalen * sizeof (float)) ;
Chris@0 969
Chris@0 970 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
Chris@0 971 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@0 972
Chris@0 973 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 974
Chris@0 975 if ((sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@0 976 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
Chris@0 977 exit (1) ;
Chris@0 978 } ;
Chris@0 979
Chris@0 980 if (sfinfo.frames < datalen / channels)
Chris@0 981 { printf ("Too few.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 982 exit (1) ;
Chris@0 983 } ;
Chris@0 984
Chris@0 985 if (sfinfo.frames > (datalen + datalen / 20))
Chris@0 986 { printf ("Too many.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 987 exit (1) ;
Chris@0 988 } ;
Chris@0 989
Chris@0 990 if (sfinfo.channels != channels)
Chris@0 991 { printf ("Incorrect number of channels in file.\n") ;
Chris@0 992 exit (1) ;
Chris@0 993 } ;
Chris@0 994
Chris@0 995 check_comment (file, filetype, __LINE__) ;
Chris@0 996
Chris@0 997 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@0 998
Chris@0 999 check_log_buffer_or_die (file, __LINE__) ;
Chris@0 1000
Chris@0 1001 check_comment (file, filetype, __LINE__) ;
Chris@0 1002
Chris@0 1003 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@0 1004
Chris@0 1005 test_readf_float_or_die (file, 0, data, datalen, __LINE__) ;
Chris@0 1006
Chris@0 1007 half_max_abs = 0.0 ;
Chris@0 1008 for (k = 0 ; k < datalen ; k++)
Chris@0 1009 { if (error_function (data [k], orig [k], margin))
Chris@0 1010 { printf ("\nLine %d: Incorrect sample A (#%d : %f should be %f).\n", __LINE__, k, data [k], orig [k]) ;
Chris@0 1011 oct_save_float (orig, data, datalen) ;
Chris@0 1012 exit (1) ;
Chris@0 1013 } ;
Chris@0 1014 half_max_abs = LCT_MAX (half_max_abs, fabs (0.5 * data [k])) ;
Chris@0 1015 } ;
Chris@0 1016
Chris@0 1017 if (half_max_abs < 1.0)
Chris@0 1018 { printf ("\n\nLine %d: Signal is all zeros.\n", __LINE__) ;
Chris@0 1019 exit (1) ;
Chris@0 1020 } ;
Chris@0 1021
Chris@0 1022 if ((k = sf_readf_float (file, data, datalen)) != sfinfo.frames - datalen)
Chris@0 1023 { printf ("\n\nLine %d: Incorrect read length (%ld should be %d).\n", __LINE__,
Chris@0 1024 SF_COUNT_TO_LONG (channels * sfinfo.frames - datalen), k) ;
Chris@0 1025 exit (1) ;
Chris@0 1026 } ;
Chris@0 1027
Chris@0 1028 /* This check is only for block based encoders which must append silence
Chris@0 1029 ** to the end of a file so as to fill out a block.
Chris@0 1030 */
Chris@0 1031 if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM)
Chris@0 1032 for (k = 0 ; k < sfinfo.frames - datalen ; k++)
Chris@0 1033 if (abs (data [channels * k]) > decay_response (channels * k))
Chris@0 1034 { printf ("\n\nLine %d : Incorrect sample B (#%d : abs (%f) should be < %d).\n", __LINE__, channels * k, data [channels * k], decay_response (channels * k)) ;
Chris@0 1035 exit (1) ;
Chris@0 1036 } ;
Chris@0 1037
Chris@0 1038 if (! sfinfo.seekable)
Chris@0 1039 { sf_close (file) ;
Chris@0 1040 unlink (filename) ;
Chris@0 1041 printf ("ok\n") ;
Chris@0 1042 return ;
Chris@0 1043 } ;
Chris@0 1044
Chris@0 1045 /* Now test sf_seek function. */
Chris@0 1046
Chris@0 1047 if ((k = sf_seek (file, 0, SEEK_SET)) != 0)
Chris@0 1048 { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ;
Chris@0 1049 exit (1) ;
Chris@0 1050 } ;
Chris@0 1051
Chris@0 1052 for (m = 0 ; m < 3 ; m++)
Chris@0 1053 { test_readf_float_or_die (file, 0, data, 11, __LINE__) ;
Chris@0 1054
Chris@0 1055 for (k = 0 ; k < channels * 11 ; k++)
Chris@0 1056 if (error_function (data [k], orig [k + channels * m * 11], margin))
Chris@0 1057 { printf ("\nLine %d: Incorrect sample (m = %d) (#%d : %f => %f).\n", __LINE__, m, k + channels * m * 11, orig [k + channels * m * 11], data [k]) ;
Chris@0 1058 for (m = 0 ; m < channels ; m++)
Chris@0 1059 printf ("%f ", data [m]) ;
Chris@0 1060 printf ("\n") ;
Chris@0 1061 exit (1) ;
Chris@0 1062 } ;
Chris@0 1063 } ;
Chris@0 1064
Chris@0 1065 seekpos = BUFFER_SIZE / 10 ;
Chris@0 1066
Chris@0 1067 /* Check seek from start of file. */
Chris@0 1068 if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos)
Chris@0 1069 { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ;
Chris@0 1070 exit (1) ;
Chris@0 1071 } ;
Chris@0 1072
Chris@0 1073 test_readf_float_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1074
Chris@0 1075 if (error_function (data [0], orig [seekpos * channels], margin))
Chris@0 1076 { printf ("\nLine %d: sf_seek (SEEK_SET) followed by sf_readf_float failed (%f, %f).\n", __LINE__, orig [1], data [0]) ;
Chris@0 1077 exit (1) ;
Chris@0 1078 } ;
Chris@0 1079
Chris@0 1080 if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1)
Chris@0 1081 { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ;
Chris@0 1082 exit (1) ;
Chris@0 1083 } ;
Chris@0 1084
Chris@0 1085 seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ;
Chris@0 1086 k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ;
Chris@0 1087 test_readf_float_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1088 if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos)
Chris@0 1089 { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_float failed (%f, %f) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ;
Chris@0 1090 exit (1) ;
Chris@0 1091 } ;
Chris@0 1092
Chris@0 1093 seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ;
Chris@0 1094 /* Check seek backward from current position. */
Chris@0 1095 k = sf_seek (file, -20, SEEK_CUR) ;
Chris@0 1096 test_readf_float_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1097 if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos)
Chris@0 1098 { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_readf_float failed (%f, %f) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ;
Chris@0 1099 exit (1) ;
Chris@0 1100 } ;
Chris@0 1101
Chris@0 1102 /* Check that read past end of file returns number of items. */
Chris@0 1103 sf_seek (file, sfinfo.frames, SEEK_SET) ;
Chris@0 1104
Chris@0 1105 if ((k = sf_readf_float (file, data, datalen)) != 0)
Chris@0 1106 { printf ("\n\nLine %d: Return value from sf_readf_float past end of file incorrect (%d).\n", __LINE__, k) ;
Chris@0 1107 exit (1) ;
Chris@0 1108 } ;
Chris@0 1109
Chris@0 1110 /* Check seek backward from end. */
Chris@0 1111 if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5)
Chris@0 1112 { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ;
Chris@0 1113 exit (1) ;
Chris@0 1114 } ;
Chris@0 1115
Chris@0 1116 test_readf_float_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1117 if (error_function (data [0], orig [5 * channels], margin))
Chris@0 1118 { printf ("\nLine %d: sf_seek (SEEK_END) followed by sf_readf_short failed (%f should be %f).\n", __LINE__, data [0], orig [5 * channels]) ;
Chris@0 1119 exit (1) ;
Chris@0 1120 } ;
Chris@0 1121
Chris@0 1122 sf_close (file) ;
Chris@0 1123
Chris@0 1124 unlink (filename) ;
Chris@0 1125 printf ("ok\n") ;
Chris@0 1126 } /* lcomp_test_float */
Chris@0 1127
Chris@0 1128 /*--------------------------------------------------------------------------------------------
Chris@0 1129 */
Chris@0 1130
Chris@0 1131 static void
Chris@0 1132 lcomp_test_double (const char *filename, int filetype, int channels, double margin)
Chris@0 1133 { SNDFILE *file ;
Chris@0 1134 SF_INFO sfinfo ;
Chris@0 1135 int k, m, seekpos ;
Chris@0 1136 long datalen ;
Chris@0 1137 double *orig, *data ;
Chris@0 1138 double half_max_abs ;
Chris@0 1139
Chris@0 1140 print_test_name ("lcomp_test_double", filename) ;
Chris@0 1141
Chris@0 1142 datalen = BUFFER_SIZE / channels ;
Chris@0 1143
Chris@0 1144 data = data_buffer.d ;
Chris@0 1145 orig = orig_buffer.d ;
Chris@0 1146
Chris@0 1147 gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ;
Chris@0 1148 for (k = 0 ; k < channels * datalen ; k++)
Chris@0 1149 orig [k] = orig_buffer.d [k] ;
Chris@0 1150
Chris@0 1151 sfinfo.samplerate = SAMPLE_RATE ;
Chris@0 1152 sfinfo.frames = 123456789 ; /* Ridiculous value. */
Chris@0 1153 sfinfo.channels = channels ;
Chris@0 1154 sfinfo.format = filetype ;
Chris@0 1155
Chris@0 1156 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 1157 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@0 1158 test_writef_double_or_die (file, 0, orig, datalen, __LINE__) ;
Chris@0 1159 sf_set_string (file, SF_STR_COMMENT, long_comment) ;
Chris@0 1160 sf_close (file) ;
Chris@0 1161
Chris@0 1162 memset (data, 0, datalen * sizeof (double)) ;
Chris@0 1163
Chris@0 1164 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
Chris@0 1165 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@0 1166
Chris@0 1167 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 1168
Chris@0 1169 if ((sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@0 1170 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
Chris@0 1171 exit (1) ;
Chris@0 1172 } ;
Chris@0 1173
Chris@0 1174 if (sfinfo.frames < datalen / channels)
Chris@0 1175 { printf ("Too few.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 1176 exit (1) ;
Chris@0 1177 } ;
Chris@0 1178
Chris@0 1179 if (sfinfo.frames > (datalen + datalen / 20))
Chris@0 1180 { printf ("Too many.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 1181 exit (1) ;
Chris@0 1182 } ;
Chris@0 1183
Chris@0 1184 if (sfinfo.channels != channels)
Chris@0 1185 { printf ("Incorrect number of channels in file.\n") ;
Chris@0 1186 exit (1) ;
Chris@0 1187 } ;
Chris@0 1188
Chris@0 1189 check_comment (file, filetype, __LINE__) ;
Chris@0 1190
Chris@0 1191 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@0 1192
Chris@0 1193 check_log_buffer_or_die (file, __LINE__) ;
Chris@0 1194
Chris@0 1195 check_comment (file, filetype, __LINE__) ;
Chris@0 1196
Chris@0 1197 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@0 1198
Chris@0 1199 test_readf_double_or_die (file, 0, data, datalen, __LINE__) ;
Chris@0 1200
Chris@0 1201 half_max_abs = 0.0 ;
Chris@0 1202 for (k = 0 ; k < datalen ; k++)
Chris@0 1203 { if (error_function (data [k], orig [k], margin))
Chris@0 1204 { printf ("\nLine %d: Incorrect sample A (#%d : %f should be %f).\n", __LINE__, k, data [k], orig [k]) ;
Chris@0 1205 oct_save_double (orig, data, datalen) ;
Chris@0 1206 exit (1) ;
Chris@0 1207 } ;
Chris@0 1208 half_max_abs = LCT_MAX (half_max_abs, abs (0.5 * data [k])) ;
Chris@0 1209 } ;
Chris@0 1210
Chris@0 1211 if (half_max_abs < 1.0)
Chris@0 1212 { printf ("\n\nLine %d: Signal is all zeros.\n", __LINE__) ;
Chris@0 1213 exit (1) ;
Chris@0 1214 } ;
Chris@0 1215
Chris@0 1216 if ((k = sf_readf_double (file, data, datalen)) != sfinfo.frames - datalen)
Chris@0 1217 { printf ("\n\nLine %d: Incorrect read length (%ld should be %d).\n", __LINE__,
Chris@0 1218 SF_COUNT_TO_LONG (channels * sfinfo.frames - datalen), k) ;
Chris@0 1219 exit (1) ;
Chris@0 1220 } ;
Chris@0 1221
Chris@0 1222 /* This check is only for block based encoders which must append silence
Chris@0 1223 ** to the end of a file so as to fill out a block.
Chris@0 1224 */
Chris@0 1225 if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM)
Chris@0 1226 for (k = 0 ; k < sfinfo.frames - datalen ; k++)
Chris@0 1227 if (abs (data [channels * k]) > decay_response (channels * k))
Chris@0 1228 { printf ("\n\nLine %d : Incorrect sample B (#%d : abs (%f) should be < %d).\n", __LINE__, channels * k, data [channels * k], decay_response (channels * k)) ;
Chris@0 1229 exit (1) ;
Chris@0 1230 } ;
Chris@0 1231
Chris@0 1232 if (! sfinfo.seekable)
Chris@0 1233 { sf_close (file) ;
Chris@0 1234 unlink (filename) ;
Chris@0 1235 printf ("ok\n") ;
Chris@0 1236 return ;
Chris@0 1237 } ;
Chris@0 1238
Chris@0 1239 /* Now test sf_seek function. */
Chris@0 1240
Chris@0 1241 if ((k = sf_seek (file, 0, SEEK_SET)) != 0)
Chris@0 1242 { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ;
Chris@0 1243 exit (1) ;
Chris@0 1244 } ;
Chris@0 1245
Chris@0 1246 for (m = 0 ; m < 3 ; m++)
Chris@0 1247 { test_readf_double_or_die (file, m, data, 11, __LINE__) ;
Chris@0 1248
Chris@0 1249 for (k = 0 ; k < channels * 11 ; k++)
Chris@0 1250 if (error_function (data [k], orig [k + channels * m * 11], margin))
Chris@0 1251 { printf ("\nLine %d: Incorrect sample (m = %d) (#%d : %f => %f).\n", __LINE__, m, k + channels * m * 11, orig [k + channels * m * 11], data [k]) ;
Chris@0 1252 for (m = 0 ; m < channels ; m++)
Chris@0 1253 printf ("%f ", data [m]) ;
Chris@0 1254 printf ("\n") ;
Chris@0 1255 exit (1) ;
Chris@0 1256 } ;
Chris@0 1257 } ;
Chris@0 1258
Chris@0 1259 seekpos = BUFFER_SIZE / 10 ;
Chris@0 1260
Chris@0 1261 /* Check seek from start of file. */
Chris@0 1262 if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos)
Chris@0 1263 { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ;
Chris@0 1264 exit (1) ;
Chris@0 1265 } ;
Chris@0 1266
Chris@0 1267 test_readf_double_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1268
Chris@0 1269 if (error_function (data [0], orig [seekpos * channels], margin))
Chris@0 1270 { printf ("\nLine %d: sf_seek (SEEK_SET) followed by sf_readf_double failed (%f, %f).\n", __LINE__, orig [1], data [0]) ;
Chris@0 1271 exit (1) ;
Chris@0 1272 } ;
Chris@0 1273
Chris@0 1274 if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1)
Chris@0 1275 { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ;
Chris@0 1276 exit (1) ;
Chris@0 1277 } ;
Chris@0 1278
Chris@0 1279 seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ;
Chris@0 1280 k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ;
Chris@0 1281 test_readf_double_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1282 if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos)
Chris@0 1283 { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_double failed (%f, %f) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ;
Chris@0 1284 exit (1) ;
Chris@0 1285 } ;
Chris@0 1286
Chris@0 1287 seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ;
Chris@0 1288 /* Check seek backward from current position. */
Chris@0 1289 k = sf_seek (file, -20, SEEK_CUR) ;
Chris@0 1290 test_readf_double_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1291 if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos)
Chris@0 1292 { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_readf_double failed (%f, %f) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ;
Chris@0 1293 exit (1) ;
Chris@0 1294 } ;
Chris@0 1295
Chris@0 1296 /* Check that read past end of file returns number of items. */
Chris@0 1297 sf_seek (file, sfinfo.frames, SEEK_SET) ;
Chris@0 1298
Chris@0 1299 if ((k = sf_readf_double (file, data, datalen)) != 0)
Chris@0 1300 { printf ("\n\nLine %d: Return value from sf_readf_double past end of file incorrect (%d).\n", __LINE__, k) ;
Chris@0 1301 exit (1) ;
Chris@0 1302 } ;
Chris@0 1303
Chris@0 1304 /* Check seek backward from end. */
Chris@0 1305 if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5)
Chris@0 1306 { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ;
Chris@0 1307 exit (1) ;
Chris@0 1308 } ;
Chris@0 1309
Chris@0 1310 test_readf_double_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1311 if (error_function (data [0], orig [5 * channels], margin))
Chris@0 1312 { printf ("\nLine %d: sf_seek (SEEK_END) followed by sf_readf_short failed (%f should be %f).\n", __LINE__, data [0], orig [5 * channels]) ;
Chris@0 1313 exit (1) ;
Chris@0 1314 } ;
Chris@0 1315
Chris@0 1316 sf_close (file) ;
Chris@0 1317
Chris@0 1318 unlink (filename) ;
Chris@0 1319 printf ("ok\n") ;
Chris@0 1320 } /* lcomp_test_double */
Chris@0 1321
Chris@0 1322 /*========================================================================================
Chris@0 1323 ** Smoothed differential loss compression tests.
Chris@0 1324 */
Chris@0 1325
Chris@0 1326 static void
Chris@0 1327 sdlcomp_test_short (const char *filename, int filetype, int channels, double margin)
Chris@0 1328 { SNDFILE *file ;
Chris@0 1329 SF_INFO sfinfo ;
Chris@0 1330 int k, m, seekpos, half_max_abs ;
Chris@0 1331 long datalen ;
Chris@0 1332 short *orig, *data, *smooth ;
Chris@0 1333
Chris@0 1334 channels = 1 ;
Chris@0 1335 print_test_name ("sdlcomp_test_short", filename) ;
Chris@0 1336
Chris@0 1337 datalen = BUFFER_SIZE ;
Chris@0 1338
Chris@0 1339 orig = orig_buffer.s ;
Chris@0 1340 data = data_buffer.s ;
Chris@0 1341 smooth = smooth_buffer.s ;
Chris@0 1342
Chris@0 1343 gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ;
Chris@0 1344 for (k = 0 ; k < datalen ; k++)
Chris@0 1345 orig [k] = lrint (orig_buffer.d [k]) ;
Chris@0 1346
Chris@0 1347 sfinfo.samplerate = SAMPLE_RATE ;
Chris@0 1348 sfinfo.frames = 123456789 ; /* Ridiculous value. */
Chris@0 1349 sfinfo.channels = channels ;
Chris@0 1350 sfinfo.format = filetype ;
Chris@0 1351
Chris@0 1352 /* The Vorbis encoder has a bug on PowerPC and X86-64 with sample rates
Chris@0 1353 ** <= 22050. Increasing the sample rate to 32000 avoids triggering it.
Chris@0 1354 ** See https://trac.xiph.org/ticket/1229
Chris@0 1355 */
Chris@0 1356 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
Chris@0 1357 { const char * errstr ;
Chris@0 1358
Chris@0 1359 errstr = sf_strerror (NULL) ;
Chris@0 1360 if (strstr (errstr, "Sample rate chosen is known to trigger a Vorbis") == NULL)
Chris@0 1361 { printf ("Line %d: sf_open_fd (SFM_WRITE) failed : %s\n", __LINE__, errstr) ;
Chris@0 1362 dump_log_buffer (NULL) ;
Chris@0 1363 exit (1) ;
Chris@0 1364 } ;
Chris@0 1365
Chris@0 1366 printf ("\n Sample rate -> 32kHz ") ;
Chris@0 1367 sfinfo.samplerate = 32000 ;
Chris@0 1368
Chris@0 1369 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@0 1370 } ;
Chris@0 1371
Chris@0 1372 test_write_short_or_die (file, 0, orig, datalen, __LINE__) ;
Chris@0 1373 sf_set_string (file, SF_STR_COMMENT, long_comment) ;
Chris@0 1374 sf_close (file) ;
Chris@0 1375
Chris@0 1376 memset (data, 0, datalen * sizeof (short)) ;
Chris@0 1377
Chris@0 1378 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
Chris@0 1379 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@0 1380
Chris@0 1381 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 1382
Chris@0 1383 if (sfinfo.format != filetype)
Chris@0 1384 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
Chris@0 1385 exit (1) ;
Chris@0 1386 } ;
Chris@0 1387
Chris@0 1388 if (sfinfo.frames < datalen / channels)
Chris@0 1389 { printf ("Too few.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 1390 exit (1) ;
Chris@0 1391 } ;
Chris@0 1392
Chris@0 1393 if (sfinfo.frames > (datalen + 400))
Chris@0 1394 { printf ("Too many.frames in file. (%ld should be a little more than %ld)\n", SF_COUNT_TO_LONG (sfinfo.frames), datalen) ;
Chris@0 1395 exit (1) ;
Chris@0 1396 } ;
Chris@0 1397
Chris@0 1398 if (sfinfo.channels != channels)
Chris@0 1399 { printf ("Incorrect number of channels in file.\n") ;
Chris@0 1400 exit (1) ;
Chris@0 1401 } ;
Chris@0 1402
Chris@0 1403 check_comment (file, filetype, __LINE__) ;
Chris@0 1404
Chris@0 1405 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@0 1406
Chris@0 1407 check_log_buffer_or_die (file, __LINE__) ;
Chris@0 1408
Chris@0 1409 test_readf_short_or_die (file, 0, data, datalen, __LINE__) ;
Chris@0 1410
Chris@0 1411 memcpy (smooth, orig, datalen * sizeof (short)) ;
Chris@0 1412 smoothed_diff_short (data, datalen) ;
Chris@0 1413 smoothed_diff_short (smooth, datalen) ;
Chris@0 1414
Chris@0 1415 half_max_abs = 0.0 ;
Chris@0 1416 for (k = 0 ; k < datalen ; k++)
Chris@0 1417 { if (error_function (1.0 * data [k], 1.0 * smooth [k], margin))
Chris@0 1418 { printf ("\nLine %d: Incorrect sample (#%d : %d should be %d).\n", __LINE__, k, data [k], smooth [k]) ;
Chris@0 1419 oct_save_short (orig, smooth, datalen) ;
Chris@0 1420 exit (1) ;
Chris@0 1421 } ;
Chris@0 1422 half_max_abs = LCT_MAX (half_max_abs, abs (0.5 * data [k])) ;
Chris@0 1423 } ;
Chris@0 1424
Chris@0 1425 if (half_max_abs < 1)
Chris@0 1426 { printf ("\n\nLine %d: Signal is all zeros.\n", __LINE__) ;
Chris@0 1427 exit (1) ;
Chris@0 1428 } ;
Chris@0 1429
Chris@0 1430 if ((k = sf_read_short (file, data, datalen)) != sfinfo.frames - datalen)
Chris@0 1431 { printf ("\n\nLine %d: Incorrect read length (%d should be %ld).\n", __LINE__, k, SF_COUNT_TO_LONG (sfinfo.frames - datalen)) ;
Chris@0 1432 exit (1) ;
Chris@0 1433 } ;
Chris@0 1434
Chris@0 1435 if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM &&
Chris@0 1436 (sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_GSM610)
Chris@0 1437 for (k = 0 ; k < sfinfo.frames - datalen ; k++)
Chris@0 1438 if (abs (data [k]) > decay_response (k))
Chris@0 1439 { printf ("\n\nLine %d: Incorrect sample (#%ld : abs (%d) should be < %d).\n", __LINE__, datalen + k, data [k], decay_response (k)) ;
Chris@0 1440 exit (1) ;
Chris@0 1441 } ;
Chris@0 1442
Chris@0 1443 /* Now test sf_seek function. */
Chris@0 1444 if (sfinfo.seekable)
Chris@0 1445 { if ((k = sf_seek (file, 0, SEEK_SET)) != 0)
Chris@0 1446 { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ;
Chris@0 1447 exit (1) ;
Chris@0 1448 } ;
Chris@0 1449
Chris@0 1450 for (m = 0 ; m < 3 ; m++)
Chris@0 1451 { test_readf_short_or_die (file, m, data, datalen / 7, __LINE__) ;
Chris@0 1452
Chris@0 1453 smoothed_diff_short (data, datalen / 7) ;
Chris@0 1454 memcpy (smooth, orig + m * datalen / 7, datalen / 7 * sizeof (short)) ;
Chris@0 1455 smoothed_diff_short (smooth, datalen / 7) ;
Chris@0 1456
Chris@0 1457 for (k = 0 ; k < datalen / 7 ; k++)
Chris@0 1458 if (error_function (1.0 * data [k], 1.0 * smooth [k], margin))
Chris@0 1459 { printf ("\nLine %d: Incorrect sample C (#%d (%ld) : %d => %d).\n", __LINE__, k, k + m * (datalen / 7), smooth [k], data [k]) ;
Chris@0 1460 for (m = 0 ; m < 10 ; m++)
Chris@0 1461 printf ("%d ", data [k]) ;
Chris@0 1462 printf ("\n") ;
Chris@0 1463 exit (1) ;
Chris@0 1464 } ;
Chris@0 1465 } ; /* for (m = 0 ; m < 3 ; m++) */
Chris@0 1466
Chris@0 1467 seekpos = BUFFER_SIZE / 10 ;
Chris@0 1468
Chris@0 1469 /* Check seek from start of file. */
Chris@0 1470 if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos)
Chris@0 1471 { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ;
Chris@0 1472 exit (1) ;
Chris@0 1473 } ;
Chris@0 1474 test_readf_short_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1475
Chris@0 1476 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin))
Chris@0 1477 { printf ("\nLine %d: sf_seek (SEEK_SET) followed by sf_read_short failed (%d, %d).\n", __LINE__, orig [1], data [0]) ;
Chris@0 1478 exit (1) ;
Chris@0 1479 } ;
Chris@0 1480
Chris@0 1481 if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1)
Chris@0 1482 { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ;
Chris@0 1483 exit (1) ;
Chris@0 1484 } ;
Chris@0 1485
Chris@0 1486 seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ;
Chris@0 1487 k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ;
Chris@0 1488 test_readf_short_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1489 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos)
Chris@0 1490 { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_read_short failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ;
Chris@0 1491 exit (1) ;
Chris@0 1492 } ;
Chris@0 1493
Chris@0 1494 seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ;
Chris@0 1495 /* Check seek backward from current position. */
Chris@0 1496 k = sf_seek (file, -20, SEEK_CUR) ;
Chris@0 1497 test_readf_short_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1498 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos)
Chris@0 1499 { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_read_short failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ;
Chris@0 1500 exit (1) ;
Chris@0 1501 } ;
Chris@0 1502
Chris@0 1503 /* Check that read past end of file returns number of items. */
Chris@0 1504 sf_seek (file, sfinfo.frames, SEEK_SET) ;
Chris@0 1505
Chris@0 1506 if ((k = sf_read_short (file, data, datalen)) != 0)
Chris@0 1507 { printf ("\n\nLine %d: Return value from sf_read_short past end of file incorrect (%d).\n", __LINE__, k) ;
Chris@0 1508 exit (1) ;
Chris@0 1509 } ;
Chris@0 1510
Chris@0 1511 /* Check seek backward from end. */
Chris@0 1512
Chris@0 1513 if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5)
Chris@0 1514 { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ;
Chris@0 1515 exit (1) ;
Chris@0 1516 } ;
Chris@0 1517
Chris@0 1518 test_read_short_or_die (file, 0, data, channels, __LINE__) ;
Chris@0 1519 if (error_function (1.0 * data [0], 1.0 * orig [5 * channels], margin))
Chris@0 1520 { printf ("\nLine %d: sf_seek (SEEK_END) followed by sf_read_short failed (%d should be %d).\n", __LINE__, data [0], orig [5 * channels]) ;
Chris@0 1521 exit (1) ;
Chris@0 1522 } ;
Chris@0 1523 } /* if (sfinfo.seekable) */
Chris@0 1524
Chris@0 1525 sf_close (file) ;
Chris@0 1526
Chris@0 1527 unlink (filename) ;
Chris@0 1528 printf ("ok\n") ;
Chris@0 1529 } /* sdlcomp_test_short */
Chris@0 1530
Chris@0 1531 static void
Chris@0 1532 sdlcomp_test_int (const char *filename, int filetype, int channels, double margin)
Chris@0 1533 { SNDFILE *file ;
Chris@0 1534 SF_INFO sfinfo ;
Chris@0 1535 int k, m, seekpos, half_max_abs ;
Chris@0 1536 long datalen ;
Chris@0 1537 int *orig, *data, *smooth ;
Chris@0 1538 double scale ;
Chris@0 1539
Chris@0 1540 channels = 1 ;
Chris@0 1541
Chris@0 1542 print_test_name ("sdlcomp_test_int", filename) ;
Chris@0 1543
Chris@0 1544 datalen = BUFFER_SIZE ;
Chris@0 1545 scale = 1.0 * 0x10000 ;
Chris@0 1546
Chris@0 1547 orig = orig_buffer.i ;
Chris@0 1548 data = data_buffer.i ;
Chris@0 1549 smooth = smooth_buffer.i ;
Chris@0 1550
Chris@0 1551 gen_signal_double (orig_buffer.d, 32000.0 * scale, channels, datalen) ;
Chris@0 1552 for (k = 0 ; k < datalen ; k++)
Chris@0 1553 orig [k] = lrint (orig_buffer.d [k]) ;
Chris@0 1554
Chris@0 1555 sfinfo.samplerate = SAMPLE_RATE ;
Chris@0 1556 sfinfo.frames = 123456789 ; /* Ridiculous value. */
Chris@0 1557 sfinfo.channels = channels ;
Chris@0 1558 sfinfo.format = filetype ;
Chris@0 1559
Chris@0 1560 /* The Vorbis encoder has a bug on PowerPC and X86-64 with sample rates
Chris@0 1561 ** <= 22050. Increasing the sample rate to 32000 avoids triggering it.
Chris@0 1562 ** See https://trac.xiph.org/ticket/1229
Chris@0 1563 */
Chris@0 1564 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
Chris@0 1565 { const char * errstr ;
Chris@0 1566
Chris@0 1567 errstr = sf_strerror (NULL) ;
Chris@0 1568 if (strstr (errstr, "Sample rate chosen is known to trigger a Vorbis") == NULL)
Chris@0 1569 { printf ("Line %d: sf_open_fd (SFM_WRITE) failed : %s\n", __LINE__, errstr) ;
Chris@0 1570 dump_log_buffer (NULL) ;
Chris@0 1571 exit (1) ;
Chris@0 1572 } ;
Chris@0 1573
Chris@0 1574 printf ("\n Sample rate -> 32kHz ") ;
Chris@0 1575 sfinfo.samplerate = 32000 ;
Chris@0 1576
Chris@0 1577 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@0 1578 } ;
Chris@0 1579
Chris@0 1580 test_writef_int_or_die (file, 0, orig, datalen, __LINE__) ;
Chris@0 1581 sf_set_string (file, SF_STR_COMMENT, long_comment) ;
Chris@0 1582 sf_close (file) ;
Chris@0 1583
Chris@0 1584 memset (data, 0, datalen * sizeof (int)) ;
Chris@0 1585
Chris@0 1586 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
Chris@0 1587 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@0 1588
Chris@0 1589 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 1590
Chris@0 1591 if (sfinfo.format != filetype)
Chris@0 1592 { printf ("Returned format incorrect (0x%08X => 0x%08X).\n", filetype, sfinfo.format) ;
Chris@0 1593 exit (1) ;
Chris@0 1594 } ;
Chris@0 1595
Chris@0 1596 if (sfinfo.frames < datalen / channels)
Chris@0 1597 { printf ("Too few.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 1598 exit (1) ;
Chris@0 1599 } ;
Chris@0 1600
Chris@0 1601 if (sfinfo.frames > (datalen + 400))
Chris@0 1602 { printf ("Too many.frames in file. (%ld should be a little more than %ld)\n", SF_COUNT_TO_LONG (sfinfo.frames), datalen) ;
Chris@0 1603 exit (1) ;
Chris@0 1604 } ;
Chris@0 1605
Chris@0 1606 if (sfinfo.channels != channels)
Chris@0 1607 { printf ("Incorrect number of channels in file.\n") ;
Chris@0 1608 exit (1) ;
Chris@0 1609 } ;
Chris@0 1610
Chris@0 1611 check_log_buffer_or_die (file, __LINE__) ;
Chris@0 1612
Chris@0 1613 test_readf_int_or_die (file, 0, data, datalen, __LINE__) ;
Chris@0 1614
Chris@0 1615 memcpy (smooth, orig, datalen * sizeof (int)) ;
Chris@0 1616 smoothed_diff_int (data, datalen) ;
Chris@0 1617 smoothed_diff_int (smooth, datalen) ;
Chris@0 1618
Chris@0 1619 half_max_abs = abs (data [0] >> 16) ;
Chris@0 1620 for (k = 1 ; k < datalen ; k++)
Chris@0 1621 { if (error_function (data [k] / scale, smooth [k] / scale, margin))
Chris@0 1622 { printf ("\nLine %d: Incorrect sample (#%d : %d should be %d).\n", __LINE__, k, data [k], smooth [k]) ;
Chris@0 1623 oct_save_int (orig, smooth, datalen) ;
Chris@0 1624 exit (1) ;
Chris@0 1625 } ;
Chris@0 1626 half_max_abs = LCT_MAX (half_max_abs, abs (data [k] / 2)) ;
Chris@0 1627 } ;
Chris@0 1628
Chris@0 1629 if (half_max_abs < 1)
Chris@0 1630 { printf ("\n\nLine %d: Signal is all zeros.\n", __LINE__) ;
Chris@0 1631 exit (1) ;
Chris@0 1632 } ;
Chris@0 1633
Chris@0 1634 if ((k = sf_readf_int (file, data, datalen)) != sfinfo.frames - datalen)
Chris@0 1635 { printf ("\n\nLine %d: Incorrect read length (%d should be %ld).\n", __LINE__, k, SF_COUNT_TO_LONG (sfinfo.frames - datalen)) ;
Chris@0 1636 exit (1) ;
Chris@0 1637 } ;
Chris@0 1638
Chris@0 1639 if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_IMA_ADPCM &&
Chris@0 1640 (sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM &&
Chris@0 1641 (sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_GSM610 &&
Chris@0 1642 (sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_G721_32 &&
Chris@0 1643 (sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_G723_24)
Chris@0 1644 for (k = 0 ; k < sfinfo.frames - datalen ; k++)
Chris@0 1645 if (abs (data [k]) > decay_response (k))
Chris@0 1646 { printf ("\n\nLine %d: Incorrect sample (#%ld : abs (%d) should be < %d).\n", __LINE__, datalen + k, data [k], decay_response (k)) ;
Chris@0 1647 exit (1) ;
Chris@0 1648 } ;
Chris@0 1649
Chris@0 1650 /* Now test sf_seek function. */
Chris@0 1651 if (sfinfo.seekable)
Chris@0 1652 { if ((k = sf_seek (file, 0, SEEK_SET)) != 0)
Chris@0 1653 { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ;
Chris@0 1654 exit (1) ;
Chris@0 1655 } ;
Chris@0 1656
Chris@0 1657 for (m = 0 ; m < 3 ; m++)
Chris@0 1658 { test_readf_int_or_die (file, m, data, datalen / 7, __LINE__) ;
Chris@0 1659
Chris@0 1660 smoothed_diff_int (data, datalen / 7) ;
Chris@0 1661 memcpy (smooth, orig + m * datalen / 7, datalen / 7 * sizeof (int)) ;
Chris@0 1662 smoothed_diff_int (smooth, datalen / 7) ;
Chris@0 1663
Chris@0 1664 for (k = 0 ; k < datalen / 7 ; k++)
Chris@0 1665 if (error_function (data [k] / scale, smooth [k] / scale, margin))
Chris@0 1666 { printf ("\nLine %d: Incorrect sample (#%d (%ld) : %d => %d).\n", __LINE__, k, k + m * (datalen / 7), smooth [k], data [k]) ;
Chris@0 1667 for (m = 0 ; m < 10 ; m++)
Chris@0 1668 printf ("%d ", data [k]) ;
Chris@0 1669 printf ("\n") ;
Chris@0 1670 exit (1) ;
Chris@0 1671 } ;
Chris@0 1672 } ; /* for (m = 0 ; m < 3 ; m++) */
Chris@0 1673
Chris@0 1674 seekpos = BUFFER_SIZE / 10 ;
Chris@0 1675
Chris@0 1676 /* Check seek from start of file. */
Chris@0 1677 if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos)
Chris@0 1678 { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ;
Chris@0 1679 exit (1) ;
Chris@0 1680 } ;
Chris@0 1681 test_readf_int_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1682
Chris@0 1683 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin))
Chris@0 1684 { printf ("\nLine %d: sf_seek (SEEK_SET) followed by sf_readf_int failed (%d, %d).\n", __LINE__, orig [1], data [0]) ;
Chris@0 1685 exit (1) ;
Chris@0 1686 } ;
Chris@0 1687
Chris@0 1688 if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1)
Chris@0 1689 { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ;
Chris@0 1690 exit (1) ;
Chris@0 1691 } ;
Chris@0 1692
Chris@0 1693 seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ;
Chris@0 1694 k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ;
Chris@0 1695 test_readf_int_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1696 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos)
Chris@0 1697 { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos + 1) ;
Chris@0 1698 exit (1) ;
Chris@0 1699 } ;
Chris@0 1700
Chris@0 1701 seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ;
Chris@0 1702 /* Check seek backward from current position. */
Chris@0 1703 k = sf_seek (file, -20, SEEK_CUR) ;
Chris@0 1704 test_readf_int_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1705 if (error_function (1.0 * data [0], 1.0 * orig [seekpos * channels], margin) || k != seekpos)
Chris@0 1706 { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], k, seekpos) ;
Chris@0 1707 exit (1) ;
Chris@0 1708 } ;
Chris@0 1709
Chris@0 1710 /* Check that read past end of file returns number of items. */
Chris@0 1711 sf_seek (file, sfinfo.frames, SEEK_SET) ;
Chris@0 1712
Chris@0 1713 if ((k = sf_readf_int (file, data, datalen)) != 0)
Chris@0 1714 { printf ("\n\nLine %d: Return value from sf_readf_int past end of file incorrect (%d).\n", __LINE__, k) ;
Chris@0 1715 exit (1) ;
Chris@0 1716 } ;
Chris@0 1717
Chris@0 1718 /* Check seek backward from end. */
Chris@0 1719
Chris@0 1720 if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5)
Chris@0 1721 { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ;
Chris@0 1722 exit (1) ;
Chris@0 1723 } ;
Chris@0 1724
Chris@0 1725 test_readf_int_or_die (file, 0, data, 1, __LINE__) ;
Chris@0 1726 if (error_function (data [0] / scale, orig [5] / scale, margin))
Chris@0 1727 { printf ("\nLine %d: sf_seek (SEEK_END) followed by sf_readf_int failed (%d should be %d).\n", __LINE__, data [0], orig [5]) ;
Chris@0 1728 exit (1) ;
Chris@0 1729 } ;
Chris@0 1730 } /* if (sfinfo.seekable) */
Chris@0 1731
Chris@0 1732 sf_close (file) ;
Chris@0 1733
Chris@0 1734 unlink (filename) ;
Chris@0 1735 printf ("ok\n") ;
Chris@0 1736 } /* sdlcomp_test_int */
Chris@0 1737
Chris@0 1738 static void
Chris@0 1739 sdlcomp_test_float (const char *filename, int filetype, int channels, double margin)
Chris@0 1740 { SNDFILE *file ;
Chris@0 1741 SF_INFO sfinfo ;
Chris@0 1742 int k, m, seekpos ;
Chris@0 1743 long datalen ;
Chris@0 1744 float *orig, *data, *smooth ;
Chris@0 1745 double half_max_abs ;
Chris@0 1746
Chris@0 1747 channels = 1 ;
Chris@0 1748
Chris@0 1749 print_test_name ("sdlcomp_test_float", filename) ;
Chris@0 1750
Chris@0 1751 if ((filetype & SF_FORMAT_SUBMASK) == SF_FORMAT_VORBIS)
Chris@0 1752 { puts ("Not working for this format.") ;
Chris@0 1753 return ;
Chris@0 1754 } ;
Chris@0 1755
Chris@0 1756 printf ("** fix this ** ") ;
Chris@0 1757
Chris@0 1758 datalen = BUFFER_SIZE ;
Chris@0 1759
Chris@0 1760 orig = orig_buffer.f ;
Chris@0 1761 data = data_buffer.f ;
Chris@0 1762 smooth = smooth_buffer.f ;
Chris@0 1763
Chris@0 1764 gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ;
Chris@0 1765 for (k = 0 ; k < datalen ; k++)
Chris@0 1766 orig [k] = lrint (orig_buffer.d [k]) ;
Chris@0 1767
Chris@0 1768 sfinfo.samplerate = SAMPLE_RATE ;
Chris@0 1769 sfinfo.frames = 123456789 ; /* Ridiculous value. */
Chris@0 1770 sfinfo.channels = channels ;
Chris@0 1771 sfinfo.format = filetype ;
Chris@0 1772
Chris@0 1773 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 1774 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@0 1775 test_write_float_or_die (file, 0, orig, datalen, __LINE__) ;
Chris@0 1776 sf_set_string (file, SF_STR_COMMENT, long_comment) ;
Chris@0 1777 sf_close (file) ;
Chris@0 1778
Chris@0 1779 memset (data, 0, datalen * sizeof (float)) ;
Chris@0 1780
Chris@0 1781 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
Chris@0 1782 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@0 1783
Chris@0 1784 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 1785
Chris@0 1786 if ((sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
Chris@0 1787 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
Chris@0 1788 exit (1) ;
Chris@0 1789 } ;
Chris@0 1790
Chris@0 1791 if (sfinfo.frames < datalen / channels)
Chris@0 1792 { printf ("Too few.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 1793 exit (1) ;
Chris@0 1794 } ;
Chris@0 1795
Chris@0 1796 if (sfinfo.frames > (datalen + 400))
Chris@0 1797 { printf ("Too many.frames in file. (%ld should be a little more than %ld)\n", SF_COUNT_TO_LONG (sfinfo.frames), datalen) ;
Chris@0 1798 exit (1) ;
Chris@0 1799 } ;
Chris@0 1800
Chris@0 1801 if (sfinfo.channels != channels)
Chris@0 1802 { printf ("Incorrect number of channels in file.\n") ;
Chris@0 1803 exit (1) ;
Chris@0 1804 } ;
Chris@0 1805
Chris@0 1806 check_comment (file, filetype, __LINE__) ;
Chris@0 1807
Chris@0 1808 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
Chris@0 1809
Chris@0 1810 check_log_buffer_or_die (file, __LINE__) ;
Chris@0 1811
Chris@0 1812 test_read_float_or_die (file, 0, data, datalen, __LINE__) ;
Chris@0 1813
Chris@0 1814 memcpy (smooth, orig, datalen * sizeof (float)) ;
Chris@0 1815 smoothed_diff_float (data, datalen) ;
Chris@0 1816 smoothed_diff_float (smooth, datalen) ;
Chris@0 1817
Chris@0 1818 half_max_abs = fabs (data [0]) ;
Chris@0 1819 for (k = 1 ; k < datalen ; k++)
Chris@0 1820 { if (error_function (data [k], smooth [k], margin))
Chris@0 1821 { printf ("\nLine %d: Incorrect sample (#%d : %d should be %d).\n", __LINE__, k, (int) data [k], (int) smooth [k]) ;
Chris@0 1822 oct_save_float (orig, smooth, datalen) ;
Chris@0 1823 exit (1) ;
Chris@0 1824 } ;
Chris@0 1825 half_max_abs = LCT_MAX (half_max_abs, abs (0.5 * data [k])) ;
Chris@0 1826 } ;
Chris@0 1827
Chris@0 1828 if (half_max_abs <= 0.0)
Chris@0 1829 { printf ("\n\nLine %d: Signal is all zeros.\n", __LINE__) ;
Chris@0 1830 printf ("half_max_abs : % 10.6f\n", half_max_abs) ;
Chris@0 1831 exit (1) ;
Chris@0 1832 } ;
Chris@0 1833
Chris@0 1834 if ((k = sf_read_float (file, data, datalen)) != sfinfo.frames - datalen)
Chris@0 1835 { printf ("\n\nLine %d: Incorrect read length (%d should be %ld).\n", __LINE__, k, SF_COUNT_TO_LONG (sfinfo.frames - datalen)) ;
Chris@0 1836 exit (1) ;
Chris@0 1837 } ;
Chris@0 1838
Chris@0 1839 if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM &&
Chris@0 1840 (sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_GSM610)
Chris@0 1841 for (k = 0 ; k < sfinfo.frames - datalen ; k++)
Chris@0 1842 if (abs (data [k]) > decay_response (k))
Chris@0 1843 { printf ("\n\nLine %d: Incorrect sample (#%ld : abs (%d) should be < %d).\n", __LINE__, datalen + k, (int) data [k], (int) decay_response (k)) ;
Chris@0 1844 exit (1) ;
Chris@0 1845 } ;
Chris@0 1846
Chris@0 1847 /* Now test sf_seek function. */
Chris@0 1848 if (sfinfo.seekable)
Chris@0 1849 { if ((k = sf_seek (file, 0, SEEK_SET)) != 0)
Chris@0 1850 { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ;
Chris@0 1851 exit (1) ;
Chris@0 1852 } ;
Chris@0 1853
Chris@0 1854 for (m = 0 ; m < 3 ; m++)
Chris@0 1855 { test_read_float_or_die (file, 0, data, datalen / 7, __LINE__) ;
Chris@0 1856
Chris@0 1857 smoothed_diff_float (data, datalen / 7) ;
Chris@0 1858 memcpy (smooth, orig + m * datalen / 7, datalen / 7 * sizeof (float)) ;
Chris@0 1859 smoothed_diff_float (smooth, datalen / 7) ;
Chris@0 1860
Chris@0 1861 for (k = 0 ; k < datalen / 7 ; k++)
Chris@0 1862 if (error_function (data [k], smooth [k], margin))
Chris@0 1863 { printf ("\nLine %d: Incorrect sample C (#%d (%ld) : %d => %d).\n", __LINE__, k, k + m * (datalen / 7), (int) smooth [k], (int) data [k]) ;
Chris@0 1864 for (m = 0 ; m < 10 ; m++)
Chris@0 1865 printf ("%d ", (int) data [k]) ;
Chris@0 1866 printf ("\n") ;
Chris@0 1867 exit (1) ;
Chris@0 1868 } ;
Chris@0 1869 } ; /* for (m = 0 ; m < 3 ; m++) */
Chris@0 1870
Chris@0 1871 seekpos = BUFFER_SIZE / 10 ;
Chris@0 1872
Chris@0 1873 /* Check seek from start of file. */
Chris@0 1874 if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos)
Chris@0 1875 { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ;
Chris@0 1876 exit (1) ;
Chris@0 1877 } ;
Chris@0 1878 test_read_float_or_die (file, 0, data, channels, __LINE__) ;
Chris@0 1879
Chris@0 1880 if (error_function (data [0], orig [seekpos * channels], margin))
Chris@0 1881 { printf ("\nLine %d: sf_seek (SEEK_SET) followed by sf_read_float failed (%d, %d).\n", __LINE__, (int) orig [1], (int) data [0]) ;
Chris@0 1882 exit (1) ;
Chris@0 1883 } ;
Chris@0 1884
Chris@0 1885 if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1)
Chris@0 1886 { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ;
Chris@0 1887 exit (1) ;
Chris@0 1888 } ;
Chris@0 1889
Chris@0 1890 seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ;
Chris@0 1891 k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ;
Chris@0 1892 test_read_float_or_die (file, 0, data, channels, __LINE__) ;
Chris@0 1893 if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos)
Chris@0 1894 { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_read_float failed (%d, %d) (%d, %d).\n", __LINE__, (int) data [0], (int) orig [seekpos * channels], k, seekpos + 1) ;
Chris@0 1895 exit (1) ;
Chris@0 1896 } ;
Chris@0 1897
Chris@0 1898 seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ;
Chris@0 1899 /* Check seek backward from current position. */
Chris@0 1900 k = sf_seek (file, -20, SEEK_CUR) ;
Chris@0 1901 test_read_float_or_die (file, 0, data, channels, __LINE__) ;
Chris@0 1902 if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos)
Chris@0 1903 { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_read_float failed (%d, %d) (%d, %d).\n", __LINE__, (int) data [0], (int) orig [seekpos * channels], k, seekpos) ;
Chris@0 1904 exit (1) ;
Chris@0 1905 } ;
Chris@0 1906
Chris@0 1907 /* Check that read past end of file returns number of items. */
Chris@0 1908 sf_seek (file, sfinfo.frames, SEEK_SET) ;
Chris@0 1909
Chris@0 1910 if ((k = sf_read_float (file, data, datalen)) != 0)
Chris@0 1911 { printf ("\n\nLine %d: Return value from sf_read_float past end of file incorrect (%d).\n", __LINE__, k) ;
Chris@0 1912 exit (1) ;
Chris@0 1913 } ;
Chris@0 1914
Chris@0 1915 /* Check seek backward from end. */
Chris@0 1916
Chris@0 1917 if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5)
Chris@0 1918 { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ;
Chris@0 1919 exit (1) ;
Chris@0 1920 } ;
Chris@0 1921
Chris@0 1922 test_read_float_or_die (file, 0, data, channels, __LINE__) ;
Chris@0 1923 if (error_function (data [0], orig [5 * channels], margin))
Chris@0 1924 { printf ("\nLine %d: sf_seek (SEEK_END) followed by sf_read_float failed (%f should be %f).\n", __LINE__, data [0], orig [5 * channels]) ;
Chris@0 1925 exit (1) ;
Chris@0 1926 } ;
Chris@0 1927 } /* if (sfinfo.seekable) */
Chris@0 1928
Chris@0 1929 sf_close (file) ;
Chris@0 1930
Chris@0 1931 unlink (filename) ;
Chris@0 1932 printf ("ok\n") ;
Chris@0 1933 } /* sdlcomp_test_float */
Chris@0 1934
Chris@0 1935 static void
Chris@0 1936 sdlcomp_test_double (const char *filename, int filetype, int channels, double margin)
Chris@0 1937 { SNDFILE *file ;
Chris@0 1938 SF_INFO sfinfo ;
Chris@0 1939 int k, m, seekpos ;
Chris@0 1940 long datalen ;
Chris@0 1941 double *orig, *data, *smooth, half_max_abs ;
Chris@0 1942
Chris@0 1943 channels = 1 ;
Chris@0 1944 print_test_name ("sdlcomp_test_double", filename) ;
Chris@0 1945
Chris@0 1946 if ((filetype & SF_FORMAT_SUBMASK) == SF_FORMAT_VORBIS)
Chris@0 1947 { puts ("Not working for this format.") ;
Chris@0 1948 return ;
Chris@0 1949 } ;
Chris@0 1950
Chris@0 1951 datalen = BUFFER_SIZE ;
Chris@0 1952
Chris@0 1953 orig = orig_buffer.d ;
Chris@0 1954 data = data_buffer.d ;
Chris@0 1955 smooth = smooth_buffer.d ;
Chris@0 1956
Chris@0 1957 gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ;
Chris@0 1958
Chris@0 1959 sfinfo.samplerate = SAMPLE_RATE ;
Chris@0 1960 sfinfo.frames = 123456789 ; /* Ridiculous value. */
Chris@0 1961 sfinfo.channels = channels ;
Chris@0 1962 sfinfo.format = filetype ;
Chris@0 1963
Chris@0 1964 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 1965 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@0 1966 test_write_double_or_die (file, 0, orig, datalen, __LINE__) ;
Chris@0 1967 sf_set_string (file, SF_STR_COMMENT, long_comment) ;
Chris@0 1968 sf_close (file) ;
Chris@0 1969
Chris@0 1970 memset (data, 0, datalen * sizeof (double)) ;
Chris@0 1971
Chris@0 1972 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
Chris@0 1973 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@0 1974
Chris@0 1975 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 1976
Chris@0 1977 if (sfinfo.format != filetype)
Chris@0 1978 { printf ("Returned format incorrect (0x%08X => 0x%08X).\n", filetype, sfinfo.format) ;
Chris@0 1979 exit (1) ;
Chris@0 1980 } ;
Chris@0 1981
Chris@0 1982 if (sfinfo.frames < datalen / channels)
Chris@0 1983 { printf ("Too few.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 1984 exit (1) ;
Chris@0 1985 } ;
Chris@0 1986
Chris@0 1987 if (sfinfo.frames > (datalen + 400))
Chris@0 1988 { printf ("Too many.frames in file. (%ld should be a little more than %ld)\n", SF_COUNT_TO_LONG (sfinfo.frames), datalen) ;
Chris@0 1989 exit (1) ;
Chris@0 1990 } ;
Chris@0 1991
Chris@0 1992 if (sfinfo.channels != channels)
Chris@0 1993 { printf ("Incorrect number of channels in file.\n") ;
Chris@0 1994 exit (1) ;
Chris@0 1995 } ;
Chris@0 1996
Chris@0 1997 check_comment (file, filetype, __LINE__) ;
Chris@0 1998
Chris@0 1999 check_comment (file, filetype, __LINE__) ;
Chris@0 2000
Chris@0 2001 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
Chris@0 2002
Chris@0 2003 check_log_buffer_or_die (file, __LINE__) ;
Chris@0 2004
Chris@0 2005 test_read_double_or_die (file, 0, data, datalen, __LINE__) ;
Chris@0 2006
Chris@0 2007 memcpy (smooth, orig, datalen * sizeof (double)) ;
Chris@0 2008 smoothed_diff_double (data, datalen) ;
Chris@0 2009 smoothed_diff_double (smooth, datalen) ;
Chris@0 2010
Chris@0 2011 half_max_abs = 0.0 ;
Chris@0 2012 for (k = 0 ; k < datalen ; k++)
Chris@0 2013 { if (error_function (data [k], smooth [k], margin))
Chris@0 2014 { printf ("\n\nLine %d: Incorrect sample (#%d : %d should be %d).\n", __LINE__, k, (int) data [k], (int) smooth [k]) ;
Chris@0 2015 oct_save_double (orig, smooth, datalen) ;
Chris@0 2016 exit (1) ;
Chris@0 2017 } ;
Chris@0 2018 half_max_abs = LCT_MAX (half_max_abs, 0.5 * fabs (data [k])) ;
Chris@0 2019 } ;
Chris@0 2020
Chris@0 2021 if (half_max_abs < 1.0)
Chris@0 2022 { printf ("\n\nLine %d: Signal is all zeros.\n", __LINE__) ;
Chris@0 2023 exit (1) ;
Chris@0 2024 } ;
Chris@0 2025
Chris@0 2026 if ((k = sf_read_double (file, data, datalen)) != sfinfo.frames - datalen)
Chris@0 2027 { printf ("\n\nLine %d: Incorrect read length (%d should be %ld).\n", __LINE__, k, SF_COUNT_TO_LONG (sfinfo.frames - datalen)) ;
Chris@0 2028 exit (1) ;
Chris@0 2029 } ;
Chris@0 2030
Chris@0 2031 if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM &&
Chris@0 2032 (sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_GSM610)
Chris@0 2033 for (k = 0 ; k < sfinfo.frames - datalen ; k++)
Chris@0 2034 if (abs (data [k]) > decay_response (k))
Chris@0 2035 { printf ("\n\nLine %d: Incorrect sample (#%ld : abs (%d) should be < %d).\n", __LINE__, datalen + k, (int) data [k], (int) decay_response (k)) ;
Chris@0 2036 exit (1) ;
Chris@0 2037 } ;
Chris@0 2038
Chris@0 2039 /* Now test sf_seek function. */
Chris@0 2040 if (sfinfo.seekable)
Chris@0 2041 { if ((k = sf_seek (file, 0, SEEK_SET)) != 0)
Chris@0 2042 { printf ("\n\nLine %d: Seek to start of file failed (%d).\n", __LINE__, k) ;
Chris@0 2043 exit (1) ;
Chris@0 2044 } ;
Chris@0 2045
Chris@0 2046 for (m = 0 ; m < 3 ; m++)
Chris@0 2047 { test_read_double_or_die (file, m, data, datalen / 7, __LINE__) ;
Chris@0 2048
Chris@0 2049 smoothed_diff_double (data, datalen / 7) ;
Chris@0 2050 memcpy (smooth, orig + m * datalen / 7, datalen / 7 * sizeof (double)) ;
Chris@0 2051 smoothed_diff_double (smooth, datalen / 7) ;
Chris@0 2052
Chris@0 2053 for (k = 0 ; k < datalen / 7 ; k++)
Chris@0 2054 if (error_function (data [k], smooth [k], margin))
Chris@0 2055 { printf ("\nLine %d: Incorrect sample C (#%d (%ld) : %d => %d).\n", __LINE__, k, k + m * (datalen / 7), (int) smooth [k], (int) data [k]) ;
Chris@0 2056 for (m = 0 ; m < 10 ; m++)
Chris@0 2057 printf ("%d ", (int) data [k]) ;
Chris@0 2058 printf ("\n") ;
Chris@0 2059 exit (1) ;
Chris@0 2060 } ;
Chris@0 2061 } ; /* for (m = 0 ; m < 3 ; m++) */
Chris@0 2062
Chris@0 2063 seekpos = BUFFER_SIZE / 10 ;
Chris@0 2064
Chris@0 2065 /* Check seek from start of file. */
Chris@0 2066 if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos)
Chris@0 2067 { printf ("Seek to start of file + %d failed (%d).\n", seekpos, k) ;
Chris@0 2068 exit (1) ;
Chris@0 2069 } ;
Chris@0 2070 test_read_double_or_die (file, 0, data, channels, __LINE__) ;
Chris@0 2071
Chris@0 2072 if (error_function (data [0], orig [seekpos * channels], margin))
Chris@0 2073 { printf ("\nLine %d: sf_seek (SEEK_SET) followed by sf_read_double failed (%d, %d).\n", __LINE__, (int) orig [1], (int) data [0]) ;
Chris@0 2074 exit (1) ;
Chris@0 2075 } ;
Chris@0 2076
Chris@0 2077 if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1)
Chris@0 2078 { printf ("\n\nLine %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %d)\n", __LINE__, k, seekpos + 1) ;
Chris@0 2079 exit (1) ;
Chris@0 2080 } ;
Chris@0 2081
Chris@0 2082 seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ;
Chris@0 2083 k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ;
Chris@0 2084 test_read_double_or_die (file, 0, data, channels, __LINE__) ;
Chris@0 2085 if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos)
Chris@0 2086 { printf ("\nLine %d: sf_seek (forwards, SEEK_CUR) followed by sf_read_double failed (%d, %d) (%d, %d).\n", __LINE__, (int) data [0], (int) orig [seekpos * channels], k, seekpos + 1) ;
Chris@0 2087 exit (1) ;
Chris@0 2088 } ;
Chris@0 2089
Chris@0 2090 seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ;
Chris@0 2091 /* Check seek backward from current position. */
Chris@0 2092 k = sf_seek (file, -20, SEEK_CUR) ;
Chris@0 2093 test_read_double_or_die (file, 0, data, channels, __LINE__) ;
Chris@0 2094 if (error_function (data [0], orig [seekpos * channels], margin) || k != seekpos)
Chris@0 2095 { printf ("\nLine %d: sf_seek (backwards, SEEK_CUR) followed by sf_read_double failed (%d, %d) (%d, %d).\n", __LINE__, (int) data [0], (int) orig [seekpos * channels], k, seekpos) ;
Chris@0 2096 exit (1) ;
Chris@0 2097 } ;
Chris@0 2098
Chris@0 2099 /* Check that read past end of file returns number of items. */
Chris@0 2100 sf_seek (file, sfinfo.frames, SEEK_SET) ;
Chris@0 2101
Chris@0 2102 if ((k = sf_read_double (file, data, datalen)) != 0)
Chris@0 2103 { printf ("\n\nLine %d: Return value from sf_read_double past end of file incorrect (%d).\n", __LINE__, k) ;
Chris@0 2104 exit (1) ;
Chris@0 2105 } ;
Chris@0 2106
Chris@0 2107 /* Check seek backward from end. */
Chris@0 2108
Chris@0 2109 if ((k = sf_seek (file, 5 - sfinfo.frames, SEEK_END)) != 5)
Chris@0 2110 { printf ("\n\nLine %d: sf_seek (SEEK_END) returned %d instead of %d.\n", __LINE__, k, 5) ;
Chris@0 2111 exit (1) ;
Chris@0 2112 } ;
Chris@0 2113
Chris@0 2114 test_read_double_or_die (file, 0, data, channels, __LINE__) ;
Chris@0 2115 if (error_function (data [0], orig [5 * channels], margin))
Chris@0 2116 { printf ("\nLine %d: sf_seek (SEEK_END) followed by sf_read_double failed (%f should be %f).\n", __LINE__, data [0], orig [5 * channels]) ;
Chris@0 2117 exit (1) ;
Chris@0 2118 } ;
Chris@0 2119 } /* if (sfinfo.seekable) */
Chris@0 2120
Chris@0 2121 sf_close (file) ;
Chris@0 2122
Chris@0 2123 unlink (filename) ;
Chris@0 2124 printf ("ok\n") ;
Chris@0 2125 } /* sdlcomp_test_double */
Chris@0 2126
Chris@0 2127 static void
Chris@0 2128 read_raw_test (const char *filename, int filetype, int channels)
Chris@0 2129 { SNDFILE *file ;
Chris@0 2130 SF_INFO sfinfo ;
Chris@0 2131 sf_count_t count ;
Chris@0 2132 long datalen ;
Chris@0 2133 short *orig, *data ;
Chris@0 2134 int k ;
Chris@0 2135
Chris@0 2136 print_test_name ("read_raw_test", filename) ;
Chris@0 2137
Chris@0 2138 datalen = ARRAY_LEN (orig_buffer.s) / 2 ;
Chris@0 2139
Chris@0 2140 orig = orig_buffer.s ;
Chris@0 2141 data = data_buffer.s ;
Chris@0 2142
Chris@0 2143 gen_signal_double (orig_buffer.d, 32000.0, channels, datalen) ;
Chris@0 2144 for (k = 0 ; k < datalen ; k++)
Chris@0 2145 orig [k] = lrint (orig_buffer.d [k]) ;
Chris@0 2146
Chris@0 2147 sfinfo.samplerate = SAMPLE_RATE ;
Chris@0 2148 sfinfo.frames = 123456789 ; /* Ridiculous value. */
Chris@0 2149 sfinfo.channels = channels ;
Chris@0 2150 sfinfo.format = filetype ;
Chris@0 2151
Chris@0 2152 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 2153 test_write_short_or_die (file, 0, orig, datalen, __LINE__) ;
Chris@0 2154 sf_set_string (file, SF_STR_COMMENT, long_comment) ;
Chris@0 2155 sf_close (file) ;
Chris@0 2156
Chris@0 2157 memset (data, 0, datalen * sizeof (double)) ;
Chris@0 2158
Chris@0 2159 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
Chris@0 2160 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@0 2161
Chris@0 2162 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
Chris@0 2163
Chris@0 2164 if (sfinfo.format != filetype)
Chris@0 2165 { printf ("Returned format incorrect (0x%08X => 0x%08X).\n", filetype, sfinfo.format) ;
Chris@0 2166 exit (1) ;
Chris@0 2167 } ;
Chris@0 2168
Chris@0 2169 if (sfinfo.frames < datalen / channels)
Chris@0 2170 { printf ("Too few.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 2171 exit (1) ;
Chris@0 2172 } ;
Chris@0 2173
Chris@0 2174 if (sfinfo.frames > (datalen + 400))
Chris@0 2175 { printf ("Too many.frames in file. (%ld should be a little more than %ld)\n", SF_COUNT_TO_LONG (sfinfo.frames), datalen) ;
Chris@0 2176 exit (1) ;
Chris@0 2177 } ;
Chris@0 2178
Chris@0 2179 if (sfinfo.channels != channels)
Chris@0 2180 { printf ("Incorrect number of channels in file.\n") ;
Chris@0 2181 exit (1) ;
Chris@0 2182 } ;
Chris@0 2183
Chris@0 2184 check_comment (file, filetype, __LINE__) ;
Chris@0 2185
Chris@0 2186 count = sf_read_raw (file, orig_buffer.c, datalen + 5 * channels) ;
Chris@0 2187 if (count != sfinfo.channels * sfinfo.frames)
Chris@0 2188 { printf ("\nLine %d : sf_read_raw returned %ld should be %ld\n", __LINE__, SF_COUNT_TO_LONG (count), sfinfo.channels * SF_COUNT_TO_LONG (sfinfo.frames)) ;
Chris@0 2189 exit (1) ;
Chris@0 2190 } ;
Chris@0 2191
Chris@0 2192 sf_close (file) ;
Chris@0 2193
Chris@0 2194 unlink (filename) ;
Chris@0 2195 printf ("ok\n") ;
Chris@0 2196 } /* read_raw_test */
Chris@0 2197
Chris@0 2198 /*========================================================================================
Chris@0 2199 ** Auxiliary functions
Chris@0 2200 */
Chris@0 2201
Chris@0 2202 #define SIGNAL_MAXVAL 30000.0
Chris@0 2203 #define DECAY_COUNT 1000
Chris@0 2204
Chris@0 2205 static int
Chris@0 2206 decay_response (int k)
Chris@0 2207 { if (k < 1)
Chris@0 2208 return (int) (1.2 * SIGNAL_MAXVAL) ;
Chris@0 2209 if (k > DECAY_COUNT)
Chris@0 2210 return 0 ;
Chris@0 2211 return (int) (1.2 * SIGNAL_MAXVAL * (DECAY_COUNT - k) / (1.0 * DECAY_COUNT)) ;
Chris@0 2212 } /* decay_response */
Chris@0 2213
Chris@0 2214 static void
Chris@0 2215 gen_signal_double (double *data, double scale, int channels, int datalen)
Chris@0 2216 { int k, ramplen ;
Chris@0 2217 double amp = 0.0 ;
Chris@0 2218
Chris@0 2219 ramplen = DECAY_COUNT ;
Chris@0 2220
Chris@0 2221 if (channels == 1)
Chris@0 2222 { for (k = 0 ; k < datalen ; k++)
Chris@0 2223 { if (k <= ramplen)
Chris@0 2224 amp = scale * k / ((double) ramplen) ;
Chris@0 2225 else if (k > datalen - ramplen)
Chris@0 2226 amp = scale * (datalen - k) / ((double) ramplen) ;
Chris@0 2227
Chris@0 2228 /*-printf ("%3d : %g\n", k, amp) ;-*/
Chris@0 2229
Chris@0 2230 data [k] = amp * (0.4 * sin (33.3 * 2.0 * M_PI * ((double) (k+1)) / ((double) SAMPLE_RATE))
Chris@0 2231 + 0.3 * cos (201.1 * 2.0 * M_PI * ((double) (k+1)) / ((double) SAMPLE_RATE))) ;
Chris@0 2232 } ;
Chris@0 2233 }
Chris@0 2234 else
Chris@0 2235 { for (k = 0 ; k < datalen ; k ++)
Chris@0 2236 { if (k <= ramplen)
Chris@0 2237 amp = scale * k / ((double) ramplen) ;
Chris@0 2238 else if (k > datalen - ramplen)
Chris@0 2239 amp = scale * (datalen - k) / ((double) ramplen) ;
Chris@0 2240
Chris@0 2241 data [2 * k] = amp * (0.4 * sin (33.3 * 2.0 * M_PI * ((double) (k+1)) / ((double) SAMPLE_RATE))
Chris@0 2242 + 0.3 * cos (201.1 * 2.0 * M_PI * ((double) (k+1)) / ((double) SAMPLE_RATE))) ;
Chris@0 2243 data [2 * k + 1] = amp * (0.4 * sin (55.5 * 2.0 * M_PI * ((double) (k+1)) / ((double) SAMPLE_RATE))
Chris@0 2244 + 0.3 * cos (201.1 * 2.0 * M_PI * ((double) (k+1)) / ((double) SAMPLE_RATE))) ;
Chris@0 2245 } ;
Chris@0 2246 } ;
Chris@0 2247
Chris@0 2248 return ;
Chris@0 2249 } /* gen_signal_double */
Chris@0 2250
Chris@0 2251 static int
Chris@0 2252 error_function (double data, double orig, double margin)
Chris@0 2253 { double error ;
Chris@0 2254
Chris@0 2255 if (fabs (orig) <= 500.0)
Chris@0 2256 error = fabs (fabs (data) - fabs (orig)) / 2000.0 ;
Chris@0 2257 else if (fabs (orig) <= 1000.0)
Chris@0 2258 error = fabs (data - orig) / 3000.0 ;
Chris@0 2259 else
Chris@0 2260 error = fabs (data - orig) / fabs (orig) ;
Chris@0 2261
Chris@0 2262 if (error > margin)
Chris@0 2263 { printf ("\n\nerror_function (data = %f, orig = %f, margin = %f) -> %f\n", data, orig, margin, error) ;
Chris@0 2264 return 1 ;
Chris@0 2265 } ;
Chris@0 2266 return 0 ;
Chris@0 2267 } /* error_function */
Chris@0 2268
Chris@0 2269 static void
Chris@0 2270 smoothed_diff_short (short *data, unsigned int datalen)
Chris@0 2271 { unsigned int k ;
Chris@0 2272 double memory = 0.0 ;
Chris@0 2273
Chris@0 2274 /* Calculate the smoothed sample-to-sample difference. */
Chris@0 2275 for (k = 0 ; k < datalen - 1 ; k++)
Chris@0 2276 { memory = 0.7 * memory + (1 - 0.7) * (double) (data [k+1] - data [k]) ;
Chris@0 2277 data [k] = (short) memory ;
Chris@0 2278 } ;
Chris@0 2279 data [datalen-1] = data [datalen-2] ;
Chris@0 2280
Chris@0 2281 } /* smoothed_diff_short */
Chris@0 2282
Chris@0 2283 static void
Chris@0 2284 smoothed_diff_int (int *data, unsigned int datalen)
Chris@0 2285 { unsigned int k ;
Chris@0 2286 double memory = 0.0 ;
Chris@0 2287
Chris@0 2288 /* Calculate the smoothed sample-to-sample difference. */
Chris@0 2289 for (k = 0 ; k < datalen - 1 ; k++)
Chris@0 2290 { memory = 0.7 * memory + (1 - 0.7) * (double) (data [k+1] - data [k]) ;
Chris@0 2291 data [k] = (int) memory ;
Chris@0 2292 } ;
Chris@0 2293 data [datalen-1] = data [datalen-2] ;
Chris@0 2294
Chris@0 2295 } /* smoothed_diff_int */
Chris@0 2296
Chris@0 2297 static void
Chris@0 2298 smoothed_diff_float (float *data, unsigned int datalen)
Chris@0 2299 { unsigned int k ;
Chris@0 2300 float memory = 0.0 ;
Chris@0 2301
Chris@0 2302 /* Calculate the smoothed sample-to-sample difference. */
Chris@0 2303 for (k = 0 ; k < datalen - 1 ; k++)
Chris@0 2304 { memory = 0.7 * memory + (1 - 0.7) * (data [k+1] - data [k]) ;
Chris@0 2305 data [k] = memory ;
Chris@0 2306 } ;
Chris@0 2307 data [datalen-1] = data [datalen-2] ;
Chris@0 2308
Chris@0 2309 } /* smoothed_diff_float */
Chris@0 2310
Chris@0 2311 static void
Chris@0 2312 smoothed_diff_double (double *data, unsigned int datalen)
Chris@0 2313 { unsigned int k ;
Chris@0 2314 double memory = 0.0 ;
Chris@0 2315
Chris@0 2316 /* Calculate the smoothed sample-to-sample difference. */
Chris@0 2317 for (k = 0 ; k < datalen - 1 ; k++)
Chris@0 2318 { memory = 0.7 * memory + (1 - 0.7) * (data [k+1] - data [k]) ;
Chris@0 2319 data [k] = memory ;
Chris@0 2320 } ;
Chris@0 2321 data [datalen-1] = data [datalen-2] ;
Chris@0 2322
Chris@0 2323 } /* smoothed_diff_double */
Chris@0 2324
Chris@0 2325 static void
Chris@0 2326 check_comment (SNDFILE * file, int format, int lineno)
Chris@0 2327 { const char *comment ;
Chris@0 2328
Chris@0 2329 switch (format & SF_FORMAT_TYPEMASK)
Chris@0 2330 { case SF_FORMAT_AIFF :
Chris@0 2331 case SF_FORMAT_WAV :
Chris@0 2332 case SF_FORMAT_WAVEX :
Chris@0 2333 break ;
Chris@0 2334 default :
Chris@0 2335 return ;
Chris@0 2336 } ;
Chris@0 2337
Chris@0 2338 comment = sf_get_string (file, SF_STR_COMMENT) ;
Chris@0 2339 if (comment == NULL)
Chris@0 2340 { printf ("\n\nLine %d : File does not contain a comment string.\n\n", lineno) ;
Chris@0 2341 exit (1) ;
Chris@0 2342 } ;
Chris@0 2343
Chris@0 2344 if (strcmp (comment, long_comment) != 0)
Chris@0 2345 { printf ("\n\nLine %d : File comment does not match comment written.\n\n", lineno) ;
Chris@0 2346 exit (1) ;
Chris@0 2347 } ;
Chris@0 2348
Chris@0 2349 return ;
Chris@0 2350 } /* check_comment */
Chris@0 2351
Chris@0 2352 static int
Chris@0 2353 is_lossy (int filetype)
Chris@0 2354 {
Chris@0 2355 switch (SF_FORMAT_SUBMASK & filetype)
Chris@0 2356 { case SF_FORMAT_PCM_U8 :
Chris@0 2357 case SF_FORMAT_PCM_S8 :
Chris@0 2358 case SF_FORMAT_PCM_16 :
Chris@0 2359 case SF_FORMAT_PCM_24 :
Chris@0 2360 case SF_FORMAT_PCM_32 :
Chris@0 2361 case SF_FORMAT_FLOAT :
Chris@0 2362 case SF_FORMAT_DOUBLE :
Chris@0 2363 return 0 ;
Chris@0 2364
Chris@0 2365 default :
Chris@0 2366 break ;
Chris@0 2367 } ;
Chris@0 2368
Chris@0 2369 return 1 ;
Chris@0 2370 } /* is_lossy */
Chris@0 2371