comparison src/libsndfile-1.0.27/tests/ogg_test.c @ 40:1df64224f5ac

Current libsndfile source
author Chris Cannam
date Tue, 18 Oct 2016 13:22:47 +0100
parents
children
comparison
equal deleted inserted replaced
39:7ddb4fc30dac 40:1df64224f5ac
1 /*
2 ** Copyright (C) 2007-2016 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 /* Test seek to end of file. */
94 test_seek_or_die (file, 0, SEEK_END, sfinfo.frames, sfinfo.channels, __LINE__) ;
95
96 sf_close (file) ;
97
98 puts ("ok") ;
99
100 unlink (filename) ;
101 } /* ogg_short_test */
102
103 static void
104 ogg_int_test (void)
105 { const char * filename = "vorbis_int.oga" ;
106
107 SNDFILE * file ;
108 SF_INFO sfinfo ;
109 int seek_data [10] ;
110 unsigned k ;
111
112 print_test_name ("ogg_int_test", filename) ;
113
114 /* Generate float data. */
115 gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 1.0 * 0x7FFF0000) ;
116
117 /* Convert to integer. */
118 for (k = 0 ; k < ARRAY_LEN (data_out.i) ; k++)
119 data_out.i [k] = lrintf (data_out.f [k]) ;
120
121 memset (&sfinfo, 0, sizeof (sfinfo)) ;
122
123 /* Set up output file type. */
124 sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
125 sfinfo.channels = 1 ;
126 sfinfo.samplerate = SAMPLE_RATE ;
127
128 /* Write the output file. */
129 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
130 test_write_int_or_die (file, 0, data_out.i, ARRAY_LEN (data_out.i), __LINE__) ;
131 sf_close (file) ;
132
133 /* Read the file in again. */
134 memset (&sfinfo, 0, sizeof (sfinfo)) ;
135
136 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
137 test_read_int_or_die (file, 0, data_in.i, ARRAY_LEN (data_in.i), __LINE__) ;
138 sf_close (file) ;
139
140 puts ("ok") ;
141
142 /* Test seeking. */
143 print_test_name ("ogg_seek_test", filename) ;
144
145 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
146
147 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
148 test_read_int_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ;
149 compare_int_or_die (seek_data, data_in.i + 10, ARRAY_LEN (seek_data), __LINE__) ;
150
151 sf_close (file) ;
152
153 puts ("ok") ;
154
155 unlink (filename) ;
156 } /* ogg_int_test */
157
158 static void
159 ogg_float_test (void)
160 { const char * filename = "vorbis_float.oga" ;
161
162 SNDFILE * file ;
163 SF_INFO sfinfo ;
164 float seek_data [10] ;
165
166 print_test_name ("ogg_float_test", filename) ;
167
168 gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 0.95) ;
169
170 memset (&sfinfo, 0, sizeof (sfinfo)) ;
171
172 /* Set up output file type. */
173 sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
174 sfinfo.channels = 1 ;
175 sfinfo.samplerate = SAMPLE_RATE ;
176
177 /* Write the output file. */
178 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
179 test_write_float_or_die (file, 0, data_out.f, ARRAY_LEN (data_out.f), __LINE__) ;
180 sf_close (file) ;
181
182 /* Read the file in again. */
183 memset (&sfinfo, 0, sizeof (sfinfo)) ;
184
185 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
186 test_read_float_or_die (file, 0, data_in.f, ARRAY_LEN (data_in.f), __LINE__) ;
187 sf_close (file) ;
188
189 puts ("ok") ;
190
191 /* Test seeking. */
192 print_test_name ("ogg_seek_test", filename) ;
193
194 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
195
196 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
197 test_read_float_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ;
198 compare_float_or_die (seek_data, data_in.f + 10, ARRAY_LEN (seek_data), __LINE__) ;
199
200 sf_close (file) ;
201
202 puts ("ok") ;
203
204 unlink (filename) ;
205 } /* ogg_float_test */
206
207 static void
208 ogg_double_test (void)
209 { const char * filename = "vorbis_double.oga" ;
210
211 SNDFILE * file ;
212 SF_INFO sfinfo ;
213 double seek_data [10] ;
214
215 print_test_name ("ogg_double_test", filename) ;
216
217 gen_windowed_sine_double (data_out.d, ARRAY_LEN (data_out.d), 0.95) ;
218
219 memset (&sfinfo, 0, sizeof (sfinfo)) ;
220
221 /* Set up output file type. */
222 sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
223 sfinfo.channels = 1 ;
224 sfinfo.samplerate = SAMPLE_RATE ;
225
226 /* Write the output file. */
227 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
228 test_write_double_or_die (file, 0, data_out.d, ARRAY_LEN (data_out.d), __LINE__) ;
229 sf_close (file) ;
230
231 /* Read the file in again. */
232 memset (&sfinfo, 0, sizeof (sfinfo)) ;
233
234 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
235 test_read_double_or_die (file, 0, data_in.d, ARRAY_LEN (data_in.d), __LINE__) ;
236 sf_close (file) ;
237
238 puts ("ok") ;
239
240 /* Test seeking. */
241 print_test_name ("ogg_seek_test", filename) ;
242
243 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
244
245 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
246 test_read_double_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ;
247 compare_double_or_die (seek_data, data_in.d + 10, ARRAY_LEN (seek_data), __LINE__) ;
248
249 sf_close (file) ;
250
251 puts ("ok") ;
252
253 unlink (filename) ;
254 } /* ogg_double_test */
255
256
257 static void
258 ogg_stereo_seek_test (const char * filename, int format)
259 { static float data [SAMPLE_RATE] ;
260 static float stereo_out [SAMPLE_RATE * 2] ;
261
262 SNDFILE * file ;
263 SF_INFO sfinfo ;
264 sf_count_t pos ;
265 unsigned k ;
266
267 print_test_name (__func__, filename) ;
268
269 gen_windowed_sine_float (data, ARRAY_LEN (data), 0.95) ;
270 for (k = 0 ; k < ARRAY_LEN (data) ; k++)
271 { stereo_out [2 * k] = data [k] ;
272 stereo_out [2 * k + 1] = data [ARRAY_LEN (data) - k - 1] ;
273 } ;
274
275 memset (&sfinfo, 0, sizeof (sfinfo)) ;
276
277 /* Set up output file type. */
278 sfinfo.format = format ;
279 sfinfo.channels = 2 ;
280 sfinfo.samplerate = SAMPLE_RATE ;
281
282 /* Write the output file. */
283 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
284 test_write_float_or_die (file, 0, stereo_out, ARRAY_LEN (stereo_out), __LINE__) ;
285 sf_close (file) ;
286
287 /* Open file in again for reading. */
288 memset (&sfinfo, 0, sizeof (sfinfo)) ;
289 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
290
291 /* Read in the whole file. */
292 test_read_float_or_die (file, 0, stereo_out, ARRAY_LEN (stereo_out), __LINE__) ;
293
294 /* Now hammer seeking code. */
295 test_seek_or_die (file, 234, SEEK_SET, 234, sfinfo.channels, __LINE__) ;
296 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
297 compare_float_or_die (data, stereo_out + (234 * sfinfo.channels), 10, __LINE__) ;
298
299 test_seek_or_die (file, 442, SEEK_SET, 442, sfinfo.channels, __LINE__) ;
300 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
301 compare_float_or_die (data, stereo_out + (442 * sfinfo.channels), 10, __LINE__) ;
302
303 test_seek_or_die (file, 12, SEEK_CUR, 442 + 10 + 12, sfinfo.channels, __LINE__) ;
304 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
305 compare_float_or_die (data, stereo_out + ((442 + 10 + 12) * sfinfo.channels), 10, __LINE__) ;
306
307 test_seek_or_die (file, 12, SEEK_CUR, 442 + 20 + 24, sfinfo.channels, __LINE__) ;
308 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
309 compare_float_or_die (data, stereo_out + ((442 + 20 + 24) * sfinfo.channels), 10, __LINE__) ;
310
311 pos = 500 - sfinfo.frames ;
312 test_seek_or_die (file, pos, SEEK_END, 500, sfinfo.channels, __LINE__) ;
313 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
314 compare_float_or_die (data, stereo_out + (500 * sfinfo.channels), 10, __LINE__) ;
315
316 pos = 10 - sfinfo.frames ;
317 test_seek_or_die (file, pos, SEEK_END, 10, sfinfo.channels, __LINE__) ;
318 test_readf_float_or_die (file, 0, data, 10, __LINE__) ;
319 compare_float_or_die (data, stereo_out + (10 * sfinfo.channels), 10, __LINE__) ;
320
321 sf_close (file) ;
322
323 puts ("ok") ;
324 unlink (filename) ;
325 } /* ogg_stereo_seek_test */
326
327
328 int
329 main (void)
330 {
331 if (HAVE_EXTERNAL_XIPH_LIBS)
332 { ogg_short_test () ;
333 ogg_int_test () ;
334 ogg_float_test () ;
335 ogg_double_test () ;
336
337 /*-ogg_stereo_seek_test ("pcm.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;-*/
338 ogg_stereo_seek_test ("vorbis_seek.ogg", SF_FORMAT_OGG | SF_FORMAT_VORBIS) ;
339 }
340 else
341 puts (" No Ogg/Vorbis tests because Ogg/Vorbis support was not compiled in.") ;
342
343 return 0 ;
344 } /* main */