static void ogg_int_test (void) { const char * filename = "vorbis_int.oga" ; SNDFILE * file ; SF_INFO sfinfo ; int seek_data [10] ; unsigned k ; print_test_name ("ogg_int_test", filename) ; /* Generate float data. */ gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 1.0 * 0x7FFF0000) ; /* Convert to integer. */ for (k = 0 ; k < ARRAY_LEN (data_out.i) ; k++) data_out.i [k] = lrintf (data_out.f [k]) ; memset (&sfinfo, 0, sizeof (sfinfo)) ; /* Set up output file type. */ sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ; sfinfo.channels = 1 ; sfinfo.samplerate = SAMPLE_RATE ; /* Write the output file. */ file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; test_write_int_or_die (file, 0, data_out.i, ARRAY_LEN (data_out.i), __LINE__) ; sf_close (file) ; /* Read the file in again. */ memset (&sfinfo, 0, sizeof (sfinfo)) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; test_read_int_or_die (file, 0, data_in.i, ARRAY_LEN (data_in.i), __LINE__) ; sf_close (file) ; puts ("ok") ; /* Test seeking. */ print_test_name ("ogg_seek_test", filename) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ; test_read_int_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ; compare_int_or_die (seek_data, data_in.i + 10, ARRAY_LEN (seek_data), __LINE__) ; sf_close (file) ; puts ("ok") ; unlink (filename) ; } /* ogg_int_test */
static void rdwr_int_test (const char *filename) { SNDFILE *file ; SF_INFO sfinfo ; sf_count_t frames ; int buffer [160] ; print_test_name ("rdwr_int_test", filename) ; memset (buffer, 0, sizeof (buffer)) ; /* Create sound file with no data. */ sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_32 ; sfinfo.samplerate = 16000 ; sfinfo.channels = 1 ; unlink (filename) ; frames = ARRAY_LEN (buffer) ; /* Open again for read/write. */ file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; test_write_int_or_die (file, 0, buffer, frames, __LINE__) ; test_read_int_or_die (file, 0, buffer, frames, __LINE__) ; sf_close (file) ; unlink (filename) ; puts ("ok") ; return ; } /* rdwr_int_test */
static void rf64_long_file_downgrade_test (const char *filename) { static int output [BUFFER_LEN] ; static int input [1] = { 0 } ; SNDFILE *file ; SF_INFO sfinfo ; sf_count_t output_frames = 0 ; print_test_name (__func__, filename) ; sf_info_clear (&sfinfo) ; memset (output, 0, sizeof (output)) ; output [0] = 0x1020304 ; sfinfo.samplerate = 44100 ; sfinfo.frames = ARRAY_LEN (output) ; sfinfo.channels = 1 ; sfinfo.format = SF_FORMAT_RF64 | SF_FORMAT_PCM_32 ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_TRUE) != SF_TRUE, "\n\nLine %d: sf_command failed.\n", __LINE__) ; while (output_frames * sizeof (output [0]) < 0x100000000) { test_write_int_or_die (file, 0, output, ARRAY_LEN (output), __LINE__) ; output_frames += ARRAY_LEN (output) ; } ; sf_close (file) ; sf_info_clear (&sfinfo) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; exit_if_true (sfinfo.format != (SF_FORMAT_RF64 | SF_FORMAT_PCM_32), "\n\nLine %d: RF64 to WAV downgrade should have failed.\n", __LINE__) ; exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ; exit_if_true (sfinfo.frames != output_frames, "\n\nLine %d: Incorrect number of frames in file (%d should be %d).\n", __LINE__, (int) sfinfo.frames, (int) output_frames) ; /* Check that the first sample read is the same as the first written. */ test_read_int_or_die (file, 0, input, ARRAY_LEN (input), __LINE__) ; exit_if_true (input [0] != output [0], "\n\nLine %d: Bad first sample (0x%08x).\n", __LINE__, input [0]) ; check_log_buffer_or_die (file, __LINE__) ; sf_close (file) ; puts ("ok") ; unlink (filename) ; return ; } /* rf64_long_file_downgrade_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.9999) ; 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). (%" PRId64 " should be %d)\n", __LINE__, 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 (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_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 */