annotate src/libsndfile-1.0.25/tests/peak_chunk_test.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 545efbb81310
children
rev   line source
cannam@85 1 /*
cannam@85 2 ** Copyright (C) 2001-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@85 3 **
cannam@85 4 ** This program is free software; you can redistribute it and/or modify
cannam@85 5 ** it under the terms of the GNU General Public License as published by
cannam@85 6 ** the Free Software Foundation; either version 2 of the License, or
cannam@85 7 ** (at your option) any later version.
cannam@85 8 **
cannam@85 9 ** This program is distributed in the hope that it will be useful,
cannam@85 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@85 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@85 12 ** GNU General Public License for more details.
cannam@85 13 **
cannam@85 14 ** You should have received a copy of the GNU General Public License
cannam@85 15 ** along with this program; if not, write to the Free Software
cannam@85 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cannam@85 17 */
cannam@85 18
cannam@85 19 #include "sfconfig.h"
cannam@85 20
cannam@85 21 #include <stdio.h>
cannam@85 22 #include <stdlib.h>
cannam@85 23 #include <string.h>
cannam@85 24 #include <math.h>
cannam@85 25
cannam@85 26 #if HAVE_UNISTD_H
cannam@85 27 #include <unistd.h>
cannam@85 28 #endif
cannam@85 29
cannam@85 30 #include <sndfile.h>
cannam@85 31
cannam@85 32 #include "utils.h"
cannam@85 33
cannam@85 34 #define BUFFER_LEN (1<<15)
cannam@85 35 #define LOG_BUFFER_SIZE 1024
cannam@85 36
cannam@85 37
cannam@85 38 static void test_float_peak (const char *filename, int filetype) ;
cannam@85 39 static void read_write_peak_test (const char *filename, int filetype) ;
cannam@85 40
cannam@85 41 static void check_logged_peaks (char *buffer) ;
cannam@85 42
cannam@85 43 /* Force the start of this buffer to be double aligned. Sparc-solaris will
cannam@85 44 ** choke if its not.
cannam@85 45 */
cannam@85 46 static double data [BUFFER_LEN] ;
cannam@85 47 static char log_buffer [LOG_BUFFER_SIZE] ;
cannam@85 48
cannam@85 49 int
cannam@85 50 main (int argc, char *argv [])
cannam@85 51 { int do_all = 0 ;
cannam@85 52 int test_count = 0 ;
cannam@85 53
cannam@85 54 if (argc != 2)
cannam@85 55 { printf ("Usage : %s <test>\n", argv [0]) ;
cannam@85 56 printf (" Where <test> is one of the following:\n") ;
cannam@85 57 printf (" aiff - test AIFF file PEAK chunk\n") ;
cannam@85 58 printf (" caf - test CAF file PEAK chunk\n") ;
cannam@85 59 printf (" wav - test WAV file peak chunk\n") ;
cannam@85 60 printf (" all - perform all tests\n") ;
cannam@85 61 exit (1) ;
cannam@85 62 } ;
cannam@85 63
cannam@85 64 do_all = ! strcmp (argv [1], "all") ;
cannam@85 65
cannam@85 66 if (do_all || ! strcmp (argv [1], "wav"))
cannam@85 67 { test_float_peak ("peak_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
cannam@85 68 test_float_peak ("peak_float.wavex", SF_FORMAT_WAVEX | SF_FORMAT_FLOAT) ;
cannam@85 69 test_float_peak ("peak_float.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
cannam@85 70
cannam@85 71 read_write_peak_test ("rw_peak.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
cannam@85 72 read_write_peak_test ("rw_peak.wavex", SF_FORMAT_WAVEX | SF_FORMAT_FLOAT) ;
cannam@85 73 test_count++ ;
cannam@85 74 } ;
cannam@85 75
cannam@85 76 if (do_all || ! strcmp (argv [1], "aiff"))
cannam@85 77 { test_float_peak ("peak_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
cannam@85 78
cannam@85 79 read_write_peak_test ("rw_peak.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
cannam@85 80 test_count++ ;
cannam@85 81 } ;
cannam@85 82
cannam@85 83 if (do_all || ! strcmp (argv [1], "caf"))
cannam@85 84 { test_float_peak ("peak_float.caf", SF_FORMAT_CAF | SF_FORMAT_FLOAT) ;
cannam@85 85
cannam@85 86 read_write_peak_test ("rw_peak.caf", SF_FORMAT_CAF | SF_FORMAT_FLOAT) ;
cannam@85 87 test_count++ ;
cannam@85 88 } ;
cannam@85 89
cannam@85 90 if (test_count == 0)
cannam@85 91 { printf ("Mono : ************************************\n") ;
cannam@85 92 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
cannam@85 93 printf ("Mono : ************************************\n") ;
cannam@85 94 return 1 ;
cannam@85 95 } ;
cannam@85 96
cannam@85 97 return 0 ;
cannam@85 98 } /* main */
cannam@85 99
cannam@85 100 /*============================================================================================
cannam@85 101 ** Here are the test functions.
cannam@85 102 */
cannam@85 103
cannam@85 104 static void
cannam@85 105 test_float_peak (const char *filename, int filetype)
cannam@85 106 { SNDFILE *file ;
cannam@85 107 SF_INFO sfinfo ;
cannam@85 108 int k, frames, count ;
cannam@85 109
cannam@85 110 print_test_name ("test_float_peak", filename) ;
cannam@85 111
cannam@85 112 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 113 sfinfo.samplerate = 44100 ;
cannam@85 114 sfinfo.format = filetype ;
cannam@85 115 sfinfo.channels = 4 ;
cannam@85 116 sfinfo.frames = 0 ;
cannam@85 117
cannam@85 118 frames = BUFFER_LEN / sfinfo.channels ;
cannam@85 119
cannam@85 120 /* Create some random data with a peak value of 0.66. */
cannam@85 121 for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@85 122 data [k] = (rand () % 2000) / 3000.0 ;
cannam@85 123
cannam@85 124 /* Insert some larger peaks a know locations. */
cannam@85 125 data [4 * (frames / 8) + 0] = (frames / 8) * 0.01 ; /* First channel */
cannam@85 126 data [4 * (frames / 6) + 1] = (frames / 6) * 0.01 ; /* Second channel */
cannam@85 127 data [4 * (frames / 4) + 2] = (frames / 4) * 0.01 ; /* Third channel */
cannam@85 128 data [4 * (frames / 2) + 3] = (frames / 2) * 0.01 ; /* Fourth channel */
cannam@85 129
cannam@85 130 /* Write a file with PEAK chunks. */
cannam@85 131 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;
cannam@85 132
cannam@85 133 /* Try to confuse the header writer by adding a removing the PEAK chunk. */
cannam@85 134 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
cannam@85 135 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
cannam@85 136 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
cannam@85 137
cannam@85 138 /* Write the data in four passed. The data is designed so that peaks will
cannam@85 139 ** be written in the different calls to sf_write_double ().
cannam@85 140 */
cannam@85 141 for (count = 0 ; count < 4 ; count ++)
cannam@85 142 test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ;
cannam@85 143
cannam@85 144 sf_close (file) ;
cannam@85 145
cannam@85 146 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ;
cannam@85 147
cannam@85 148 if (sfinfo.format != filetype)
cannam@85 149 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
cannam@85 150 exit (1) ;
cannam@85 151 } ;
cannam@85 152
cannam@85 153 if (sfinfo.frames != frames)
cannam@85 154 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__, frames, (long) sfinfo.frames) ;
cannam@85 155 exit (1) ;
cannam@85 156 } ;
cannam@85 157
cannam@85 158 if (sfinfo.channels != 4)
cannam@85 159 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
cannam@85 160 exit (1) ;
cannam@85 161 } ;
cannam@85 162
cannam@85 163 /* Check these two commands. */
cannam@85 164 if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double)) == SF_FALSE)
cannam@85 165 { printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ;
cannam@85 166 exit (1) ;
cannam@85 167 } ;
cannam@85 168
cannam@85 169 if (fabs (data [0] - (frames / 2) * 0.01) > 0.01)
cannam@85 170 { 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@85 171 exit (1) ;
cannam@85 172 } ;
cannam@85 173
cannam@85 174 if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels) == SF_FALSE)
cannam@85 175 { printf ("\n\nLine %d: Command should have returned SF_TRUE.\n", __LINE__) ;
cannam@85 176 exit (1) ;
cannam@85 177 } ;
cannam@85 178
cannam@85 179 if (fabs (data [3] - (frames / 2) * 0.01) > 0.01)
cannam@85 180 { 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@85 181 exit (1) ;
cannam@85 182 } ;
cannam@85 183
cannam@85 184 /* Get the log buffer data. */
cannam@85 185 log_buffer [0] = 0 ;
cannam@85 186 sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ;
cannam@85 187
cannam@85 188 if (strlen (log_buffer) == 0)
cannam@85 189 { printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ;
cannam@85 190 exit (1) ;
cannam@85 191 } ;
cannam@85 192
cannam@85 193 check_logged_peaks (log_buffer) ;
cannam@85 194
cannam@85 195 sf_close (file) ;
cannam@85 196
cannam@85 197 /* Write a file ***without*** PEAK chunks. */
cannam@85 198 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;
cannam@85 199
cannam@85 200 /* Try to confuse the header writer by adding a removing the PEAK chunk. */
cannam@85 201 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
cannam@85 202 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
cannam@85 203 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
cannam@85 204
cannam@85 205 /* Write the data in four passed. The data is designed so that peaks will
cannam@85 206 ** be written in the different calls to sf_write_double ().
cannam@85 207 */
cannam@85 208 for (count = 0 ; count < 4 ; count ++)
cannam@85 209 test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ;
cannam@85 210
cannam@85 211 sf_close (file) ;
cannam@85 212
cannam@85 213 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ;
cannam@85 214
cannam@85 215 /* Check these two commands. */
cannam@85 216 if (sf_command (file, SFC_GET_SIGNAL_MAX, data, sizeof (double)))
cannam@85 217 { printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ;
cannam@85 218 exit (1) ;
cannam@85 219 } ;
cannam@85 220
cannam@85 221 if (sf_command (file, SFC_GET_MAX_ALL_CHANNELS, data, sizeof (double) * sfinfo.channels))
cannam@85 222 { printf ("\n\nLine %d: Command should have returned SF_FALSE.\n", __LINE__) ;
cannam@85 223 exit (1) ;
cannam@85 224 } ;
cannam@85 225
cannam@85 226 /* Get the log buffer data. */
cannam@85 227 log_buffer [0] = 0 ;
cannam@85 228 sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ;
cannam@85 229
cannam@85 230 if (strlen (log_buffer) == 0)
cannam@85 231 { printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ;
cannam@85 232 exit (1) ;
cannam@85 233 } ;
cannam@85 234
cannam@85 235 if (strstr (log_buffer, "PEAK :") != NULL)
cannam@85 236 { printf ("\n\nLine %d: Should not have a PEAK chunk in this file.\n\n", __LINE__) ;
cannam@85 237 puts (log_buffer) ;
cannam@85 238 exit (1) ;
cannam@85 239 } ;
cannam@85 240
cannam@85 241 sf_close (file) ;
cannam@85 242
cannam@85 243 unlink (filename) ;
cannam@85 244 printf ("ok\n") ;
cannam@85 245 } /* test_float_peak */
cannam@85 246
cannam@85 247 static void
cannam@85 248 check_logged_peaks (char *buffer)
cannam@85 249 { char *cptr ;
cannam@85 250 int k, chan, channel_count, position ;
cannam@85 251 float value ;
cannam@85 252
cannam@85 253 if (strstr (buffer, "should") || strstr (buffer, "*"))
cannam@85 254 { printf ("\n\nLine %d: Something wrong in buffer. Dumping.\n", __LINE__) ;
cannam@85 255 puts (buffer) ;
cannam@85 256 exit (1) ;
cannam@85 257 } ;
cannam@85 258
cannam@85 259 channel_count = 0 ;
cannam@85 260 cptr = strstr (buffer, "Channels") ;
cannam@85 261 if (cptr && sscanf (cptr, "Channels : %d", &k) == 1)
cannam@85 262 channel_count = k ;
cannam@85 263 else if (cptr && sscanf (cptr, "Channels / frame : %d", &k) == 1)
cannam@85 264 channel_count = k ;
cannam@85 265 else
cannam@85 266 { printf ("\n\nLine %d: Couldn't find channel count.\n", __LINE__) ;
cannam@85 267 exit (1) ;
cannam@85 268 } ;
cannam@85 269
cannam@85 270 if (channel_count != 4)
cannam@85 271 { printf ("\n\nLine %d: Wrong channel count (4 ->%d).\n", __LINE__, channel_count) ;
cannam@85 272 exit (1) ;
cannam@85 273 } ;
cannam@85 274
cannam@85 275 if (! (cptr = strstr (buffer, "Ch Position Value")))
cannam@85 276 { printf ("\n\nLine %d: Can't find PEAK data.\n", __LINE__) ;
cannam@85 277 exit (1) ;
cannam@85 278 } ;
cannam@85 279
cannam@85 280 for (k = 0 ; k < channel_count ; k++)
cannam@85 281 { if (! (cptr = strchr (cptr, '\n')))
cannam@85 282 { printf ("\n\nLine %d: Got lost.\n", __LINE__) ;
cannam@85 283 exit (1) ;
cannam@85 284 } ;
cannam@85 285 if (sscanf (cptr, "%d %d %f", &chan, &position, &value) != 3)
cannam@85 286 { printf ("\n\nLine %d: sscanf failed.\n", __LINE__) ;
cannam@85 287 exit (1) ;
cannam@85 288 } ;
cannam@85 289 if (position == 0)
cannam@85 290 { printf ("\n\nLine %d: peak position for channel %d should not be at offset 0.\n", __LINE__, chan) ;
cannam@85 291 printf ("%s", buffer) ;
cannam@85 292 exit (1) ;
cannam@85 293 } ;
cannam@85 294 if (chan != k || fabs ((position) * 0.01 - value) > 1e-6)
cannam@85 295 { printf ("\n\nLine %d: Error : peak value incorrect!\n", __LINE__) ;
cannam@85 296 printf ("%s", buffer) ;
cannam@85 297 printf ("\n\nLine %d: %d %f %f\n", __LINE__, chan, position * 0.01, value) ;
cannam@85 298 exit (1) ;
cannam@85 299 } ;
cannam@85 300 cptr ++ ; /* Move past current newline. */
cannam@85 301 } ;
cannam@85 302
cannam@85 303 } /* check_logged_peaks */
cannam@85 304
cannam@85 305 static void
cannam@85 306 read_write_peak_test (const char *filename, int filetype)
cannam@85 307 { SNDFILE *file ;
cannam@85 308 SF_INFO sfinfo ;
cannam@85 309
cannam@85 310 double small_data [10] ;
cannam@85 311 double max_peak = 0.0 ;
cannam@85 312 unsigned k ;
cannam@85 313
cannam@85 314 print_test_name (__func__, filename) ;
cannam@85 315
cannam@85 316 for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++)
cannam@85 317 small_data [k] = 0.1 ;
cannam@85 318
cannam@85 319 sfinfo.samplerate = 44100 ;
cannam@85 320 sfinfo.channels = 2 ;
cannam@85 321 sfinfo.format = filetype ;
cannam@85 322 sfinfo.frames = 0 ;
cannam@85 323
cannam@85 324 /* Open the file, add peak chunk and write samples with value 0.1. */
cannam@85 325 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
cannam@85 326
cannam@85 327 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
cannam@85 328
cannam@85 329 test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ;
cannam@85 330
cannam@85 331 sf_close (file) ;
cannam@85 332
cannam@85 333 /* Open the fiel RDWR, write sample valied 1.25. */
cannam@85 334 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
cannam@85 335
cannam@85 336 for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++)
cannam@85 337 small_data [k] = 1.0 ;
cannam@85 338
cannam@85 339 test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ;
cannam@85 340
cannam@85 341 sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (max_peak)) ;
cannam@85 342
cannam@85 343 sf_close (file) ;
cannam@85 344
cannam@85 345 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@85 346
cannam@85 347 /* Open the file and test the values written to the PEAK chunk. */
cannam@85 348 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
cannam@85 349
cannam@85 350 exit_if_true (sfinfo.channels * sfinfo.frames != 2 * ARRAY_LEN (small_data),
cannam@85 351 "Line %d : frame count is %ld, should be %d\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * ARRAY_LEN (small_data)) ;
cannam@85 352
cannam@85 353 sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (double)) ;
cannam@85 354
cannam@85 355 sf_close (file) ;
cannam@85 356
cannam@85 357 exit_if_true (max_peak < 1.0, "\n\nLine %d : max peak (%5.3f) should be 1.0.\n\n", __LINE__, max_peak) ;
cannam@85 358
cannam@85 359 unlink (filename) ;
cannam@85 360 puts ("ok") ;
cannam@85 361 } /* read_write_peak_test */
cannam@85 362