Update readme, install and license files.
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / ogg_test.c
1 /*
2 ** Copyright (C) 2007-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 "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <math.h>
27
28 #include <sndfile.h>
29
30 #include "utils.h"
31
32 #define SAMPLE_RATE 44100
33 #define DATA_LENGTH (SAMPLE_RATE / 8)
34
35 typedef union
36 { double d [DATA_LENGTH] ;
37 float f [DATA_LENGTH] ;
38 int i [DATA_LENGTH] ;
39 short s [DATA_LENGTH] ;
40 } BUFFER ;
41
42 static BUFFER data_out ;
43 static BUFFER data_in ;
44
45 static void
46 ogg_short_test (void)
47 { const char * filename = "vorbis_short.oga" ;
48
49 SNDFILE * file ;
50 SF_INFO sfinfo ;
51 short seek_data [10] ;
52 unsigned k ;
53
54 print_test_name ("ogg_short_test", filename) ;
55
56 /* Generate float data. */
57 gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 1.0 * 0x7F00) ;
58
59 /* Convert to shorteger. */
60 for (k = 0 ; k < ARRAY_LEN (data_out.s) ; k++)
61 data_out.s [k] = lrintf (data_out.f [k]) ;
62
63 memset (&sfinfo, 0, sizeof (sfinfo)) ;
64
65 /* Set up output file type. */
66 sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
67 sfinfo.channels = 1 ;
68 sfinfo.samplerate = SAMPLE_RATE ;
69
70 /* Write the output file. */
71 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
72 test_write_short_or_die (file, 0, data_out.s, ARRAY_LEN (data_out.s), __LINE__) ;
73 sf_close (file) ;
74
75 /* Read the file in again. */
76 memset (&sfinfo, 0, sizeof (sfinfo)) ;
77
78 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
79 test_read_short_or_die (file, 0, data_in.s, ARRAY_LEN (data_in.s), __LINE__) ;
80 sf_close (file) ;
81
82 puts ("ok") ;
83
84 /* Test seeking. */
85 print_test_name ("ogg_seek_test", filename) ;
86
87 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
88
89 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
90 test_read_short_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ;
91 compare_short_or_die (seek_data, data_in.s + 10, ARRAY_LEN (seek_data), __LINE__) ;
92
93 sf_close (file) ;
94
95 puts ("ok") ;
96
97 unlink (filename) ;
98 } /* ogg_short_test */
99
100 static void
101 ogg_int_test (void)
102 { const char * filename = "vorbis_int.oga" ;
103
104 SNDFILE * file ;
105 SF_INFO sfinfo ;
106 int seek_data [10] ;
107 unsigned k ;
108
109 print_test_name ("ogg_int_test", filename) ;
110
111 /* Generate float data. */
112 gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 1.0 * 0x7FFF0000) ;
113
114 /* Convert to integer. */
115 for (k = 0 ; k < ARRAY_LEN (data_out.i) ; k++)
116 data_out.i [k] = lrintf (data_out.f [k]) ;
117
118 memset (&sfinfo, 0, sizeof (sfinfo)) ;
119
120 /* Set up output file type. */
121 sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
122 sfinfo.channels = 1 ;
123 sfinfo.samplerate = SAMPLE_RATE ;
124
125 /* Write the output file. */
126 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
127 test_write_int_or_die (file, 0, data_out.i, ARRAY_LEN (data_out.i), __LINE__) ;
128 sf_close (file) ;
129
130 /* Read the file in again. */
131 memset (&sfinfo, 0, sizeof (sfinfo)) ;
132
133 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
134 test_read_int_or_die (file, 0, data_in.i, ARRAY_LEN (data_in.i), __LINE__) ;
135 sf_close (file) ;
136
137 puts ("ok") ;
138
139 /* Test seeking. */
140 print_test_name ("ogg_seek_test", filename) ;
141
142 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
143
144 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
145 test_read_int_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ;
146 compare_int_or_die (seek_data, data_in.i + 10, ARRAY_LEN (seek_data), __LINE__) ;
147
148 sf_close (file) ;
149
150 puts ("ok") ;
151
152 unlink (filename) ;
153 } /* ogg_int_test */
154
155 static void
156 ogg_float_test (void)
157 { const char * filename = "vorbis_float.oga" ;
158
159 SNDFILE * file ;
160 SF_INFO sfinfo ;
161 float seek_data [10] ;
162
163 print_test_name ("ogg_float_test", filename) ;
164
165 gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 0.95) ;
166
167 memset (&sfinfo, 0, sizeof (sfinfo)) ;
168
169 /* Set up output file type. */
170 sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
171 sfinfo.channels = 1 ;
172 sfinfo.samplerate = SAMPLE_RATE ;
173
174 /* Write the output file. */
175 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
176 test_write_float_or_die (file, 0, data_out.f, ARRAY_LEN (data_out.f), __LINE__) ;
177 sf_close (file) ;
178
179 /* Read the file in again. */
180 memset (&sfinfo, 0, sizeof (sfinfo)) ;
181
182 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
183 test_read_float_or_die (file, 0, data_in.f, ARRAY_LEN (data_in.f), __LINE__) ;
184 sf_close (file) ;
185
186 puts ("ok") ;
187
188 /* Test seeking. */
189 print_test_name ("ogg_seek_test", filename) ;
190
191 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
192
193 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
194 test_read_float_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ;
195 compare_float_or_die (seek_data, data_in.f + 10, ARRAY_LEN (seek_data), __LINE__) ;
196
197 sf_close (file) ;
198
199 puts ("ok") ;
200
201 unlink (filename) ;
202 } /* ogg_float_test */
203
204 static void
205 ogg_double_test (void)
206 { const char * filename = "vorbis_double.oga" ;
207
208 SNDFILE * file ;
209 SF_INFO sfinfo ;
210 double seek_data [10] ;
211
212 print_test_name ("ogg_double_test", filename) ;
213
214 gen_windowed_sine_double (data_out.d, ARRAY_LEN (data_out.d), 0.95) ;
215
216 memset (&sfinfo, 0, sizeof (sfinfo)) ;
217
218 /* Set up output file type. */
219 sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
220 sfinfo.channels = 1 ;
221 sfinfo.samplerate = SAMPLE_RATE ;
222
223 /* Write the output file. */
224 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
225 test_write_double_or_die (file, 0, data_out.d, ARRAY_LEN (data_out.d), __LINE__) ;
226 sf_close (file) ;
227
228 /* Read the file in again. */
229 memset (&sfinfo, 0, sizeof (sfinfo)) ;
230
231 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
232 test_read_double_or_die (file, 0, data_in.d, ARRAY_LEN (data_in.d), __LINE__) ;
233 sf_close (file) ;
234
235 puts ("ok") ;
236
237 /* Test seeking. */
238 print_test_name ("ogg_seek_test", filename) ;
239
240 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
241
242 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
243 test_read_double_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ;
244 compare_double_or_die (seek_data, data_in.d + 10, ARRAY_LEN (seek_data), __LINE__) ;
245
246 sf_close (file) ;
247
248 puts ("ok") ;
249
250 unlink (filename) ;
251 } /* ogg_double_test */
252
253
254 static void
255 ogg_stereo_seek_test (const char * filename, int format)
256 { static float data [SAMPLE_RATE] ;
257 static float stereo_out [SAMPLE_RATE * 2] ;
258
259 SNDFILE * file ;
260 SF_INFO sfinfo ;
261 sf_count_t pos ;
262 unsigned k ;
263
264 print_test_name (__func__, filename) ;
265
266 gen_windowed_sine_float (data, ARRAY_LEN (data), 0.95) ;
267 for (k = 0 ; k < ARRAY_LEN (data) ; k++)
268 { stereo_out [2 * k] = data [k] ;
269 stereo_out [2 * k + 1] = data [ARRAY_LEN (data) - k - 1] ;
270 } ;
271
272 memset (&sfinfo, 0, sizeof (sfinfo)) ;
273
274 /* Set up output file type. */
275 sfinfo.format = format ;
276 sfinfo.channels = 2 ;
277 sfinfo.samplerate = SAMPLE_RATE ;
278
279 /* Write the output file. */
280 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
281 test_write_float_or_die (file, 0, stereo_out, ARRAY_LEN (stereo_out), __LINE__) ;
282 sf_close (file) ;
283
284 /* Open file in again for reading. */
285 memset (&sfinfo, 0, sizeof (sfinfo)) ;
286 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
287
288 /* Read in the whole file. */
289 test_read_float_or_die (file, 0, stereo_out, ARRAY_LEN (stereo_out), __LINE__) ;
290
291 /* Now hammer seeking code. */
292 test_seek_or_die (file, 234, SEEK_SET, 234, sfinfo.channels, __LINE__) ;
293 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
294 compare_float_or_die (data, stereo_out + (234 * sfinfo.channels), 10, __LINE__) ;
295
296 test_seek_or_die (file, 442, SEEK_SET, 442, sfinfo.channels, __LINE__) ;
297 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
298 compare_float_or_die (data, stereo_out + (442 * sfinfo.channels), 10, __LINE__) ;
299
300 test_seek_or_die (file, 12, SEEK_CUR, 442 + 10 + 12, sfinfo.channels, __LINE__) ;
301 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
302 compare_float_or_die (data, stereo_out + ((442 + 10 + 12) * sfinfo.channels), 10, __LINE__) ;
303
304 test_seek_or_die (file, 12, SEEK_CUR, 442 + 20 + 24, sfinfo.channels, __LINE__) ;
305 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
306 compare_float_or_die (data, stereo_out + ((442 + 20 + 24) * sfinfo.channels), 10, __LINE__) ;
307
308 pos = 500 - sfinfo.frames ;
309 test_seek_or_die (file, pos, SEEK_END, 500, sfinfo.channels, __LINE__) ;
310 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
311 compare_float_or_die (data, stereo_out + (500 * sfinfo.channels), 10, __LINE__) ;
312
313 pos = 10 - sfinfo.frames ;
314 test_seek_or_die (file, pos, SEEK_END, 10, sfinfo.channels, __LINE__) ;
315 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
316 compare_float_or_die (data, stereo_out + (10 * sfinfo.channels), 10, __LINE__) ;
317
318 sf_close (file) ;
319
320 puts ("ok") ;
321 unlink (filename) ;
322 } /* ogg_stereo_seek_test */
323
324
325 int
326 main (void)
327 {
328 if (HAVE_EXTERNAL_LIBS)
329 { ogg_short_test () ;
330 ogg_int_test () ;
331 ogg_float_test () ;
332 ogg_double_test () ;
333
334 /*-ogg_stereo_seek_test ("pcm.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;-*/
335 ogg_stereo_seek_test ("vorbis_seek.ogg", SF_FORMAT_OGG | SF_FORMAT_VORBIS) ;
336 }
337 else
338 puts (" No Ogg/Vorbis tests because Ogg/Vorbis support was not compiled in.") ;
339
340 return 0 ;
341 } /* main */