Debian Codegraph @

tests/sample_test.c

raw
1 /*
2 * sample_test.c - Sample audio conversion tests
3 *
4 * Tests every combination of audio format conversions (484 combinations).
5 * This is done by creating a double audio format triangle waveform and then
6 * for each transformation format pair, converting this waveform to the
7 * first format, then the second, then back to double again and comparing
8 * against the original.
9 *
10 * libInstPatch
11 * Copyright (C) 1999-2014 Element Green <[email protected]>
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public License
15 * as published by the Free Software Foundation; version 2.1
16 * of the License only.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, see <https://www.gnu.org/licenses/>.
25 */
26 #include <libinstpatch/libinstpatch.h>
27
28 #define DEFAULT_AUDIO_SIZE (32 * 1024) /* default test waveform size in samples */
29 #define WAVEFORM_PERIOD 1684 /* something to exercise LSB bytes too */
30 #define MAX_DIFF_ALLOWED 0.016 /* maximum difference allowed */
31
32 #define WAVEFORM_QUARTER (WAVEFORM_PERIOD / 4)
33
34 /* all available sample format combinations */
35 int testformats[] =
36 {
37 IPATCH_SAMPLE_8BIT,
38 IPATCH_SAMPLE_16BIT,
39 IPATCH_SAMPLE_24BIT,
40 IPATCH_SAMPLE_32BIT,
41 IPATCH_SAMPLE_FLOAT,
42 IPATCH_SAMPLE_DOUBLE,
43 IPATCH_SAMPLE_REAL24BIT,
44
45 IPATCH_SAMPLE_8BIT | IPATCH_SAMPLE_UNSIGNED,
46 IPATCH_SAMPLE_16BIT | IPATCH_SAMPLE_UNSIGNED,
47 IPATCH_SAMPLE_24BIT | IPATCH_SAMPLE_UNSIGNED,
48 IPATCH_SAMPLE_32BIT | IPATCH_SAMPLE_UNSIGNED,
49 IPATCH_SAMPLE_REAL24BIT | IPATCH_SAMPLE_UNSIGNED,
50
51 IPATCH_SAMPLE_16BIT | IPATCH_SAMPLE_BENDIAN,
52 IPATCH_SAMPLE_24BIT | IPATCH_SAMPLE_BENDIAN,
53 IPATCH_SAMPLE_32BIT | IPATCH_SAMPLE_BENDIAN,
54 IPATCH_SAMPLE_FLOAT | IPATCH_SAMPLE_BENDIAN,
55 IPATCH_SAMPLE_DOUBLE | IPATCH_SAMPLE_BENDIAN,
56 IPATCH_SAMPLE_REAL24BIT | IPATCH_SAMPLE_BENDIAN,
57
58 IPATCH_SAMPLE_16BIT | IPATCH_SAMPLE_UNSIGNED | IPATCH_SAMPLE_BENDIAN,
59 IPATCH_SAMPLE_24BIT | IPATCH_SAMPLE_UNSIGNED | IPATCH_SAMPLE_BENDIAN,
60 IPATCH_SAMPLE_32BIT | IPATCH_SAMPLE_UNSIGNED | IPATCH_SAMPLE_BENDIAN,
61 IPATCH_SAMPLE_REAL24BIT | IPATCH_SAMPLE_UNSIGNED | IPATCH_SAMPLE_BENDIAN
62 };
63
64 #define SAMPLE_FORMAT_COUNT G_N_ELEMENTS (testformats)
65
66 static int test_size = DEFAULT_AUDIO_SIZE;
67 static gboolean verbose = FALSE;
68
69 static GOptionEntry entries[] =
70 {
71 { "size", 's', 0, G_OPTION_ARG_INT, &test_size, "Size of test waveform in samples (default=32768)", NULL },
72 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
73 { NULL }
74 };
75
76 int main(int argc, char *argv[])
77 {
78 IpatchSampleTransform *trans;
79 gpointer srctrans, desttrans;
80 double *dwave, *fintrans;
81 guint periodpos;
82 int srcform, destform;
83 int isrc, idest;
84 double d, maxdiff;
85 int i, maxindex, failcount = 0;
86 GError *error = NULL;
87 GOptionContext *context;
88
89 context = g_option_context_new("- test libInstPatch sample format conversions");
90 g_option_context_add_main_entries(context, entries, "sample_test");
91
92 if(!g_option_context_parse(context, &argc, &argv, &error))
93 {
94 printf("Failed to parse command line arguments: %s\n",
95 ipatch_gerror_message(error));
96 return 1;
97 }
98
99 g_option_context_free(context);
100
101 ipatch_init();
102
103 dwave = g_new(double, test_size); /* allocate audio buffer (double format) */
104 srctrans = g_malloc(8 * test_size); /* source format transform buf */
105 desttrans = g_malloc(8 * test_size); /* destination format transform buf */
106 fintrans = g_malloc(8 * test_size); /* final transform buf */
107
108 /* create sample transform object and allocate its buffer */
109 trans = ipatch_sample_transform_new(0, 0, 0);
110 ipatch_sample_transform_alloc_size(trans, 32 * 1024);
111
112 /* generate triangle waveform */
113 for(i = 0; i < test_size; i++)
114 {
115 periodpos = i % WAVEFORM_PERIOD;
116
117 if(periodpos <= WAVEFORM_QUARTER)
118 {
119 dwave[i] = periodpos / (double)WAVEFORM_QUARTER;
120 }
121 else if(periodpos <= WAVEFORM_QUARTER * 3)
122 {
123 dwave[i] = -((periodpos - WAVEFORM_QUARTER) / (double)WAVEFORM_QUARTER - 1.0);
124 }
125 else
126 {
127 dwave[i] = (periodpos - WAVEFORM_QUARTER * 3) / (double)WAVEFORM_QUARTER - 1.0;
128 }
129 }
130
131 /* Convert between all possible format pairs using the following steps:
132 * - Convert double to first format of format pair
133 * - Convert from first format to second format
134 * - Convert second format back to double
135 * - Calculate maximum sample difference between new double audio and original
136 */
137
138 for(isrc = 0; isrc < SAMPLE_FORMAT_COUNT; isrc++)
139 {
140 srcform = testformats[isrc];
141
142 for(idest = 0; idest < SAMPLE_FORMAT_COUNT; idest++)
143 {
144 destform = testformats[idest];
145
146 /* convert double waveform to source format */
147 ipatch_sample_transform_set_formats(trans, IPATCH_SAMPLE_DOUBLE
148 | IPATCH_SAMPLE_ENDIAN_HOST, srcform,
149 IPATCH_SAMPLE_UNITY_CHANNEL_MAP);
150 ipatch_sample_transform_convert(trans, dwave, srctrans, test_size);
151
152 /* convert source format to destination format */
153 ipatch_sample_transform_set_formats(trans, srcform, destform,
154 IPATCH_SAMPLE_UNITY_CHANNEL_MAP);
155 ipatch_sample_transform_convert(trans, srctrans, desttrans, test_size);
156
157 /* convert destination format to final double output */
158 ipatch_sample_transform_set_formats(trans, destform, IPATCH_SAMPLE_DOUBLE
159 | IPATCH_SAMPLE_ENDIAN_HOST,
160 IPATCH_SAMPLE_UNITY_CHANNEL_MAP);
161 ipatch_sample_transform_convert(trans, desttrans, fintrans, test_size);
162
163 /* compare final waveform against original */
164 for(i = 0, maxdiff = 0.0, maxindex = 0; i < test_size; i++)
165 {
166 d = dwave[i] - fintrans[i];
167
168 if(d < 0.0)
169 {
170 d = -d;
171 }
172
173 if(d > maxdiff)
174 {
175 maxdiff = d;
176 maxindex = i;
177 }
178 }
179
180 if(verbose)
181 printf("Convert format %03x to %03x%s: maxdiff=%0.6f, sample=%d\n",
182 srcform, destform, maxdiff > MAX_DIFF_ALLOWED ? " FAILED" : "",
183 maxdiff, maxindex);
184
185 if(maxdiff > MAX_DIFF_ALLOWED)
186 {
187 if(!verbose)
188 printf("Convert format %03x to %03x FAILED: maxdiff=%0.6f, sample=%d\n",
189 srcform, destform, maxdiff, maxindex);
190
191 failcount++;
192 }
193 }
194 }
195
196 ipatch_sample_transform_free(trans);
197
198 g_free(dwave);
199 g_free(srctrans);
200 g_free(desttrans);
201 g_free(fintrans);
202
203 if(failcount > 0)
204 {
205 printf("%d of %d format conversions FAILED\n", failcount,
206 SAMPLE_FORMAT_COUNT * SAMPLE_FORMAT_COUNT);
207 return 1;
208 }
209
210 printf("All %d sample format conversions PASSED\n",
211 SAMPLE_FORMAT_COUNT * SAMPLE_FORMAT_COUNT);
212
213 return 0;
214 }