static void vorbis_quality_test (void) { /* ** Encode two files, one at quality 0.3 and one at quality 0.5 and then ** make sure that the quality 0.3 files is the smaller of the two. */ const char * q3_fname = "q3_vorbis.oga" ; const char * q5_fname = "q5_vorbis.oga" ; SNDFILE *q3_file, *q5_file ; SF_INFO sfinfo ; int q3_size, q5_size ; double quality ; int k ; print_test_name (__func__, "q[35]_vorbis.oga") ; 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. */ q3_file = test_open_file_or_die (q3_fname, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; q5_file = test_open_file_or_die (q5_fname, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; quality = 0.3 ; sf_command (q3_file, SFC_SET_VBR_ENCODING_QUALITY, &quality, sizeof (quality)) ; quality = 0.5 ; sf_command (q5_file, SFC_SET_VBR_ENCODING_QUALITY, &quality, sizeof (quality)) ; for (k = 0 ; k < 5 ; k++) { gen_lowpass_noise_float (data_out, ARRAY_LEN (data_out)) ; test_write_float_or_die (q3_file, 0, data_out, ARRAY_LEN (data_out), __LINE__) ; test_write_float_or_die (q5_file, 0, data_out, ARRAY_LEN (data_out), __LINE__) ; } ; sf_close (q3_file) ; sf_close (q5_file) ; q3_size = file_length (q3_fname) ; q5_size = file_length (q5_fname) ; if (q3_size >= q5_size) { printf ("\n\nLine %d : q3 size (%d) >= q5 size (%d)\n\n", __LINE__, q3_size, q5_size) ; exit (1) ; } ; puts ("ok") ; unlink (q3_fname) ; unlink (q5_fname) ; } /* vorbis_quality_test */
static void rdwr_float_test (const char *filename) { SNDFILE *file ; SF_INFO sfinfo ; sf_count_t frames ; float buffer [160] ; print_test_name ("rdwr_float_test", filename) ; memset (buffer, 0, sizeof (buffer)) ; /* Create sound file with no data. */ sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT ; 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_float_or_die (file, 0, buffer, frames, __LINE__) ; test_read_float_or_die (file, 0, buffer, frames, __LINE__) ; sf_close (file) ; unlink (filename) ; puts ("ok") ; return ; } /* rdwr_float_test */
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 ; int byterate ; print_test_name ("float_scaled_test", filename) ; gen_windowed_sine_float (float_data, DFT_DATA_LENGTH, 0.9999) ; 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). (%" PRId64 " should be %d)\n", __LINE__, 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__) ; byterate = sf_current_byterate (file) ; exit_if_true (byterate <= 0, "\n\nLine %d: byterate is zero.\n", __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 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 float_int_big_test (const char * filename) { SNDFILE *file ; SF_INFO sfinfo ; unsigned k, max ; print_test_name ("float_int_big_test", filename) ; gen_windowed_sine_float (float_data, ARRAY_LEN (float_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_FLOAT ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ; sf_close (file) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; if (sfinfo.frames != ARRAY_LEN (float_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") ; } /* float_int_big_test */
static void ogg_float_test (void) { const char * filename = "vorbis_float.oga" ; SNDFILE * file ; SF_INFO sfinfo ; float seek_data [10] ; print_test_name ("ogg_float_test", filename) ; gen_windowed_sine_float (data_out.f, ARRAY_LEN (data_out.f), 0.95) ; 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_float_or_die (file, 0, data_out.f, ARRAY_LEN (data_out.f), __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_float_or_die (file, 0, data_in.f, ARRAY_LEN (data_in.f), __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_float_or_die (file, 0, seek_data, ARRAY_LEN (seek_data), __LINE__) ; compare_float_or_die (seek_data, data_in.f + 10, ARRAY_LEN (seek_data), __LINE__) ; sf_close (file) ; puts ("ok") ; unlink (filename) ; } /* ogg_float_test */
static void largefile_test (int filetype, const char * filename) { static float data [BUFFER_LEN] ; SNDFILE *file ; SF_INFO sfinfo ; int k ; print_test_name ("largefile_test", filename) ; sfinfo.samplerate = 44100 ; sfinfo.channels = 2 ; sfinfo.frames = 0 ; sfinfo.format = (filetype | SF_FORMAT_PCM_32) ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; for (k = 0 ; k < BUFFER_COUNT ; k++) test_write_float_or_die (file, k, data, BUFFER_LEN, __LINE__) ; sf_close (file) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; if ((sfinfo.frames * sfinfo.channels) / BUFFER_LEN != BUFFER_COUNT) { printf ("\n\nLine %d : bad frame count.\n", __LINE__) ; exit (1) ; } ; sf_close (file) ; unlink (filename) ; puts ("ok") ; return ; } /* largefile_test */
static void update_seek_float_test (const char *filename, int filetype) { SNDFILE *outfile, *infile ; SF_INFO sfinfo ; sf_count_t frames ; float buffer [8] ; int k ; print_test_name ("update_seek_float_test", filename) ; memset (buffer, 0, sizeof (buffer)) ; /* Create sound outfile with no data. */ sfinfo.format = filetype | SF_FORMAT_FLOAT ; 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_float_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ; else test_writef_float_or_die (outfile, k, buffer, frames, __LINE__) ; } ; sf_close (outfile) ; unlink (filename) ; puts ("ok") ; return ; } /* update_seek_float_test */
static void ogg_stereo_seek_test (const char * filename, int format) { static float data [SAMPLE_RATE] ; static float stereo_out [SAMPLE_RATE * 2] ; SNDFILE * file ; SF_INFO sfinfo ; sf_count_t pos ; unsigned k ; print_test_name (__func__, filename) ; gen_windowed_sine_float (data, ARRAY_LEN (data), 0.95) ; for (k = 0 ; k < ARRAY_LEN (data) ; k++) { stereo_out [2 * k] = data [k] ; stereo_out [2 * k + 1] = data [ARRAY_LEN (data) - k - 1] ; } ; memset (&sfinfo, 0, sizeof (sfinfo)) ; /* Set up output file type. */ sfinfo.format = format ; sfinfo.channels = 2 ; sfinfo.samplerate = SAMPLE_RATE ; /* Write the output file. */ file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ; test_write_float_or_die (file, 0, stereo_out, ARRAY_LEN (stereo_out), __LINE__) ; sf_close (file) ; /* Open file in again for reading. */ memset (&sfinfo, 0, sizeof (sfinfo)) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; /* Read in the whole file. */ test_read_float_or_die (file, 0, stereo_out, ARRAY_LEN (stereo_out), __LINE__) ; /* Now hammer seeking code. */ test_seek_or_die (file, 234, SEEK_SET, 234, sfinfo.channels, __LINE__) ; test_readf_float_or_die (file, 0, data, 10, __LINE__) ; compare_float_or_die (data, stereo_out + (234 * sfinfo.channels), 10, __LINE__) ; test_seek_or_die (file, 442, SEEK_SET, 442, sfinfo.channels, __LINE__) ; test_readf_float_or_die (file, 0, data, 10, __LINE__) ; compare_float_or_die (data, stereo_out + (442 * sfinfo.channels), 10, __LINE__) ; test_seek_or_die (file, 12, SEEK_CUR, 442 + 10 + 12, sfinfo.channels, __LINE__) ; test_readf_float_or_die (file, 0, data, 10, __LINE__) ; compare_float_or_die (data, stereo_out + ((442 + 10 + 12) * sfinfo.channels), 10, __LINE__) ; test_seek_or_die (file, 12, SEEK_CUR, 442 + 20 + 24, sfinfo.channels, __LINE__) ; test_readf_float_or_die (file, 0, data, 10, __LINE__) ; compare_float_or_die (data, stereo_out + ((442 + 20 + 24) * sfinfo.channels), 10, __LINE__) ; pos = 500 - sfinfo.frames ; test_seek_or_die (file, pos, SEEK_END, 500, sfinfo.channels, __LINE__) ; test_readf_float_or_die (file, 0, data, 10, __LINE__) ; compare_float_or_die (data, stereo_out + (500 * sfinfo.channels), 10, __LINE__) ; pos = 10 - sfinfo.frames ; test_seek_or_die (file, pos, SEEK_END, 10, sfinfo.channels, __LINE__) ; test_readf_float_or_die (file, 0, data, 10, __LINE__) ; compare_float_or_die (data, stereo_out + (10 * sfinfo.channels), 10, __LINE__) ; sf_close (file) ; puts ("ok") ; unlink (filename) ; } /* ogg_stereo_seek_test */
static void float_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) { static float float_orig [DFT_DATA_LENGTH] ; static float float_test [DFT_DATA_LENGTH] ; SNDFILE *file ; SF_INFO sfinfo ; int k ; double snr ; print_test_name ("float_scaled_test", filename) ; gen_windowed_sine (orig_data, DFT_DATA_LENGTH, 1.0) ; for (k = 0 ; k < DFT_DATA_LENGTH ; k++) float_orig [k] = orig_data [k] ; 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_float_or_die (file, 0, float_orig, DFT_DATA_LENGTH, __LINE__) ; sf_close (file) ; memset (float_test, 0, sizeof (float_test)) ; 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_float_or_die (file, 0, float_test, DFT_DATA_LENGTH, __LINE__) ; sf_close (file) ; for (k = 0 ; k < DFT_DATA_LENGTH ; k++) test_data [k] = float_test [k] ; 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 ; } /* float_scaled_test */
static void vorbis_test (void) { static float float_data [DFT_DATA_LENGTH] ; const char * filename = "vorbis_test.oga" ; SNDFILE * file ; SF_INFO sfinfo ; float max_abs = 0.0 ; unsigned k ; print_test_name ("vorbis_test", filename) ; /* Generate float data. */ gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 1.0) ; /* Set up output file type. */ memset (&sfinfo, 0, sizeof (sfinfo)) ; sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ; sfinfo.channels = 1 ; sfinfo.samplerate = SAMPLE_RATE ; /* Write the output file. */ /* The Vorbis encoder has a bug on PowerPC and X86-64 with sample rates ** <= 22050. Increasing the sample rate to 32000 avoids triggering it. ** See https://trac.xiph.org/ticket/1229 */ if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL) { const char * errstr ; errstr = sf_strerror (NULL) ; if (strstr (errstr, "Sample rate chosen is known to trigger a Vorbis") == NULL) { printf ("Line %d: sf_open (SFM_WRITE) failed : %s\n", __LINE__, errstr) ; dump_log_buffer (NULL) ; exit (1) ; } ; printf ("\n Sample rate -> 32kHz ") ; sfinfo.samplerate = 32000 ; file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; } ; test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ; sf_close (file) ; memset (float_data, 0, sizeof (float_data)) ; /* Read the file back in again. */ memset (&sfinfo, 0, sizeof (sfinfo)) ; file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ; test_read_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ; sf_close (file) ; for (k = 0 ; k < ARRAY_LEN (float_data) ; k ++) max_abs = max_float (max_abs, fabs (float_data [k])) ; if (max_abs > 1.02) { printf ("\n\n Error : max_abs %f should be < 1.02.\n\n", max_abs) ; exit (1) ; } ; puts ("ok") ; unlink (filename) ; } /* vorbis_test */