cannam@125: /* cannam@125: ** Copyright (C) 2001-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 << 15) cannam@125: #define LOG_BUFFER_SIZE 1024 cannam@125: cannam@125: cannam@125: static void test_float_peak (const char *filename, int filetype) ; cannam@125: static void read_write_peak_test (const char *filename, int filetype) ; cannam@125: cannam@125: static void check_logged_peaks (char *buffer) ; cannam@125: cannam@125: /* Force the start of this buffer to be double aligned. Sparc-solaris will cannam@125: ** choke if its not. cannam@125: */ cannam@125: static double data [BUFFER_LEN] ; cannam@125: static char log_buffer [LOG_BUFFER_SIZE] ; 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 (" aiff - test AIFF file PEAK chunk\n") ; cannam@125: printf (" caf - test CAF file PEAK chunk\n") ; cannam@125: printf (" wav - test WAV file peak chunk\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: { test_float_peak ("peak_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ; cannam@125: test_float_peak ("peak_float.wavex", SF_FORMAT_WAVEX | SF_FORMAT_FLOAT) ; cannam@125: test_float_peak ("peak_float.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT) ; cannam@125: cannam@125: read_write_peak_test ("rw_peak.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ; cannam@125: read_write_peak_test ("rw_peak.wavex", SF_FORMAT_WAVEX | SF_FORMAT_FLOAT) ; cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "aiff")) cannam@125: { test_float_peak ("peak_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ; cannam@125: cannam@125: read_write_peak_test ("rw_peak.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ; cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "caf")) cannam@125: { test_float_peak ("peak_float.caf", SF_FORMAT_CAF | SF_FORMAT_FLOAT) ; cannam@125: cannam@125: read_write_peak_test ("rw_peak.caf", SF_FORMAT_CAF | SF_FORMAT_FLOAT) ; cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "rf64")) cannam@125: { test_float_peak ("peak_float.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ; cannam@125: cannam@125: read_write_peak_test ("rw_peak.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ; 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: ** Here are the test functions. cannam@125: */ cannam@125: cannam@125: static void cannam@125: test_float_peak (const char *filename, int filetype) cannam@125: { SNDFILE *file ; cannam@125: SF_INFO sfinfo ; cannam@125: int k, frames, count ; cannam@125: cannam@125: print_test_name ("test_float_peak", filename) ; cannam@125: cannam@125: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@125: sfinfo.samplerate = 44100 ; cannam@125: sfinfo.format = filetype ; cannam@125: sfinfo.channels = 4 ; cannam@125: sfinfo.frames = 0 ; cannam@125: cannam@125: frames = BUFFER_LEN / sfinfo.channels ; cannam@125: cannam@125: /* Create some random data with a peak value of 0.66. */ cannam@125: for (k = 0 ; k < BUFFER_LEN ; k++) cannam@125: data [k] = (rand () % 2000) / 3000.0 ; cannam@125: cannam@125: /* Insert some larger peaks a know locations. */ cannam@125: data [4 * (frames / 8) + 0] = (frames / 8) * 0.01 ; /* First channel */ cannam@125: data [4 * (frames / 6) + 1] = (frames / 6) * 0.01 ; /* Second channel */ cannam@125: data [4 * (frames / 4) + 2] = (frames / 4) * 0.01 ; /* Third channel */ cannam@125: data [4 * (frames / 2) + 3] = (frames / 2) * 0.01 ; /* Fourth channel */ cannam@125: cannam@125: /* Write a file with PEAK chunks. */ cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ; cannam@125: cannam@125: /* Try to confuse the header writer by adding a removing the PEAK chunk. */ cannam@125: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ; cannam@125: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; cannam@125: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ; cannam@125: cannam@125: /* Write the data in four passed. The data is designed so that peaks will cannam@125: ** be written in the different calls to sf_write_double (). cannam@125: */ cannam@125: for (count = 0 ; count < 4 ; count ++) cannam@125: test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ; cannam@125: cannam@125: if (sfinfo.format != filetype) cannam@125: { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (sfinfo.frames != frames) cannam@125: { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__, frames, (long) sfinfo.frames) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (sfinfo.channels != 4) cannam@125: { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: /* Check these two commands. */ cannam@125: if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double)) == SF_FALSE) cannam@125: { printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (fabs (data [0] - (frames / 2) * 0.01) > 0.01) cannam@125: { printf ("\n\nLine %d: Bad peak value (%f should be %f) for command SFC_GET_SIGNAL_MAX.\n", __LINE__, data [0], (frames / 2) * 0.01) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels) == SF_FALSE) cannam@125: { printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (fabs (data [3] - (frames / 2) * 0.01) > 0.01) cannam@125: { printf ("\n\nLine %d: Bad peak value (%f should be %f) for command SFC_GET_MAX_ALL_CHANNELS.\n", __LINE__, data [0], (frames / 2) * 0.01) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: /* Get the log buffer data. */ cannam@125: log_buffer [0] = 0 ; cannam@125: sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ; cannam@125: cannam@125: if (strlen (log_buffer) == 0) cannam@125: { printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: check_logged_peaks (log_buffer) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: /* Write a file ***without*** PEAK chunks. */ cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ; cannam@125: cannam@125: /* Try to confuse the header writer by adding a removing the PEAK chunk. */ cannam@125: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; cannam@125: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ; cannam@125: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; cannam@125: cannam@125: /* Write the data in four passed. The data is designed so that peaks will cannam@125: ** be written in the different calls to sf_write_double (). cannam@125: */ cannam@125: for (count = 0 ; count < 4 ; count ++) cannam@125: test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ; cannam@125: cannam@125: /* Check these two commands. */ cannam@125: if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double))) cannam@125: { printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels)) cannam@125: { printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: /* Get the log buffer data. */ cannam@125: log_buffer [0] = 0 ; cannam@125: sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ; cannam@125: cannam@125: if (strlen (log_buffer) == 0) cannam@125: { printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (strstr (log_buffer, "PEAK :") != NULL) cannam@125: { printf ("\n\nLine %d: Should not have a PEAK chunk in this file.\n\n", __LINE__) ; cannam@125: puts (log_buffer) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: unlink (filename) ; cannam@125: printf ("ok\n") ; cannam@125: } /* test_float_peak */ cannam@125: cannam@125: static void cannam@125: check_logged_peaks (char *buffer) cannam@125: { char *cptr ; cannam@125: int k, chan, channel_count, position ; cannam@125: float value ; cannam@125: cannam@125: if (strstr (buffer, "should") || strstr (buffer, "*")) cannam@125: { printf ("\n\nLine %d: Something wrong in buffer. Dumping.\n", __LINE__) ; cannam@125: puts (buffer) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: channel_count = 0 ; cannam@125: cptr = strstr (buffer, "Channels") ; cannam@125: if (cptr && sscanf (cptr, "Channels : %d", &k) == 1) cannam@125: channel_count = k ; cannam@125: else if (cptr && sscanf (cptr, "Channels / frame : %d", &k) == 1) cannam@125: channel_count = k ; cannam@125: else cannam@125: { printf ("\n\nLine %d: Couldn't find channel count.\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (channel_count != 4) cannam@125: { printf ("\n\nLine %d: Wrong channel count (4 ->%d).\n", __LINE__, channel_count) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (! (cptr = strstr (buffer, "Ch Position Value"))) cannam@125: { printf ("\n\nLine %d: Can't find PEAK data.\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: for (k = 0 ; k < channel_count ; k++) cannam@125: { if (! (cptr = strchr (cptr, '\n'))) cannam@125: { printf ("\n\nLine %d: Got lost.\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: if (sscanf (cptr, "%d %d %f", &chan, &position, &value) != 3) cannam@125: { printf ("\n\nLine %d: sscanf failed.\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: if (position == 0) cannam@125: { printf ("\n\nLine %d: peak position for channel %d should not be at offset 0.\n", __LINE__, chan) ; cannam@125: printf ("%s", buffer) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: if (chan != k || fabs ((position) * 0.01 - value) > 1e-6) cannam@125: { printf ("\n\nLine %d: Error : peak value incorrect!\n", __LINE__) ; cannam@125: printf ("%s", buffer) ; cannam@125: printf ("\n\nLine %d: %d %f %f\n", __LINE__, chan, position * 0.01, value) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cptr ++ ; /* Move past current newline. */ cannam@125: } ; cannam@125: cannam@125: } /* check_logged_peaks */ cannam@125: cannam@125: static void cannam@125: read_write_peak_test (const char *filename, int filetype) cannam@125: { SNDFILE *file ; cannam@125: SF_INFO sfinfo ; cannam@125: cannam@125: double small_data [10], max_peak = 0.0 ; cannam@125: unsigned k ; cannam@125: cannam@125: print_test_name (__func__, filename) ; cannam@125: cannam@125: for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++) cannam@125: small_data [k] = 0.1 ; cannam@125: cannam@125: sfinfo.samplerate = 44100 ; cannam@125: sfinfo.channels = 2 ; cannam@125: sfinfo.format = filetype ; cannam@125: sfinfo.frames = 0 ; cannam@125: cannam@125: /* Open the file, add peak chunk and write samples with value 0.1. */ cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; cannam@125: cannam@125: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ; cannam@125: cannam@125: test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: /* Open the fiel RDWR, write sample valied 1.25. */ cannam@125: file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ; cannam@125: cannam@125: for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++) cannam@125: small_data [k] = 1.0 ; cannam@125: cannam@125: test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ; cannam@125: cannam@125: sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (max_peak)) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: exit_if_true (max_peak < 0.1, "\n\nLine %d : max peak (%5.3f) should not be 0.1.\n\n", __LINE__, max_peak) ; cannam@125: cannam@125: /* Open the file and test the values written to the PEAK chunk. */ cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; cannam@125: cannam@125: exit_if_true (sfinfo.channels * sfinfo.frames != 2 * ARRAY_LEN (small_data), cannam@125: "Line %d : frame count is %" PRId64 ", should be %zd\n", __LINE__, sfinfo.frames, 2 * ARRAY_LEN (small_data)) ; cannam@125: cannam@125: sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (double)) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: exit_if_true (max_peak < 1.0, "\n\nLine %d : max peak (%5.3f) should be 1.0.\n\n", __LINE__, max_peak) ; cannam@125: cannam@125: unlink (filename) ; cannam@125: puts ("ok") ; cannam@125: } /* read_write_peak_test */ cannam@125: