annotate src/libsndfile-1.0.25/tests/lossy_comp_test.c @ 85:545efbb81310

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