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