Fix a bug that prevented the turning off of PEAK chunks.

This commit is contained in:
Erik de Castro Lopo 2005-12-30 03:20:23 +00:00
parent 020b1df767
commit ba42ebbeb0
3 changed files with 74 additions and 35 deletions

View File

@ -1,3 +1,11 @@
2005-12-30 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
* tests/peak_chunk_test.c
Extend and clean up tests.
* src/sndfile.c
Fix a bug that prevented the turning off of PEAK chunks.
2005-12-29 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
* tests/error_test.c

View File

@ -912,8 +912,16 @@ sf_command (SNDFILE *sndfile, int command, void *data, int datasize)
if (psf->have_written)
return SF_FALSE ;
/* Everything seems OK, so set psf->has_peak and re-write header. */
if (datasize)
if (datasize == SF_FALSE)
{ if (psf->peak_info)
{ free (psf->peak_info) ;
psf->peak_info = NULL ;
} ;
return SF_FALSE ;
} ;
if (psf->peak_info == NULL)
psf->peak_info = peak_info_calloc (psf->sf.channels) ;
if (psf->write_header)
psf->write_header (psf, SF_TRUE) ;
return SF_TRUE ;

View File

@ -35,7 +35,7 @@
#define LOG_BUFFER_SIZE 1024
static void test_float_peak (const char *filename, int typemajor) ;
static void test_float_peak (const char *filename, int filetype) ;
static void check_logged_peaks (char *buffer) ;
@ -62,12 +62,12 @@ main (int argc, char *argv [])
do_all=!strcmp (argv [1], "all") ;
if (do_all || ! strcmp (argv [1], "wav"))
{ test_float_peak ("peak_chunk.wav", SF_FORMAT_WAV) ;
{ test_float_peak ("peak_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
test_count++ ;
} ;
if (do_all || ! strcmp (argv [1], "aiff"))
{ test_float_peak ("peak_chunk.aiff", SF_FORMAT_AIFF) ;
{ test_float_peak ("peak_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
test_count++ ;
} ;
@ -86,7 +86,7 @@ main (int argc, char *argv [])
*/
static void
test_float_peak (const char *filename, int typemajor)
test_float_peak (const char *filename, int filetype)
{ SNDFILE *file ;
SF_INFO sfinfo ;
int k, frames, count ;
@ -94,7 +94,7 @@ test_float_peak (const char *filename, int typemajor)
print_test_name ("test_float_peak", filename) ;
sfinfo.samplerate = 44100 ;
sfinfo.format = (typemajor | SF_FORMAT_FLOAT) ;
sfinfo.format = filetype ;
sfinfo.channels = 4 ;
sfinfo.frames = 0 ;
@ -110,42 +110,33 @@ test_float_peak (const char *filename, int typemajor)
data [4 * (frames / 4) + 2] = (frames / 4) * 0.01 ; /* Third channel */
data [4 * (frames / 2) + 3] = (frames / 2) * 0.01 ; /* Fourth channel */
if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
{ printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
puts (sf_strerror (NULL)) ;
exit (1) ;
} ;
/* Write a file with PEAK chunks. */
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;
sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
/* Write the data in four passed. The data is designed so that peaks will
** be written in the different calls to sf_write_double ().
*/
for (count = 0 ; count < 4 ; count ++)
{ if ((k = sf_write_double (file, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4)) != BUFFER_LEN / 4)
{ printf ("Line %d: sf_write_double # %d failed with short write (%d ->%d)\n", __LINE__, count, BUFFER_LEN / 4, k) ;
exit (1) ;
} ;
} ;
test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ;
sf_close (file) ;
if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
{ printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
puts (sf_strerror (NULL)) ;
exit (1) ;
} ;
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ;
if (sfinfo.format != (typemajor | SF_FORMAT_FLOAT))
{ printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (typemajor | SF_FORMAT_FLOAT), sfinfo.format) ;
if (sfinfo.format != filetype)
{ printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
exit (1) ;
} ;
if (sfinfo.frames != frames)
{ printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, frames, (long) sfinfo.frames) ;
{ printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__, frames, (long) sfinfo.frames) ;
exit (1) ;
} ;
if (sfinfo.channels != 4)
{ printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
{ printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
exit (1) ;
} ;
@ -154,7 +145,7 @@ test_float_peak (const char *filename, int typemajor)
sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ;
if (strlen (log_buffer) == 0)
{ printf ("Line %d: Empty log buffer,\n", __LINE__) ;
{ printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ;
exit (1) ;
} ;
@ -162,6 +153,38 @@ test_float_peak (const char *filename, int typemajor)
sf_close (file) ;
/* Write a file ***without*** PEAK chunks. */
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;
sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
/* Write the data in four passed. The data is designed so that peaks will
** be written in the different calls to sf_write_double ().
*/
for (count = 0 ; count < 4 ; count ++)
test_write_double_or_die (file, 0, data + count * BUFFER_LEN / 4, BUFFER_LEN / 4, BUFFER_LEN / 4) ;
sf_close (file) ;
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, 0, __LINE__) ;
/* Get the log buffer data. */
log_buffer [0] = 0 ;
sf_command (file, SFC_GET_LOG_INFO, log_buffer, LOG_BUFFER_SIZE) ;
if (strlen (log_buffer) == 0)
{ printf ("\n\nLine %d: Empty log buffer,\n", __LINE__) ;
exit (1) ;
} ;
if (strstr (log_buffer, "PEAK :") != NULL)
{ printf ("\n\nLine %d: Should not have a PEAK chunk in this file.\n\n", __LINE__) ;
puts (log_buffer) ;
exit (1) ;
} ;
sf_close (file) ;
unlink (filename) ;
printf ("ok\n") ;
} /* test_float_peak */
@ -173,44 +196,44 @@ check_logged_peaks (char *buffer)
float value ;
if (strstr (buffer, "should") || strstr (buffer, "*"))
{ printf ("Line %d: Something wrong in buffer. Dumping.\n", __LINE__) ;
{ printf ("\n\nLine %d: Something wrong in buffer. Dumping.\n", __LINE__) ;
puts (buffer) ;
exit (1) ;
} ;
if (! (cptr = strstr (buffer, "Channels")) || sscanf (cptr, "Channels : %d", &channel_count) != 1)
{ printf ("Line %d: Couldn't find channel count.\n", __LINE__) ;
{ printf ("\n\nLine %d: Couldn't find channel count.\n", __LINE__) ;
exit (1) ;
} ;
if (channel_count != 4)
{ printf ("Line %d: Wrong channel count (4 ->%d).\n", __LINE__, channel_count) ;
{ printf ("\n\nLine %d: Wrong channel count (4 ->%d).\n", __LINE__, channel_count) ;
exit (1) ;
} ;
if (! (cptr = strstr (buffer, "Ch Position Value")))
{ printf ("Line %d: Can't find PEAK data.\n", __LINE__) ;
{ printf ("\n\nLine %d: Can't find PEAK data.\n", __LINE__) ;
exit (1) ;
} ;
for (k = 0 ; k < channel_count ; k++)
{ if (! (cptr = strchr (cptr, '\n')))
{ printf ("Line %d: Got lost.\n", __LINE__) ;
{ printf ("\n\nLine %d: Got lost.\n", __LINE__) ;
exit (1) ;
} ;
if (sscanf (cptr, "%d %d %f", &chan, &position, &value) != 3)
{ printf ("Line %d: sscanf failed.\n", __LINE__) ;
{ printf ("\n\nLine %d: sscanf failed.\n", __LINE__) ;
exit (1) ;
} ;
if (position == 0)
{ printf ("Line %d: peak position for channel %d should not be at offset 0.\n", __LINE__, chan) ;
{ printf ("\n\nLine %d: peak position for channel %d should not be at offset 0.\n", __LINE__, chan) ;
printf (buffer) ;
exit (1) ;
} ;
if (chan != k || fabs ((position) * 0.01 - value) > 1e-6)
{ printf ("Line %d: Error : peak value incorrect!\n", __LINE__) ;
{ printf ("\n\nLine %d: Error : peak value incorrect!\n", __LINE__) ;
printf (buffer) ;
printf ("Line %d: %d %f %f\n", __LINE__, chan, position * 0.01, value) ;
printf ("\n\nLine %d: %d %f %f\n", __LINE__, chan, position * 0.01, value) ;
exit (1) ;
} ;
cptr ++ ; /* Move past current newline. */