3 * Bluetooth low-complexity, subband codec (SBC) library
5 * Copyright (C) 2008-2010 Nokia Corporation
6 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
7 * Copyright (C) 2007-2008 Frederic Dalleau <fdalleau@free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 static double sampletobits(short sample16, int verbose)
46 printf("===> sampletobits(%hd, %04hX)\n", sample16, sample16);
53 printf("%d", (sample16 < 0) ? 1 : 0);
56 for (i = 1; i < 16; i++) {
57 bit = (unsigned short) sample16;
65 bits += (1.0 / pow(2.0, i));
74 static int calculate_rms_level(SNDFILE * sndref, SF_INFO * infosref,
75 SNDFILE * sndtst, SF_INFO * infostst,
76 int accuracy, char *csvname)
78 short refsample[MAXCHANNELS], tstsample[MAXCHANNELS];
79 double refbits, tstbits;
80 double rms_accu[MAXCHANNELS];
81 double rms_level[MAXCHANNELS];
82 double rms_limit = 1.0 / (pow(2.0, accuracy - 1) * pow(12.0, 0.5));
84 int i, j, r1, r2, verdict;
87 csv = fopen(csvname, "wt");
91 for (j = 0; j < infostst->channels; j++)
92 fprintf(csv, "ref channel %d;tst channel %d;", j, j);
96 sf_seek(sndref, 0, SEEK_SET);
97 sf_seek(sndtst, 0, SEEK_SET);
99 memset(rms_accu, 0, sizeof(rms_accu));
100 memset(rms_level, 0, sizeof(rms_level));
102 for (i = 0; i < infostst->frames; i++) {
104 fprintf(csv, "%d;", i);
106 r1 = sf_read_short(sndref, refsample, infostst->channels);
107 if (r1 != infostst->channels) {
108 printf("Failed to read reference data: %s "
109 "(r1=%d, channels=%d)",
110 sf_strerror(sndref), r1,
117 r2 = sf_read_short(sndtst, tstsample, infostst->channels);
118 if (r2 != infostst->channels) {
119 printf("Failed to read test data: %s "
120 "(r2=%d, channels=%d)\n",
121 sf_strerror(sndtst), r2,
128 for (j = 0; j < infostst->channels; j++) {
130 fprintf(csv, "%d;%d;", refsample[j],
133 refbits = sampletobits(refsample[j], 0);
134 tstbits = sampletobits(tstsample[j], 0);
136 rms_accu[j] += pow(tstbits - refbits, 2.0);
140 fprintf(csv, "\r\n");
143 printf("Limit: %f\n", rms_limit);
145 for (j = 0; j < infostst->channels; j++) {
146 printf("Channel %d\n", j);
147 printf("Accumulated %f\n", rms_accu[j]);
148 rms_accu[j] /= (double) infostst->frames;
149 printf("Accumulated / %f = %f\n", (double) infostst->frames,
151 rms_level[j] = sqrt(rms_accu[j]);
152 printf("Level = %f (%f x %f = %f)\n",
153 rms_level[j], rms_level[j], rms_level[j],
154 rms_level[j] * rms_level[j]);
159 for (j = 0; j < infostst->channels; j++) {
160 printf("Channel %d: %f\n", j, rms_level[j]);
162 if (rms_level[j] > rms_limit)
166 printf("%s return %d\n", __FUNCTION__, verdict);
171 static int check_absolute_diff(SNDFILE * sndref, SF_INFO * infosref,
172 SNDFILE * sndtst, SF_INFO * infostst,
175 short refsample[MAXCHANNELS], tstsample[MAXCHANNELS];
176 short refmax[MAXCHANNELS], tstmax[MAXCHANNELS];
177 double refbits, tstbits;
178 double rms_absolute = 1.0 / (pow(2, accuracy - 2));
179 double calc_max[MAXCHANNELS];
185 memset(&refmax, 0, sizeof(refmax));
186 memset(&tstmax, 0, sizeof(tstmax));
187 memset(&calc_max, 0, sizeof(calc_max));
188 memset(&refsample, 0, sizeof(refsample));
189 memset(&tstsample, 0, sizeof(tstsample));
191 sf_seek(sndref, 0, SEEK_SET);
192 sf_seek(sndtst, 0, SEEK_SET);
196 printf("Absolute max: %f\n", rms_absolute);
197 for (i = 0; i < infostst->frames; i++) {
198 r1 = sf_read_short(sndref, refsample, infostst->channels);
200 if (r1 != infostst->channels) {
201 printf("Failed to read reference data: %s "
202 "(r1=%d, channels=%d)",
203 sf_strerror(sndref), r1,
208 r2 = sf_read_short(sndtst, tstsample, infostst->channels);
209 if (r2 != infostst->channels) {
210 printf("Failed to read test data: %s "
211 "(r2=%d, channels=%d)\n",
212 sf_strerror(sndtst), r2,
217 for (j = 0; j < infostst->channels; j++) {
218 refbits = sampletobits(refsample[j], 0);
219 tstbits = sampletobits(tstsample[j], 0);
221 cur_diff = fabs(tstbits - refbits);
223 if (cur_diff > rms_absolute) {
225 /* printf("Channel %d exceeded : fabs(%f - %f) = %f > %f\n", j, tstbits, refbits, cur_diff, rms_absolute); */
229 if (cur_diff > calc_max[j]) {
230 calc_max[j] = cur_diff;
231 refmax[j] = refsample[j];
232 tstmax[j] = tstsample[j];
237 for (j = 0; j < infostst->channels; j++) {
238 printf("Calculated max: %f (%hd-%hd=%hd)\n",
239 calc_max[j], tstmax[j], refmax[j],
240 tstmax[j] - refmax[j]);
243 printf("%s return %d\n", __FUNCTION__, verdict);
248 static void usage(void)
250 printf("SBC conformance test ver %s\n", VERSION);
251 printf("Copyright (c) 2007-2010 Marcel Holtmann\n");
252 printf("Copyright (c) 2007-2008 Frederic Dalleau\n\n");
255 "\tsbctester reference.wav checkfile.wav\n"
256 "\tsbctester integer\n"
259 printf("To test the encoder:\n");
260 printf("\tUse a reference codec to encode original.wav to reference.sbc\n");
261 printf("\tUse sbcenc to encode original.wav to checkfile.sbc\n");
262 printf("\tDecode both file using the reference decoder\n");
263 printf("\tRun sbctester with these two wav files to get the result\n\n");
265 printf("\tA file called out.csv is generated to use the data in a\n");
266 printf("\tspreadsheet application or database.\n\n");
269 int main(int argc, char *argv[])
271 SNDFILE *sndref = NULL;
272 SNDFILE *sndtst = NULL;
277 int pass_rms, pass_absolute, pass, accuracy;
282 printf("Test sampletobits\n");
283 db = sampletobits((short) atoi(argv[1]), 1);
284 printf("db = %f\n", db);
296 printf("opening reference %s\n", ref);
298 sndref = sf_open(ref, SFM_READ, &infosref);
300 printf("Failed to open reference file\n");
304 printf("opening testfile %s\n", tst);
305 sndtst = sf_open(tst, SFM_READ, &infostst);
307 printf("Failed to open test file\n");
312 printf("reference:\n\t%d frames,\n\t%d hz,\n\t%d channels\n",
313 (int) infosref.frames, (int) infosref.samplerate,
314 (int) infosref.channels);
315 printf("testfile:\n\t%d frames,\n\t%d hz,\n\t%d channels\n",
316 (int) infostst.frames, (int) infostst.samplerate,
317 (int) infostst.channels);
319 /* check number of channels */
320 if (infosref.channels > 2 || infostst.channels > 2) {
321 printf("Too many channels\n");
325 /* compare number of samples */
326 if (infosref.samplerate != infostst.samplerate ||
327 infosref.channels != infostst.channels) {
328 printf("Cannot compare files with different charasteristics\n");
332 accuracy = DEFACCURACY;
333 printf("Accuracy: %d\n", accuracy);
335 /* Condition 1 rms level */
336 pass_rms = calculate_rms_level(sndref, &infosref, sndtst, &infostst,
337 accuracy, "out.csv");
341 /* Condition 2 absolute difference */
342 pass_absolute = check_absolute_diff(sndref, &infosref, sndtst,
343 &infostst, accuracy);
344 if (pass_absolute < 0)
348 pass = pass_rms && pass_absolute;
349 printf("Verdict: %s\n", pass ? "pass" : "fail");