annotate src/libsndfile-1.0.27/tests/peak_chunk_test.c @ 148:b4bfdf10c4b3

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