📄 lossy_comp_test.c
字号:
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 (abs (data [k]) - 256)) ; } ; if (sum_abs < 1.0) { printf ("Line %d: Signal is all zeros (%d, 0x%X).\n", __LINE__, sum_abs, sum_abs) ; 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 (channels * 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 [channels * k] / scale) > decay_response (channels * k)) { printf ("Line %d : Incorrect sample B (#%d : abs (%d) should be < %d).\n", __LINE__, channels * k, data [channels * k], decay_response (channels * k)) ; exit (1) ; } ; if (! sfinfo.seekable) { unlink (filename) ; 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++) { 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 < channels * 11 ; k++) if (error_function (data [k] / scale, orig [k + channels * m * 11] / scale, margin)) { printf ("Line %d: Incorrect sample (m = %d) (#%d : %d => %d).\n", __LINE__, m, k + channels * m * 11, orig [k + channels * m * 11], data [k]) ; for (m = 0 ; m < channels ; m++) printf ("%d ", data [m]) ; 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 * channels], 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 * channels], 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 * channels], 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 * channels], margin) || k != seekpos) { printf ("sf_seek (backwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %ld).\n", data [0], orig [seekpos * channels], 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, channels) ; 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) ; unlink (filename) ; printf ("ok\n") ;} /* lcomp_test_int *//*--------------------------------------------------------------------------------------------*/static voidlcomp_test_float (const char *filename, int filetype, int channels, double margin){ SNDFILE *file ; SF_INFO sfinfo ; int k, m, seekpos, sum_abs ; long datalen ; float *orig, *data ; print_test_name ("lcomp_test_float", filename) ; datalen = BUFFER_SIZE / channels ; data = (float*) data_buffer ; orig = (float*) orig_buffer ; gen_signal_double (orig_buffer, 32000.0, channels, datalen) ; for (k = 0 ; k < channels * datalen ; k++) orig [k] = (float) (orig_buffer [k]) ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = 123456789 ; /* Ridiculous value. */ sfinfo.channels = channels ; sfinfo.format = filetype ; if (! (file = sf_open (filename, SFM_WRITE, &sfinfo))) { printf ("sf_open_write failed with error : ") ; puts (sf_strerror (NULL)) ; exit (1) ; } ; sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; if ((k = sf_writef_float (file, orig, datalen)) != datalen) { printf ("sf_writef_float failed with short write (%ld => %d).\n", datalen, k) ; exit (1) ; } ; sf_close (file) ; memset (data, 0, datalen * sizeof (float)) ; 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 / channels) { 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 != channels) { printf ("Incorrect number of channels in file.\n") ; exit (1) ; } ; sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; check_log_buffer_or_die (file, __LINE__) ; sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ; if ((k = sf_readf_float (file, data, datalen)) != datalen) { printf ("Line %d: short read (%d should be %ld).\n", __LINE__, k, datalen) ; exit (1) ; } ; sum_abs = 0.0 ; for (k = 0 ; k < datalen ; k++) { if (error_function ((double) data [k], (double) orig [k], margin)) { printf ("Line %d: Incorrect sample A (#%d : %f should be %f).\n", __LINE__, k, data [k], orig [k]) ; oct_save_float (orig, data, datalen) ; exit (1) ; } ; sum_abs += fabs (data [k]) ; } ; if (sum_abs < 1.0) { printf ("Line %d: Signal is all zeros.\n", __LINE__) ; exit (1) ; } ; if ((k = sf_readf_float (file, data, datalen)) != sfinfo.frames - datalen) { printf ("Line %d: Incorrect read length (%ld should be %d).\n", __LINE__, SF_COUNT_TO_LONG (channels * 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 [channels * k]) > decay_response (channels * k)) { printf ("Line %d : Incorrect sample B (#%d : abs (%f) should be < %d).\n", __LINE__, channels * k, data [channels * k], decay_response (channels * k)) ; exit (1) ; } ; if (! sfinfo.seekable) { unlink (filename) ; 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++) { if ((k = sf_readf_float (file, data, 11)) != 11) { printf ("Line %d: Incorrect read length (11 => %d).\n", __LINE__, k) ; exit (1) ; } ; for (k = 0 ; k < channels * 11 ; k++) if (error_function ((double) data [k], (double) orig [k + channels * m * 11], margin)) { printf ("Line %d: Incorrect sample (m = %d) (#%d : %f => %f).\n", __LINE__, m, k + channels * m * 11, orig [k + channels * m * 11], data [k]) ; for (m = 0 ; m < channels ; m++) printf ("%f ", data [m]) ; 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 + %d failed (%d).\n", seekpos, k) ; exit (1) ; } ; if ((k = sf_readf_float (file, data, 1)) != 1) { printf ("Line %d: sf_readf_float (file, data, 1) returned %d.\n", __LINE__, k) ; exit (1) ; } ; if (error_function ((double) data [0], (double) orig [seekpos * channels], margin)) { printf ("Line %d: sf_seek (SEEK_SET) followed by sf_readf_float failed (%f, %f).\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 %d)\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_float (file, data, 1) ; if (error_function ((double) data [0], (double) orig [seekpos * channels], margin) || k != seekpos) { printf ("Line %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_float failed (%f, %f) (%d, %d).\n", __LINE__, data [0], orig [seekpos * channels], 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_float (file, data, 1) ; if (error_function ((double) data [0], (double) orig [seekpos * channels], margin) || k != seekpos) { printf ("sf_seek (backwards, SEEK_CUR) followed by sf_readf_float failed (%f, %f) (%d, %d).\n", data [0], orig [seekpos * channels], k, seekpos) ; exit (1) ; } ; /* Check that read past end of file returns number of items. */ sf_seek (file, (float) sfinfo.frames, SEEK_SET) ; if ((k = sf_readf_float (file, data, datalen)) != 0) { printf ("Line %d: Return value from sf_readf_float past end of file incorrect (%d).\n", __LINE__, k) ; exit (1) ; } ; /* Check seek backward from end. */ if ((k = sf_seek (file, 5 - (float) sfinfo.frames, SEEK_END)) != 5) { printf ("sf_seek (SEEK_END) returned %d instead of %d.\n", k, 5) ; exit (1) ; } ; sf_readf_float (file, data, channels) ; if (error_function ((double) data [0], (double) orig [5], margin)) { printf ("Line %d: sf_seek (SEEK_END) followed by sf_readf_short failed (%f should be %f).\n", __LINE__, data [0], orig [5]) ; exit (1) ; } ; sf_close (file) ; unlink (filename) ; printf ("ok\n") ;} /* lcomp_test_float *//*--------------------------------------------------------------------------------------------*/static voidlcomp_test_double (const char *filename, int filetype, int channels, double margin){ SNDFILE *file ; SF_INFO sfinfo ; int k, m, seekpos, sum_abs ; long datalen ; double *orig, *data ; print_test_name ("lcomp_test_double", filename) ; datalen = BUFFER_SIZE / channels ; data = (double*) data_buffer ; orig = (double*) orig_buffer ; gen_signal_double (orig_buffer, 32000.0, channels, datalen) ; for (k = 0 ; k < channels * datalen ; k++) orig [k] = (double) (orig_buffer [k]) ; sfinfo.samplerate = SAMPLE_RATE ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -