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