1 /* SoX Resampler Library      Copyright (c) 2007-16 robs@users.sourceforge.net
2  * Licence for this file: LGPL v2.1                  See LICENCE for details. */
3 
4 /* Utility used to help test the library; not for general consumption.
5  *
6  * Measure the peak bit difference between two files.  */
7 
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include "../src/rint.h"
11 #include "../examples/examples-common.h"
12 
13 #define TYPE 0 /* As vector-gen */
14 
15 #if TYPE
16   #define sample_t double
17   #define N 50
18   #define DIFF(s1,s2) abs(rint32((s1-s2)*ldexp(1,N-1)))
19 #else
20   #define sample_t int32_t
21   #define N 32
22   #define DIFF(s1,s2) abs((int)(s1-s2))
23 #endif
24 
main(int argc,char const * arg[])25 int main(int argc, char const * arg[])
26 {
27   int     two      = !!arg[2][0];
28   FILE    * f1 = fopen(arg[1], "rb"), * f2 = two? fopen(arg[2], "rb") : 0;
29   double  rate     = atof (arg[3]), /* Sample-rate */
30           skip_len = atof (arg[4]), /* Skip length in seconds */
31           len      = atof (arg[5]), /* Compare length in seconds */ r;
32   int i = 0, count = rint32(rate * len), max = 0, diff;
33   sample_t s1, s2;
34 
35   fseek(f1, rint32(rate * skip_len) * (int)sizeof(s1), SEEK_CUR);
36   if (two) {
37     fseek(f2, rint32(rate * skip_len) * (int)sizeof(s2), SEEK_CUR);
38     for (; i < count &&
39         fread(&s1, sizeof(s1), 1, f1) &&
40         fread(&s2, sizeof(s2), 1, f2); ++i) {
41       diff = DIFF(s1, s2);
42       max = max(max, diff);
43     }
44   }
45   else for (; i < count && fread(&s1, sizeof(s1), 1, f1); ++i) {
46     diff = DIFF(s1, 0);
47     max = max(max, diff);
48   }
49 
50   if (i != count) {
51     fprintf(stderr, "incorrect file length\n");
52     return 1;
53   }
54   printf("%f\n", r = N-log(max)/log(2));
55   return argc>6? r<atof(arg[6]) : 0;
56 }
57