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