Chris@0: /* Chris@0: ** Copyright (C) 2001-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** This program is free software; you can redistribute it and/or modify Chris@0: ** it under the terms of the GNU General Public License as published by Chris@0: ** the Free Software Foundation; either version 2 of the License, or Chris@0: ** (at your option) any later version. Chris@0: ** Chris@0: ** This program is distributed in the hope that it will be useful, Chris@0: ** but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: ** GNU General Public License for more details. Chris@0: ** Chris@0: ** You should have received a copy of the GNU General Public License Chris@0: ** along with this program; if not, write to the Free Software Chris@0: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Chris@0: */ Chris@0: Chris@0: #include "sfconfig.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #if HAVE_UNISTD_H Chris@0: #include Chris@0: #endif Chris@0: Chris@0: #include Chris@0: Chris@0: #include "utils.h" Chris@0: Chris@0: #define BUFFER_LEN (1<<15) Chris@0: #define LOG_BUFFER_SIZE 1024 Chris@0: Chris@0: Chris@0: static void test_float_peak (const char *filename, int filetype) ; Chris@0: static void read_write_peak_test (const char *filename, int filetype) ; Chris@0: Chris@0: static void check_logged_peaks (char *buffer) ; Chris@0: Chris@0: /* Force the start of this buffer to be double aligned. Sparc-solaris will Chris@0: ** choke if its not. Chris@0: */ Chris@0: static double data [BUFFER_LEN] ; Chris@0: static char log_buffer [LOG_BUFFER_SIZE] ; Chris@0: Chris@0: int Chris@0: main (int argc, char *argv []) Chris@0: { int do_all = 0 ; Chris@0: int test_count = 0 ; Chris@0: Chris@0: if (argc != 2) Chris@0: { printf ("Usage : %s \n", argv [0]) ; Chris@0: printf (" Where is one of the following:\n") ; Chris@0: printf (" aiff - test AIFF file PEAK chunk\n") ; Chris@0: printf (" caf - test CAF file PEAK chunk\n") ; Chris@0: printf (" wav - test WAV file peak chunk\n") ; Chris@0: printf (" all - perform all tests\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: do_all = ! strcmp (argv [1], "all") ; Chris@0: Chris@0: if (do_all || ! strcmp (argv [1], "wav")) Chris@0: { test_float_peak ("peak_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ; Chris@0: test_float_peak ("peak_float.wavex", SF_FORMAT_WAVEX | SF_FORMAT_FLOAT) ; Chris@0: test_float_peak ("peak_float.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT) ; Chris@0: Chris@0: read_write_peak_test ("rw_peak.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ; Chris@0: read_write_peak_test ("rw_peak.wavex", SF_FORMAT_WAVEX | SF_FORMAT_FLOAT) ; Chris@0: test_count++ ; Chris@0: } ; Chris@0: Chris@0: if (do_all || ! strcmp (argv [1], "aiff")) Chris@0: { test_float_peak ("peak_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ; Chris@0: Chris@0: read_write_peak_test ("rw_peak.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ; Chris@0: test_count++ ; Chris@0: } ; Chris@0: Chris@0: if (do_all || ! strcmp (argv [1], "caf")) Chris@0: { test_float_peak ("peak_float.caf", SF_FORMAT_CAF | SF_FORMAT_FLOAT) ; Chris@0: Chris@0: read_write_peak_test ("rw_peak.caf", SF_FORMAT_CAF | SF_FORMAT_FLOAT) ; Chris@0: test_count++ ; Chris@0: } ; Chris@0: Chris@0: if (test_count == 0) Chris@0: { printf ("Mono : ************************************\n") ; Chris@0: printf ("Mono : * No '%s' test defined.\n", argv [1]) ; Chris@0: printf ("Mono : ************************************\n") ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*============================================================================================ Chris@0: ** Here are the test functions. Chris@0: */ Chris@0: Chris@0: static void Chris@0: test_float_peak (const char *filename, int filetype) Chris@0: { SNDFILE *file ; Chris@0: SF_INFO sfinfo ; Chris@0: int k, frames, count ; Chris@0: Chris@0: print_test_name ("test_float_peak", filename) ; Chris@0: Chris@0: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@0: sfinfo.samplerate = 44100 ; Chris@0: sfinfo.format = filetype ; Chris@0: sfinfo.channels = 4 ; Chris@0: sfinfo.frames = 0 ; Chris@0: Chris@0: frames = BUFFER_LEN / sfinfo.channels ; Chris@0: Chris@0: /* Create some random data with a peak value of 0.66. */ Chris@0: for (k = 0 ; k < BUFFER_LEN ; k++) Chris@0: data [k] = (rand () % 2000) / 3000.0 ; Chris@0: Chris@0: /* Insert some larger peaks a know locations. */ Chris@0: data [4 * (frames / 8) + 0] = (frames / 8) * 0.01 ; /* First channel */ Chris@0: data [4 * (frames / 6) + 1] = (frames / 6) * 0.01 ; /* Second channel */ Chris@0: data [4 * (frames / 4) + 2] = (frames / 4) * 0.01 ; /* Third channel */ Chris@0: data [4 * (frames / 2) + 3] = (frames / 2) * 0.01 ; /* Fourth channel */ Chris@0: Chris@0: /* Write a file with PEAK chunks. */ Chris@0: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ; Chris@0: Chris@0: /* Try to confuse the header writer by adding a removing the PEAK chunk. */ Chris@0: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ; Chris@0: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; Chris@0: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ; Chris@0: Chris@0: /* Write the data in four passed. The data is designed so that peaks will Chris@0: ** be written in the different calls to sf_write_double (). Chris@0: */ Chris@0: for (count = 0 ; count < 4 ; count ++) Chris@0: test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ; Chris@0: Chris@0: if (sfinfo.format != filetype) Chris@0: { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.frames != frames) Chris@0: { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__, frames, (long) sfinfo.frames) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.channels != 4) Chris@0: { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Check these two commands. */ Chris@0: if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double)) == SF_FALSE) Chris@0: { printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (fabs (data [0] - (frames / 2) * 0.01) > 0.01) Chris@0: { 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) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels) == SF_FALSE) Chris@0: { printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (fabs (data [3] - (frames / 2) * 0.01) > 0.01) Chris@0: { 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) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Get the log buffer data. */ Chris@0: log_buffer [0] = 0 ; Chris@0: sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ; Chris@0: Chris@0: if (strlen (log_buffer) == 0) Chris@0: { printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: check_logged_peaks (log_buffer) ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: /* Write a file ***without*** PEAK chunks. */ Chris@0: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ; Chris@0: Chris@0: /* Try to confuse the header writer by adding a removing the PEAK chunk. */ Chris@0: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; Chris@0: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ; Chris@0: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; Chris@0: Chris@0: /* Write the data in four passed. The data is designed so that peaks will Chris@0: ** be written in the different calls to sf_write_double (). Chris@0: */ Chris@0: for (count = 0 ; count < 4 ; count ++) Chris@0: test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ; Chris@0: Chris@0: /* Check these two commands. */ Chris@0: if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double))) Chris@0: { printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels)) Chris@0: { printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Get the log buffer data. */ Chris@0: log_buffer [0] = 0 ; Chris@0: sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ; Chris@0: Chris@0: if (strlen (log_buffer) == 0) Chris@0: { printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (strstr (log_buffer, "PEAK :") != NULL) Chris@0: { printf ("\n\nLine %d: Should not have a PEAK chunk in this file.\n\n", __LINE__) ; Chris@0: puts (log_buffer) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: unlink (filename) ; Chris@0: printf ("ok\n") ; Chris@0: } /* test_float_peak */ Chris@0: Chris@0: static void Chris@0: check_logged_peaks (char *buffer) Chris@0: { char *cptr ; Chris@0: int k, chan, channel_count, position ; Chris@0: float value ; Chris@0: Chris@0: if (strstr (buffer, "should") || strstr (buffer, "*")) Chris@0: { printf ("\n\nLine %d: Something wrong in buffer. Dumping.\n", __LINE__) ; Chris@0: puts (buffer) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: channel_count = 0 ; Chris@0: cptr = strstr (buffer, "Channels") ; Chris@0: if (cptr && sscanf (cptr, "Channels : %d", &k) == 1) Chris@0: channel_count = k ; Chris@0: else if (cptr && sscanf (cptr, "Channels / frame : %d", &k) == 1) Chris@0: channel_count = k ; Chris@0: else Chris@0: { printf ("\n\nLine %d: Couldn't find channel count.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (channel_count != 4) Chris@0: { printf ("\n\nLine %d: Wrong channel count (4 ->%d).\n", __LINE__, channel_count) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (! (cptr = strstr (buffer, "Ch Position Value"))) Chris@0: { printf ("\n\nLine %d: Can't find PEAK data.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: for (k = 0 ; k < channel_count ; k++) Chris@0: { if (! (cptr = strchr (cptr, '\n'))) Chris@0: { printf ("\n\nLine %d: Got lost.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: if (sscanf (cptr, "%d %d %f", &chan, &position, &value) != 3) Chris@0: { printf ("\n\nLine %d: sscanf failed.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: if (position == 0) Chris@0: { printf ("\n\nLine %d: peak position for channel %d should not be at offset 0.\n", __LINE__, chan) ; Chris@0: printf ("%s", buffer) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: if (chan != k || fabs ((position) * 0.01 - value) > 1e-6) Chris@0: { printf ("\n\nLine %d: Error : peak value incorrect!\n", __LINE__) ; Chris@0: printf ("%s", buffer) ; Chris@0: printf ("\n\nLine %d: %d %f %f\n", __LINE__, chan, position * 0.01, value) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: cptr ++ ; /* Move past current newline. */ Chris@0: } ; Chris@0: Chris@0: } /* check_logged_peaks */ Chris@0: Chris@0: static void Chris@0: read_write_peak_test (const char *filename, int filetype) Chris@0: { SNDFILE *file ; Chris@0: SF_INFO sfinfo ; Chris@0: Chris@0: double small_data [10] ; Chris@0: double max_peak = 0.0 ; Chris@0: unsigned k ; Chris@0: Chris@0: print_test_name (__func__, filename) ; Chris@0: Chris@0: for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++) Chris@0: small_data [k] = 0.1 ; Chris@0: Chris@0: sfinfo.samplerate = 44100 ; Chris@0: sfinfo.channels = 2 ; Chris@0: sfinfo.format = filetype ; Chris@0: sfinfo.frames = 0 ; Chris@0: Chris@0: /* Open the file, add peak chunk and write samples with value 0.1. */ Chris@0: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; Chris@0: Chris@0: sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ; Chris@0: Chris@0: test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: /* Open the fiel RDWR, write sample valied 1.25. */ Chris@0: file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ; Chris@0: Chris@0: for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++) Chris@0: small_data [k] = 1.0 ; Chris@0: Chris@0: test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ; Chris@0: Chris@0: sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (max_peak)) ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: exit_if_true (max_peak < 0.1, "\n\nLine %d : max peak (%5.3f) should not be 0.1.\n\n", __LINE__, max_peak) ; Chris@0: Chris@0: /* Open the file and test the values written to the PEAK chunk. */ Chris@0: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; Chris@0: Chris@0: exit_if_true (sfinfo.channels * sfinfo.frames != 2 * ARRAY_LEN (small_data), Chris@0: "Line %d : frame count is %ld, should be %d\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * ARRAY_LEN (small_data)) ; Chris@0: Chris@0: sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (double)) ; Chris@0: Chris@0: sf_close (file) ; Chris@0: Chris@0: exit_if_true (max_peak < 1.0, "\n\nLine %d : max peak (%5.3f) should be 1.0.\n\n", __LINE__, max_peak) ; Chris@0: Chris@0: unlink (filename) ; Chris@0: puts ("ok") ; Chris@0: } /* read_write_peak_test */ Chris@0: