INSTALL.txt review.
[Faustine.git] / interpreter / lib / src / libsndfile-1.0.25 / tests / fix_this.c
1 /*
2 ** Copyright (C) 1999-2011 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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <math.h>
24
25 #include <sndfile.h>
26
27 #include "utils.h"
28
29 #define BUFFER_SIZE (1<<14) /* Should be (1<<14) */
30 #define SAMPLE_RATE (11025)
31
32 #ifndef M_PI
33 #define M_PI 3.14159265358979323846264338
34 #endif
35
36 static void lcomp_test_int (const char *str, const char *filename, int filetype, double margin) ;
37
38 static int error_function (double data, double orig, double margin) ;
39 static int decay_response (int k) ;
40
41 static void gen_signal_double (double *data, double scale, int datalen) ;
42
43 /* Force the start of these buffers to be double aligned. Sparc-solaris will
44 ** choke if they are not.
45 */
46
47 typedef union
48 { double d [BUFFER_SIZE + 1] ;
49 int i [BUFFER_SIZE + 1] ;
50 } BUFFER ;
51
52 static BUFFER data_buffer ;
53 static BUFFER orig_buffer ;
54
55 int
56 main (void)
57 { const char *filename = "test.au" ;
58
59 lcomp_test_int ("au_g721", filename, SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G721_32, 0.06) ;
60
61 return 0 ;
62 } /* main */
63
64 /*============================================================================================
65 ** Here are the test functions.
66 */
67
68 static void
69 lcomp_test_int (const char *str, const char *filename, int filetype, double margin)
70 { SNDFILE *file ;
71 SF_INFO sfinfo ;
72 int k, m, *orig, *data, sum_abs ;
73 long datalen, seekpos ;
74 double scale ;
75
76 printf ("\nThis is program is not part of the libsndfile test suite.\n\n") ;
77
78 printf (" lcomp_test_int : %s ... ", str) ;
79 fflush (stdout) ;
80
81 datalen = BUFFER_SIZE ;
82
83 scale = 1.0 * 0x10000 ;
84
85 data = data_buffer.i ;
86 orig = orig_buffer.i ;
87
88 gen_signal_double (orig_buffer.d, 32000.0 * scale, datalen) ;
89 for (k = 0 ; k < datalen ; k++)
90 orig [k] = orig_buffer.d [k] ;
91
92
93 sfinfo.samplerate = SAMPLE_RATE ;
94 sfinfo.frames = 123456789 ; /* Ridiculous value. */
95 sfinfo.channels = 1 ;
96 sfinfo.format = filetype ;
97
98 if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
99 { printf ("sf_open_write failed with error : ") ;
100 puts (sf_strerror (NULL)) ;
101 exit (1) ;
102 } ;
103
104 if ((k = sf_writef_int (file, orig, datalen)) != datalen)
105 { printf ("sf_writef_int failed with short write (%ld => %d).\n", datalen, k) ;
106 exit (1) ;
107 } ;
108 sf_close (file) ;
109
110 memset (data, 0, datalen * sizeof (int)) ;
111
112 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
113 memset (&sfinfo, 0, sizeof (sfinfo)) ;
114
115 if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
116 { printf ("sf_open_read failed with error : ") ;
117 puts (sf_strerror (NULL)) ;
118 exit (1) ;
119 } ;
120
121 if ((sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
122 { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
123 exit (1) ;
124 } ;
125
126 if (sfinfo.frames < datalen)
127 { printf ("Too few.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
128 exit (1) ;
129 } ;
130
131 if (sfinfo.frames > (datalen + datalen / 2))
132 { printf ("Too many.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ;
133 exit (1) ;
134 } ;
135
136 if (sfinfo.channels != 1)
137 { printf ("Incorrect number of channels in file.\n") ;
138 exit (1) ;
139 } ;
140
141 check_log_buffer_or_die (file, __LINE__) ;
142
143 if ((k = sf_readf_int (file, data, datalen)) != datalen)
144 { printf ("Line %d: short read (%d should be %ld).\n", __LINE__, k, datalen) ;
145 exit (1) ;
146 } ;
147
148 sum_abs = 0 ;
149 for (k = 0 ; k < datalen ; k++)
150 { if (error_function (data [k] / scale, orig [k] / scale, margin))
151 { printf ("Line %d: Incorrect sample (#%d : %f should be %f).\n", __LINE__, k, data [k] / scale, orig [k] / scale) ;
152 oct_save_int (orig, data, datalen) ;
153 exit (1) ;
154 } ;
155 sum_abs = abs (sum_abs + abs (data [k])) ;
156 } ;
157
158 if (sum_abs < 1.0)
159 { printf ("Line %d: Signal is all zeros.\n", __LINE__) ;
160 exit (1) ;
161 } ;
162
163 if ((k = sf_readf_int (file, data, datalen)) != sfinfo.frames - datalen)
164 { printf ("Line %d: Incorrect read length (%ld should be %d).\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames - datalen), k) ;
165 exit (1) ;
166 } ;
167
168 /* This check is only for block based encoders which must append silence
169 ** to the end of a file so as to fill out a block.
170 */
171 if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM)
172 for (k = 0 ; k < sfinfo.frames - datalen ; k++)
173 if (abs (data [k] / scale) > decay_response (k))
174 { printf ("Line %d : Incorrect sample B (#%d : abs (%d) should be < %d).\n", __LINE__, k, data [k], decay_response (k)) ;
175 exit (1) ;
176 } ;
177
178 if (! sfinfo.seekable)
179 { printf ("ok\n") ;
180 return ;
181 } ;
182
183 /* Now test sf_seek function. */
184
185 if ((k = sf_seek (file, 0, SEEK_SET)) != 0)
186 { printf ("Line %d: Seek to start of file failed (%d).\n", __LINE__, k) ;
187 exit (1) ;
188 } ;
189
190 for (m = 0 ; m < 3 ; m++)
191 { int n ;
192
193 if ((k = sf_readf_int (file, data, 11)) != 11)
194 { printf ("Line %d: Incorrect read length (11 => %d).\n", __LINE__, k) ;
195 exit (1) ;
196 } ;
197
198 for (k = 0 ; k < 11 ; k++)
199 if (error_function (data [k] / scale, orig [k + m * 11] / scale, margin))
200 { printf ("Line %d: Incorrect sample (m = %d) (#%d : %d => %d).\n", __LINE__, m, k + m * 11, orig [k + m * 11], data [k]) ;
201 for (n = 0 ; n < 1 ; n++)
202 printf ("%d ", data [n]) ;
203 printf ("\n") ;
204 exit (1) ;
205 } ;
206 } ;
207
208 seekpos = BUFFER_SIZE / 10 ;
209
210 /* Check seek from start of file. */
211 if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos)
212 { printf ("Seek to start of file + %ld failed (%d).\n", seekpos, k) ;
213 exit (1) ;
214 } ;
215
216 if ((k = sf_readf_int (file, data, 1)) != 1)
217 { printf ("Line %d: sf_readf_int (file, data, 1) returned %d.\n", __LINE__, k) ;
218 exit (1) ;
219 } ;
220
221 if (error_function ((double) data [0], (double) orig [seekpos], margin))
222 { printf ("Line %d: sf_seek (SEEK_SET) followed by sf_readf_int failed (%d, %d).\n", __LINE__, orig [1], data [0]) ;
223 exit (1) ;
224 } ;
225
226 if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1)
227 { printf ("Line %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %ld)\n", __LINE__, k, seekpos + 1) ;
228 exit (1) ;
229 } ;
230
231 seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ;
232 k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ;
233 sf_readf_int (file, data, 1) ;
234 if (error_function ((double) data [0], (double) orig [seekpos], margin) || k != seekpos)
235 { printf ("Line %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %ld).\n", __LINE__, data [0], orig [seekpos], k, seekpos + 1) ;
236 exit (1) ;
237 } ;
238
239 seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ;
240 /* Check seek backward from current position. */
241 k = sf_seek (file, -20, SEEK_CUR) ;
242 sf_readf_int (file, data, 1) ;
243 if (error_function ((double) data [0], (double) orig [seekpos], margin) || k != seekpos)
244 { printf ("sf_seek (backwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %ld).\n", data [0], orig [seekpos], k, seekpos) ;
245 exit (1) ;
246 } ;
247
248 /* Check that read past end of file returns number of items. */
249 sf_seek (file, (int) sfinfo.frames, SEEK_SET) ;
250
251 if ((k = sf_readf_int (file, data, datalen)) != 0)
252 { printf ("Line %d: Return value from sf_readf_int past end of file incorrect (%d).\n", __LINE__, k) ;
253 exit (1) ;
254 } ;
255
256 /* Check seek backward from end. */
257 if ((k = sf_seek (file, 5 - (int) sfinfo.frames, SEEK_END)) != 5)
258 { printf ("sf_seek (SEEK_END) returned %d instead of %d.\n", k, 5) ;
259 exit (1) ;
260 } ;
261
262 sf_readf_int (file, data, 1) ;
263 if (error_function (data [0] / scale, orig [5] / scale, margin))
264 { printf ("Line %d: sf_seek (SEEK_END) followed by sf_readf_short failed (%d should be %d).\n", __LINE__, data [0], orig [5]) ;
265 exit (1) ;
266 } ;
267
268 sf_close (file) ;
269
270 printf ("ok\n") ;
271 } /* lcomp_test_int */
272
273 /*========================================================================================
274 ** Auxiliary functions
275 */
276
277 #define SIGNAL_MAXVAL 30000.0
278 #define DECAY_COUNT 800
279
280 static int
281 decay_response (int k)
282 { if (k < 1)
283 return (int) (1.2 * SIGNAL_MAXVAL) ;
284 if (k > DECAY_COUNT)
285 return 0 ;
286 return (int) (1.2 * SIGNAL_MAXVAL * (DECAY_COUNT - k) / (1.0 * DECAY_COUNT)) ;
287 } /* decay_response */
288
289 static void
290 gen_signal_double (double *data, double scale, int datalen)
291 { int k, ramplen ;
292 double amp = 0.0 ;
293
294 ramplen = datalen / 18 ;
295
296 for (k = 0 ; k < datalen ; k++)
297 { if (k <= ramplen)
298 amp = scale * k / ((double) ramplen) ;
299 else if (k > datalen - ramplen)
300 amp = scale * (datalen - k) / ((double) ramplen) ;
301
302 data [k] = amp * (0.4 * sin (33.3 * 2.0 * M_PI * ((double) (k + 1)) / ((double) SAMPLE_RATE))
303 + 0.3 * cos (201.1 * 2.0 * M_PI * ((double) (k + 1)) / ((double) SAMPLE_RATE))) ;
304 } ;
305
306 return ;
307 } /* gen_signal_double */
308
309 static int
310 error_function (double data, double orig, double margin)
311 { double error ;
312
313 if (fabs (orig) <= 500.0)
314 error = fabs (fabs (data) - fabs (orig)) / 2000.0 ;
315 else if (fabs (orig) <= 1000.0)
316 error = fabs (data - orig) / 3000.0 ;
317 else
318 error = fabs (data - orig) / fabs (orig) ;
319
320 if (error > margin)
321 { printf ("\n\n*******************\nError : %f\n", error) ;
322 return 1 ;
323 } ;
324 return 0 ;
325 } /* error_function */
326