cannam@125: /* cannam@125: ** Copyright (C) 2003-2016 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 "sfconfig.h" cannam@125: cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: cannam@125: #if HAVE_UNISTD_H cannam@125: #include cannam@125: #endif cannam@125: cannam@125: #include cannam@125: cannam@125: #include "utils.h" cannam@125: cannam@125: #define BUFFER_LEN (1 << 10) cannam@125: #define LOG_BUFFER_SIZE 1024 cannam@125: cannam@125: static void string_start_test (const char *filename, int typemajor) ; cannam@125: static void string_start_end_test (const char *filename, int typemajor) ; cannam@125: static void string_multi_set_test (const char *filename, int typemajor) ; cannam@125: static void string_rdwr_test (const char *filename, int typemajor) ; cannam@125: static void string_short_rdwr_test (const char *filename, int typemajor) ; cannam@125: static void string_rdwr_grow_test (const char *filename, int typemajor) ; cannam@125: static void string_header_update (const char *filename, int typemajor) ; cannam@125: cannam@125: static void software_string_test (const char *filename) ; cannam@125: cannam@125: static int str_count (const char * haystack, const char * needle) ; cannam@125: cannam@125: int cannam@125: main (int argc, char *argv []) cannam@125: { int do_all = 0 ; cannam@125: int test_count = 0 ; cannam@125: cannam@125: if (argc != 2) cannam@125: { printf ("Usage : %s \n", argv [0]) ; cannam@125: printf (" Where is one of the following:\n") ; cannam@125: printf (" wav - test adding strings to WAV files\n") ; cannam@125: printf (" aiff - test adding strings to AIFF files\n") ; cannam@125: printf (" flac - test adding strings to FLAC files\n") ; cannam@125: printf (" ogg - test adding strings to OGG files\n") ; cannam@125: printf (" all - perform all tests\n") ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: do_all = ! strcmp (argv [1], "all") ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "wav")) cannam@125: { string_start_end_test ("strings.wav", SF_FORMAT_WAV) ; cannam@125: string_multi_set_test ("multi.wav", SF_FORMAT_WAV) ; cannam@125: string_rdwr_test ("rdwr.wav", SF_FORMAT_WAV) ; cannam@125: string_short_rdwr_test ("short_rdwr.wav", SF_FORMAT_WAV) ; cannam@125: string_rdwr_grow_test ("rdwr_grow.wav", SF_FORMAT_WAV) ; cannam@125: string_header_update ("header_update.wav", SF_FORMAT_WAV) ; cannam@125: cannam@125: string_start_end_test ("strings.wavex", SF_FORMAT_WAVEX) ; cannam@125: string_multi_set_test ("multi.wavex", SF_FORMAT_WAVEX) ; cannam@125: string_rdwr_test ("rdwr.wavex", SF_FORMAT_WAVEX) ; cannam@125: string_short_rdwr_test ("short_rdwr.wavex", SF_FORMAT_WAVEX) ; cannam@125: cannam@125: string_start_end_test ("strings.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV) ; cannam@125: string_multi_set_test ("multi.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV) ; cannam@125: string_rdwr_test ("rdwr.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV) ; cannam@125: string_short_rdwr_test ("short_rdwr.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV) ; cannam@125: cannam@125: software_string_test ("software_string.wav") ; cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "aiff")) cannam@125: { string_start_test ("strings.aiff", SF_FORMAT_AIFF) ; cannam@125: string_start_end_test ("strings.aiff", SF_FORMAT_AIFF) ; cannam@125: /* cannam@125: TODO : Fix src/aiff.c so these tests pass. cannam@125: string_multi_set_test ("multi.aiff", SF_FORMAT_AIFF) ; cannam@125: string_rdwr_test ("rdwr.aiff", SF_FORMAT_AIFF) ; cannam@125: string_short_rdwr_test ("short_rdwr.aiff", SF_FORMAT_AIFF) ; cannam@125: string_rdwr_grow_test ("rdwr_grow.aiff", SF_FORMAT_AIFF) ; cannam@125: string_header_update ("header_update.aiff", SF_FORMAT_AIFF) ; cannam@125: */ cannam@125: cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "flac")) cannam@125: { if (HAVE_EXTERNAL_XIPH_LIBS) cannam@125: string_start_test ("strings.flac", SF_FORMAT_FLAC) ; cannam@125: else cannam@125: puts (" No FLAC tests because FLAC support was not compiled in.") ; cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "ogg")) cannam@125: { if (HAVE_EXTERNAL_XIPH_LIBS) cannam@125: string_start_test ("vorbis.oga", SF_FORMAT_OGG) ; cannam@125: else cannam@125: puts (" No Ogg/Vorbis tests because Ogg/Vorbis support was not compiled in.") ; cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "caf")) cannam@125: { string_start_test ("strings.caf", SF_FORMAT_CAF) ; cannam@125: string_start_end_test ("strings.caf", SF_FORMAT_CAF) ; cannam@125: string_multi_set_test ("multi.caf", SF_FORMAT_CAF) ; cannam@125: /* cannam@125: TODO : Fix src/caf.c so these tests pass. cannam@125: string_rdwr_test ("rdwr.caf", SF_FORMAT_CAF) ; cannam@125: string_short_rdwr_test ("short_rdwr.caf", SF_FORMAT_CAF) ; cannam@125: string_header_update ("header_update.caf", SF_FORMAT_CAF) ; cannam@125: */ cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "rf64")) cannam@125: { string_start_test ("strings.rf64", SF_FORMAT_RF64) ; cannam@125: string_start_end_test ("strings.rf64", SF_FORMAT_RF64) ; cannam@125: string_multi_set_test ("multi.rf64", SF_FORMAT_RF64) ; cannam@125: /* cannam@125: TODO : Fix src/rf64.c so these tests pass. cannam@125: string_rdwr_test ("rdwr.rf64", SF_FORMAT_RF64) ; cannam@125: string_short_rdwr_test ("short_rdwr.rf64", SF_FORMAT_RF64) ; cannam@125: string_header_update ("header_update.rf64", SF_FORMAT_RF64) ; cannam@125: */ cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "w64")) cannam@125: { puts ("\n\n **** String test not working yet for W64 format. ****\n") ; cannam@125: /* cannam@125: string_start_test ("strings.w64", SF_FORMAT_W64) ; cannam@125: string_start_end_test ("strings.w64", SF_FORMAT_W64) ; cannam@125: string_multi_set_test ("multi.w64", SF_FORMAT_W64) ; cannam@125: string_rdwr_test ("rdwr.w64", SF_FORMAT_W64) ; cannam@125: string_short_rdwr_test ("short_rdwr.w64", SF_FORMAT_W64) ; cannam@125: string_header_update ("header_update.w64", SF_FORMAT_W64) ; cannam@125: */ cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (test_count == 0) cannam@125: { printf ("Mono : ************************************\n") ; cannam@125: printf ("Mono : * No '%s' test defined.\n", argv [1]) ; cannam@125: printf ("Mono : ************************************\n") ; cannam@125: return 1 ; cannam@125: } ; cannam@125: cannam@125: return 0 ; cannam@125: } /* main */ cannam@125: cannam@125: cannam@125: /*============================================================================================ cannam@125: ** Here are the test functions. cannam@125: */ cannam@125: cannam@125: static const char cannam@125: software [] = "software (libsndfile-X.Y.Z)", cannam@125: artist [] = "The Artist", cannam@125: copyright [] = "Copyright (c) 2001 Artist", cannam@125: comment [] = "Comment goes here!!!", cannam@125: date [] = "2001/01/27", cannam@125: album [] = "The Album", cannam@125: license [] = "The license", cannam@125: title [] = "This is the title", cannam@125: long_title [] = "This is a very long and very boring title for this file", cannam@125: long_artist [] = "The artist who kept on changing its name", cannam@125: genre [] = "The genre", cannam@125: trackno [] = "Track three" ; cannam@125: cannam@125: cannam@125: static short data_out [BUFFER_LEN] ; cannam@125: cannam@125: static void cannam@125: string_start_end_test (const char *filename, int typemajor) cannam@125: { const char *cptr ; cannam@125: SNDFILE *file ; cannam@125: SF_INFO sfinfo ; cannam@125: int errors = 0 ; cannam@125: cannam@125: print_test_name ("string_start_end_test", filename) ; cannam@125: cannam@125: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@125: sfinfo.samplerate = 44100 ; cannam@125: sfinfo.channels = 1 ; cannam@125: sfinfo.frames = 0 ; cannam@125: sfinfo.format = typemajor | SF_FORMAT_PCM_16 ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: cannam@125: /* Write stuff at start of file. */ cannam@125: sf_set_string (file, SF_STR_TITLE, filename) ; cannam@125: sf_set_string (file, SF_STR_SOFTWARE, software) ; cannam@125: sf_set_string (file, SF_STR_ARTIST, artist) ; cannam@125: sf_set_string (file, SF_STR_GENRE, genre) ; cannam@125: sf_set_string (file, SF_STR_TRACKNUMBER, trackno) ; cannam@125: cannam@125: /* Write data to file. */ cannam@125: test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ; cannam@125: test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; cannam@125: cannam@125: /* Write more stuff at end of file. */ cannam@125: sf_set_string (file, SF_STR_COPYRIGHT, copyright) ; cannam@125: sf_set_string (file, SF_STR_COMMENT, comment) ; cannam@125: sf_set_string (file, SF_STR_DATE, date) ; cannam@125: sf_set_string (file, SF_STR_ALBUM, album) ; cannam@125: sf_set_string (file, SF_STR_LICENSE, license) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: cannam@125: check_log_buffer_or_die (file, __LINE__) ; cannam@125: cannam@125: if (sfinfo.frames != BUFFER_LEN) cannam@125: { printf ("***** Bad frame count %d (should be %d)\n\n", (int) sfinfo.frames, BUFFER_LEN) ; cannam@125: errors ++ ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_TITLE) ; cannam@125: if (cptr == NULL || strcmp (filename, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad filename : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_COPYRIGHT) ; cannam@125: if (cptr == NULL || strcmp (copyright, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad copyright : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_SOFTWARE) ; cannam@125: if (cptr == NULL || strstr (cptr, software) != cptr) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad software : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: if (str_count (cptr, "libsndfile") != 1) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad software : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_ARTIST) ; cannam@125: if (cptr == NULL || strcmp (artist, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad artist : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_COMMENT) ; cannam@125: if (cptr == NULL || strcmp (comment, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad comment : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: if (typemajor != SF_FORMAT_AIFF) cannam@125: { cptr = sf_get_string (file, SF_STR_DATE) ; cannam@125: if (cptr == NULL || strcmp (date, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad date : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_GENRE) ; cannam@125: if (cptr == NULL || strcmp (genre, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad genre : %s\n", cptr) ; cannam@125: } ; cannam@125: } ; cannam@125: cannam@125: switch (typemajor) cannam@125: { case SF_FORMAT_AIFF : cannam@125: case SF_FORMAT_WAV : cannam@125: case SF_FORMAT_WAVEX : cannam@125: case SF_ENDIAN_BIG | SF_FORMAT_WAV : cannam@125: case SF_FORMAT_RF64 : cannam@125: /* These formats do not support the following. */ cannam@125: break ; cannam@125: cannam@125: default : cannam@125: cptr = sf_get_string (file, SF_STR_ALBUM) ; cannam@125: if (cptr == NULL || strcmp (album, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad album : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_LICENSE) ; cannam@125: if (cptr == NULL || strcmp (license, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad license : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_TRACKNUMBER) ; cannam@125: if (cptr == NULL || strcmp (trackno, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad track no. : %s\n", cptr) ; cannam@125: } ; cannam@125: break ; cannam@125: } ; cannam@125: cannam@125: if (errors > 0) cannam@125: { printf ("\n*** Error count : %d ***\n\n", errors) ; cannam@125: dump_log_buffer (file) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: sf_close (file) ; cannam@125: unlink (filename) ; cannam@125: cannam@125: puts ("ok") ; cannam@125: } /* string_start_end_test */ cannam@125: cannam@125: static void cannam@125: string_start_test (const char *filename, int typemajor) cannam@125: { const char *cptr ; cannam@125: SNDFILE *file ; cannam@125: SF_INFO sfinfo ; cannam@125: int errors = 0 ; cannam@125: cannam@125: print_test_name ("string_start_test", filename) ; cannam@125: cannam@125: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@125: sfinfo.samplerate = 44100 ; cannam@125: sfinfo.channels = 1 ; cannam@125: sfinfo.frames = 0 ; cannam@125: cannam@125: switch (typemajor) cannam@125: { case SF_FORMAT_OGG : cannam@125: sfinfo.format = typemajor | SF_FORMAT_VORBIS ; cannam@125: break ; cannam@125: cannam@125: default : cannam@125: sfinfo.format = typemajor | SF_FORMAT_PCM_16 ; cannam@125: break ; cannam@125: } ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: cannam@125: /* Write stuff at start of file. */ cannam@125: sf_set_string (file, SF_STR_TITLE, filename) ; cannam@125: sf_set_string (file, SF_STR_SOFTWARE, software) ; cannam@125: sf_set_string (file, SF_STR_ARTIST, artist) ; cannam@125: sf_set_string (file, SF_STR_COPYRIGHT, copyright) ; cannam@125: sf_set_string (file, SF_STR_COMMENT, comment) ; cannam@125: sf_set_string (file, SF_STR_DATE, date) ; cannam@125: sf_set_string (file, SF_STR_ALBUM, album) ; cannam@125: sf_set_string (file, SF_STR_LICENSE, license) ; cannam@125: cannam@125: /* Write data to file. */ cannam@125: test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: cannam@125: check_log_buffer_or_die (file, __LINE__) ; cannam@125: cannam@125: if (sfinfo.frames != BUFFER_LEN) cannam@125: { printf ("***** Bad frame count %d (should be %d)\n\n", (int) sfinfo.frames, BUFFER_LEN) ; cannam@125: errors ++ ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_TITLE) ; cannam@125: if (cptr == NULL || strcmp (filename, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad filename : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_COPYRIGHT) ; cannam@125: if (cptr == NULL || strcmp (copyright, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad copyright : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_SOFTWARE) ; cannam@125: if (cptr == NULL || strstr (cptr, software) != cptr) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad software : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: if (cptr && str_count (cptr, "libsndfile") != 1) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad software : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_ARTIST) ; cannam@125: if (cptr == NULL || strcmp (artist, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad artist : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: cptr = sf_get_string (file, SF_STR_COMMENT) ; cannam@125: if (cptr == NULL || strcmp (comment, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad comment : %s\n", cptr) ; cannam@125: } ; cannam@125: cannam@125: if (typemajor != SF_FORMAT_AIFF) cannam@125: { cptr = sf_get_string (file, SF_STR_DATE) ; cannam@125: if (cptr == NULL || strcmp (date, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad date : %s\n", cptr) ; cannam@125: } ; cannam@125: } ; cannam@125: cannam@125: if (typemajor != SF_FORMAT_WAV && typemajor != SF_FORMAT_AIFF) cannam@125: { cptr = sf_get_string (file, SF_STR_ALBUM) ; cannam@125: if (cptr == NULL || strcmp (album, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad album : %s\n", cptr) ; cannam@125: } ; cannam@125: } ; cannam@125: cannam@125: if (typemajor != SF_FORMAT_WAV && typemajor != SF_FORMAT_AIFF && typemajor != SF_FORMAT_RF64) cannam@125: { cptr = sf_get_string (file, SF_STR_LICENSE) ; cannam@125: if (cptr == NULL || strcmp (license, cptr) != 0) cannam@125: { if (errors++ == 0) cannam@125: puts ("\n") ; cannam@125: printf (" Bad license : %s\n", cptr) ; cannam@125: } ; cannam@125: } ; cannam@125: cannam@125: if (errors > 0) cannam@125: { printf ("\n*** Error count : %d ***\n\n", errors) ; cannam@125: dump_log_buffer (file) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: sf_close (file) ; cannam@125: unlink (filename) ; cannam@125: cannam@125: puts ("ok") ; cannam@125: } /* string_start_test */ cannam@125: cannam@125: static void cannam@125: string_multi_set_test (const char *filename, int typemajor) cannam@125: { static const char cannam@125: new_software [] = "new software (libsndfile-X.Y.Z)", cannam@125: new_copyright [] = "Copyright (c) 2001 New Artist", cannam@125: new_artist [] = "The New Artist", cannam@125: new_title [] = "This is the new title" ; cannam@125: cannam@125: static char buffer [2048] ; cannam@125: SNDFILE *file ; cannam@125: SF_INFO sfinfo ; cannam@125: int count ; cannam@125: cannam@125: print_test_name (__func__, filename) ; cannam@125: cannam@125: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@125: sfinfo.format = typemajor | SF_FORMAT_PCM_16 ; cannam@125: sfinfo.samplerate = 44100 ; cannam@125: sfinfo.channels = 1 ; cannam@125: sfinfo.frames = 0 ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: cannam@125: /* Write stuff at start of file. */ cannam@125: sf_set_string (file, SF_STR_TITLE, title) ; cannam@125: sf_set_string (file, SF_STR_SOFTWARE, software) ; cannam@125: sf_set_string (file, SF_STR_ARTIST, artist) ; cannam@125: cannam@125: /* Write data to file. */ cannam@125: test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ; cannam@125: cannam@125: /* Write it all again. */ cannam@125: cannam@125: sf_set_string (file, SF_STR_TITLE, new_title) ; cannam@125: sf_set_string (file, SF_STR_SOFTWARE, new_software) ; cannam@125: sf_set_string (file, SF_STR_ARTIST, new_artist) ; cannam@125: cannam@125: sf_set_string (file, SF_STR_COPYRIGHT, copyright) ; cannam@125: sf_set_string (file, SF_STR_COMMENT, comment) ; cannam@125: sf_set_string (file, SF_STR_DATE, date) ; cannam@125: sf_set_string (file, SF_STR_ALBUM, album) ; cannam@125: sf_set_string (file, SF_STR_LICENSE, license) ; cannam@125: sf_set_string (file, SF_STR_COPYRIGHT, new_copyright) ; cannam@125: sf_set_string (file, SF_STR_COMMENT, comment) ; cannam@125: sf_set_string (file, SF_STR_DATE, date) ; cannam@125: sf_set_string (file, SF_STR_ALBUM, album) ; cannam@125: sf_set_string (file, SF_STR_LICENSE, license) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: sf_command (file, SFC_GET_LOG_INFO, buffer, sizeof (buffer)) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: count = str_count (buffer, new_title) ; cannam@125: exit_if_true (count < 1, "\n\nLine %d : Could not find new_title in :\n%s\n", __LINE__, buffer) ; cannam@125: exit_if_true (count > 1, "\n\nLine %d : new_title appears %d times in :\n\n%s\n", __LINE__, count, buffer) ; cannam@125: cannam@125: count = str_count (buffer, software) ; cannam@125: exit_if_true (count < 1, "\n\nLine %d : Could not find new_software in :\n%s\n", __LINE__, buffer) ; cannam@125: exit_if_true (count > 1, "\n\nLine %d : new_software appears %d times in :\n\n%s\n", __LINE__, count, buffer) ; cannam@125: cannam@125: count = str_count (buffer, new_artist) ; cannam@125: exit_if_true (count < 1, "\n\nLine %d : Could not find new_artist in :\n%s\n", __LINE__, buffer) ; cannam@125: exit_if_true (count > 1, "\n\nLine %d : new_artist appears %d times in :\n\n%s\n", __LINE__, count, buffer) ; cannam@125: cannam@125: count = str_count (buffer, new_copyright) ; cannam@125: exit_if_true (count < 1, "\n\nLine %d : Could not find new_copyright in :\n%s\n", __LINE__, buffer) ; cannam@125: exit_if_true (count > 1, "\n\nLine %d : new_copyright appears %d times in :\n\n%s\n", __LINE__, count, buffer) ; cannam@125: cannam@125: unlink (filename) ; cannam@125: cannam@125: puts ("ok") ; cannam@125: } /* string_multi_set_test */ cannam@125: cannam@125: static void cannam@125: string_rdwr_test (const char *filename, int typemajor) cannam@125: { SNDFILE *file ; cannam@125: SF_INFO sfinfo ; cannam@125: sf_count_t frames ; cannam@125: const char * str ; cannam@125: cannam@125: print_test_name (__func__, filename) ; cannam@125: create_short_sndfile (filename, typemajor | SF_FORMAT_PCM_16, 2) ; cannam@125: cannam@125: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@125: file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ; cannam@125: frames = sfinfo.frames ; cannam@125: sf_set_string (file, SF_STR_TITLE, title) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; cannam@125: exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ; cannam@125: str = sf_get_string (file, SF_STR_TITLE) ; cannam@125: exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ; cannam@125: exit_if_true (strcmp (str, title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ; cannam@125: frames = sfinfo.frames ; cannam@125: sf_set_string (file, SF_STR_TITLE, title) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ; cannam@125: str = sf_get_string (file, SF_STR_TITLE) ; cannam@125: exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ; cannam@125: sf_set_string (file, SF_STR_ARTIST, artist) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; cannam@125: cannam@125: str = sf_get_string (file, SF_STR_ARTIST) ; cannam@125: exit_if_true (str == NULL, "\n\nLine %d : SF_STR_ARTIST string is NULL.\n", __LINE__) ; cannam@125: exit_if_true (strcmp (str, artist) != 0, "\n\nLine %d : SF_STR_ARTIST doesn't match what was written.\n", __LINE__) ; cannam@125: cannam@125: str = sf_get_string (file, SF_STR_TITLE) ; cannam@125: exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ; cannam@125: exit_if_true (strcmp (str, title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ; cannam@125: cannam@125: exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: unlink (filename) ; cannam@125: cannam@125: puts ("ok") ; cannam@125: } /* string_rdwr_test */ cannam@125: cannam@125: static void cannam@125: string_short_rdwr_test (const char *filename, int typemajor) cannam@125: { SNDFILE *file ; cannam@125: SF_INFO sfinfo ; cannam@125: sf_count_t frames = BUFFER_LEN ; cannam@125: const char * str ; cannam@125: cannam@125: print_test_name (__func__, filename) ; cannam@125: cannam@125: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@125: sfinfo.format = typemajor | SF_FORMAT_PCM_16 ; cannam@125: sfinfo.samplerate = 44100 ; cannam@125: sfinfo.channels = 1 ; cannam@125: sfinfo.frames = 0 ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; cannam@125: cannam@125: /* Write data to file. */ cannam@125: test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ; cannam@125: cannam@125: sf_set_string (file, SF_STR_TITLE, long_title) ; cannam@125: sf_set_string (file, SF_STR_ARTIST, long_artist) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: /* Open the file RDWR. */ cannam@125: file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ; cannam@125: exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ; cannam@125: str = sf_get_string (file, SF_STR_TITLE) ; cannam@125: exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ; cannam@125: exit_if_true (strcmp (str, long_title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ; cannam@125: str = sf_get_string (file, SF_STR_ARTIST) ; cannam@125: exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ; cannam@125: exit_if_true (strcmp (str, long_artist) != 0, "\n\nLine %d : SF_STR_ARTIST doesn't match what was written.\n", __LINE__) ; cannam@125: cannam@125: /* Change title and artist. */ cannam@125: sf_set_string (file, SF_STR_TITLE, title) ; cannam@125: sf_set_string (file, SF_STR_ARTIST, artist) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; cannam@125: cannam@125: check_log_buffer_or_die (file, __LINE__) ; cannam@125: cannam@125: str = sf_get_string (file, SF_STR_TITLE) ; cannam@125: exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ; cannam@125: exit_if_true (strcmp (str, title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ; cannam@125: cannam@125: str = sf_get_string (file, SF_STR_ARTIST) ; cannam@125: exit_if_true (str == NULL, "\n\nLine %d : SF_STR_ARTIST string is NULL.\n", __LINE__) ; cannam@125: exit_if_true (strcmp (str, artist) != 0, "\n\nLine %d : SF_STR_ARTIST doesn't match what was written.\n", __LINE__) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: unlink (filename) ; cannam@125: cannam@125: puts ("ok") ; cannam@125: } /* string_short_rdwr_test */ cannam@125: cannam@125: static int cannam@125: str_count (const char * haystack, const char * needle) cannam@125: { int count = 0 ; cannam@125: cannam@125: while ((haystack = strstr (haystack, needle)) != NULL) cannam@125: { count ++ ; cannam@125: haystack ++ ; cannam@125: } ; cannam@125: cannam@125: return count ; cannam@125: } /* str_count */ cannam@125: cannam@125: #define MIN(a, b) ((a) < (b) ? (a) : (b)) cannam@125: cannam@125: static void cannam@125: software_string_test (const char *filename) cannam@125: { size_t k ; cannam@125: cannam@125: print_test_name (__func__, filename) ; cannam@125: cannam@125: for (k = 0 ; k < 50 ; k++) cannam@125: { const char *result ; cannam@125: char sfname [64] = "" ; cannam@125: SNDFILE *file ; cannam@125: SF_INFO info ; cannam@125: cannam@125: sf_info_setup (&info, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 44100, 1) ; cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &info, SF_TRUE, __LINE__) ; cannam@125: cannam@125: snprintf (sfname, MIN (k, sizeof (sfname)), "%s", "abcdefghijklmnopqrestvwxyz0123456789abcdefghijklmnopqrestvwxyz") ; cannam@125: cannam@125: exit_if_true (sf_set_string (file, SF_STR_SOFTWARE, sfname), cannam@125: "\n\nLine %d : sf_set_string (f, SF_STR_SOFTWARE, '%s') failed : %s\n", __LINE__, sfname, sf_strerror (file)) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_READ, &info, SF_TRUE, __LINE__) ; cannam@125: result = sf_get_string (file, SF_STR_SOFTWARE) ; cannam@125: cannam@125: exit_if_true (result == NULL, "\n\nLine %d : sf_get_string (file, SF_STR_SOFTWARE) returned NULL.\n\n", __LINE__) ; cannam@125: cannam@125: exit_if_true (strstr (result, sfname) != result, cannam@125: "\n\nLine %d : Can't fine string '%s' in '%s'\n\n", __LINE__, sfname, result) ; cannam@125: sf_close (file) ; cannam@125: } ; cannam@125: cannam@125: unlink (filename) ; cannam@125: puts ("ok") ; cannam@125: } /* software_string_test */ cannam@125: cannam@125: cannam@125: static void cannam@125: string_rdwr_grow_test (const char *filename, int typemajor) cannam@125: { SNDFILE *file ; cannam@125: SF_INFO sfinfo ; cannam@125: sf_count_t frames ; cannam@125: const char * str ; cannam@125: cannam@125: print_test_name (__func__, filename) ; cannam@125: cannam@125: /* Create a file that contains some strings. Then open the file in RDWR mode and cannam@125: grow the file by writing more audio data to it. Check that the audio data has cannam@125: been added to the file, and that the strings are still there. */ cannam@125: cannam@125: /* Create a short file that contains a string. */ cannam@125: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@125: sfinfo.samplerate = 44100 ; cannam@125: sfinfo.channels = 2 ; cannam@125: sfinfo.frames = 0 ; cannam@125: sfinfo.format = typemajor | SF_FORMAT_PCM_16 ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: /* Write data to file. */ cannam@125: test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ; cannam@125: cannam@125: /* Write some strings at end of file. */ cannam@125: sf_set_string (file, SF_STR_TITLE , title) ; cannam@125: sf_set_string (file, SF_STR_COMMENT, comment) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: cannam@125: /* Now open file again in SFM_RDWR mode and write more audio data to it. */ cannam@125: file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: /* Write more data to file. */ cannam@125: test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: cannam@125: /* Now open file again. It should now contain two BUFFER_LEN's worth of frames and the strings. */ cannam@125: frames = 2 * BUFFER_LEN / sfinfo.channels ; cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ; cannam@125: cannam@125: /* Check the strings */ cannam@125: str = sf_get_string (file, SF_STR_TITLE) ; cannam@125: exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ; cannam@125: exit_if_true (strcmp (str, title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ; cannam@125: cannam@125: str = sf_get_string (file, SF_STR_COMMENT) ; cannam@125: exit_if_true (str == NULL, "\n\nLine %d : SF_STR_COMMENT string is NULL.\n", __LINE__) ; cannam@125: exit_if_true (strcmp (str, comment) != 0, "\n\nLine %d : SF_STR_COMMENT doesn't match what was written.\n", __LINE__) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: unlink (filename) ; cannam@125: cannam@125: puts ("ok") ; cannam@125: } /* string_rdwr_grow_test */ cannam@125: cannam@125: static void cannam@125: string_header_update (const char *filename, int typemajor) cannam@125: { SNDFILE *file , *file1 ; cannam@125: SF_INFO sfinfo , sfinfo1 ; cannam@125: sf_count_t frames ; cannam@125: const char * str ; cannam@125: const int GROW_BUFFER_AMOUNT = 4 ; /* this should be less than half the size of the string header */ cannam@125: cannam@125: print_test_name (__func__, filename) ; cannam@125: cannam@125: /* Create a short file. */ cannam@125: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@125: sfinfo.samplerate = 44100 ; cannam@125: sfinfo.channels = 2 ; cannam@125: sfinfo.frames = 0 ; cannam@125: sfinfo.format = typemajor | SF_FORMAT_PCM_16 ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ; cannam@125: sf_set_string (file, SF_STR_TITLE, long_title) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: cannam@125: /* Check that SFC_UPDATE_HEADER_NOW correctly calculates datalength. */ cannam@125: file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: /* Write a very small amount of new audio data that doesn't completely overwrite the existing header. */ cannam@125: test_write_short_or_die (file, 0, data_out, GROW_BUFFER_AMOUNT, __LINE__) ; cannam@125: cannam@125: /* Update the header without closing the file. */ cannam@125: sf_command (file, SFC_UPDATE_HEADER_NOW, NULL, 0) ; cannam@125: cannam@125: /* The file should now contain BUFFER_LEN + GROW_BUFFER_AMOUNT frames. cannam@125: Open a second handle to the file and check the reported length. */ cannam@125: memset (&sfinfo1, 0, sizeof (sfinfo1)) ; cannam@125: file1 = test_open_file_or_die (filename, SFM_READ, &sfinfo1, SF_TRUE, __LINE__) ; cannam@125: cannam@125: frames = (BUFFER_LEN + GROW_BUFFER_AMOUNT) / sfinfo.channels ; cannam@125: exit_if_true (frames != sfinfo1.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo1.frames, frames) ; cannam@125: cannam@125: /* The strings are probably not readable by the second soundfile handle because write_tailer has not yet been called. cannam@125: It's a design decision whether SFC_UPDATE_HEADER_NOW should write the tailer. I think it's fine that it doesn't. */ cannam@125: cannam@125: sf_close (file1) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: cannam@125: /* Check that sf_close correctly calculates datalength. */ cannam@125: file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: /* Write a very small amount of new audio data that doesn't completely overwrite the existing header. */ cannam@125: test_write_short_or_die (file, 0, data_out, GROW_BUFFER_AMOUNT, __LINE__) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: cannam@125: /* Open file again and verify data and string. */ cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: frames = (BUFFER_LEN + 2*GROW_BUFFER_AMOUNT) / sfinfo.channels ; cannam@125: exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ; cannam@125: str = sf_get_string (file, SF_STR_TITLE) ; cannam@125: exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ; cannam@125: exit_if_true (strcmp (str, long_title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ; cannam@125: sf_close (file) ; cannam@125: unlink (filename) ; cannam@125: puts ("ok") ; cannam@125: } /* string_header_update */