752f00edc912d6dc15cb1860b793e6820bf55e0e
2 ** Copyright (C) 2006-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
27 static short sbuffer
[100] ;
28 static int ibuffer
[100] ;
29 static float fbuffer
[100] ;
30 static double dbuffer
[100] ;
33 ceeplusplus_wchar_test (void)
36 LPCWSTR filename
= L
"wchar_test.wav" ;
38 print_test_name (__func__
, "ceeplusplus_wchar_test.wav") ;
40 /* Use this scope to make sure the created file is closed. */
42 SndfileHandle
file (filename
, SFM_WRITE
, SF_FORMAT_WAV
| SF_FORMAT_PCM_16
, 2, 44100) ;
44 if (file
.refCount () != 1)
45 { printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__
, __LINE__
, file
.refCount ()) ;
49 /* This should check that the file did in fact get created with a
50 ** wchar_t * filename.
53 GetFileAttributesW (filename
) == INVALID_FILE_ATTRIBUTES
,
54 "\n\nLine %d : GetFileAttributes failed.\n\n", __LINE__
58 /* Use this because the file was created with CreateFileW. */
59 DeleteFileW (filename
) ;
63 } /* ceeplusplus_wchar_test */
68 create_file (const char * filename
, int format
)
69 { SndfileHandle file
;
71 if (file
.refCount () != 0)
72 { printf ("\n\n%s %d : Error : Reference count (%d) should be zero.\n\n", __func__
, __LINE__
, file
.refCount ()) ;
76 file
= SndfileHandle (filename
, SFM_WRITE
, format
, 2, 48000) ;
78 if (file
.refCount () != 1)
79 { printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__
, __LINE__
, file
.refCount ()) ;
83 file
.setString (SF_STR_TITLE
, filename
) ;
86 file
.write (sbuffer
, ARRAY_LEN (sbuffer
)) ;
87 file
.write (ibuffer
, ARRAY_LEN (ibuffer
)) ;
88 file
.write (fbuffer
, ARRAY_LEN (fbuffer
)) ;
89 file
.write (dbuffer
, ARRAY_LEN (dbuffer
)) ;
92 file
.writef (sbuffer
, ARRAY_LEN (sbuffer
) / file
.channels ()) ;
93 file
.writef (ibuffer
, ARRAY_LEN (ibuffer
) / file
.channels ()) ;
94 file
.writef (fbuffer
, ARRAY_LEN (fbuffer
) / file
.channels ()) ;
95 file
.writef (dbuffer
, ARRAY_LEN (dbuffer
) / file
.channels ()) ;
97 /* RAII takes care of the SndfileHandle. */
101 check_title (const SndfileHandle
& file
, const char * filename
)
102 { const char *title
= NULL
;
104 title
= file
.getString (SF_STR_TITLE
) ;
107 { printf ("\n\n%s %d : Error : No title.\n\n", __func__
, __LINE__
) ;
111 if (strcmp (filename
, title
) != 0)
112 { printf ("\n\n%s %d : Error : title '%s' should be '%s'\n\n", __func__
, __LINE__
, title
, filename
) ;
120 read_file (const char * filename
, int format
)
121 { SndfileHandle file
;
125 { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__
, __LINE__
) ;
129 file
= SndfileHandle (filename
) ;
132 { SndfileHandle file2
= file
;
134 if (file
.refCount () != 2 || file2
.refCount () != 2)
135 { printf ("\n\n%s %d : Error : Reference count (%d) should be two.\n\n", __func__
, __LINE__
, file
.refCount ()) ;
140 if (file
.refCount () != 1)
141 { printf ("\n\n%s %d : Error : Reference count (%d) should be one.\n\n", __func__
, __LINE__
, file
.refCount ()) ;
146 { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__
, __LINE__
) ;
150 if (file
.format () != format
)
151 { printf ("\n\n%s %d : Error : format 0x%08x should be 0x%08x.\n\n", __func__
, __LINE__
, file
.format (), format
) ;
155 if (file
.channels () != 2)
156 { printf ("\n\n%s %d : Error : channels %d should be 2.\n\n", __func__
, __LINE__
, file
.channels ()) ;
160 if (file
.frames () != ARRAY_LEN (sbuffer
) * 4)
161 { printf ("\n\n%s %d : Error : frames %ld should be %lu.\n\n", __func__
, __LINE__
,
162 SF_COUNT_TO_LONG (file
.frames ()), (long unsigned int) ARRAY_LEN (sbuffer
) * 4 / 2) ;
166 switch (format
& SF_FORMAT_TYPEMASK
)
167 { case SF_FORMAT_AU
:
171 check_title (file
, filename
) ;
176 file
.read (sbuffer
, ARRAY_LEN (sbuffer
)) ;
177 file
.read (ibuffer
, ARRAY_LEN (ibuffer
)) ;
178 file
.read (fbuffer
, ARRAY_LEN (fbuffer
)) ;
179 file
.read (dbuffer
, ARRAY_LEN (dbuffer
)) ;
182 file
.readf (sbuffer
, ARRAY_LEN (sbuffer
) / file
.channels ()) ;
183 file
.readf (ibuffer
, ARRAY_LEN (ibuffer
) / file
.channels ()) ;
184 file
.readf (fbuffer
, ARRAY_LEN (fbuffer
) / file
.channels ()) ;
185 file
.readf (dbuffer
, ARRAY_LEN (dbuffer
) / file
.channels ()) ;
187 count
= file
.seek (file
.frames () - 10, SEEK_SET
) ;
188 if (count
!= file
.frames () - 10)
189 { printf ("\n\n%s %d : Error : offset (%ld) should be %ld\n\n", __func__
, __LINE__
,
190 SF_COUNT_TO_LONG (count
), SF_COUNT_TO_LONG (file
.frames () - 10)) ;
194 count
= file
.read (sbuffer
, ARRAY_LEN (sbuffer
)) ;
195 if (count
!= 10 * file
.channels ())
196 { printf ("\n\n%s %d : Error : count (%ld) should be %ld\n\n", __func__
, __LINE__
,
197 SF_COUNT_TO_LONG (count
), SF_COUNT_TO_LONG (10 * file
.channels ())) ;
201 /* RAII takes care of the SndfileHandle. */
205 ceeplusplus_test (const char *filename
, int format
)
207 print_test_name ("ceeplusplus_test", filename
) ;
209 create_file (filename
, format
) ;
210 read_file (filename
, format
) ;
214 } /* ceeplusplus_test */
217 ceeplusplus_extra_test (void)
218 { SndfileHandle file
;
219 const char * filename
= "bad_file_name.wav" ;
222 print_test_name ("ceeplusplus_extra_test", filename
) ;
224 file
= SndfileHandle (filename
) ;
226 error
= file
.error () ;
228 { printf ("\n\n%s %d : error should be zero.\n\n", __func__
, __LINE__
) ;
232 if (file
.strError () == NULL
)
233 { printf ("\n\n%s %d : strError should not return NULL.\n\n", __func__
, __LINE__
) ;
237 if (file
.seek (0, SEEK_SET
) != 0)
238 { printf ("\n\n%s %d : bad seek ().\n\n", __func__
, __LINE__
) ;
243 } /* ceeplusplus_extra_test */
247 ceeplusplus_rawhandle_test (const char *filename
)
251 SndfileHandle
file (filename
) ;
252 handle
= file
.rawHandle () ;
253 sf_read_float (handle
, fbuffer
, ARRAY_LEN (fbuffer
)) ;
255 } /* ceeplusplus_rawhandle_test */
258 ceeplusplus_takeOwnership_test (const char *filename
)
262 SndfileHandle
file (filename
) ;
263 handle
= file
.takeOwnership () ;
266 if (sf_read_float (handle
, fbuffer
, ARRAY_LEN (fbuffer
)) <= 0)
267 { printf ("\n\n%s %d : error when taking ownership of handle.\n\n", __func__
, __LINE__
) ;
271 if (sf_close (handle
) != 0)
272 { printf ("\n\n%s %d : cannot close file.\n\n", __func__
, __LINE__
) ;
276 SndfileHandle
file (filename
) ;
277 SndfileHandle
file2 (file
) ;
279 if (file2
.takeOwnership ())
280 { printf ("\n\n%s %d : taking ownership of shared handle is not allowed.\n\n", __func__
, __LINE__
) ;
283 } /* ceeplusplus_takeOwnership_test */
286 ceeplusplus_handle_test (const char *filename
, int format
)
288 print_test_name ("ceeplusplus_handle_test", filename
) ;
290 create_file (filename
, format
) ;
292 if (0) ceeplusplus_rawhandle_test (filename
) ;
293 ceeplusplus_takeOwnership_test (filename
) ;
297 } /* ceeplusplus_test */
302 ceeplusplus_test ("cpp_test.wav", SF_FORMAT_WAV
| SF_FORMAT_PCM_16
) ;
303 ceeplusplus_test ("cpp_test.aiff", SF_FORMAT_AIFF
| SF_FORMAT_PCM_S8
) ;
304 ceeplusplus_test ("cpp_test.au", SF_FORMAT_AU
| SF_FORMAT_FLOAT
) ;
306 ceeplusplus_extra_test () ;
307 ceeplusplus_handle_test ("cpp_test.wav", SF_FORMAT_WAV
| SF_FORMAT_PCM_16
) ;
309 ceeplusplus_wchar_test () ;