1 /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <time.h>
9
10 #include "dsp_test_util.h"
11 #include "dsp_util.h"
12 #include "eq2.h"
13 #include "raw.h"
14
15 #ifndef min
16 #define min(a, b) \
17 ({ \
18 __typeof__(a) _a = (a); \
19 __typeof__(b) _b = (b); \
20 _a < _b ? _a : _b; \
21 })
22 #endif
23
tp_diff(struct timespec * tp2,struct timespec * tp1)24 static double tp_diff(struct timespec *tp2, struct timespec *tp1)
25 {
26 return (tp2->tv_sec - tp1->tv_sec) +
27 (tp2->tv_nsec - tp1->tv_nsec) * 1e-9;
28 }
29
30 /* Processes a buffer of data chunk by chunk using eq2 */
process(struct eq2 * eq2,float * data0,float * data1,int count)31 static void process(struct eq2 *eq2, float *data0, float *data1, int count)
32 {
33 int start;
34 for (start = 0; start < count; start += 2048)
35 eq2_process(eq2, data0 + start, data1 + start,
36 min(2048, count - start));
37 }
38
39 /* Runs the filters on an input file */
test_file(const char * input_filename,const char * output_filename)40 static void test_file(const char *input_filename, const char *output_filename)
41 {
42 size_t frames;
43 int i;
44 double NQ = 44100 / 2; /* nyquist frequency */
45 struct timespec tp1, tp2;
46 struct eq2 *eq2;
47
48 float *data = read_raw(input_filename, &frames);
49
50 /* Set some data to 0 to test for denormals. */
51 for (i = frames / 10; i < frames; i++)
52 data[i] = 0.0;
53
54 /* eq chain */
55 eq2 = eq2_new();
56 eq2_append_biquad(eq2, 0, BQ_PEAKING, 380 / NQ, 3, -10);
57 eq2_append_biquad(eq2, 0, BQ_PEAKING, 720 / NQ, 3, -12);
58 eq2_append_biquad(eq2, 0, BQ_PEAKING, 1705 / NQ, 3, -8);
59 eq2_append_biquad(eq2, 0, BQ_HIGHPASS, 218 / NQ, 0.7, -10.2);
60 eq2_append_biquad(eq2, 0, BQ_PEAKING, 580 / NQ, 6, -8);
61 eq2_append_biquad(eq2, 0, BQ_HIGHSHELF, 8000 / NQ, 3, 2);
62 eq2_append_biquad(eq2, 1, BQ_PEAKING, 450 / NQ, 3, -12);
63 eq2_append_biquad(eq2, 1, BQ_PEAKING, 721 / NQ, 3, -12);
64 eq2_append_biquad(eq2, 1, BQ_PEAKING, 1800 / NQ, 8, -10.2);
65 eq2_append_biquad(eq2, 1, BQ_PEAKING, 580 / NQ, 6, -8);
66 eq2_append_biquad(eq2, 1, BQ_HIGHPASS, 250 / NQ, 0.6578, 0);
67 eq2_append_biquad(eq2, 1, BQ_HIGHSHELF, 8000 / NQ, 0, 2);
68 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp1);
69 process(eq2, data, data + frames, frames);
70 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp2);
71 printf("processing takes %g seconds for %zu samples\n",
72 tp_diff(&tp2, &tp1), frames * 2);
73 eq2_free(eq2);
74
75 write_raw(output_filename, data, frames);
76 free(data);
77 }
78
main(int argc,char ** argv)79 int main(int argc, char **argv)
80 {
81 dsp_enable_flush_denormal_to_zero();
82 if (dsp_util_has_denormal())
83 printf("denormal still supported?\n");
84 else
85 printf("denormal disabled\n");
86 dsp_util_clear_fp_exceptions();
87
88 if (argc == 3)
89 test_file(argv[1], argv[2]);
90 else
91 printf("Usage: eq2_test [input.raw output.raw]\n");
92
93 dsp_util_print_fp_exceptions();
94 return 0;
95 }
96