2 ** Copyright (C) 2006 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 create_file (const char * filename, int format)
34 { SndfileHandle file ;
36 if (file.refCount () != 0)
37 { printf ("\n\n%s %d : Error : Reference count (%d) should be zero.\n\n", __func__, __LINE__, file.refCount ()) ;
41 file = SndfileHandle (filename, SFM_WRITE, format, 2, 48000) ;
43 if (file.refCount () != 1)
44 { printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__, __LINE__, file.refCount ()) ;
48 file.setString (SF_STR_TITLE, filename) ;
51 file.write (sbuffer, ARRAY_LEN (sbuffer)) ;
52 file.write (ibuffer, ARRAY_LEN (ibuffer)) ;
53 file.write (fbuffer, ARRAY_LEN (fbuffer)) ;
54 file.write (dbuffer, ARRAY_LEN (dbuffer)) ;
57 file.writef (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ;
58 file.writef (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ;
59 file.writef (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ;
60 file.writef (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ;
62 /* RAII takes care of the SndfileHandle. */
66 check_title (const SndfileHandle & file, const char * filename)
67 { const char *title = NULL ;
69 title = file.getString (SF_STR_TITLE) ;
72 { printf ("\n\n%s %d : Error : No title.\n\n", __func__, __LINE__) ;
76 if (strcmp (filename, title) != 0)
77 { printf ("\n\n%s %d : Error : title '%s' should be '%s'\n\n", __func__, __LINE__, title, filename) ;
85 read_file (const char * filename, int format)
86 { SndfileHandle file ;
90 { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ;
94 file = SndfileHandle (filename) ;
97 { SndfileHandle file2 = file ;
99 if (file.refCount () != 2 || file2.refCount () != 2)
100 { printf ("\n\n%s %d : Error : Reference count (%d) should be two.\n\n", __func__, __LINE__, file.refCount ()) ;
105 if (file.refCount () != 1)
106 { printf ("\n\n%s %d : Error : Reference count (%d) should be one.\n\n", __func__, __LINE__, file.refCount ()) ;
111 { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ;
115 if (file.format () != format)
116 { printf ("\n\n%s %d : Error : format 0x%08x should be 0x%08x.\n\n", __func__, __LINE__, file.format (), format) ;
120 if (file.channels () != 2)
121 { printf ("\n\n%s %d : Error : channels %d should be 2.\n\n", __func__, __LINE__, file.channels ()) ;
125 if (file.frames () != ARRAY_LEN (sbuffer) * 4)
126 { printf ("\n\n%s %d : Error : frames %ld should be %lu.\n\n", __func__, __LINE__,
127 SF_COUNT_TO_LONG (file.frames ()), (long unsigned int) ARRAY_LEN (sbuffer) * 4 / 2) ;
131 switch (format & SF_FORMAT_TYPEMASK)
132 { case SF_FORMAT_AU :
136 check_title (file, filename) ;
141 file.read (sbuffer, ARRAY_LEN (sbuffer)) ;
142 file.read (ibuffer, ARRAY_LEN (ibuffer)) ;
143 file.read (fbuffer, ARRAY_LEN (fbuffer)) ;
144 file.read (dbuffer, ARRAY_LEN (dbuffer)) ;
147 file.readf (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ;
148 file.readf (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ;
149 file.readf (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ;
150 file.readf (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ;
152 count = file.seek (file.frames () - 10, SEEK_SET) ;
153 if (count != file.frames () - 10)
154 { printf ("\n\n%s %d : Error : offset (%ld) should be %ld\n\n", __func__, __LINE__,
155 SF_COUNT_TO_LONG (count), SF_COUNT_TO_LONG (file.frames () - 10)) ;
159 count = file.read (sbuffer, ARRAY_LEN (sbuffer)) ;
160 if (count != 10 * file.channels ())
161 { printf ("\n\n%s %d : Error : count (%ld) should be %ld\n\n", __func__, __LINE__,
162 SF_COUNT_TO_LONG (count), SF_COUNT_TO_LONG (10 * file.channels ())) ;
166 /* RAII takes care of the SndfileHandle. */
170 ceeplusplus_test (const char *filename, int format)
172 print_test_name ("ceeplusplus_test", filename) ;
174 create_file (filename, format) ;
175 read_file (filename, format) ;
179 } /* ceeplusplus_test */
182 ceeplusplus_extra_test (void)
183 { SndfileHandle file ;
184 const char * filename = "bad_file_name.wav" ;
187 print_test_name ("ceeplusplus_extra_test", filename) ;
189 file = SndfileHandle (filename) ;
191 error = file.error () ;
193 { printf ("\n\n%s %d : error should be zero.\n\n", __func__, __LINE__) ;
197 if (file.strError () == NULL)
198 { printf ("\n\n%s %d : strError should not return NULL.\n\n", __func__, __LINE__) ;
202 if (file.seek (0, SEEK_SET) != 0)
203 { printf ("\n\n%s %d : bad seek ().\n\n", __func__, __LINE__) ;
208 } /* ceeplusplus_extra_test */
213 ceeplusplus_test ("cpp_test.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
214 ceeplusplus_test ("cpp_test.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_S8) ;
215 ceeplusplus_test ("cpp_test.au", SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
217 ceeplusplus_extra_test () ;