cannam@125: /* cannam@125: ** Copyright (C) 2001-2015 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 "sfconfig.h" cannam@125: cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: cannam@125: #if HAVE_UNISTD_H cannam@125: #include cannam@125: #endif cannam@125: cannam@125: #include cannam@125: cannam@125: #include cannam@125: cannam@125: #include "utils.h" cannam@125: cannam@125: #define BUFFER_LEN (1 << 10) cannam@125: #define LOG_BUFFER_SIZE 1024 cannam@125: cannam@125: static void channel_test (void) ; cannam@125: static double max_diff (const float *a, const float *b, unsigned int len, unsigned int * position) ; cannam@125: cannam@125: int cannam@125: main (void) // int argc, char *argv []) cannam@125: { channel_test () ; 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: channel_test (void) cannam@125: { static float float_data [1024] ; cannam@125: static float read_float [1024] ; cannam@125: static int read_int [1024] ; cannam@125: static short read_short [1024] ; cannam@125: unsigned int ch, k, position = 0 ; cannam@125: cannam@125: gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.9) ; cannam@125: cannam@125: for (ch = 1 ; ch <= 8 ; ch++) cannam@125: { SNDFILE *file ; cannam@125: SF_INFO wsfinfo, rsfinfo ; cannam@125: sf_count_t wframes = ARRAY_LEN (float_data) / ch ; cannam@125: double maxdiff ; cannam@125: char filename [256] ; cannam@125: cannam@125: snprintf (filename, sizeof (filename), "chan_%d.wav", ch) ; cannam@125: print_test_name (__func__, filename) ; cannam@125: cannam@125: sf_info_setup (&wsfinfo, SF_FORMAT_WAV | SF_FORMAT_FLOAT, 48000, ch) ; cannam@125: sf_info_clear (&rsfinfo) ; cannam@125: cannam@125: /* Write the test file. */ cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &wsfinfo, SF_FALSE, __LINE__) ; cannam@125: test_writef_float_or_die (file, 0, float_data, wframes, __LINE__) ; cannam@125: sf_close (file) ; cannam@125: cannam@125: /* Read it as float and test. */ cannam@125: file = test_open_file_or_die (filename, SFM_READ, &rsfinfo, SF_FALSE, __LINE__) ; cannam@125: exit_if_true (rsfinfo.frames == 0, cannam@125: "\n\nLine %d : Frames in file %" PRId64 ".\n\n", __LINE__, rsfinfo.frames) ; cannam@125: exit_if_true (wframes != rsfinfo.frames, cannam@125: "\n\nLine %d : Wrote %" PRId64 ", read %" PRId64 " frames.\n\n", __LINE__, wframes, rsfinfo.frames) ; cannam@125: cannam@125: sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; cannam@125: cannam@125: test_readf_float_or_die (file, 0, read_float, rsfinfo.frames, __LINE__) ; cannam@125: compare_float_or_die (float_data, read_float, ch * rsfinfo.frames, __LINE__) ; cannam@125: cannam@125: /* Read it as short and test. */ cannam@125: test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ; cannam@125: test_readf_short_or_die (file, 0, read_short, rsfinfo.frames, __LINE__) ; cannam@125: cannam@125: for (k = 0 ; k < ARRAY_LEN (read_float) ; k++) cannam@125: read_float [k] = read_short [k] * (0.9 / 0x8000) ; cannam@125: cannam@125: maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ; cannam@125: exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ; cannam@125: cannam@125: /* Read it as int and test. */ cannam@125: test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ; cannam@125: test_readf_int_or_die (file, 0, read_int, rsfinfo.frames, __LINE__) ; cannam@125: cannam@125: for (k = 0 ; k < ARRAY_LEN (read_float) ; k++) cannam@125: read_float [k] = read_int [k] * (0.9 / 0x80000000) ; cannam@125: cannam@125: maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ; cannam@125: exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: unlink (filename) ; cannam@125: printf ("ok\n") ; cannam@125: } ; cannam@125: cannam@125: return ; cannam@125: } /* channel_test */ cannam@125: cannam@125: static double cannam@125: max_diff (const float *a, const float *b, unsigned int len, unsigned int * position) cannam@125: { double mdiff = 0.0, diff ; cannam@125: unsigned int k ; cannam@125: cannam@125: for (k = 0 ; k < len ; k++) cannam@125: { diff = fabs (a [k] - b [k]) ; cannam@125: if (diff > mdiff) cannam@125: { mdiff = diff ; cannam@125: *position = k ; cannam@125: } ; cannam@125: } ; cannam@125: cannam@125: return mdiff ; cannam@125: } /* max_diff */