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 "eq.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 /* Generates impulse response */
test_ir()31 static void test_ir()
32 {
33 int N = 32768;
34 float *data;
35 struct eq *eq;
36 double NQ = 44100 / 2; /* nyquist frequency */
37 struct timespec tp1, tp2;
38 int i;
39 FILE *ir;
40
41 data = calloc(1, sizeof(float) * N);
42 data[0] = 1;
43
44 eq = eq_new();
45 eq_append_biquad(eq, BQ_PEAKING, 380 / NQ, 3, -10);
46 eq_append_biquad(eq, BQ_PEAKING, 720 / NQ, 3, -12);
47 eq_append_biquad(eq, BQ_PEAKING, 1705 / NQ, 3, -8);
48 eq_append_biquad(eq, BQ_HIGHPASS, 218 / NQ, 0.7, -10.2);
49 eq_append_biquad(eq, BQ_PEAKING, 580 / NQ, 6, -8);
50 eq_append_biquad(eq, BQ_HIGHSHELF, 8000 / NQ, 3, 2);
51
52 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp1);
53 eq_process(eq, data, N);
54 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp2);
55 printf("processing takes %g seconds\n", tp_diff(&tp2, &tp1));
56 eq_free(eq);
57
58 ir = fopen("ir.dat", "w");
59 for (i = 0; i < N; i++)
60 fprintf(ir, "%g\n", data[i]);
61 fclose(ir);
62 free(data);
63 }
64
65 /* Processes a buffer of data chunk by chunk using eq */
process(struct eq * eq,float * data,int count)66 static void process(struct eq *eq, float *data, int count)
67 {
68 int start;
69 for (start = 0; start < count; start += 2048)
70 eq_process(eq, data + start, min(2048, count - start));
71 }
72
73 /* Runs the filters on an input file */
test_file(const char * input_filename,const char * output_filename)74 static void test_file(const char *input_filename, const char *output_filename)
75 {
76 size_t frames;
77 int i;
78 double NQ = 44100 / 2; /* nyquist frequency */
79 struct timespec tp1, tp2;
80 struct eq *eq;
81
82 float *data = read_raw(input_filename, &frames);
83
84 /* Set some data to 0 to test for denormals. */
85 for (i = frames / 10; i < frames; i++)
86 data[i] = 0.0;
87
88 /* Left eq chain */
89 eq = eq_new();
90 eq_append_biquad(eq, BQ_PEAKING, 380 / NQ, 3, -10);
91 eq_append_biquad(eq, BQ_PEAKING, 720 / NQ, 3, -12);
92 eq_append_biquad(eq, BQ_PEAKING, 1705 / NQ, 3, -8);
93 eq_append_biquad(eq, BQ_HIGHPASS, 218 / NQ, 0.7, -10.2);
94 eq_append_biquad(eq, BQ_PEAKING, 580 / NQ, 6, -8);
95 eq_append_biquad(eq, BQ_HIGHSHELF, 8000 / NQ, 3, 2);
96 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp1);
97 process(eq, data, frames);
98 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp2);
99 printf("processing takes %g seconds for %zu samples\n",
100 tp_diff(&tp2, &tp1), frames);
101 eq_free(eq);
102
103 /* Right eq chain */
104 eq = eq_new();
105 eq_append_biquad(eq, BQ_PEAKING, 450 / NQ, 3, -12);
106 eq_append_biquad(eq, BQ_PEAKING, 721 / NQ, 3, -12);
107 eq_append_biquad(eq, BQ_PEAKING, 1800 / NQ, 8, -10.2);
108 eq_append_biquad(eq, BQ_PEAKING, 580 / NQ, 6, -8);
109 eq_append_biquad(eq, BQ_HIGHPASS, 250 / NQ, 0.6578, 0);
110 eq_append_biquad(eq, BQ_HIGHSHELF, 8000 / NQ, 0, 2);
111 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp1);
112 process(eq, data + frames, frames);
113 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp2);
114 printf("processing takes %g seconds for %zu samples\n",
115 tp_diff(&tp2, &tp1), frames);
116 eq_free(eq);
117
118 write_raw(output_filename, data, frames);
119 free(data);
120 }
121
main(int argc,char ** argv)122 int main(int argc, char **argv)
123 {
124 dsp_enable_flush_denormal_to_zero();
125 if (dsp_util_has_denormal())
126 printf("denormal still supported?\n");
127 else
128 printf("denormal disabled\n");
129 dsp_util_clear_fp_exceptions();
130
131 if (argc == 1)
132 test_ir();
133 else if (argc == 3)
134 test_file(argv[1], argv[2]);
135 else
136 printf("Usage: eq_test [input.raw output.raw]\n");
137
138 dsp_util_print_fp_exceptions();
139 return 0;
140 }
141