📄 lossy_comp_test.c
字号:
/*** Copyright (C) 1999-2001 Erik de Castro Lopo <erikd@zip.com.au>** ** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.** ** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU General Public License for more details.** ** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#include <stdio.h>#include <string.h>#include <unistd.h>#include <math.h>#include <sndfile.h>#include "check_log_buffer.h"#ifndef M_PI #define M_PI 3.14159265359#endif#define BUFFER_SIZE (1<<14)#define SAMPLE_RATE 11025static void lcomp_test_short (char *str, char *filename, int typemajor, int typeminor, double margin) ;static void lcomp_test_int (char *str, char *filename, int typemajor, int typeminor, double margin) ;static void lcomp_test_float (char *str, char *filename, int typemajor, int typeminor, double margin) ;static void lcomp_test_double (char *str, char *filename, int typemajor, int typeminor, double margin) ;static void sdlcomp_test_short (char *str, char *filename, int typemajor, int typeminor, double margin) ;static void sdlcomp_test_int (char *str, char *filename, int typemajor, int typeminor, double margin) ;static void sdlcomp_test_float (char *str, char *filename, int typemajor, int typeminor, double margin) ;static void sdlcomp_test_double (char *str, char *filename, int typemajor, int typeminor, double margin) ;static int error_function (double data, double orig, double margin) ;static int decay_response (int k) ;static void gen_signal_double (double *data, unsigned int datalen) ;static void smoothed_diff_short (short *data, unsigned int datalen) ;static void smoothed_diff_int (int *data, unsigned int datalen) ;static void smoothed_diff_float (float *data, unsigned int datalen) ;static void smoothed_diff_double (double *data, unsigned int datalen) ;/* Force the start of these buffers to be double aligned. Sparc-solaris will** choke if they are not.*/static double data_buffer [BUFFER_SIZE + 1] ;static double orig_buffer [BUFFER_SIZE + 1] ;static double smooth_buffer [BUFFER_SIZE + 1] ;int main (int argc, char *argv[]){ char *filename ; int bDoAll = 0 ; int nTests = 0 ; if (argc != 2) { printf ("Usage : %s <test>\n", argv [0]) ; printf (" Where <test> is one of the following:\n") ; printf (" wav_ima - test IMA ADPCM WAV file functions\n") ; printf (" wav_msadpcm - test MS ADPCM WAV file functions\n") ; printf (" wav_gsm610 - test GSM 6.10 WAV file functions\n") ; printf (" wav_ulaw - test u-law WAV file functions\n") ; printf (" wav_alaw - test A-law WAV file functions\n") ; printf (" wav_pcm - test PCM WAV file functions\n") ; printf (" all - perform all tests\n") ; exit (1) ; } ; bDoAll = !strcmp (argv [1], "all") ; if (bDoAll || ! strcmp (argv [1], "wav_pcm")) { filename = "test.wav" ; lcomp_test_short ("wav_pcm", filename, SF_FORMAT_WAV, SF_FORMAT_PCM, 0.00001) ; lcomp_test_int ("wav_pcm", filename, SF_FORMAT_WAV, SF_FORMAT_PCM, 0.00001) ; lcomp_test_float ("wav_pcm", filename, SF_FORMAT_WAV, SF_FORMAT_PCM, 0.005) ; lcomp_test_double ("wav_pcm", filename, SF_FORMAT_WAV, SF_FORMAT_PCM, 0.005) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "wav_ima")) { filename = "test.wav" ; lcomp_test_short ("wav_ima", filename, SF_FORMAT_WAV, SF_FORMAT_IMA_ADPCM, 0.17) ; lcomp_test_int ("wav_ima", filename, SF_FORMAT_WAV, SF_FORMAT_IMA_ADPCM, 0.17) ; lcomp_test_float ("wav_ima", filename, SF_FORMAT_WAV, SF_FORMAT_IMA_ADPCM, 0.17) ; lcomp_test_double ("wav_ima", filename, SF_FORMAT_WAV, SF_FORMAT_IMA_ADPCM, 0.17) ; sdlcomp_test_short ("wav_ima", filename, SF_FORMAT_WAV, SF_FORMAT_IMA_ADPCM, 0.17) ; sdlcomp_test_int ("wav_ima", filename, SF_FORMAT_WAV, SF_FORMAT_IMA_ADPCM, 0.17) ; sdlcomp_test_float ("wav_ima", filename, SF_FORMAT_WAV, SF_FORMAT_IMA_ADPCM, 0.17) ; sdlcomp_test_double ("wav_ima", filename, SF_FORMAT_WAV, SF_FORMAT_IMA_ADPCM, 0.17) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "wav_msadpcm")) { filename = "test.wav" ; lcomp_test_short ("wav_msadpcm", filename, SF_FORMAT_WAV, SF_FORMAT_MS_ADPCM, 0.36) ; lcomp_test_int ("wav_msadpcm", filename, SF_FORMAT_WAV, SF_FORMAT_MS_ADPCM, 0.36) ; lcomp_test_float ("wav_msadpcm", filename, SF_FORMAT_WAV, SF_FORMAT_MS_ADPCM, 0.36) ; lcomp_test_double ("wav_msadpcm", filename, SF_FORMAT_WAV, SF_FORMAT_MS_ADPCM, 0.36) ; sdlcomp_test_short ("wav_msadpcm", filename, SF_FORMAT_WAV, SF_FORMAT_MS_ADPCM, 0.36) ; sdlcomp_test_int ("wav_msadpcm", filename, SF_FORMAT_WAV, SF_FORMAT_MS_ADPCM, 0.36) ; sdlcomp_test_float ("wav_msadpcm", filename, SF_FORMAT_WAV, SF_FORMAT_MS_ADPCM, 0.36) ; sdlcomp_test_double ("wav_msadpcm", filename, SF_FORMAT_WAV, SF_FORMAT_MS_ADPCM, 0.36) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "wav_ulaw")) { filename = "test.wav" ; lcomp_test_short ("wav_ulaw", filename, SF_FORMAT_WAV, SF_FORMAT_ULAW, 0.04) ; lcomp_test_int ("wav_ulaw", filename, SF_FORMAT_WAV, SF_FORMAT_ULAW, 0.04) ; lcomp_test_float ("wav_ulaw", filename, SF_FORMAT_WAV, SF_FORMAT_ULAW, 0.04) ; lcomp_test_double ("wav_ulaw", filename, SF_FORMAT_WAV, SF_FORMAT_ULAW, 0.04) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "wav_alaw")) { filename = "test.wav" ; lcomp_test_short ("wav_alaw", filename, SF_FORMAT_WAV, SF_FORMAT_ALAW, 0.04) ; lcomp_test_int ("wav_alaw", filename, SF_FORMAT_WAV, SF_FORMAT_ALAW, 0.04) ; lcomp_test_float ("wav_alaw", filename, SF_FORMAT_WAV, SF_FORMAT_ALAW, 0.04) ; lcomp_test_double ("wav_alaw", filename, SF_FORMAT_WAV, SF_FORMAT_ALAW, 0.04) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "wav_gsm610")) { filename = "test.wav" ; /* Don't do lcomp_test_XXX as the errors are too big. */ sdlcomp_test_short ("wav_gsm610", filename, SF_FORMAT_WAV, SF_FORMAT_GSM610, 0.2) ; sdlcomp_test_int ("wav_gsm610", filename, SF_FORMAT_WAV, SF_FORMAT_GSM610, 0.2) ; sdlcomp_test_float ("wav_gsm610", filename, SF_FORMAT_WAV, SF_FORMAT_GSM610, 0.2) ; sdlcomp_test_double ("wav_gsm610", filename, SF_FORMAT_WAV, SF_FORMAT_GSM610, 0.2) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "au_ulaw")) { filename = "test.au" ; lcomp_test_short ("au_ulaw", filename, SF_FORMAT_AU, SF_FORMAT_ULAW, 0.04) ; lcomp_test_int ("au_ulaw", filename, SF_FORMAT_AU, SF_FORMAT_ULAW, 0.04) ; lcomp_test_float ("au_ulaw", filename, SF_FORMAT_AU, SF_FORMAT_ULAW, 0.04) ; lcomp_test_double ("au_ulaw", filename, SF_FORMAT_AU, SF_FORMAT_ULAW, 0.04) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "au_alaw")) { filename = "test.au" ; lcomp_test_short ("au_alaw", filename, SF_FORMAT_AU, SF_FORMAT_ALAW, 0.04) ; lcomp_test_int ("au_alaw", filename, SF_FORMAT_AU, SF_FORMAT_ALAW, 0.04) ; lcomp_test_float ("au_alaw", filename, SF_FORMAT_AU, SF_FORMAT_ALAW, 0.04) ; lcomp_test_double ("au_alaw", filename, SF_FORMAT_AU, SF_FORMAT_ALAW, 0.04) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "aule_ulaw")) { filename = "test.au" ; lcomp_test_short ("aule_ulaw", filename, SF_FORMAT_AULE, SF_FORMAT_ULAW, 0.04) ; lcomp_test_int ("aule_ulaw", filename, SF_FORMAT_AULE, SF_FORMAT_ULAW, 0.04) ; lcomp_test_float ("aule_ulaw", filename, SF_FORMAT_AULE, SF_FORMAT_ULAW, 0.04) ; lcomp_test_double ("aule_ulaw", filename, SF_FORMAT_AULE, SF_FORMAT_ULAW, 0.04) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "aule_alaw")) { filename = "test.au" ; lcomp_test_short ("aule_alaw", filename, SF_FORMAT_AULE, SF_FORMAT_ALAW, 0.04) ; lcomp_test_int ("aule_alaw", filename, SF_FORMAT_AULE, SF_FORMAT_ALAW, 0.04) ; lcomp_test_float ("aule_alaw", filename, SF_FORMAT_AULE, SF_FORMAT_ALAW, 0.04) ; lcomp_test_double ("aule_alaw", filename, SF_FORMAT_AULE, SF_FORMAT_ALAW, 0.04) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "au_g721")) { filename = "test.au" ; lcomp_test_short ("au_g721", filename, SF_FORMAT_AU, SF_FORMAT_G721_32, 0.05) ; lcomp_test_int ("au_g721", filename, SF_FORMAT_AU, SF_FORMAT_G721_32, 0.05) ; lcomp_test_float ("au_g721", filename, SF_FORMAT_AU, SF_FORMAT_G721_32, 0.05) ; lcomp_test_double ("au_g721", filename, SF_FORMAT_AU, SF_FORMAT_G721_32, 0.05) ; sdlcomp_test_short ("au_g721", filename, SF_FORMAT_AU, SF_FORMAT_G721_32, 0.05) ; sdlcomp_test_int ("au_g721", filename, SF_FORMAT_AU, SF_FORMAT_G721_32, 0.05) ; sdlcomp_test_float ("au_g721", filename, SF_FORMAT_AU, SF_FORMAT_G721_32, 0.05) ; sdlcomp_test_double ("au_g721", filename, SF_FORMAT_AU, SF_FORMAT_G721_32, 0.05) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "aule_g721")) { filename = "test.au" ; lcomp_test_short ("aule_g721", filename, SF_FORMAT_AULE, SF_FORMAT_G721_32, 0.05) ; lcomp_test_int ("aule_g721", filename, SF_FORMAT_AULE, SF_FORMAT_G721_32, 0.05) ; lcomp_test_float ("aule_g721", filename, SF_FORMAT_AULE, SF_FORMAT_G721_32, 0.05) ; lcomp_test_double ("aule_g721", filename, SF_FORMAT_AULE, SF_FORMAT_G721_32, 0.05) ; sdlcomp_test_short ("aule_g721", filename, SF_FORMAT_AULE, SF_FORMAT_G721_32, 0.05) ; sdlcomp_test_int ("aule_g721", filename, SF_FORMAT_AULE, SF_FORMAT_G721_32, 0.05) ; sdlcomp_test_float ("aule_g721", filename, SF_FORMAT_AULE, SF_FORMAT_G721_32, 0.05) ; sdlcomp_test_double ("aule_g721", filename, SF_FORMAT_AULE, SF_FORMAT_G721_32, 0.05) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "au_g723")) { filename = "test.au" ; lcomp_test_short ("au_g723", filename, SF_FORMAT_AU, SF_FORMAT_G723_24, 0.15) ; lcomp_test_int ("au_g723", filename, SF_FORMAT_AU, SF_FORMAT_G723_24, 0.15) ; lcomp_test_float ("au_g723", filename, SF_FORMAT_AU, SF_FORMAT_G723_24, 0.15) ; lcomp_test_double ("au_g723", filename, SF_FORMAT_AU, SF_FORMAT_G723_24, 0.15) ; sdlcomp_test_short ("au_g723", filename, SF_FORMAT_AU, SF_FORMAT_G723_24, 0.15) ; sdlcomp_test_int ("au_g723", filename, SF_FORMAT_AU, SF_FORMAT_G723_24, 0.15) ; sdlcomp_test_float ("au_g723", filename, SF_FORMAT_AU, SF_FORMAT_G723_24, 0.15) ; sdlcomp_test_double ("au_g723", filename, SF_FORMAT_AU, SF_FORMAT_G723_24, 0.15) ; unlink (filename) ; nTests++ ; } ; if (bDoAll || ! strcmp (argv [1], "aule_g723")) { filename = "test.au" ; lcomp_test_short ("aule_g723", filename, SF_FORMAT_AULE, SF_FORMAT_G723_24, 0.15) ; lcomp_test_int ("aule_g723", filename, SF_FORMAT_AULE, SF_FORMAT_G723_24, 0.15) ; lcomp_test_float ("aule_g723", filename, SF_FORMAT_AULE, SF_FORMAT_G723_24, 0.15) ; lcomp_test_double ("aule_g723", filename, SF_FORMAT_AULE, SF_FORMAT_G723_24, 0.15) ; sdlcomp_test_short ("aule_g723", filename, SF_FORMAT_AULE, SF_FORMAT_G723_24, 0.15) ; sdlcomp_test_int ("aule_g723", filename, SF_FORMAT_AULE, SF_FORMAT_G723_24, 0.15) ; sdlcomp_test_float ("aule_g723", filename, SF_FORMAT_AULE, SF_FORMAT_G723_24, 0.15) ; sdlcomp_test_double ("aule_g723", filename, SF_FORMAT_AULE, SF_FORMAT_G723_24, 0.15) ; unlink (filename) ; nTests++ ; } ; if (nTests == 0) { printf ("************************************\n") ; printf ("* No '%s' test defined.\n", argv [1]) ; printf ("************************************\n") ; return 1 ; } ; return 0;} /* main *//*============================================================================================** Here are the test functions.*/ static void lcomp_test_short (char *str, char *filename, int typemajor, int typeminor, double margin){ SNDFILE *file ; SF_INFO sfinfo ; int k, m, seekpos ; unsigned int datalen ; short *orig, *data ; printf (" lcomp_test_short : %s ... ", str) ; datalen = BUFFER_SIZE ; orig = (short*) orig_buffer ; data = (short*) data_buffer ; gen_signal_double (orig_buffer, datalen) ; for (k = 0 ; k < datalen ; k++) orig [k] = (short) (orig_buffer [k]) ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.samples = 123456789 ; /* Ridiculous value. */ sfinfo.channels = 1 ; sfinfo.pcmbitwidth = 16 ; sfinfo.format = (typemajor | typeminor) ; if (! (file = sf_open_write (filename, &sfinfo))) { printf ("sf_open_write failed with error : ") ; sf_perror (NULL) ; exit (1) ; } ; if ((k = sf_write_short (file, orig, datalen)) != datalen) { printf ("sf_write_short failed with short write (%d => %d).\n", datalen, k) ; exit (1) ; } ; sf_close (file) ; memset (data, 0, datalen * sizeof (short)) ; memset (&sfinfo, 0, sizeof (sfinfo)) ; if (! (file = sf_open_read (filename, &sfinfo))) { printf ("sf_open_read failed with error : ") ; sf_perror (NULL) ; exit (1) ; } ; if (sfinfo.format != (typemajor | typeminor)) { printf ("Returned format incorrect (0x%08X => 0x%08X).\n", (typemajor | typeminor), sfinfo.format) ; exit (1) ; } ; if (sfinfo.samples < datalen) { printf ("Too few samples in file. (%d should be a little more than %d)\n", datalen, sfinfo.samples) ; exit (1) ; } ; if (sfinfo.samples > (datalen + datalen/2)) { printf ("Too many samples in file. (%d should be a little more than %d)\n", sfinfo.samples, datalen) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("Incorrect number of channels in file.\n") ; exit (1) ; } ; if (sfinfo.pcmbitwidth != 16) { printf ("Incorrect bit width (%d).\n", sfinfo.pcmbitwidth) ; exit (1) ; } ; check_log_buffer (file) ; if ((k = sf_read_short (file, data, datalen)) < 0.99 * datalen) { printf ("short read (%d).\n", k) ; exit (1) ; } ; for (k = 0 ; k < datalen ; k++) { if (error_function ((double) data [k], (double) orig [k], margin)) { printf ("Incorrect sample A (#%d : %d should be %d).\n", k, data [k], orig [k]) ; exit (1) ; } ; } ; if ((k = sf_read_short (file, data, datalen)) != sfinfo.samples - datalen) { printf ("Incorrect read length A (%d should be %d).\n", sfinfo.samples - datalen, k) ; exit (1) ; } ; if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM) for (k = 0 ; k < sfinfo.samples - datalen ; k++) if (abs (data [k]) > decay_response (k)) { printf ("Incorrect sample B (#%d : abs (%d) should be < %d).\n", datalen + k, data [k], decay_response (k)) ; exit (1) ; } ; if (! sfinfo.seekable) { printf ("ok\n") ; return ; } ; /* Now test sf_seek function. */ if ((k = sf_seek (file, 0, SEEK_SET)) != 0) { printf ("Seek to start of file failed (%d).\n", k) ; exit (1) ; } ; for (m = 0 ; m < 3 ; m++) { if ((k = sf_read_short (file, data, datalen / 7)) != datalen / 7) { printf ("Incorrect read length B (%d => %d).\n", datalen / 7, k) ; exit (1) ; } ; for (k = 0 ; k < datalen / 7 ; k++) if (error_function ((double) data [k], (double) orig [k + m * (datalen / 7)], margin))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -