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