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