📄 write_read_test.c
字号:
if (data [k] != k * ((k % 2) ? 1 : -1)) { printf ("Mono : Incorrect sample (#%d : %d => %d).\n", k, k * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; } ; /* Seek to start of file. */ if ((k = sf_seek (file, 0, SEEK_SET) != 0)) { printf ("Mono : sf_seek (file, 0, SEEK_SET) failed.\n") ; exit (1) ; } ; sf_read_short (file, data, 4) ; for (k = 0 ; k < 4 ; k++) if (data [k] != k * ((k % 2) ? 1 : -1)) { printf ("Mono : Incorrect sample A (#%d : %d => %d).\n", k, k * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from start of file. */ if ((k = sf_seek (file, 10, SEEK_SET) != 10)) { printf ("Mono : sf_seek (file, 10, SEEK_SET) failed. XXXXX\n") ; exit (1) ; } ; sf_read_short (file, data + 10, 4) ; for (k = 10 ; k < 14 ; k++) if (data [k] != k * ((k % 2) ? 1 : -1)) { printf ("Mono : Incorrect sample B (#%d : %d => %d).\n", k, k * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from current position. */ if ((k = sf_seek (file, 6, SEEK_CUR) != 20)) { printf ("Mono : sf_seek (file, 7, SEEK_CUR) failed.\n") ; exit (1) ; } ; sf_read_short (file, data + 20, 4) ; for (k = 20 ; k < 24 ; k++) if (data [k] != k * ((k % 2) ? 1 : -1)) { printf ("Mono : Incorrect sample C (#%d : %d => %d).\n", k, k * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from end of file. */ if ((k = sf_seek (file, -(sfinfo.samples - 10), SEEK_END) != 10)) { printf ("Mono : sf_seek (file, -(sfinfo.samples - 10), SEEK_END) failed (%d).\n", k) ; exit (1) ; } ; sf_read_short (file, data + 10, 4) ; for (k = 10 ; k < 14 ; k++) if (data [k] != k * ((k % 2) ? 1 : -1)) { printf ("Mono : Incorrect sample D (#%d : %d => %d).\n", k, k * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; sf_close (file) ; /* Now test Stereo. */ if (typemajor == SF_FORMAT_SVX) { printf ("ok\n") ; return ; } ; items = BUFFER_SIZE / sizeof (short) ; data = (short*) test_buffer ; for (k = 0 ; k < items ; k++) data [k] = k * ((k % 2) ? 1 : -1) ; sfinfo.samplerate = 44100 ; sfinfo.samples = items ; sfinfo.channels = 2 ; sfinfo.pcmbitwidth = 16 ; sfinfo.format = (typemajor | typeminor) ; frames = items / sfinfo.channels ; if (! (file = sf_open_write (filename, &sfinfo))) { printf ("Stereo : sf_open_write failed with error : ") ; sf_perror (NULL) ; exit (1) ; } ; if ((k = sf_writef_short (file, data, frames)) != frames) { printf ("Stereo : sf_writef_short failed with short write (%d => %d).\n", frames, k) ; sf_perror (file) ; exit (1) ; } ; sf_close (file) ; memset (data, 0, items * sizeof (short)) ; if (typemajor != SF_FORMAT_RAW) memset (&sfinfo, 0, sizeof (sfinfo)) ; if (! (file = sf_open_read (filename, &sfinfo))) { printf ("Stereo : sf_open_read failed with error : ") ; sf_perror (NULL) ; exit (1) ; } ; if (sfinfo.format != (typemajor | typeminor)) { printf ("Stereo : Returned format incorrect (0x%08X => 0x%08X).\n", (typemajor | typeminor), sfinfo.format) ; exit (1) ; } ; if (sfinfo.samples != frames) { printf ("Stereo : Incorrect number of samples in file. (%d => %d)\n", frames, sfinfo.samples) ; exit (1) ; } ; if (sfinfo.channels != 2) { printf ("Stereo : Incorrect number of channels in file.\n") ; exit (1) ; } ; if (sfinfo.pcmbitwidth != 16) { printf ("Stereo : Incorrect bit width (%d).\n", sfinfo.pcmbitwidth) ; exit (1) ; } ; check_log_buffer (file) ; if ((k = sf_readf_short (file, data, frames)) != frames) { printf ("Stereo : short read (%d).\n", k) ; exit (1) ; } ; for (k = 0 ; k < items ; k++) if (data [k] != k * ((k % 2) ? 1 : -1)) { printf ("Stereo : Incorrect sample (#%d : %d => %d).\n", k, k * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; } ; /* Seek to start of file. */ if ((k = sf_seek (file, 0, SEEK_SET) != 0)) { printf ("Stereo : sf_seek (file, 0, SEEK_SET) failed.\n") ; exit (1) ; } ; sf_read_short (file, data, 4) ; for (k = 0 ; k < 4 ; k++) if (data [k] != k * ((k % 2) ? 1 : -1)) { printf ("Stereo : Incorrect sample A (#%d : %d => %d).\n", k, k * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from start of file. */ if ((k = sf_seek (file, 10, SEEK_SET) != 10)) { printf ("Stereo : sf_seek (file, 10, SEEK_SET) failed.\n") ; exit (1) ; } ; sf_read_short (file, data + 10, 4) ; for (k = 20 ; k < 24 ; k++) if (data [k] != k * ((k % 2) ? 1 : -1)) { printf ("Stereo : Incorrect sample B (#%d : %d => %d).\n", k, k * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from current position. */ if ((k = sf_seek (file, 8, SEEK_CUR)) != 20) { printf ("Stereo : sf_seek (file, 8, SEEK_CUR) failed. (20 -> %d)\n", k) ; exit (1) ; } ; sf_read_short (file, data + 40, 4) ; for (k = 40 ; k < 44 ; k++) if (data [k] != k * ((k % 2) ? 1 : -1)) { printf ("Stereo : Incorrect sample C (#%d : %d => %d).\n", k, k * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from end of file. */ if ((k = sf_seek (file, -(sfinfo.samples - 10), SEEK_END) != 10)) { printf ("Stereo : sf_seek (file, -(sfinfo.samples - 10), SEEK_END) failed (%d).\n", k) ; exit (1) ; } ; sf_read_short (file, data + 20, 4) ; for (k = 20 ; k < 24 ; k++) if (data [k] != k * ((k % 2) ? 1 : -1)) { printf ("Stereo : Incorrect sample D (#%d : %d => %d).\n", k, k * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; sf_close (file) ; printf ("ok\n") ;} /* pcm_test_short */static void pcm_test_24bit (char *str, char *filename, int typemajor, int typeminor, int long_file_ok){ SNDFILE *file ; SF_INFO sfinfo ; unsigned int k, items, frames ; int *data ; printf (" pcm_test_24bit : %s ... ", str) ; items = BUFFER_SIZE / sizeof (int) ; data = (int*) test_buffer ; for (k = 0 ; k < items ; k++) data [k] = k * 256 * ((k % 2) ? 1 : -1) ; sfinfo.samplerate = 44100 ; sfinfo.samples = items ; sfinfo.channels = 1 ; sfinfo.pcmbitwidth = 24 ; sfinfo.format = (typemajor | typeminor) ; if (! (file = sf_open_write (filename, &sfinfo))) { printf ("Mono : sf_open_write failed with error : ") ; sf_perror (NULL) ; exit (1) ; } ; if (sf_write_int (file, data, items) != items) { printf ("Mono : sf_write_int failed with error : ") ; sf_perror (file) ; exit (1) ; } ; sf_close (file) ; memset (data, 0, items * 3) ; if (typemajor != SF_FORMAT_RAW) memset (&sfinfo, 0, sizeof (sfinfo)) ; if (! (file = sf_open_read (filename, &sfinfo))) { printf ("Mono : sf_open_read failed with error : ") ; sf_perror (NULL) ; exit (1) ; } ; if (sfinfo.format != (typemajor | typeminor)) { printf ("Mono : Returned format incorrect (0x%08X => 0x%08X).\n", (typemajor | typeminor), sfinfo.format) ; exit (1) ; } ; if (sfinfo.samples < items) { printf ("Mono : Incorrect number of samples in file (too short). (%d should be %d)\n", sfinfo.samples, items) ; exit (1) ; } ; if (! long_file_ok && sfinfo.samples > items) { printf ("Mono : Incorrect number of samples in file (too long). (%d should be %d)\n", sfinfo.samples, items) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("Mono : Incorrect number of channels in file.\n") ; exit (1) ; } ; if (sfinfo.pcmbitwidth != 24) { printf ("Mono : Incorrect bit width (%d).\n", sfinfo.pcmbitwidth) ; exit (1) ; } ; check_log_buffer (file) ; if ((k = sf_read_int (file, data, items)) != items) { printf ("Mono : short read (%d).\n", k) ; exit (1) ; } ; for (k = 0 ; k < items ; k++) if (data [k] != k * 256 * ((k % 2) ? 1 : -1)) { printf ("Mono : Incorrect sample (#%d : %d => %d).\n", k, k * 256 * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; } ; /* Seek to start of file. */ if ((k = sf_seek (file, 0, SEEK_SET) != 0)) { printf ("Mono : sf_seek (file, 0, SEEK_SET) failed.\n") ; exit (1) ; } ; sf_read_int (file, data, 4) ; for (k = 0 ; k < 4 ; k++) if (data [k] != k * 256 * ((k % 2) ? 1 : -1)) { printf ("Mono : Incorrect sample A (#%d : %d => %d).\n", k, k * 256 * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from start of file. */ if ((k = sf_seek (file, 10, SEEK_SET) != 10)) { printf ("Mono : sf_seek (file, 10, SEEK_SET) failed. XXXXX\n") ; exit (1) ; } ; sf_read_int (file, data + 10, 4) ; for (k = 10 ; k < 14 ; k++) if (data [k] != k * 256 * ((k % 2) ? 1 : -1)) { printf ("Mono : Incorrect sample B (#%d : %d => %d).\n", k, k * 256 * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from current position. */ if ((k = sf_seek (file, 6, SEEK_CUR) != 20)) { printf ("Mono : sf_seek (file, 7, SEEK_CUR) failed.\n") ; exit (1) ; } ; sf_read_int (file, data + 20, 4) ; for (k = 20 ; k < 24 ; k++) if (data [k] != k * 256 * ((k % 2) ? 1 : -1)) { printf ("Mono : Incorrect sample C (#%d : %d => %d).\n", k, k * 256 * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from end of file. */ if ((k = sf_seek (file, -(sfinfo.samples - 10), SEEK_END) != 10)) { printf ("Mono : sf_seek (file, -(sfinfo.samples - 10), SEEK_END) failed (%d).\n", k) ; exit (1) ; } ; sf_read_int (file, data + 10, 4) ; for (k = 10 ; k < 14 ; k++) if (data [k] != k * 256 * ((k % 2) ? 1 : -1)) { printf ("Mono : Incorrect sample D (#%d : %d => %d).\n", k, k * 256 * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; sf_close (file) ; /* Now test Stereo. */ if (typemajor == SF_FORMAT_SVX) { printf ("ok\n") ; return ; } ; items = BUFFER_SIZE / sizeof (int) ; data = (int*) test_buffer ; for (k = 0 ; k < items ; k++) data [k] = k * 256 * ((k % 2) ? 1 : -1) ; sfinfo.samplerate = 44100 ; sfinfo.samples = items ; sfinfo.channels = 2 ; sfinfo.pcmbitwidth = 24 ; sfinfo.format = (typemajor | typeminor) ; frames = items / sfinfo.channels ; if (! (file = sf_open_write (filename, &sfinfo))) { printf ("Stereo : sf_open_write failed with error : ") ; sf_perror (NULL) ; exit (1) ; } ; if (sf_writef_int (file, data, frames) != frames) { printf ("Stereo : sf_writef_int failed with error : ") ; sf_perror (file) ; exit (1) ; } ; sf_close (file) ; memset (data, 0, items * 3) ; if (typemajor != SF_FORMAT_RAW) memset (&sfinfo, 0, sizeof (sfinfo)) ; if (! (file = sf_open_read (filename, &sfinfo))) { printf ("Stereo : sf_open_read failed with error : ") ; sf_perror (NULL) ; exit (1) ; } ; if (sfinfo.format != (typemajor | typeminor)) { printf ("Stereo : Returned format incorrect (0x%08X => 0x%08X).\n", (typemajor | typeminor), sfinfo.format) ; exit (1) ; } ; if (sfinfo.samples < frames) { printf ("Stereo : Incorrect number of samples in file (too short). (%d should be %d)\n", sfinfo.samples, frames) ; exit (1) ; } ; if (! long_file_ok && sfinfo.samples > frames) { printf ("Stereo : Incorrect number of samples in file (too long). (%d should be %d)\n", sfinfo.samples, frames) ; exit (1) ; } ; if (sfinfo.channels != 2) { printf ("Stereo : Incorrect number of channels in file.\n") ; exit (1) ; } ; if (sfinfo.pcmbitwidth != 24) { printf ("Stereo : Incorrect bit width (%d).\n", sfinfo.pcmbitwidth) ; exit (1) ; } ; check_log_buffer (file) ; if ((k = sf_readf_int (file, data, frames)) != frames) { printf ("Stereo : short read (%d).\n", k) ; exit (1) ; } ; for (k = 0 ; k < items ; k++) if (data [k] != k * 256 * ((k % 2) ? 1 : -1)) { printf ("Stereo : Incorrect sample (#%d : %d => %d).\n", k, k * 256 * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; } ; /* Seek to start of file. */ if ((k = sf_seek (file, 0, SEEK_SET) != 0)) { printf ("Stereo : sf_seek (file, 0, SEEK_SET) failed.\n") ; exit (1) ; } ; sf_read_int (file, data, 4) ; for (k = 0 ; k < 4 ; k++) if (data [k] != k * 256 * ((k % 2) ? 1 : -1)) { printf ("Stereo : Incorrect sample A (#%d : %d => %d).\n", k, k * 256 * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from start of file. */ if ((k = sf_seek (file, 10, SEEK_SET) != 10)) { printf ("Stereo : sf_seek (file, 10, SEEK_SET) failed.\n") ; exit (1) ; } ; sf_read_int (file, data + 10, 4) ; for (k = 20 ; k < 24 ; k++) if (data [k] != k * 256 * ((k % 2) ? 1 : -1)) { printf ("Stereo : Incorrect sample B (#%d : %d => %d).\n", k, k * 256 * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from current position. */ if ((k = sf_seek (file, 8, SEEK_CUR)) != 20) { printf ("Stereo : sf_seek (file, 8, SEEK_CUR) failed. (20 -> %d)\n", k) ; exit (1) ; } ; sf_read_int (file, data + 40, 4) ; for (k = 40 ; k < 44 ; k++) if (data [k] != k * 256 * ((k % 2) ? 1 : -1)) { printf ("Stereo : Incorrect sample C (#%d : %d => %d).\n", k, k * 256 * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; /* Seek to offset from end of file. */ if ((k = sf_seek (file, -(sfinfo.samples - 10), SEEK_END) != 10)) { printf ("Stereo : sf_seek (file, -(sfinfo.samples - 10), SEEK_END) failed (%d).\n", k) ; exit (1) ; } ; sf_read_int (file, data + 20, 4) ; for (k = 20 ; k < 24 ; k++) if (data [k] != k * 256 * ((k % 2) ? 1 : -1)) { printf ("Stereo : Incorrect sample D (#%d : %d => %d).\n", k, k * 256 * ((k % 2) ? 1 : -1), data [k]) ; exit (1) ; }; sf_close (file) ; printf ("ok\n") ;} /* pcm_test_24bit */static void pcm_test_int (char *str, char *filename, int typemajor, int typeminor){ SNDFILE *file ; SF_INFO sfinfo ; unsigned int k, items, frames ; int sign, *data ; printf (" pcm_test_int : %s ... ", str) ; items = BUFFER_SIZE / sizeof (int) ; data = (int*) test_buffer ; for (sign = 1, k = 0 ; k < items ; k++) data [k] = k * 200000 * ((k % 2) ? 1 : -1) ; sfinfo.samplerate = 44100 ; sfinfo.samples = items ; sfinfo.channels = 1 ; sfinfo.pcmbitwidth = 32 ; sfinfo.format = (typemajor | typeminor) ; if (! (file = sf_open_write (filename, &sfinfo))) { printf ("Mono : sf_open_write failed with error : ") ; sf_perror (NULL) ; exit (1) ; } ; if (sf_write_int (file, data, items) != items) { printf ("Mono : sf_write_int failed with error : ") ; sf_perror (file) ; exit (1) ; } ; sf_close (file) ; memset (data, 0, items * sizeof (short)) ; if (typemajor != SF_FORMAT_RAW) memset (&sfinfo, 0, sizeof (sfinfo)) ; if (! (file = sf_open_read (filename, &sfinfo)))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -