cannam@125: /* cannam@125: ** Copyright (C) 2006-2012 Erik de Castro Lopo cannam@125: ** cannam@125: ** This program is free software; you can redistribute it and/or modify cannam@125: ** it under the terms of the GNU General Public License as published by cannam@125: ** the Free Software Foundation; either version 2 of the License, or cannam@125: ** (at your option) any later version. cannam@125: ** cannam@125: ** This program is distributed in the hope that it will be useful, cannam@125: ** but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@125: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@125: ** GNU General Public License for more details. cannam@125: ** cannam@125: ** You should have received a copy of the GNU General Public License cannam@125: ** along with this program; if not, write to the Free Software cannam@125: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cannam@125: */ cannam@125: cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: cannam@125: #include cannam@125: cannam@125: #include "utils.h" cannam@125: cannam@125: static short sbuffer [100] ; cannam@125: static int ibuffer [100] ; cannam@125: static float fbuffer [100] ; cannam@125: static double dbuffer [100] ; cannam@125: cannam@125: static void cannam@125: ceeplusplus_wchar_test (void) cannam@125: { cannam@125: #if 0 cannam@125: LPCWSTR filename = L"wchar_test.wav" ; cannam@125: cannam@125: print_test_name (__func__, "ceeplusplus_wchar_test.wav") ; cannam@125: cannam@125: /* Use this scope to make sure the created file is closed. */ cannam@125: { cannam@125: SndfileHandle file (filename, SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 2, 44100) ; cannam@125: cannam@125: if (file.refCount () != 1) cannam@125: { printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__, __LINE__, file.refCount ()) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: /* This should check that the file did in fact get created with a cannam@125: ** wchar_t * filename. cannam@125: */ cannam@125: exit_if_true ( cannam@125: GetFileAttributesW (filename) == INVALID_FILE_ATTRIBUTES, cannam@125: "\n\nLine %d : GetFileAttributes failed.\n\n", __LINE__ cannam@125: ) ; cannam@125: } cannam@125: cannam@125: /* Use this because the file was created with CreateFileW. */ cannam@125: DeleteFileW (filename) ; cannam@125: cannam@125: puts ("ok") ; cannam@125: #endif cannam@125: } /* ceeplusplus_wchar_test */ cannam@125: cannam@125: cannam@125: cannam@125: static void cannam@125: create_file (const char * filename, int format) cannam@125: { SndfileHandle file ; cannam@125: cannam@125: if (file.refCount () != 0) cannam@125: { printf ("\n\n%s %d : Error : Reference count (%d) should be zero.\n\n", __func__, __LINE__, file.refCount ()) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: file = SndfileHandle (filename, SFM_WRITE, format, 2, 48000) ; cannam@125: cannam@125: if (file.refCount () != 1) cannam@125: { printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__, __LINE__, file.refCount ()) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: file.setString (SF_STR_TITLE, filename) ; cannam@125: cannam@125: /* Item write. */ cannam@125: file.write (sbuffer, ARRAY_LEN (sbuffer)) ; cannam@125: file.write (ibuffer, ARRAY_LEN (ibuffer)) ; cannam@125: file.write (fbuffer, ARRAY_LEN (fbuffer)) ; cannam@125: file.write (dbuffer, ARRAY_LEN (dbuffer)) ; cannam@125: cannam@125: /* Frame write. */ cannam@125: file.writef (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ; cannam@125: file.writef (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ; cannam@125: file.writef (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ; cannam@125: file.writef (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ; cannam@125: cannam@125: /* RAII takes care of the SndfileHandle. */ cannam@125: } /* create_file */ cannam@125: cannam@125: static void cannam@125: check_title (const SndfileHandle & file, const char * filename) cannam@125: { const char *title = NULL ; cannam@125: cannam@125: title = file.getString (SF_STR_TITLE) ; cannam@125: cannam@125: if (title == NULL) cannam@125: { printf ("\n\n%s %d : Error : No title.\n\n", __func__, __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (strcmp (filename, title) != 0) cannam@125: { printf ("\n\n%s %d : Error : title '%s' should be '%s'\n\n", __func__, __LINE__, title, filename) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: return ; cannam@125: } /* check_title */ cannam@125: cannam@125: static void cannam@125: read_file (const char * filename, int format) cannam@125: { SndfileHandle file ; cannam@125: sf_count_t count ; cannam@125: cannam@125: if (file) cannam@125: { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: file = SndfileHandle (filename) ; cannam@125: cannam@125: if (1) cannam@125: { SndfileHandle file2 = file ; cannam@125: cannam@125: if (file.refCount () != 2 || file2.refCount () != 2) cannam@125: { printf ("\n\n%s %d : Error : Reference count (%d) should be two.\n\n", __func__, __LINE__, file.refCount ()) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: } ; cannam@125: cannam@125: if (file.refCount () != 1) cannam@125: { printf ("\n\n%s %d : Error : Reference count (%d) should be one.\n\n", __func__, __LINE__, file.refCount ()) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (! file) cannam@125: { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (file.format () != format) cannam@125: { printf ("\n\n%s %d : Error : format 0x%08x should be 0x%08x.\n\n", __func__, __LINE__, file.format (), format) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (file.channels () != 2) cannam@125: { printf ("\n\n%s %d : Error : channels %d should be 2.\n\n", __func__, __LINE__, file.channels ()) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (file.frames () != ARRAY_LEN (sbuffer) * 4) cannam@125: { printf ("\n\n%s %d : Error : frames %ld should be %lu.\n\n", __func__, __LINE__, cannam@125: (long) file.frames (), (long) ARRAY_LEN (sbuffer) * 4 / 2) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: switch (format & SF_FORMAT_TYPEMASK) cannam@125: { case SF_FORMAT_AU : cannam@125: break ; cannam@125: cannam@125: default : cannam@125: check_title (file, filename) ; cannam@125: break ; cannam@125: } ; cannam@125: cannam@125: /* Item read. */ cannam@125: file.read (sbuffer, ARRAY_LEN (sbuffer)) ; cannam@125: file.read (ibuffer, ARRAY_LEN (ibuffer)) ; cannam@125: file.read (fbuffer, ARRAY_LEN (fbuffer)) ; cannam@125: file.read (dbuffer, ARRAY_LEN (dbuffer)) ; cannam@125: cannam@125: /* Frame read. */ cannam@125: file.readf (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ; cannam@125: file.readf (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ; cannam@125: file.readf (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ; cannam@125: file.readf (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ; cannam@125: cannam@125: count = file.seek (file.frames () - 10, SEEK_SET) ; cannam@125: if (count != file.frames () - 10) cannam@125: { printf ("\n\n%s %d : Error : offset (%ld) should be %ld\n\n", __func__, __LINE__, cannam@125: (long) count, (long) (file.frames () - 10)) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: count = file.read (sbuffer, ARRAY_LEN (sbuffer)) ; cannam@125: if (count != 10 * file.channels ()) cannam@125: { printf ("\n\n%s %d : Error : count (%ld) should be %ld\n\n", __func__, __LINE__, cannam@125: (long) count, (long) (10 * file.channels ())) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: /* RAII takes care of the SndfileHandle. */ cannam@125: } /* read_file */ cannam@125: cannam@125: static void cannam@125: ceeplusplus_test (const char *filename, int format) cannam@125: { cannam@125: print_test_name ("ceeplusplus_test", filename) ; cannam@125: cannam@125: create_file (filename, format) ; cannam@125: read_file (filename, format) ; cannam@125: cannam@125: remove (filename) ; cannam@125: puts ("ok") ; cannam@125: } /* ceeplusplus_test */ cannam@125: cannam@125: static void cannam@125: ceeplusplus_extra_test (void) cannam@125: { SndfileHandle file ; cannam@125: const char * filename = "bad_file_name.wav" ; cannam@125: int error ; cannam@125: cannam@125: print_test_name ("ceeplusplus_extra_test", filename) ; cannam@125: cannam@125: file = SndfileHandle (filename) ; cannam@125: cannam@125: error = file.error () ; cannam@125: if (error == 0) cannam@125: { printf ("\n\n%s %d : error should not be zero.\n\n", __func__, __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (file.strError () == NULL) cannam@125: { printf ("\n\n%s %d : strError should not return NULL.\n\n", __func__, __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (file.seek (0, SEEK_SET) != 0) cannam@125: { printf ("\n\n%s %d : bad seek ().\n\n", __func__, __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: puts ("ok") ; cannam@125: } /* ceeplusplus_extra_test */ cannam@125: cannam@125: cannam@125: static void cannam@125: ceeplusplus_rawhandle_test (const char *filename) cannam@125: { cannam@125: SNDFILE* handle ; cannam@125: { cannam@125: SndfileHandle file (filename) ; cannam@125: handle = file.rawHandle () ; cannam@125: sf_read_float (handle, fbuffer, ARRAY_LEN (fbuffer)) ; cannam@125: } cannam@125: } /* ceeplusplus_rawhandle_test */ cannam@125: cannam@125: static void cannam@125: ceeplusplus_takeOwnership_test (const char *filename) cannam@125: { cannam@125: SNDFILE* handle ; cannam@125: { cannam@125: SndfileHandle file (filename) ; cannam@125: handle = file.takeOwnership () ; cannam@125: } cannam@125: cannam@125: if (sf_read_float (handle, fbuffer, ARRAY_LEN (fbuffer)) <= 0) cannam@125: { printf ("\n\n%s %d : error when taking ownership of handle.\n\n", __func__, __LINE__) ; cannam@125: exit (1) ; cannam@125: } cannam@125: cannam@125: if (sf_close (handle) != 0) cannam@125: { printf ("\n\n%s %d : cannot close file.\n\n", __func__, __LINE__) ; cannam@125: exit (1) ; cannam@125: } cannam@125: cannam@125: SndfileHandle file (filename) ; cannam@125: SndfileHandle file2 (file) ; cannam@125: cannam@125: if (file2.takeOwnership ()) cannam@125: { printf ("\n\n%s %d : taking ownership of shared handle is not allowed.\n\n", __func__, __LINE__) ; cannam@125: exit (1) ; cannam@125: } cannam@125: } /* ceeplusplus_takeOwnership_test */ cannam@125: cannam@125: static void cannam@125: ceeplusplus_handle_test (const char *filename, int format) cannam@125: { cannam@125: print_test_name ("ceeplusplus_handle_test", filename) ; cannam@125: cannam@125: create_file (filename, format) ; cannam@125: cannam@125: if (0) ceeplusplus_rawhandle_test (filename) ; cannam@125: ceeplusplus_takeOwnership_test (filename) ; cannam@125: cannam@125: remove (filename) ; cannam@125: puts ("ok") ; cannam@125: } /* ceeplusplus_test */ cannam@125: cannam@125: int cannam@125: main (void) cannam@125: { cannam@125: ceeplusplus_test ("cpp_test.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ; cannam@125: ceeplusplus_test ("cpp_test.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_S8) ; cannam@125: ceeplusplus_test ("cpp_test.au", SF_FORMAT_AU | SF_FORMAT_FLOAT) ; cannam@125: cannam@125: ceeplusplus_extra_test () ; cannam@125: ceeplusplus_handle_test ("cpp_test.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ; cannam@125: cannam@125: ceeplusplus_wchar_test () ; cannam@125: cannam@125: return 0 ; cannam@125: } /* main */ cannam@125: