static void ogg_short_test (void) { const char * filename = "vorbis_short.oga" ; SNDFILE * file ; SF_INFO sfinfo ; short seek_data [10] ; unsigned k ; print_test_name ("ogg_short_test", filename) ; /* Generate float data. */ gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 1.0 * 0x7F00) ; /* Convert to shorteger. */ for (k = 0 ; k < ARRAY_LEN (data_out.s) ; k++) data_out.s [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_short_or_die (file, 0, data_out.s, ARRAY_LEN (data_out.s), __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_short_or_die (file, 0, data_in.s, ARRAY_LEN (data_in.s), __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_short_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ; compare_short_or_die (seek_data, data_in.s + 10, ARRAY_LEN (seek_data), __LINE__) ; sf_close (file) ; puts ("ok") ; unlink (filename) ; } /* ogg_short_test */
static void rdwr_short_test (const char *filename) { SNDFILE *file ; SF_INFO sfinfo ; sf_count_t frames ; short buffer [160] ; print_test_name ("rdwr_short_test", filename) ; memset (buffer, 0, sizeof (buffer)) ; /* Create sound file with no data. */ sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ; 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_short_or_die (file, 0, buffer, frames, __LINE__) ; test_read_short_or_die (file, 0, buffer, frames, __LINE__) ; sf_close (file) ; unlink (filename) ; puts ("ok") ; return ; } /* rdwr_short_test */
static void rf64_downgrade_test (const char *filename) { static short output [BUFFER_LEN] ; static short input [BUFFER_LEN] ; SNDFILE *file ; SF_INFO sfinfo ; unsigned k ; print_test_name (__func__, filename) ; sf_info_clear (&sfinfo) ; sfinfo.samplerate = 44100 ; sfinfo.frames = ARRAY_LEN (output) ; sfinfo.channels = 1 ; sfinfo.format = SF_FORMAT_RF64 | SF_FORMAT_PCM_16 ; 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_FALSE) != SF_FALSE, "\n\nLine %d: sf_command failed.\n", __LINE__) ; exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_TRUE) != SF_TRUE, "\n\nLine %d: sf_command failed.\n", __LINE__) ; test_write_short_or_die (file, 0, output, ARRAY_LEN (output), __LINE__) ; exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_FALSE) != SF_TRUE, "\n\nLine %d: sf_command failed.\n", __LINE__) ; exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_TRUE) != SF_TRUE, "\n\nLine %d: sf_command failed.\n", __LINE__) ; sf_close (file) ; memset (input, 0, sizeof (input)) ; sf_info_clear (&sfinfo) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; exit_if_true (sfinfo.format != (SF_FORMAT_WAVEX | SF_FORMAT_PCM_16), "\n\nLine %d: RF64 to WAV downgrade failed.\n", __LINE__) ; exit_if_true (sfinfo.frames != ARRAY_LEN (output), "\n\nLine %d: Incorrect number of frames in file (too short). (%d should be %d)\n", __LINE__, (int) sfinfo.frames, (int) ARRAY_LEN (output)) ; 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_short_or_die (file, 0, input, ARRAY_LEN (input), __LINE__) ; sf_close (file) ; for (k = 0 ; k < ARRAY_LEN (input) ; k++) exit_if_true (input [k] != output [k], "\n\nLine: %d: Error on input %d, expected %d, got %d\n", __LINE__, k, output [k], input [k]) ; puts ("ok") ; unlink (filename) ; return ; } /* rf64_downgrade_test */
static void checksum_test (const CHECKSUM * cksum) { SNDFILE * file ; SF_INFO info ; print_test_name (__func__, cksum->enc_name) ; memset (&info, 0, sizeof (SF_INFO)) ; info.format = cksum->enc_fmt ; info.channels = 1 ; info.samplerate = SAMPLE_RATE ; file = test_open_file_or_die (cksum->enc_name, SFM_WRITE, &info, 0, __LINE__) ; test_write_float_or_die (file, 0, orig, ARRAY_LEN (orig), __LINE__) ; sf_close (file) ; check_file_hash_or_die (cksum->enc_name, cksum->enc_cksum, __LINE__) ; puts ("ok") ; /*------------------------------------------------------------------------*/ print_test_name (__func__, cksum->dec_name) ; memset (&info, 0, sizeof (SF_INFO)) ; info.format = cksum->enc_fmt ; info.channels = 1 ; info.samplerate = SAMPLE_RATE ; file = test_open_file_or_die (cksum->enc_name, SFM_READ, &info, 0, __LINE__) ; test_read_short_or_die (file, 0, data, ARRAY_LEN (data), __LINE__) ; sf_close (file) ; memset (&info, 0, sizeof (SF_INFO)) ; info.format = SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16 ; info.channels = 1 ; info.samplerate = SAMPLE_RATE ; file = test_open_file_or_die (cksum->dec_name, SFM_WRITE, &info, 0, __LINE__) ; test_write_short_or_die (file, 0, data, ARRAY_LEN (data), __LINE__) ; sf_close (file) ; check_file_hash_or_die (cksum->dec_name, cksum->dec_cksum, __LINE__) ; remove (cksum->enc_name) ; remove (cksum->dec_name) ; puts ("ok") ; } /* checksum_test */
static void double_short_big_test (const char * filename) { SNDFILE *file ; SF_INFO sfinfo ; unsigned k, max ; print_test_name ("double_short_big_test", filename) ; gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.9999) ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = ARRAY_LEN (short_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_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ; sf_close (file) ; max = 0 ; for (k = 0 ; k < ARRAY_LEN (short_data) ; k++) if (abs (short_data [k]) > max) max = abs (short_data [k]) ; if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01) { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ; exit (1) ; } ; unlink (filename) ; puts ("ok") ; } /* double_short_big_test */
static void locale_test (const LOCALE_DATA * ldata) { #if !HAVE_LOCALE_H || !HAVE_SETLOCALE locname = filename = NULL ; width = 0 ; return ; #else const short wdata [] = { 1, 2, 3, 4, 5, 6, 7, 8 } ; short rdata [ARRAY_LEN (wdata)] ; const char *old_locale ; char utf8_locname [32] ; SNDFILE *file ; SF_INFO sfinfo ; snprintf (utf8_locname, sizeof (utf8_locname), "%s%s", ldata->locale, ldata->utf8 ? ".UTF-8" : "") ; /* Change the locale saving the old one. */ if ((old_locale = setlocale (LC_CTYPE, utf8_locname)) == NULL) return ; printf (" locale_test %-8s : %s %*c ", ldata->locale, ldata->filename, 24 - ldata->width, ' ') ; fflush (stdout) ; sfinfo.format = SF_FORMAT_AU | SF_FORMAT_PCM_16 ; sfinfo.channels = 1 ; sfinfo.samplerate = 44100 ; file = test_open_file_or_die (ldata->filename, SFM_WRITE, &sfinfo, 0, __LINE__) ; test_write_short_or_die (file, 0, wdata, ARRAY_LEN (wdata), __LINE__) ; sf_close (file) ; file = test_open_file_or_die (ldata->filename, SFM_READ, &sfinfo, 0, __LINE__) ; test_read_short_or_die (file, 0, rdata, ARRAY_LEN (rdata), __LINE__) ; sf_close (file) ; unlink (ldata->filename) ; /* Restore old locale. */ setlocale (LC_CTYPE, old_locale) ; puts ("ok") ; #endif } /* locale_test */
static void raw_offset_test (const char *filename, int typeminor) { SNDFILE *sndfile ; SF_INFO sfinfo ; sf_count_t start ; int k ; print_test_name ("raw_offset_test", filename) ; sfinfo.samplerate = 44100 ; sfinfo.format = SF_FORMAT_RAW | typeminor ; sfinfo.channels = 1 ; sfinfo.frames = 0 ; sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; start = 0 ; sf_command (sndfile, SFC_FILE_TRUNCATE, &start, sizeof (start)) ; for (k = 0 ; k < BUFFER_LEN ; k++) data [k] = k ; test_write_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ; sf_close (sndfile) ; sndfile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; check_log_buffer_or_die (sndfile, __LINE__) ; if (ABS (BUFFER_LEN - sfinfo.frames) > 1) { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, BUFFER_LEN) ; dump_log_buffer (sndfile) ; exit (1) ; } ; memset (data, 0 , sizeof (data)) ; test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ; for (k = 0 ; k < BUFFER_LEN ; k++) if (data [k] != k) printf ("Error : line %d\n", __LINE__) ; /* Set dataoffset to 2 bytes from beginning of file. */ start = 2 ; sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ; /* Seek to new start */ test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; memset (data, 0 , sizeof (data)) ; test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 1, __LINE__) ; for (k = 0 ; k < BUFFER_LEN - 1 ; k++) if (data [k] != k + 1) { printf ("Error : line %d\n", __LINE__) ; exit (1) ; } ; /* Set dataoffset to 4 bytes from beginning of file. */ start = 4 ; sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ; /* Seek to new start */ test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; memset (data, 0 , sizeof (data)) ; test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 2, __LINE__) ; for (k = 0 ; k < BUFFER_LEN - 2 ; k++) if (data [k] != k + 2) { printf ("Error : line %d\n", __LINE__) ; exit (1) ; } ; /* Set dataoffset back to 0 bytes from beginning of file. */ start = 0 ; sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ; /* Seek to new start */ test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; memset (data, 0 , sizeof (data)) ; test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ; for (k = 0 ; k < BUFFER_LEN ; k++) if (data [k] != k) { printf ("Error : line %d\n", __LINE__) ; exit (1) ; } ; sf_close (sndfile) ; unlink (filename) ; puts ("ok") ; } /* raw_offset_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 */