Chris@0
|
1 /*
|
Chris@0
|
2 ** Copyright (C) 2006-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
|
Chris@0
|
3 **
|
Chris@0
|
4 ** This program is free software; you can redistribute it and/or modify
|
Chris@0
|
5 ** it under the terms of the GNU General Public License as published by
|
Chris@0
|
6 ** the Free Software Foundation; either version 2 of the License, or
|
Chris@0
|
7 ** (at your option) any later version.
|
Chris@0
|
8 **
|
Chris@0
|
9 ** This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 ** GNU General Public License for more details.
|
Chris@0
|
13 **
|
Chris@0
|
14 ** You should have received a copy of the GNU General Public License
|
Chris@0
|
15 ** along with this program; if not, write to the Free Software
|
Chris@0
|
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
Chris@0
|
17 */
|
Chris@0
|
18
|
Chris@0
|
19 #include <cstdio>
|
Chris@0
|
20 #include <cstdlib>
|
Chris@0
|
21 #include <cstring>
|
Chris@0
|
22
|
Chris@0
|
23 #include <sndfile.hh>
|
Chris@0
|
24
|
Chris@0
|
25 #include "utils.h"
|
Chris@0
|
26
|
Chris@0
|
27 static short sbuffer [100] ;
|
Chris@0
|
28 static int ibuffer [100] ;
|
Chris@0
|
29 static float fbuffer [100] ;
|
Chris@0
|
30 static double dbuffer [100] ;
|
Chris@0
|
31
|
Chris@0
|
32 static void
|
Chris@0
|
33 ceeplusplus_wchar_test (void)
|
Chris@0
|
34 {
|
Chris@0
|
35 #if 0
|
Chris@0
|
36 LPCWSTR filename = L"wchar_test.wav" ;
|
Chris@0
|
37
|
Chris@0
|
38 print_test_name (__func__, "ceeplusplus_wchar_test.wav") ;
|
Chris@0
|
39
|
Chris@0
|
40 /* Use this scope to make sure the created file is closed. */
|
Chris@0
|
41 {
|
Chris@0
|
42 SndfileHandle file (filename, SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 2, 44100) ;
|
Chris@0
|
43
|
Chris@0
|
44 if (file.refCount () != 1)
|
Chris@0
|
45 { printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__, __LINE__, file.refCount ()) ;
|
Chris@0
|
46 exit (1) ;
|
Chris@0
|
47 } ;
|
Chris@0
|
48
|
Chris@0
|
49 /* This should check that the file did in fact get created with a
|
Chris@0
|
50 ** wchar_t * filename.
|
Chris@0
|
51 */
|
Chris@0
|
52 exit_if_true (
|
Chris@0
|
53 GetFileAttributesW (filename) == INVALID_FILE_ATTRIBUTES,
|
Chris@0
|
54 "\n\nLine %d : GetFileAttributes failed.\n\n", __LINE__
|
Chris@0
|
55 ) ;
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /* Use this because the file was created with CreateFileW. */
|
Chris@0
|
59 DeleteFileW (filename) ;
|
Chris@0
|
60
|
Chris@0
|
61 puts ("ok") ;
|
Chris@0
|
62 #endif
|
Chris@0
|
63 } /* ceeplusplus_wchar_test */
|
Chris@0
|
64
|
Chris@0
|
65
|
Chris@0
|
66
|
Chris@0
|
67 static void
|
Chris@0
|
68 create_file (const char * filename, int format)
|
Chris@0
|
69 { SndfileHandle file ;
|
Chris@0
|
70
|
Chris@0
|
71 if (file.refCount () != 0)
|
Chris@0
|
72 { printf ("\n\n%s %d : Error : Reference count (%d) should be zero.\n\n", __func__, __LINE__, file.refCount ()) ;
|
Chris@0
|
73 exit (1) ;
|
Chris@0
|
74 } ;
|
Chris@0
|
75
|
Chris@0
|
76 file = SndfileHandle (filename, SFM_WRITE, format, 2, 48000) ;
|
Chris@0
|
77
|
Chris@0
|
78 if (file.refCount () != 1)
|
Chris@0
|
79 { printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__, __LINE__, file.refCount ()) ;
|
Chris@0
|
80 exit (1) ;
|
Chris@0
|
81 } ;
|
Chris@0
|
82
|
Chris@0
|
83 file.setString (SF_STR_TITLE, filename) ;
|
Chris@0
|
84
|
Chris@0
|
85 /* Item write. */
|
Chris@0
|
86 file.write (sbuffer, ARRAY_LEN (sbuffer)) ;
|
Chris@0
|
87 file.write (ibuffer, ARRAY_LEN (ibuffer)) ;
|
Chris@0
|
88 file.write (fbuffer, ARRAY_LEN (fbuffer)) ;
|
Chris@0
|
89 file.write (dbuffer, ARRAY_LEN (dbuffer)) ;
|
Chris@0
|
90
|
Chris@0
|
91 /* Frame write. */
|
Chris@0
|
92 file.writef (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ;
|
Chris@0
|
93 file.writef (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ;
|
Chris@0
|
94 file.writef (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ;
|
Chris@0
|
95 file.writef (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ;
|
Chris@0
|
96
|
Chris@0
|
97 /* RAII takes care of the SndfileHandle. */
|
Chris@0
|
98 } /* create_file */
|
Chris@0
|
99
|
Chris@0
|
100 static void
|
Chris@0
|
101 check_title (const SndfileHandle & file, const char * filename)
|
Chris@0
|
102 { const char *title = NULL ;
|
Chris@0
|
103
|
Chris@0
|
104 title = file.getString (SF_STR_TITLE) ;
|
Chris@0
|
105
|
Chris@0
|
106 if (title == NULL)
|
Chris@0
|
107 { printf ("\n\n%s %d : Error : No title.\n\n", __func__, __LINE__) ;
|
Chris@0
|
108 exit (1) ;
|
Chris@0
|
109 } ;
|
Chris@0
|
110
|
Chris@0
|
111 if (strcmp (filename, title) != 0)
|
Chris@0
|
112 { printf ("\n\n%s %d : Error : title '%s' should be '%s'\n\n", __func__, __LINE__, title, filename) ;
|
Chris@0
|
113 exit (1) ;
|
Chris@0
|
114 } ;
|
Chris@0
|
115
|
Chris@0
|
116 return ;
|
Chris@0
|
117 } /* check_title */
|
Chris@0
|
118
|
Chris@0
|
119 static void
|
Chris@0
|
120 read_file (const char * filename, int format)
|
Chris@0
|
121 { SndfileHandle file ;
|
Chris@0
|
122 sf_count_t count ;
|
Chris@0
|
123
|
Chris@0
|
124 if (file)
|
Chris@0
|
125 { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ;
|
Chris@0
|
126 exit (1) ;
|
Chris@0
|
127 } ;
|
Chris@0
|
128
|
Chris@0
|
129 file = SndfileHandle (filename) ;
|
Chris@0
|
130
|
Chris@0
|
131 if (1)
|
Chris@0
|
132 { SndfileHandle file2 = file ;
|
Chris@0
|
133
|
Chris@0
|
134 if (file.refCount () != 2 || file2.refCount () != 2)
|
Chris@0
|
135 { printf ("\n\n%s %d : Error : Reference count (%d) should be two.\n\n", __func__, __LINE__, file.refCount ()) ;
|
Chris@0
|
136 exit (1) ;
|
Chris@0
|
137 } ;
|
Chris@0
|
138 } ;
|
Chris@0
|
139
|
Chris@0
|
140 if (file.refCount () != 1)
|
Chris@0
|
141 { printf ("\n\n%s %d : Error : Reference count (%d) should be one.\n\n", __func__, __LINE__, file.refCount ()) ;
|
Chris@0
|
142 exit (1) ;
|
Chris@0
|
143 } ;
|
Chris@0
|
144
|
Chris@0
|
145 if (! file)
|
Chris@0
|
146 { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ;
|
Chris@0
|
147 exit (1) ;
|
Chris@0
|
148 } ;
|
Chris@0
|
149
|
Chris@0
|
150 if (file.format () != format)
|
Chris@0
|
151 { printf ("\n\n%s %d : Error : format 0x%08x should be 0x%08x.\n\n", __func__, __LINE__, file.format (), format) ;
|
Chris@0
|
152 exit (1) ;
|
Chris@0
|
153 } ;
|
Chris@0
|
154
|
Chris@0
|
155 if (file.channels () != 2)
|
Chris@0
|
156 { printf ("\n\n%s %d : Error : channels %d should be 2.\n\n", __func__, __LINE__, file.channels ()) ;
|
Chris@0
|
157 exit (1) ;
|
Chris@0
|
158 } ;
|
Chris@0
|
159
|
Chris@0
|
160 if (file.frames () != ARRAY_LEN (sbuffer) * 4)
|
Chris@0
|
161 { printf ("\n\n%s %d : Error : frames %ld should be %lu.\n\n", __func__, __LINE__,
|
Chris@0
|
162 SF_COUNT_TO_LONG (file.frames ()), (long unsigned int) ARRAY_LEN (sbuffer) * 4 / 2) ;
|
Chris@0
|
163 exit (1) ;
|
Chris@0
|
164 } ;
|
Chris@0
|
165
|
Chris@0
|
166 switch (format & SF_FORMAT_TYPEMASK)
|
Chris@0
|
167 { case SF_FORMAT_AU :
|
Chris@0
|
168 break ;
|
Chris@0
|
169
|
Chris@0
|
170 default :
|
Chris@0
|
171 check_title (file, filename) ;
|
Chris@0
|
172 break ;
|
Chris@0
|
173 } ;
|
Chris@0
|
174
|
Chris@0
|
175 /* Item read. */
|
Chris@0
|
176 file.read (sbuffer, ARRAY_LEN (sbuffer)) ;
|
Chris@0
|
177 file.read (ibuffer, ARRAY_LEN (ibuffer)) ;
|
Chris@0
|
178 file.read (fbuffer, ARRAY_LEN (fbuffer)) ;
|
Chris@0
|
179 file.read (dbuffer, ARRAY_LEN (dbuffer)) ;
|
Chris@0
|
180
|
Chris@0
|
181 /* Frame read. */
|
Chris@0
|
182 file.readf (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ;
|
Chris@0
|
183 file.readf (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ;
|
Chris@0
|
184 file.readf (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ;
|
Chris@0
|
185 file.readf (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ;
|
Chris@0
|
186
|
Chris@0
|
187 count = file.seek (file.frames () - 10, SEEK_SET) ;
|
Chris@0
|
188 if (count != file.frames () - 10)
|
Chris@0
|
189 { printf ("\n\n%s %d : Error : offset (%ld) should be %ld\n\n", __func__, __LINE__,
|
Chris@0
|
190 SF_COUNT_TO_LONG (count), SF_COUNT_TO_LONG (file.frames () - 10)) ;
|
Chris@0
|
191 exit (1) ;
|
Chris@0
|
192 } ;
|
Chris@0
|
193
|
Chris@0
|
194 count = file.read (sbuffer, ARRAY_LEN (sbuffer)) ;
|
Chris@0
|
195 if (count != 10 * file.channels ())
|
Chris@0
|
196 { printf ("\n\n%s %d : Error : count (%ld) should be %ld\n\n", __func__, __LINE__,
|
Chris@0
|
197 SF_COUNT_TO_LONG (count), SF_COUNT_TO_LONG (10 * file.channels ())) ;
|
Chris@0
|
198 exit (1) ;
|
Chris@0
|
199 } ;
|
Chris@0
|
200
|
Chris@0
|
201 /* RAII takes care of the SndfileHandle. */
|
Chris@0
|
202 } /* read_file */
|
Chris@0
|
203
|
Chris@0
|
204 static void
|
Chris@0
|
205 ceeplusplus_test (const char *filename, int format)
|
Chris@0
|
206 {
|
Chris@0
|
207 print_test_name ("ceeplusplus_test", filename) ;
|
Chris@0
|
208
|
Chris@0
|
209 create_file (filename, format) ;
|
Chris@0
|
210 read_file (filename, format) ;
|
Chris@0
|
211
|
Chris@0
|
212 remove (filename) ;
|
Chris@0
|
213 puts ("ok") ;
|
Chris@0
|
214 } /* ceeplusplus_test */
|
Chris@0
|
215
|
Chris@0
|
216 static void
|
Chris@0
|
217 ceeplusplus_extra_test (void)
|
Chris@0
|
218 { SndfileHandle file ;
|
Chris@0
|
219 const char * filename = "bad_file_name.wav" ;
|
Chris@0
|
220 int error ;
|
Chris@0
|
221
|
Chris@0
|
222 print_test_name ("ceeplusplus_extra_test", filename) ;
|
Chris@0
|
223
|
Chris@0
|
224 file = SndfileHandle (filename) ;
|
Chris@0
|
225
|
Chris@0
|
226 error = file.error () ;
|
Chris@0
|
227 if (error == 0)
|
Chris@0
|
228 { printf ("\n\n%s %d : error should be zero.\n\n", __func__, __LINE__) ;
|
Chris@0
|
229 exit (1) ;
|
Chris@0
|
230 } ;
|
Chris@0
|
231
|
Chris@0
|
232 if (file.strError () == NULL)
|
Chris@0
|
233 { printf ("\n\n%s %d : strError should not return NULL.\n\n", __func__, __LINE__) ;
|
Chris@0
|
234 exit (1) ;
|
Chris@0
|
235 } ;
|
Chris@0
|
236
|
Chris@0
|
237 if (file.seek (0, SEEK_SET) != 0)
|
Chris@0
|
238 { printf ("\n\n%s %d : bad seek ().\n\n", __func__, __LINE__) ;
|
Chris@0
|
239 exit (1) ;
|
Chris@0
|
240 } ;
|
Chris@0
|
241
|
Chris@0
|
242 puts ("ok") ;
|
Chris@0
|
243 } /* ceeplusplus_extra_test */
|
Chris@0
|
244
|
Chris@0
|
245
|
Chris@0
|
246 static void
|
Chris@0
|
247 ceeplusplus_rawhandle_test (const char *filename)
|
Chris@0
|
248 {
|
Chris@0
|
249 SNDFILE* handle ;
|
Chris@0
|
250 {
|
Chris@0
|
251 SndfileHandle file (filename) ;
|
Chris@0
|
252 handle = file.rawHandle () ;
|
Chris@0
|
253 sf_read_float (handle, fbuffer, ARRAY_LEN (fbuffer)) ;
|
Chris@0
|
254 }
|
Chris@0
|
255 } /* ceeplusplus_rawhandle_test */
|
Chris@0
|
256
|
Chris@0
|
257 static void
|
Chris@0
|
258 ceeplusplus_takeOwnership_test (const char *filename)
|
Chris@0
|
259 {
|
Chris@0
|
260 SNDFILE* handle ;
|
Chris@0
|
261 {
|
Chris@0
|
262 SndfileHandle file (filename) ;
|
Chris@0
|
263 handle = file.takeOwnership () ;
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 if (sf_read_float (handle, fbuffer, ARRAY_LEN (fbuffer)) <= 0)
|
Chris@0
|
267 { printf ("\n\n%s %d : error when taking ownership of handle.\n\n", __func__, __LINE__) ;
|
Chris@0
|
268 exit (1) ;
|
Chris@0
|
269 }
|
Chris@0
|
270
|
Chris@0
|
271 if (sf_close (handle) != 0)
|
Chris@0
|
272 { printf ("\n\n%s %d : cannot close file.\n\n", __func__, __LINE__) ;
|
Chris@0
|
273 exit (1) ;
|
Chris@0
|
274 }
|
Chris@0
|
275
|
Chris@0
|
276 SndfileHandle file (filename) ;
|
Chris@0
|
277 SndfileHandle file2 (file) ;
|
Chris@0
|
278
|
Chris@0
|
279 if (file2.takeOwnership ())
|
Chris@0
|
280 { printf ("\n\n%s %d : taking ownership of shared handle is not allowed.\n\n", __func__, __LINE__) ;
|
Chris@0
|
281 exit (1) ;
|
Chris@0
|
282 }
|
Chris@0
|
283 } /* ceeplusplus_takeOwnership_test */
|
Chris@0
|
284
|
Chris@0
|
285 static void
|
Chris@0
|
286 ceeplusplus_handle_test (const char *filename, int format)
|
Chris@0
|
287 {
|
Chris@0
|
288 print_test_name ("ceeplusplus_handle_test", filename) ;
|
Chris@0
|
289
|
Chris@0
|
290 create_file (filename, format) ;
|
Chris@0
|
291
|
Chris@0
|
292 if (0) ceeplusplus_rawhandle_test (filename) ;
|
Chris@0
|
293 ceeplusplus_takeOwnership_test (filename) ;
|
Chris@0
|
294
|
Chris@0
|
295 remove (filename) ;
|
Chris@0
|
296 puts ("ok") ;
|
Chris@0
|
297 } /* ceeplusplus_test */
|
Chris@0
|
298
|
Chris@0
|
299 int
|
Chris@0
|
300 main (void)
|
Chris@0
|
301 {
|
Chris@0
|
302 ceeplusplus_test ("cpp_test.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
|
Chris@0
|
303 ceeplusplus_test ("cpp_test.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_S8) ;
|
Chris@0
|
304 ceeplusplus_test ("cpp_test.au", SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
|
Chris@0
|
305
|
Chris@0
|
306 ceeplusplus_extra_test () ;
|
Chris@0
|
307 ceeplusplus_handle_test ("cpp_test.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
|
Chris@0
|
308
|
Chris@0
|
309 ceeplusplus_wchar_test () ;
|
Chris@0
|
310
|
Chris@0
|
311 return 0 ;
|
Chris@0
|
312 } /* main */
|
Chris@0
|
313
|