comparison src/libsndfile-1.0.27/tests/channel_test.c @ 125:cd6cdf86811e

Current libsndfile source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:22:47 +0100
parents
children
comparison
equal deleted inserted replaced
124:e3d5853d5918 125:cd6cdf86811e
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 #endif
31
32 #include <math.h>
33
34 #include <sndfile.h>
35
36 #include "utils.h"
37
38 #define BUFFER_LEN (1 << 10)
39 #define LOG_BUFFER_SIZE 1024
40
41 static void channel_test (void) ;
42 static double max_diff (const float *a, const float *b, unsigned int len, unsigned int * position) ;
43
44 int
45 main (void) // int argc, char *argv [])
46 { channel_test () ;
47 return 0 ;
48 } /* main */
49
50 /*============================================================================================
51 ** Here are the test functions.
52 */
53
54 static void
55 channel_test (void)
56 { static float float_data [1024] ;
57 static float read_float [1024] ;
58 static int read_int [1024] ;
59 static short read_short [1024] ;
60 unsigned int ch, k, position = 0 ;
61
62 gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.9) ;
63
64 for (ch = 1 ; ch <= 8 ; ch++)
65 { SNDFILE *file ;
66 SF_INFO wsfinfo, rsfinfo ;
67 sf_count_t wframes = ARRAY_LEN (float_data) / ch ;
68 double maxdiff ;
69 char filename [256] ;
70
71 snprintf (filename, sizeof (filename), "chan_%d.wav", ch) ;
72 print_test_name (__func__, filename) ;
73
74 sf_info_setup (&wsfinfo, SF_FORMAT_WAV | SF_FORMAT_FLOAT, 48000, ch) ;
75 sf_info_clear (&rsfinfo) ;
76
77 /* Write the test file. */
78 file = test_open_file_or_die (filename, SFM_WRITE, &wsfinfo, SF_FALSE, __LINE__) ;
79 test_writef_float_or_die (file, 0, float_data, wframes, __LINE__) ;
80 sf_close (file) ;
81
82 /* Read it as float and test. */
83 file = test_open_file_or_die (filename, SFM_READ, &rsfinfo, SF_FALSE, __LINE__) ;
84 exit_if_true (rsfinfo.frames == 0,
85 "\n\nLine %d : Frames in file %" PRId64 ".\n\n", __LINE__, rsfinfo.frames) ;
86 exit_if_true (wframes != rsfinfo.frames,
87 "\n\nLine %d : Wrote %" PRId64 ", read %" PRId64 " frames.\n\n", __LINE__, wframes, rsfinfo.frames) ;
88
89 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
90
91 test_readf_float_or_die (file, 0, read_float, rsfinfo.frames, __LINE__) ;
92 compare_float_or_die (float_data, read_float, ch * rsfinfo.frames, __LINE__) ;
93
94 /* Read it as short and test. */
95 test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ;
96 test_readf_short_or_die (file, 0, read_short, rsfinfo.frames, __LINE__) ;
97
98 for (k = 0 ; k < ARRAY_LEN (read_float) ; k++)
99 read_float [k] = read_short [k] * (0.9 / 0x8000) ;
100
101 maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ;
102 exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ;
103
104 /* Read it as int and test. */
105 test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ;
106 test_readf_int_or_die (file, 0, read_int, rsfinfo.frames, __LINE__) ;
107
108 for (k = 0 ; k < ARRAY_LEN (read_float) ; k++)
109 read_float [k] = read_int [k] * (0.9 / 0x80000000) ;
110
111 maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ;
112 exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ;
113
114 sf_close (file) ;
115 unlink (filename) ;
116 printf ("ok\n") ;
117 } ;
118
119 return ;
120 } /* channel_test */
121
122 static double
123 max_diff (const float *a, const float *b, unsigned int len, unsigned int * position)
124 { double mdiff = 0.0, diff ;
125 unsigned int k ;
126
127 for (k = 0 ; k < len ; k++)
128 { diff = fabs (a [k] - b [k]) ;
129 if (diff > mdiff)
130 { mdiff = diff ;
131 *position = k ;
132 } ;
133 } ;
134
135 return mdiff ;
136 } /* max_diff */