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