void test_read_write_position_or_die (SNDFILE *file, int line_num, int pass, sf_count_t read_pos, sf_count_t write_pos) { sf_count_t pos ; /* Check the current read position. */ if (read_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_READ)) != read_pos) { printf ("\n\nLine %d ", line_num) ; if (pass > 0) printf ("(pass %d): ", pass) ; printf ("Read position (%ld) should be %ld.\n", SF_COUNT_TO_LONG (pos), SF_COUNT_TO_LONG (read_pos)) ; exit (1) ; } ; /* Check the current write position. */ if (write_pos >= 0 && (pos = sf_seek (file, 0, SEEK_CUR | SFM_WRITE)) != write_pos) { printf ("\n\nLine %d", line_num) ; if (pass > 0) printf (" (pass %d)", pass) ; printf (" : Write position (%ld) should be %ld.\n", SF_COUNT_TO_LONG (pos), SF_COUNT_TO_LONG (write_pos)) ; exit (1) ; } ; return ; } /* test_read_write_position */
static void rw_test (const char *filename) { SNDFILE *file ; SF_INFO sfinfo_rd, sfinfo_rw ; memset (&sfinfo_rd, 0, sizeof (sfinfo_rd)) ; memset (&sfinfo_rw, 0, sizeof (sfinfo_rw)) ; /* Open the file in read only mode and fill in the SF_INFO struct. */ if ((file = sf_open (filename, SFM_READ, &sfinfo_rd)) == NULL) { printf ("\n\nLine %d : sf_open SFM_READ failed : %s\n\n", __LINE__, sf_strerror (NULL)) ; exit (1) ; } ; check_log_buffer_or_die (file, __LINE__) ; sf_close (file) ; /* Now open read/write and close the file. */ if ((file = sf_open (filename, SFM_RDWR, &sfinfo_rw)) == NULL) { printf ("\n\nLine %d : sf_open SFM_RDWR failed : %s\n\n", __LINE__, sf_strerror (NULL)) ; exit (1) ; } ; check_log_buffer_or_die (file, __LINE__) ; sf_close (file) ; /* Open again as read only again and fill in a new SF_INFO struct. */ memset (&sfinfo_rw, 0, sizeof (sfinfo_rw)) ; if ((file = sf_open (filename, SFM_READ, &sfinfo_rw)) == NULL) { printf ("\n\nLine %d : sf_open SFM_RDWR failed : %s\n\n", __LINE__, sf_strerror (NULL)) ; exit (1) ; } ; check_log_buffer_or_die (file, __LINE__) ; sf_close (file) ; /* Now compare the two. */ if (sfinfo_rd.format != sfinfo_rw.format) { printf ("\n\nLine %d : format mismatch (0x%08X != 0x%08X).\n\n", __LINE__, sfinfo_rd.format, sfinfo_rw.format) ; exit (1) ; } ; if (sfinfo_rd.channels != sfinfo_rw.channels) { printf ("\n\nLine %d : channel count mismatch (%d != %d).\n\n", __LINE__, sfinfo_rd.channels, sfinfo_rw.channels) ; exit (1) ; } ; if (sfinfo_rd.frames != sfinfo_rw.frames) { printf ("\n\nLine %d : frame count mismatch (rd %ld != rw %ld).\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo_rd.frames), SF_COUNT_TO_LONG (sfinfo_rw.frames)) ; exit (1) ; } ; return ; } /* rw_test */
void test_seek_or_die (SNDFILE *file, sf_count_t offset, int whence, sf_count_t new_pos, int channels, int line_num) { sf_count_t position ; const char *channel_name, *whence_name ; switch (whence) { case SEEK_SET : whence_name = "SEEK_SET" ; break ; case SEEK_CUR : whence_name = "SEEK_CUR" ; break ; case SEEK_END : whence_name = "SEEK_END" ; break ; /* SFM_READ */ case SEEK_SET | SFM_READ : whence_name = "SFM_READ | SEEK_SET" ; break ; case SEEK_CUR | SFM_READ : whence_name = "SFM_READ | SEEK_CUR" ; break ; case SEEK_END | SFM_READ : whence_name = "SFM_READ | SEEK_END" ; break ; /* SFM_WRITE */ case SEEK_SET | SFM_WRITE : whence_name = "SFM_WRITE | SEEK_SET" ; break ; case SEEK_CUR | SFM_WRITE : whence_name = "SFM_WRITE | SEEK_CUR" ; break ; case SEEK_END | SFM_WRITE : whence_name = "SFM_WRITE | SEEK_END" ; break ; default : printf ("\n\nLine %d: bad whence parameter.\n", line_num) ; exit (1) ; } ; channel_name = (channels == 1) ? "Mono" : "Stereo" ; if ((position = sf_seek (file, offset, whence)) != new_pos) { printf ("\n\nLine %d : %s : sf_seek (file, %ld, %s) returned %ld (should be %ld).\n\n", line_num, channel_name, SF_COUNT_TO_LONG (offset), whence_name, SF_COUNT_TO_LONG (position), SF_COUNT_TO_LONG (new_pos)) ; exit (1) ; } ; } /* test_seek_or_die */
void hexdump_file (const char * filename, sf_count_t offset, sf_count_t length) { FILE * file ; char buffer [16] ; int k, m, ch, readcount ; if (length > 1000000) { printf ("\n\nError : length (%ld) too long.\n\n", SF_COUNT_TO_LONG (offset)) ; exit (1) ; } ; if ((file = fopen (filename, "r")) == NULL) { printf ("\n\nError : hexdump_file (%s) could not open file for read.\n\n", filename) ; exit (1) ; } ; if (fseek (file, offset, SEEK_SET) != 0) { printf ("\n\nError : fseek(file, %ld, SEEK_SET) failed : %s\n\n", SF_COUNT_TO_LONG (offset), strerror (errno)) ; exit (1) ; } ; puts ("\n\n") ; for (k = 0 ; k < length ; k+= sizeof (buffer)) { readcount = fread (buffer, 1, sizeof (buffer), file) ; printf ("%08lx : ", SF_COUNT_TO_LONG (offset + k)) ; for (m = 0 ; m < readcount ; m++) printf ("%02x ", buffer [m] & 0xFF) ; for (m = readcount ; m < SIGNED_SIZEOF (buffer) ; m++) printf (" ") ; printf (" ") ; for (m = 0 ; m < readcount ; m++) { ch = isprint (buffer [m]) ? buffer [m] : '.' ; putchar (ch) ; } ; if (readcount < SIGNED_SIZEOF (buffer)) break ; putchar ('\n') ; } ; puts ("\n") ; fclose (file) ; } /* hexdump_file */
static void current_sf_info_test (const char *filename) { SNDFILE *outfile, *infile ; SF_INFO outinfo, ininfo ; sf_count_t last_count ; print_test_name ("current_sf_info_test", filename) ; outinfo.samplerate = 44100 ; outinfo.format = (SF_FORMAT_WAV | SF_FORMAT_PCM_16) ; outinfo.channels = 1 ; outinfo.frames = 0 ; outfile = test_open_file_or_die (filename, SFM_WRITE, &outinfo, SF_TRUE, __LINE__) ; sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, 0) ; exit_if_true (outinfo.frames != 0, "\n\nLine %d : Initial sfinfo.frames is not zero.\n\n", __LINE__ ) ; test_write_double_or_die (outfile, 0, double_data, BUFFER_LEN, __LINE__) ; sf_command (outfile, SFC_GET_CURRENT_SF_INFO, &outinfo, sizeof (outinfo)) ; exit_if_true (outinfo.frames != BUFFER_LEN, "\n\nLine %d : Initial sfinfo.frames (%ld) should be %d.\n\n", __LINE__, SF_COUNT_TO_LONG (outinfo.frames), BUFFER_LEN ) ; /* Read file making sure no channel map exists. */ memset (&ininfo, 0, sizeof (ininfo)) ; infile = test_open_file_or_die (filename, SFM_READ, &ininfo, SF_TRUE, __LINE__) ; last_count = ininfo.frames ; test_write_double_or_die (outfile, 0, double_data, BUFFER_LEN, __LINE__) ; sf_command (infile, SFC_GET_CURRENT_SF_INFO, &ininfo, sizeof (ininfo)) ; exit_if_true (ininfo.frames != BUFFER_LEN, "\n\nLine %d : Initial sfinfo.frames (%ld) should be %d.\n\n", __LINE__, SF_COUNT_TO_LONG (ininfo.frames), BUFFER_LEN ) ; sf_close (outfile) ; sf_close (infile) ; unlink (filename) ; puts ("ok") ; } /* current_sf_info_test */
void test_write_float_or_die (SNDFILE *file, int pass, float *test, sf_count_t items, int line_num) { sf_count_t count ; if ((count = sf_write_float (file, test, items)) != items) { printf ("\n\nLine %d", line_num) ; if (pass > 0) printf (" (pass %d)", pass) ; printf (" : sf_write_float failed with short write (%ld => %ld).\n", SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ; fflush (stdout) ; puts (sf_strerror (file)) ; exit (1) ; } ; return ; } /* test_write_float */
void test_writef_double_or_die (SNDFILE *file, int pass, double *test, sf_count_t frames, int line_num) { sf_count_t count ; if ((count = sf_writef_double (file, test, frames)) != frames) { printf ("\n\nLine %d", line_num) ; if (pass > 0) printf (" (pass %d)", pass) ; printf (" : sf_writef_double failed with short writef (%ld => %ld).\n", SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ; fflush (stdout) ; puts (sf_strerror (file)) ; exit (1) ; } ; return ; } /* test_writef_double */
static void read_write_peak_test (const char *filename, int filetype) { SNDFILE *file ; SF_INFO sfinfo ; double small_data [10] ; double max_peak = 0.0 ; unsigned k ; print_test_name (__func__, filename) ; for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++) small_data [k] = 0.1 ; sfinfo.samplerate = 44100 ; sfinfo.channels = 2 ; sfinfo.format = filetype ; sfinfo.frames = 0 ; /* Open the file, add peak chunk and write samples with value 0.1. */ file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ; test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ; sf_close (file) ; /* Open the fiel RDWR, write sample valied 1.25. */ file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ; for (k = 0 ; k < ARRAY_LEN (small_data) ; k ++) small_data [k] = 1.0 ; test_write_double_or_die (file, 0, small_data, ARRAY_LEN (small_data), __LINE__) ; sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (max_peak)) ; sf_close (file) ; exit_if_true (max_peak < 0.1, "\n\nLine %d : max peak (%5.3f) should not be 0.1.\n\n", __LINE__, max_peak) ; /* Open the file and test the values written to the PEAK chunk. */ file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; exit_if_true (sfinfo.channels * sfinfo.frames != 2 * ARRAY_LEN (small_data), "Line %d : frame count is %ld, should be %d\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * ARRAY_LEN (small_data)) ; sf_command (file, SFC_GET_SIGNAL_MAX, &max_peak, sizeof (double)) ; sf_close (file) ; exit_if_true (max_peak < 1.0, "\n\nLine %d : max peak (%5.3f) should be 1.0.\n\n", __LINE__, max_peak) ; unlink (filename) ; puts ("ok") ; } /* read_write_peak_test */
static void old_test (void) { static short buffer [BUFFER_SIZE] ; SNDFILE *file ; SF_INFO sfinfo ; int k, filetype ; const char *filename = "headerless.wav" ; print_test_name (__func__, "") ; for (k = 0 ; k < BUFFER_SIZE ; k++) buffer [k] = k ; filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ; sfinfo.samplerate = 32000 ; sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */ sfinfo.channels = 1 ; sfinfo.format = filetype ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ; fflush (stdout) ; puts (sf_strerror (file)) ; exit (1) ; } ; sf_close (file) ; memset (buffer, 0, sizeof (buffer)) ; /* Read as RAW but get the bit width and endian-ness correct. */ sfinfo.format = filetype = SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16 ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; if (sfinfo.format != filetype) { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ; exit (1) ; } ; if (sfinfo.frames < BUFFER_SIZE) { printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ; exit (1) ; } ; check_log_buffer_or_die (file, __LINE__) ; if ((k = sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) { printf ("Line %d: short read (%d).\n", __LINE__, k) ; exit (1) ; } ; for (k = 0 ; k < BUFFER_SIZE - 22 ; k++) if (buffer [k + 22] != k) { printf ("Line %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, k, buffer [k]) ; exit (1) ; } ; sf_close (file) ; printf ("ok\n") ; unlink (filename) ; } /* old_test */
static void headerless_test (const char * filename, int format, int expected) { static short buffer [BUFFER_SIZE] ; SNDFILE *file ; SF_INFO sfinfo ; int k ; format &= SF_FORMAT_SUBMASK ; print_test_name (__func__, filename) ; for (k = 0 ; k < BUFFER_SIZE ; k++) buffer [k] = k ; sfinfo.samplerate = 8000 ; sfinfo.frames = 0 ; sfinfo.channels = 1 ; sfinfo.format = SF_FORMAT_RAW | format ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE) { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ; fflush (stdout) ; puts (sf_strerror (file)) ; exit (1) ; } ; sf_close (file) ; memset (buffer, 0, sizeof (buffer)) ; /* We should be able to detect these so clear sfinfo. */ memset (&sfinfo, 0, sizeof (sfinfo)) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; if (sfinfo.format != expected) { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, expected, sfinfo.format) ; exit (1) ; } ; if (sfinfo.frames < BUFFER_SIZE) { printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ; exit (1) ; } ; check_log_buffer_or_die (file, __LINE__) ; sf_close (file) ; printf ("ok\n") ; unlink (filename) ; } /* headerless_test */
static void update_seek_double_test (const char *filename, int filetype) { SNDFILE *outfile, *infile ; SF_INFO sfinfo ; sf_count_t frames ; double buffer [8] ; int k ; print_test_name ("update_seek_double_test", filename) ; memset (buffer, 0, sizeof (buffer)) ; /* Create sound outfile with no data. */ sfinfo.format = filetype | SF_FORMAT_DOUBLE ; sfinfo.samplerate = 48000 ; sfinfo.channels = 2 ; if (sf_format_check (&sfinfo) == SF_FALSE) sfinfo.channels = 1 ; outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; sf_close (outfile) ; /* Open again for read/write. */ outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; /* ** In auto header update mode, seeking to the end of the file with ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END ** will seek to 0 anyway */ if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0) { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ; exit (1) ; } ; /* Now write some frames. */ frames = ARRAY_LEN (buffer) / sfinfo.channels ; for (k = 0 ; k < 6 ; k++) { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ; test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ; /* Open file again and make sure no errors in log buffer. */ infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; check_log_buffer_or_die (infile, __LINE__) ; sf_close (infile) ; if (sfinfo.frames != k * frames) { printf ("\n\nLine %d : Incorrect sample count (%ld should be %ld)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), SF_COUNT_TO_LONG (k + frames)) ; dump_log_buffer (infile) ; exit (1) ; } ; if ((k & 1) == 0) test_write_double_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ; else test_writef_double_or_die (outfile, k, buffer, frames, __LINE__) ; } ; sf_close (outfile) ; unlink (filename) ; puts ("ok") ; return ; } /* update_seek_double_test */
static void extra_header_test (const char *filename, int filetype) { SNDFILE *outfile ; SF_INFO sfinfo ; sf_count_t frames ; short buffer [8] ; int k = 0 ; print_test_name ("extra_header_test", filename) ; sfinfo.samplerate = 44100 ; sfinfo.format = (filetype | SF_FORMAT_PCM_16) ; sfinfo.channels = 1 ; memset (buffer, 0xA0, sizeof (buffer)) ; /* Now write some frames. */ frames = ARRAY_LEN (buffer) / sfinfo.channels ; /* Test the file with extra header data. */ outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, 464) ; sf_set_string (outfile, SF_STR_TITLE, filename) ; test_writef_short_or_die (outfile, k, buffer, frames, 466) ; sf_set_string (outfile, SF_STR_COPYRIGHT, "(c) 1980 Erik") ; sf_close (outfile) ; #if 1 /* ** Erik de Castro Lopo <*****@*****.**> May 23 2004. ** ** This file has extra string data in the header and therefore cannot ** currently be opened in SFM_RDWR mode. This is fixable, but its in ** a part of the code I don't want to fiddle with until the Ogg/Vorbis ** integration is done. */ if (sf_open (filename, SFM_RDWR, &sfinfo) != NULL) { printf ("\n\nError : should not be able to open this file in SFM_RDWR.\n\n") ; exit (1) ; } ; unlink (filename) ; puts ("ok") ; return ; #else hexdump_file (filename, 0, 100000) ; /* Open again for read/write. */ outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, 493) ; /* ** In auto header update mode, seeking to the end of the file with ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END ** will seek to 0 anyway */ if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0) { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ; exit (1) ; } ; /* Now write some frames. */ frames = ARRAY_LEN (buffer) / sfinfo.channels ; for (k = 1 ; k < 6 ; k++) { printf ("\n*** pass %d\n", k) ; memset (buffer, 0xA0 + k, sizeof (buffer)) ; test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, 514) ; test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, 515) ; /* Open file again and make sure no errors in log buffer. */ if (0) { infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, 519) ; check_log_buffer_or_die (infile, 520) ; sf_close (infile) ; } ; if (sfinfo.frames != k * frames) { printf ("\n\nLine %d : Incorrect sample count (%ld should be %ld)\n", 525, SF_COUNT_TO_LONG (sfinfo.frames), SF_COUNT_TO_LONG (k + frames)) ; dump_log_buffer (infile) ; exit (1) ; } ; if ((k & 1) == 0) test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, 531) ; else test_writef_short_or_die (outfile, k, buffer, frames, 533) ; hexdump_file (filename, 0, 100000) ; } ; sf_close (outfile) ; unlink (filename) ; puts ("ok") ; return ; #endif } /* extra_header_test */
static void calc_peak_test (int filetype, const char *filename) { SNDFILE *file ; SF_INFO sfinfo ; int k, format ; double peak ; print_test_name ("calc_peak_test", filename) ; format = (filetype | SF_FORMAT_PCM_16) ; sfinfo.samplerate = 44100 ; sfinfo.format = format ; sfinfo.channels = 1 ; sfinfo.frames = BUFFER_LEN ; /* Create double_data with max value of 0.5. */ for (k = 0 ; k < BUFFER_LEN ; k++) double_data [k] = (k + 1) / (2.0 * BUFFER_LEN) ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ; sf_close (file) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; if (sfinfo.format != format) { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ; exit (1) ; } ; if (sfinfo.frames != BUFFER_LEN) { printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ; exit (1) ; } ; sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ; if (fabs (peak - (1 << 14)) > 1.0) { printf ("Line %d : Peak value should be %d (is %f).\n", __LINE__, (1 << 14), peak) ; exit (1) ; } ; sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ; if (fabs (peak - 0.5) > 4e-5) { printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ; exit (1) ; } ; sf_close (file) ; format = (filetype | SF_FORMAT_FLOAT) ; sfinfo.samplerate = 44100 ; sfinfo.format = format ; sfinfo.channels = 1 ; sfinfo.frames = BUFFER_LEN ; /* Create double_data with max value of 0.5. */ for (k = 0 ; k < BUFFER_LEN ; k++) double_data [k] = (k + 1) / (2.0 * BUFFER_LEN) ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ; sf_close (file) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; if (sfinfo.format != format) { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ; exit (1) ; } ; if (sfinfo.frames != BUFFER_LEN) { printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ; exit (1) ; } ; sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ; if (fabs (peak - 0.5) > 1e-5) { printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ; exit (1) ; } ; sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ; if (fabs (peak - 0.5) > 1e-5) { printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ; exit (1) ; } ; sf_close (file) ; unlink (filename) ; printf ("ok\n") ; } /* calc_peak_test */
static void update_header_sub (const char *filename, int typemajor, int write_mode) { SNDFILE *outfile, *infile ; SF_INFO sfinfo ; int k, frames ; sfinfo.samplerate = 44100 ; sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ; sfinfo.channels = 1 ; sfinfo.frames = 0 ; frames = BUFFER_LEN / sfinfo.channels ; outfile = test_open_file_or_die (filename, write_mode, &sfinfo, SF_TRUE, __LINE__) ; for (k = 0 ; k < BUFFER_LEN ; k++) data_out [k] = k + 1 ; test_write_int_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ; if (typemajor != SF_FORMAT_HTK) { /* The HTK header is not correct when the file is first written. */ infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; sf_close (infile) ; } ; sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, 0) ; /* ** Open file and check log buffer for an error. If header update failed ** the the log buffer will contain errors. */ infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; check_log_buffer_or_die (infile, __LINE__) ; if (sfinfo.frames < BUFFER_LEN || sfinfo.frames > BUFFER_LEN + 50) { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), BUFFER_LEN) ; dump_log_buffer (infile) ; exit (1) ; } ; test_read_int_or_die (infile, 0, data_in, BUFFER_LEN, __LINE__) ; for (k = 0 ; k < BUFFER_LEN ; k++) if (data_out [k] != k + 1) printf ("Error : line %d\n", __LINE__) ; sf_close (infile) ; /* Set auto update on. */ sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ; /* Write more data_out. */ for (k = 0 ; k < BUFFER_LEN ; k++) data_out [k] = k + 2 ; test_write_int_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ; /* Open file again and make sure no errors in log buffer. */ infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; check_log_buffer_or_die (infile, __LINE__) ; if (sfinfo.frames < 2 * BUFFER_LEN || sfinfo.frames > 2 * BUFFER_LEN + 50) { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * BUFFER_LEN) ; dump_log_buffer (infile) ; exit (1) ; } ; sf_close (infile) ; sf_close (outfile) ; unlink (filename) ; } /* update_header_sub */
static void multi_file_test (const char *filename, int *formats, int format_count) { SNDFILE *sndfile ; SF_INFO sfinfo ; SF_EMBED_FILE_INFO embed_info ; sf_count_t filelen ; int fd, k, file_count = 0 ; print_test_name ("multi_file_test", filename) ; unlink (filename) ; if ((fd = open (filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) { printf ("\n\nLine %d: open failed : %s\n", __LINE__, strerror (errno)) ; exit (1) ; } ; k = write (fd, "1234", 4) ; for (k = 0 ; k < format_count ; k++) write_file_at_end (fd, formats [k], 2, k) ; filelen = file_length_fd (fd) ; embed_info.offset = 4 ; embed_info.length = 0 ; for (file_count = 1 ; embed_info.offset + embed_info.length < filelen ; file_count ++) { if (verbose) { puts ("\n------------------------------------") ; printf ("This offset : %ld\n", SF_COUNT_TO_LONG (embed_info.offset + embed_info.length)) ; } ; if (lseek (fd, embed_info.offset + embed_info.length, SEEK_SET) < 0) { printf ("\n\nLine %d: lseek failed : %s\n", __LINE__, strerror (errno)) ; exit (1) ; } ; memset (&sfinfo, 0, sizeof (sfinfo)) ; if ((sndfile = sf_open_fd (fd, SFM_READ, &sfinfo, SF_FALSE)) == NULL) { printf ("\n\nLine %d: sf_open_fd failed\n", __LINE__) ; printf ("Embedded file number : %d offset : %ld\n", file_count, SF_COUNT_TO_LONG (embed_info.offset)) ; puts (sf_strerror (sndfile)) ; dump_log_buffer (sndfile) ; exit (1) ; } ; sf_command (sndfile, SFC_GET_EMBED_FILE_INFO, &embed_info, sizeof (embed_info)) ; sf_close (sndfile) ; if (verbose) printf ("\nNext offset : %ld\nNext length : %ld\n", SF_COUNT_TO_LONG (embed_info.offset), SF_COUNT_TO_LONG (embed_info.length)) ; } ; file_count -- ; if (file_count != format_count) { printf ("\n\nLine %d: file count (%d) not equal to %d.\n\n", __LINE__, file_count, format_count) ; printf ("Embedded file number : %d\n", file_count) ; exit (1) ; } ; close (fd) ; unlink (filename) ; printf ("ok\n") ; return ; } /* multi_file_test */
static void double_norm_test (const char *filename) { SNDFILE *file ; SF_INFO sfinfo ; unsigned int k ; print_test_name ("double_norm_test", filename) ; sfinfo.samplerate = 44100 ; sfinfo.format = (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ; sfinfo.channels = 1 ; sfinfo.frames = BUFFER_LEN ; /* Create double_data with all values being less than 1.0. */ for (k = 0 ; k < BUFFER_LEN / 2 ; k++) double_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ; for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++) double_data [k] = (k + 5) ; if (! (file = sf_open (filename, SFM_WRITE, &sfinfo))) { printf ("Line %d: sf_open_write failed with error : ", __LINE__) ; fflush (stdout) ; puts (sf_strerror (NULL)) ; exit (1) ; } ; /* Normailsation is on by default so no need to do anything here. */ /*-sf_command (file, "set-norm-double", "true", 0) ;-*/ if ((k = sf_write_double (file, double_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2) { printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; /* Turn normalisation off. */ sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; if ((k = sf_write_double (file, double_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2) { printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; sf_close (file) ; if (! (file = sf_open (filename, SFM_READ, &sfinfo))) { printf ("Line %d: sf_open_read failed with error : ", __LINE__) ; fflush (stdout) ; puts (sf_strerror (NULL)) ; exit (1) ; } ; if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16)) { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ; exit (1) ; } ; if (sfinfo.frames != BUFFER_LEN) { printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ; exit (1) ; } ; /* Read double_data and check that it is normalised (ie default). */ if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN) { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; for (k = 0 ; k < BUFFER_LEN ; k++) if (double_data [k] >= 1.0) { printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ; exit (1) ; } ; /* Seek to start of file, turn normalisation off, read double_data and check again. */ sf_seek (file, 0, SEEK_SET) ; sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN) { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; for (k = 0 ; k < BUFFER_LEN ; k++) if (double_data [k] < 1.0) { printf ("\n\nLine %d: double_data [%d] == %f which is less than 1.0\n", __LINE__, k, double_data [k]) ; exit (1) ; } ; /* Seek to start of file, turn normalisation on, read double_data and do final check. */ sf_seek (file, 0, SEEK_SET) ; sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_TRUE) ; if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN) { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ; exit (1) ; } ; for (k = 0 ; k < BUFFER_LEN ; k++) if (double_data [k] > 1.0) { printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ; exit (1) ; } ; sf_close (file) ; unlink (filename) ; printf ("ok\n") ; } /* double_norm_test */
static void double_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) { SNDFILE *file ; SF_INFO sfinfo ; double snr ; print_test_name ("double_scaled_test", filename) ; gen_windowed_sine (orig_data, DFT_DATA_LENGTH, 0.95) ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = DFT_DATA_LENGTH ; sfinfo.channels = 1 ; sfinfo.format = filetype ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, __LINE__) ; sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ; test_write_double_or_die (file, 0, orig_data, DFT_DATA_LENGTH, __LINE__) ; sf_close (file) ; memset (test_data, 0, sizeof (test_data)) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, __LINE__) ; sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ; 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 < DFT_DATA_LENGTH) { printf ("\n\nLine %d: Incorrect number of.frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; exit (1) ; } ; check_log_buffer_or_die (file, __LINE__) ; test_read_double_or_die (file, 0, test_data, DFT_DATA_LENGTH, __LINE__) ; sf_close (file) ; snr = dft_cmp (__LINE__, orig_data, test_data, DFT_DATA_LENGTH, target_snr, allow_exit) ; if (snr < target_snr) printf ("% 6.1fdB SNR ... ok\n", snr) ; unlink (filename) ; return ; } /* double_scaled_test */
static void lcomp_test_int (const char *str, const char *filename, int filetype, double margin) { SNDFILE *file ; SF_INFO sfinfo ; int k, m, *orig, *data, sum_abs ; long datalen, seekpos ; double scale ; printf ("\nThis is program is not part of the libsndfile test suite.\n\n") ; printf (" lcomp_test_int : %s ... ", str) ; fflush (stdout) ; datalen = BUFFER_SIZE ; scale = 1.0 * 0x10000 ; data = data_buffer.i ; orig = orig_buffer.i ; gen_signal_double (orig_buffer.d, 32000.0 * scale, datalen) ; for (k = 0 ; k < datalen ; k++) orig [k] = orig_buffer.d [k] ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = 123456789 ; /* Ridiculous value. */ sfinfo.channels = 1 ; sfinfo.format = filetype ; if (! (file = sf_open (filename, SFM_WRITE, &sfinfo))) { printf ("sf_open_write failed with error : ") ; puts (sf_strerror (NULL)) ; exit (1) ; } ; if ((k = sf_writef_int (file, orig, datalen)) != datalen) { printf ("sf_writef_int failed with short write (%ld => %d).\n", datalen, k) ; exit (1) ; } ; sf_close (file) ; memset (data, 0, datalen * sizeof (int)) ; if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW) memset (&sfinfo, 0, sizeof (sfinfo)) ; if (! (file = sf_open (filename, SFM_READ, &sfinfo))) { printf ("sf_open_read failed with error : ") ; puts (sf_strerror (NULL)) ; exit (1) ; } ; if ((sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ; exit (1) ; } ; if (sfinfo.frames < datalen) { printf ("Too few.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ; exit (1) ; } ; if (sfinfo.frames > (datalen + datalen / 2)) { printf ("Too many.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("Incorrect number of channels in file.\n") ; exit (1) ; } ; check_log_buffer_or_die (file, __LINE__) ; if ((k = sf_readf_int (file, data, datalen)) != datalen) { printf ("Line %d: short read (%d should be %ld).\n", __LINE__, k, datalen) ; exit (1) ; } ; sum_abs = 0 ; for (k = 0 ; k < datalen ; k++) { if (error_function (data [k] / scale, orig [k] / scale, margin)) { printf ("Line %d: Incorrect sample (#%d : %f should be %f).\n", __LINE__, k, data [k] / scale, orig [k] / scale) ; oct_save_int (orig, data, datalen) ; exit (1) ; } ; sum_abs = abs (sum_abs + abs (data [k])) ; } ; if (sum_abs < 1.0) { printf ("Line %d: Signal is all zeros.\n", __LINE__) ; exit (1) ; } ; if ((k = sf_readf_int (file, data, datalen)) != sfinfo.frames - datalen) { printf ("Line %d: Incorrect read length (%ld should be %d).\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames - datalen), k) ; exit (1) ; } ; /* This check is only for block based encoders which must append silence ** to the end of a file so as to fill out a block. */ if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM) for (k = 0 ; k < sfinfo.frames - datalen ; k++) if (abs (data [k] / scale) > decay_response (k)) { printf ("Line %d : Incorrect sample B (#%d : abs (%d) should be < %d).\n", __LINE__, k, data [k], decay_response (k)) ; exit (1) ; } ; if (! sfinfo.seekable) { printf ("ok\n") ; return ; } ; /* Now test sf_seek function. */ if ((k = sf_seek (file, 0, SEEK_SET)) != 0) { printf ("Line %d: Seek to start of file failed (%d).\n", __LINE__, k) ; exit (1) ; } ; for (m = 0 ; m < 3 ; m++) { int n ; if ((k = sf_readf_int (file, data, 11)) != 11) { printf ("Line %d: Incorrect read length (11 => %d).\n", __LINE__, k) ; exit (1) ; } ; for (k = 0 ; k < 11 ; k++) if (error_function (data [k] / scale, orig [k + m * 11] / scale, margin)) { printf ("Line %d: Incorrect sample (m = %d) (#%d : %d => %d).\n", __LINE__, m, k + m * 11, orig [k + m * 11], data [k]) ; for (n = 0 ; n < 1 ; n++) printf ("%d ", data [n]) ; printf ("\n") ; exit (1) ; } ; } ; seekpos = BUFFER_SIZE / 10 ; /* Check seek from start of file. */ if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos) { printf ("Seek to start of file + %ld failed (%d).\n", seekpos, k) ; exit (1) ; } ; if ((k = sf_readf_int (file, data, 1)) != 1) { printf ("Line %d: sf_readf_int (file, data, 1) returned %d.\n", __LINE__, k) ; exit (1) ; } ; if (error_function ((double) data [0], (double) orig [seekpos], margin)) { printf ("Line %d: sf_seek (SEEK_SET) followed by sf_readf_int failed (%d, %d).\n", __LINE__, orig [1], data [0]) ; exit (1) ; } ; if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) { printf ("Line %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %ld)\n", __LINE__, k, seekpos + 1) ; exit (1) ; } ; seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; sf_readf_int (file, data, 1) ; if (error_function ((double) data [0], (double) orig [seekpos], margin) || k != seekpos) { printf ("Line %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %ld).\n", __LINE__, data [0], orig [seekpos], k, seekpos + 1) ; exit (1) ; } ; seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ; /* Check seek backward from current position. */ k = sf_seek (file, -20, SEEK_CUR) ; sf_readf_int (file, data, 1) ; if (error_function ((double) data [0], (double) orig [seekpos], margin) || k != seekpos) { printf ("sf_seek (backwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %ld).\n", data [0], orig [seekpos], k, seekpos) ; exit (1) ; } ; /* Check that read past end of file returns number of items. */ sf_seek (file, (int) sfinfo.frames, SEEK_SET) ; if ((k = sf_readf_int (file, data, datalen)) != 0) { printf ("Line %d: Return value from sf_readf_int past end of file incorrect (%d).\n", __LINE__, k) ; exit (1) ; } ; /* Check seek backward from end. */ if ((k = sf_seek (file, 5 - (int) sfinfo.frames, SEEK_END)) != 5) { printf ("sf_seek (SEEK_END) returned %d instead of %d.\n", k, 5) ; exit (1) ; } ; sf_readf_int (file, data, 1) ; if (error_function (data [0] / scale, orig [5] / scale, margin)) { printf ("Line %d: sf_seek (SEEK_END) followed by sf_readf_short failed (%d should be %d).\n", __LINE__, data [0], orig [5]) ; exit (1) ; } ; sf_close (file) ; printf ("ok\n") ; } /* lcomp_test_int */
static void float_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) { SNDFILE *file ; SF_INFO sfinfo ; double snr ; print_test_name ("float_scaled_test", filename) ; gen_windowed_sine_float (float_data, DFT_DATA_LENGTH, 1.0) ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = DFT_DATA_LENGTH ; sfinfo.channels = 1 ; sfinfo.format = filetype ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ; test_write_float_or_die (file, 0, float_data, DFT_DATA_LENGTH, __LINE__) ; sf_close (file) ; memset (float_test, 0, sizeof (float_test)) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ; exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ; exit_if_true (sfinfo.frames < DFT_DATA_LENGTH, "\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ; exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; check_log_buffer_or_die (file, __LINE__) ; test_read_float_or_die (file, 0, float_test, DFT_DATA_LENGTH, __LINE__) ; sf_close (file) ; snr = dft_cmp_float (__LINE__, float_data, float_test, DFT_DATA_LENGTH, target_snr, allow_exit) ; exit_if_true (snr > target_snr, "% 6.1fdB SNR\n\n Error : should be better than % 6.1fdB\n\n", snr, target_snr) ; printf ("% 6.1fdB SNR ... ok\n", snr) ; unlink (filename) ; return ; } /* float_scaled_test */
static void double_int_big_test (const char * filename) { SNDFILE *file ; SF_INFO sfinfo ; unsigned k, max ; print_test_name ("double_int_big_test", filename) ; gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.98) ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = ARRAY_LEN (int_data) ; sfinfo.channels = 1 ; sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ; sf_close (file) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; if (sfinfo.frames != ARRAY_LEN (double_data)) { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; exit (1) ; } ; sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ; sf_close (file) ; max = 0 ; for (k = 0 ; k < ARRAY_LEN (int_data) ; k++) if ((unsigned) abs (int_data [k]) > max) max = abs (int_data [k]) ; if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01) { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ; exit (1) ; } ; unlink (filename) ; puts ("ok") ; } /* double_int_big_test */
static void update_header_test (char *filename, int typemajor) { SNDFILE *outfile, *infile ; SF_INFO sfinfo ; int k, frames ; printf (" update_header_test : %s ", filename) ; fflush (stdout) ; k = abs (18 - strlen (filename)) ; PUT_DOTS (k) ; sfinfo.samplerate = 44100 ; sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ; sfinfo.channels = 1 ; sfinfo.frames = 0 ; frames = BUFFER_LEN / sfinfo.channels ; outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, __LINE__) ; for (k = 0 ; k < BUFFER_LEN ; k++) data_out [k] = k + 1 ; test_write_short_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ; infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, __LINE__) ; sf_close (infile) ; sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, 0) ; /* Open file and check log buffer for an error. If header update failed ** the the log buffer will contain errors. */ infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, __LINE__) ; check_log_buffer_or_die (infile) ; if (abs (BUFFER_LEN - sfinfo.frames) > 1) { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), BUFFER_LEN) ; dump_log_buffer (infile) ; exit (1) ; } ; test_read_short_or_die (infile, 0, data_in, BUFFER_LEN, __LINE__) ; for (k = 0 ; k < BUFFER_LEN ; k++) if (data_out [k] != k + 1) printf ("Error : line %d\n", __LINE__) ; sf_close (infile) ; /* Set auto update on. */ sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ; /* Write more data_out. */ for (k = 0 ; k < BUFFER_LEN ; k++) data_out [k] = k + 2 ; test_write_short_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ; /* Open file again and make sure no errors in log buffer. */ infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, __LINE__) ; check_log_buffer_or_die (infile) ; if (abs (2 * BUFFER_LEN - sfinfo.frames) > 1) { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * BUFFER_LEN) ; dump_log_buffer (infile) ; exit (1) ; } ; sf_close (infile) ; sf_close (outfile) ; unlink (filename) ; puts ("ok") ; } /* update_header_test */