cannam@125: /* cannam@125: ** Copyright (C) 1999-2014 Erik de Castro Lopo cannam@125: ** cannam@125: ** This program is free software; you can redistribute it and/or modify cannam@125: ** it under the terms of the GNU General Public License as published by cannam@125: ** the Free Software Foundation; either version 2 of the License, or cannam@125: ** (at your option) any later version. cannam@125: ** cannam@125: ** This program is distributed in the hope that it will be useful, cannam@125: ** but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@125: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@125: ** GNU General Public License for more details. cannam@125: ** cannam@125: ** You should have received a copy of the GNU General Public License cannam@125: ** along with this program; if not, write to the Free Software cannam@125: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cannam@125: */ cannam@125: cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: cannam@125: #include cannam@125: cannam@125: #include "utils.h" cannam@125: cannam@125: #define BUFFER_SIZE (1 << 14) cannam@125: #define SAMPLE_RATE (11025) cannam@125: cannam@125: #ifndef M_PI cannam@125: #define M_PI 3.14159265358979323846264338 cannam@125: #endif cannam@125: cannam@125: static void lcomp_test_int (const char *str, const char *filename, int filetype, double margin) ; cannam@125: cannam@125: static int error_function (double data, double orig, double margin) ; cannam@125: static int decay_response (int k) ; cannam@125: cannam@125: static void gen_signal_double (double *data, double scale, int datalen) ; cannam@125: cannam@125: /* Force the start of these buffers to be double aligned. Sparc-solaris will cannam@125: ** choke if they are not. cannam@125: */ cannam@125: cannam@125: typedef union cannam@125: { double d [BUFFER_SIZE + 1] ; cannam@125: int i [BUFFER_SIZE + 1] ; cannam@125: } BUFFER ; cannam@125: cannam@125: static BUFFER data_buffer ; cannam@125: static BUFFER orig_buffer ; cannam@125: cannam@125: int cannam@125: main (void) cannam@125: { const char *filename = "test.au" ; cannam@125: cannam@125: lcomp_test_int ("au_g721", filename, SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G721_32, 0.06) ; cannam@125: cannam@125: return 0 ; cannam@125: } /* main */ cannam@125: cannam@125: /*============================================================================================ cannam@125: ** Here are the test functions. cannam@125: */ cannam@125: cannam@125: static void cannam@125: lcomp_test_int (const char *str, const char *filename, int filetype, double margin) cannam@125: { SNDFILE *file ; cannam@125: SF_INFO sfinfo ; cannam@125: int k, m, *orig, *data ; cannam@125: sf_count_t datalen, seekpos ; cannam@125: int64_t sum_abs ; cannam@125: double scale ; cannam@125: cannam@125: printf ("\nThis is program is not part of the libsndfile test suite.\n\n") ; cannam@125: cannam@125: printf (" lcomp_test_int : %s ... ", str) ; cannam@125: fflush (stdout) ; cannam@125: cannam@125: datalen = BUFFER_SIZE ; cannam@125: cannam@125: scale = 1.0 * 0x10000 ; cannam@125: cannam@125: data = data_buffer.i ; cannam@125: orig = orig_buffer.i ; cannam@125: cannam@125: gen_signal_double (orig_buffer.d, 32000.0 * scale, datalen) ; cannam@125: for (k = 0 ; k < datalen ; k++) cannam@125: orig [k] = orig_buffer.d [k] ; cannam@125: cannam@125: cannam@125: sfinfo.samplerate = SAMPLE_RATE ; cannam@125: sfinfo.frames = 123456789 ; /* Ridiculous value. */ cannam@125: sfinfo.channels = 1 ; cannam@125: sfinfo.format = filetype ; cannam@125: cannam@125: if (! (file = sf_open (filename, SFM_WRITE, &sfinfo))) cannam@125: { printf ("sf_open_write failed with error : ") ; cannam@125: puts (sf_strerror (NULL)) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if ((k = sf_writef_int (file, orig, datalen)) != datalen) cannam@125: { printf ("sf_writef_int failed with short write (%" PRId64 " => %d).\n", datalen, k) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: sf_close (file) ; cannam@125: cannam@125: memset (data, 0, datalen * sizeof (int)) ; cannam@125: cannam@125: if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW) cannam@125: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@125: cannam@125: if (! (file = sf_open (filename, SFM_READ, &sfinfo))) cannam@125: { printf ("sf_open_read failed with error : ") ; cannam@125: puts (sf_strerror (NULL)) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if ((sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) cannam@125: { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (sfinfo.frames < datalen) cannam@125: { printf ("Too few.frames in file. (%" PRId64 " should be a little more than %" PRId64 ")\n", datalen, sfinfo.frames) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (sfinfo.frames > (datalen + datalen / 2)) cannam@125: { printf ("Too many.frames in file. (%" PRId64 " should be a little more than %" PRId64 ")\n", datalen, sfinfo.frames) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (sfinfo.channels != 1) cannam@125: { printf ("Incorrect number of channels in file.\n") ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: check_log_buffer_or_die (file, __LINE__) ; cannam@125: cannam@125: if ((k = sf_readf_int (file, data, datalen)) != datalen) cannam@125: { printf ("Line %d: short read (%d should be %" PRId64 ").\n", __LINE__, k, datalen) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: sum_abs = 0 ; cannam@125: for (k = 0 ; k < datalen ; k++) cannam@125: { if (error_function (data [k] / scale, orig [k] / scale, margin)) cannam@125: { printf ("Line %d: Incorrect sample (#%d : %f should be %f).\n", __LINE__, k, data [k] / scale, orig [k] / scale) ; cannam@125: oct_save_int (orig, data, datalen) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: sum_abs += abs (data [k]) ; cannam@125: } ; cannam@125: cannam@125: if (sum_abs < 1.0) cannam@125: { printf ("Line %d: Signal is all zeros.\n", __LINE__) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if ((k = sf_readf_int (file, data, datalen)) != sfinfo.frames - datalen) cannam@125: { printf ("Line %d: Incorrect read length (%" PRId64 " should be %d).\n", __LINE__, sfinfo.frames - datalen, k) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: /* This check is only for block based encoders which must append silence cannam@125: ** to the end of a file so as to fill out a block. cannam@125: */ cannam@125: if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM) cannam@125: for (k = 0 ; k < sfinfo.frames - datalen ; k++) cannam@125: if (ABS (data [k] / scale) > decay_response (k)) cannam@125: { printf ("Line %d : Incorrect sample B (#%d : abs (%d) should be < %d).\n", __LINE__, k, data [k], decay_response (k)) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (! sfinfo.seekable) cannam@125: { printf ("ok\n") ; cannam@125: return ; cannam@125: } ; cannam@125: cannam@125: /* Now test sf_seek function. */ cannam@125: cannam@125: if ((k = sf_seek (file, 0, SEEK_SET)) != 0) cannam@125: { printf ("Line %d: Seek to start of file failed (%d).\n", __LINE__, k) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: for (m = 0 ; m < 3 ; m++) cannam@125: { int n ; cannam@125: cannam@125: if ((k = sf_readf_int (file, data, 11)) != 11) cannam@125: { printf ("Line %d: Incorrect read length (11 => %d).\n", __LINE__, k) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: for (k = 0 ; k < 11 ; k++) cannam@125: if (error_function (data [k] / scale, orig [k + m * 11] / scale, margin)) cannam@125: { printf ("Line %d: Incorrect sample (m = %d) (#%d : %d => %d).\n", __LINE__, m, k + m * 11, orig [k + m * 11], data [k]) ; cannam@125: for (n = 0 ; n < 1 ; n++) cannam@125: printf ("%d ", data [n]) ; cannam@125: printf ("\n") ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: } ; cannam@125: cannam@125: seekpos = BUFFER_SIZE / 10 ; cannam@125: cannam@125: /* Check seek from start of file. */ cannam@125: if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos) cannam@125: { printf ("Seek to start of file + %" PRId64 " failed (%d).\n", seekpos, k) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if ((k = sf_readf_int (file, data, 1)) != 1) cannam@125: { printf ("Line %d: sf_readf_int (file, data, 1) returned %d.\n", __LINE__, k) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (error_function ((double) data [0], (double) orig [seekpos], margin)) cannam@125: { printf ("Line %d: sf_seek (SEEK_SET) followed by sf_readf_int failed (%d, %d).\n", __LINE__, orig [1], data [0]) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) cannam@125: { printf ("Line %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %" PRId64 ")\n", __LINE__, k, seekpos + 1) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; cannam@125: k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; cannam@125: sf_readf_int (file, data, 1) ; cannam@125: if (error_function ((double) data [0], (double) orig [seekpos], margin) || k != seekpos) cannam@125: { printf ("Line %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %" PRId64 ").\n", __LINE__, data [0], orig [seekpos], k, seekpos + 1) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ; cannam@125: /* Check seek backward from current position. */ cannam@125: k = sf_seek (file, -20, SEEK_CUR) ; cannam@125: sf_readf_int (file, data, 1) ; cannam@125: if (error_function ((double) data [0], (double) orig [seekpos], margin) || k != seekpos) cannam@125: { printf ("sf_seek (backwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %" PRId64 ").\n", data [0], orig [seekpos], k, seekpos) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: /* Check that read past end of file returns number of items. */ cannam@125: sf_seek (file, (int) sfinfo.frames, SEEK_SET) ; cannam@125: cannam@125: if ((k = sf_readf_int (file, data, datalen)) != 0) cannam@125: { printf ("Line %d: Return value from sf_readf_int past end of file incorrect (%d).\n", __LINE__, k) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: /* Check seek backward from end. */ cannam@125: if ((k = sf_seek (file, 5 - (int) sfinfo.frames, SEEK_END)) != 5) cannam@125: { printf ("sf_seek (SEEK_END) returned %d instead of %d.\n", k, 5) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: sf_readf_int (file, data, 1) ; cannam@125: if (error_function (data [0] / scale, orig [5] / scale, margin)) cannam@125: { printf ("Line %d: sf_seek (SEEK_END) followed by sf_readf_short failed (%d should be %d).\n", __LINE__, data [0], orig [5]) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: printf ("ok\n") ; cannam@125: } /* lcomp_test_int */ cannam@125: cannam@125: /*======================================================================================== cannam@125: ** Auxiliary functions cannam@125: */ cannam@125: cannam@125: #define SIGNAL_MAXVAL 30000.0 cannam@125: #define DECAY_COUNT 800 cannam@125: cannam@125: static int cannam@125: decay_response (int k) cannam@125: { if (k < 1) cannam@125: return (int) (1.2 * SIGNAL_MAXVAL) ; cannam@125: if (k > DECAY_COUNT) cannam@125: return 0 ; cannam@125: return (int) (1.2 * SIGNAL_MAXVAL * (DECAY_COUNT - k) / (1.0 * DECAY_COUNT)) ; cannam@125: } /* decay_response */ cannam@125: cannam@125: static void cannam@125: gen_signal_double (double *data, double scale, int datalen) cannam@125: { int k, ramplen ; cannam@125: double amp = 0.0 ; cannam@125: cannam@125: ramplen = datalen / 18 ; cannam@125: cannam@125: for (k = 0 ; k < datalen ; k++) cannam@125: { if (k <= ramplen) cannam@125: amp = scale * k / ((double) ramplen) ; cannam@125: else if (k > datalen - ramplen) cannam@125: amp = scale * (datalen - k) / ((double) ramplen) ; cannam@125: cannam@125: data [k] = amp * (0.4 * sin (33.3 * 2.0 * M_PI * ((double) (k + 1)) / ((double) SAMPLE_RATE)) cannam@125: + 0.3 * cos (201.1 * 2.0 * M_PI * ((double) (k + 1)) / ((double) SAMPLE_RATE))) ; cannam@125: } ; cannam@125: cannam@125: return ; cannam@125: } /* gen_signal_double */ cannam@125: cannam@125: static int cannam@125: error_function (double data, double orig, double margin) cannam@125: { double error ; cannam@125: cannam@125: if (fabs (orig) <= 500.0) cannam@125: error = fabs (fabs (data) - fabs (orig)) / 2000.0 ; cannam@125: else if (fabs (orig) <= 1000.0) cannam@125: error = fabs (data - orig) / 3000.0 ; cannam@125: else cannam@125: error = fabs (data - orig) / fabs (orig) ; cannam@125: cannam@125: if (error > margin) cannam@125: { printf ("\n\n*******************\nError : %f\n", error) ; cannam@125: return 1 ; cannam@125: } ; cannam@125: return 0 ; cannam@125: } /* error_function */ cannam@125: