~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/libsndfile/tests/lossy_comp_test.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

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