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