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

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