annotate src/libsndfile-1.0.27/tests/channel_test.c @ 127:7867fa7e1b6b

Current fftw source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:40:26 +0100
parents cd6cdf86811e
children
rev   line source
cannam@125 1 /*
cannam@125 2 ** Copyright (C) 2001-2015 Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@125 3 **
cannam@125 4 ** This program is free software; you can redistribute it and/or modify
cannam@125 5 ** it under the terms of the GNU General Public License as published by
cannam@125 6 ** the Free Software Foundation; either version 2 of the License, or
cannam@125 7 ** (at your option) any later version.
cannam@125 8 **
cannam@125 9 ** This program is distributed in the hope that it will be useful,
cannam@125 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@125 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@125 12 ** GNU General Public License for more details.
cannam@125 13 **
cannam@125 14 ** You should have received a copy of the GNU General Public License
cannam@125 15 ** along with this program; if not, write to the Free Software
cannam@125 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cannam@125 17 */
cannam@125 18
cannam@125 19 #include "sfconfig.h"
cannam@125 20
cannam@125 21 #include <stdio.h>
cannam@125 22 #include <stdlib.h>
cannam@125 23 #include <stdint.h>
cannam@125 24 #include <inttypes.h>
cannam@125 25 #include <string.h>
cannam@125 26 #include <time.h>
cannam@125 27
cannam@125 28 #if HAVE_UNISTD_H
cannam@125 29 #include <unistd.h>
cannam@125 30 #endif
cannam@125 31
cannam@125 32 #include <math.h>
cannam@125 33
cannam@125 34 #include <sndfile.h>
cannam@125 35
cannam@125 36 #include "utils.h"
cannam@125 37
cannam@125 38 #define BUFFER_LEN (1 << 10)
cannam@125 39 #define LOG_BUFFER_SIZE 1024
cannam@125 40
cannam@125 41 static void channel_test (void) ;
cannam@125 42 static double max_diff (const float *a, const float *b, unsigned int len, unsigned int * position) ;
cannam@125 43
cannam@125 44 int
cannam@125 45 main (void) // int argc, char *argv [])
cannam@125 46 { channel_test () ;
cannam@125 47 return 0 ;
cannam@125 48 } /* main */
cannam@125 49
cannam@125 50 /*============================================================================================
cannam@125 51 ** Here are the test functions.
cannam@125 52 */
cannam@125 53
cannam@125 54 static void
cannam@125 55 channel_test (void)
cannam@125 56 { static float float_data [1024] ;
cannam@125 57 static float read_float [1024] ;
cannam@125 58 static int read_int [1024] ;
cannam@125 59 static short read_short [1024] ;
cannam@125 60 unsigned int ch, k, position = 0 ;
cannam@125 61
cannam@125 62 gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.9) ;
cannam@125 63
cannam@125 64 for (ch = 1 ; ch <= 8 ; ch++)
cannam@125 65 { SNDFILE *file ;
cannam@125 66 SF_INFO wsfinfo, rsfinfo ;
cannam@125 67 sf_count_t wframes = ARRAY_LEN (float_data) / ch ;
cannam@125 68 double maxdiff ;
cannam@125 69 char filename [256] ;
cannam@125 70
cannam@125 71 snprintf (filename, sizeof (filename), "chan_%d.wav", ch) ;
cannam@125 72 print_test_name (__func__, filename) ;
cannam@125 73
cannam@125 74 sf_info_setup (&wsfinfo, SF_FORMAT_WAV | SF_FORMAT_FLOAT, 48000, ch) ;
cannam@125 75 sf_info_clear (&rsfinfo) ;
cannam@125 76
cannam@125 77 /* Write the test file. */
cannam@125 78 file = test_open_file_or_die (filename, SFM_WRITE, &wsfinfo, SF_FALSE, __LINE__) ;
cannam@125 79 test_writef_float_or_die (file, 0, float_data, wframes, __LINE__) ;
cannam@125 80 sf_close (file) ;
cannam@125 81
cannam@125 82 /* Read it as float and test. */
cannam@125 83 file = test_open_file_or_die (filename, SFM_READ, &rsfinfo, SF_FALSE, __LINE__) ;
cannam@125 84 exit_if_true (rsfinfo.frames == 0,
cannam@125 85 "\n\nLine %d : Frames in file %" PRId64 ".\n\n", __LINE__, rsfinfo.frames) ;
cannam@125 86 exit_if_true (wframes != rsfinfo.frames,
cannam@125 87 "\n\nLine %d : Wrote %" PRId64 ", read %" PRId64 " frames.\n\n", __LINE__, wframes, rsfinfo.frames) ;
cannam@125 88
cannam@125 89 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
cannam@125 90
cannam@125 91 test_readf_float_or_die (file, 0, read_float, rsfinfo.frames, __LINE__) ;
cannam@125 92 compare_float_or_die (float_data, read_float, ch * rsfinfo.frames, __LINE__) ;
cannam@125 93
cannam@125 94 /* Read it as short and test. */
cannam@125 95 test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ;
cannam@125 96 test_readf_short_or_die (file, 0, read_short, rsfinfo.frames, __LINE__) ;
cannam@125 97
cannam@125 98 for (k = 0 ; k < ARRAY_LEN (read_float) ; k++)
cannam@125 99 read_float [k] = read_short [k] * (0.9 / 0x8000) ;
cannam@125 100
cannam@125 101 maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ;
cannam@125 102 exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ;
cannam@125 103
cannam@125 104 /* Read it as int and test. */
cannam@125 105 test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ;
cannam@125 106 test_readf_int_or_die (file, 0, read_int, rsfinfo.frames, __LINE__) ;
cannam@125 107
cannam@125 108 for (k = 0 ; k < ARRAY_LEN (read_float) ; k++)
cannam@125 109 read_float [k] = read_int [k] * (0.9 / 0x80000000) ;
cannam@125 110
cannam@125 111 maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ;
cannam@125 112 exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ;
cannam@125 113
cannam@125 114 sf_close (file) ;
cannam@125 115 unlink (filename) ;
cannam@125 116 printf ("ok\n") ;
cannam@125 117 } ;
cannam@125 118
cannam@125 119 return ;
cannam@125 120 } /* channel_test */
cannam@125 121
cannam@125 122 static double
cannam@125 123 max_diff (const float *a, const float *b, unsigned int len, unsigned int * position)
cannam@125 124 { double mdiff = 0.0, diff ;
cannam@125 125 unsigned int k ;
cannam@125 126
cannam@125 127 for (k = 0 ; k < len ; k++)
cannam@125 128 { diff = fabs (a [k] - b [k]) ;
cannam@125 129 if (diff > mdiff)
cannam@125 130 { mdiff = diff ;
cannam@125 131 *position = k ;
cannam@125 132 } ;
cannam@125 133 } ;
cannam@125 134
cannam@125 135 return mdiff ;
cannam@125 136 } /* max_diff */