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 #include <stdio.h>
6 #include <stdlib.h>
7 #include <time.h>
8 
9 #include "dsp_test_util.h"
10 #include "dsp_util.h"
11 #include "drc.h"
12 #include "raw.h"
13 
14 #ifndef min
15 #define min(a, b) ({ __typeof__(a) _a = (a);	\
16 			__typeof__(b) _b = (b);	\
17 			_a < _b ? _a : _b; })
18 #endif
19 
tp_diff(struct timespec * tp2,struct timespec * tp1)20 static double tp_diff(struct timespec *tp2, struct timespec *tp1)
21 {
22 	return (tp2->tv_sec - tp1->tv_sec)
23 		+ (tp2->tv_nsec - tp1->tv_nsec) * 1e-9;
24 }
25 
process(struct drc * drc,float * buf,size_t frames)26 static void process(struct drc *drc, float *buf, size_t frames)
27 {
28 	struct timespec tp1, tp2;
29 	int start;
30 	float *data[2];
31 	int chunk;
32 	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp1);
33 	for (start = 0; start < frames;) {
34 		data[0] = buf + start;
35 		data[1] = buf + start + frames;
36 		chunk = min(DRC_PROCESS_MAX_FRAMES, frames - start);
37 		drc_process(drc, data, chunk);
38 		start += chunk;
39 	}
40 	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp2);
41 	printf("drc processing takes %g seconds for %zu samples\n",
42 	       tp_diff(&tp2, &tp1), frames * 2);
43 }
44 
main(int argc,char ** argv)45 int main(int argc, char **argv)
46 {
47 	double NQ = 44100 / 2; /* nyquist frequency */
48 	struct drc *drc;
49 	size_t frames;
50 	float *buf;
51 
52 	if (argc != 3) {
53 		printf("Usage: drc_test input.raw output.raw\n");
54 		return 1;
55 	}
56 
57 	dsp_enable_flush_denormal_to_zero();
58 	dsp_util_clear_fp_exceptions();
59 	drc = drc_new(44100);
60 
61 	drc->emphasis_disabled = 0;
62 	drc_set_param(drc, 0, PARAM_CROSSOVER_LOWER_FREQ, 0);
63 	drc_set_param(drc, 0, PARAM_ENABLED, 1);
64 	drc_set_param(drc, 0, PARAM_THRESHOLD, -29);
65 	drc_set_param(drc, 0, PARAM_KNEE, 3);
66 	drc_set_param(drc, 0, PARAM_RATIO, 6.677);
67 	drc_set_param(drc, 0, PARAM_ATTACK, 0.02);
68 	drc_set_param(drc, 0, PARAM_RELEASE, 0.2);
69 	drc_set_param(drc, 0, PARAM_POST_GAIN, -7);
70 
71 	drc_set_param(drc, 1, PARAM_CROSSOVER_LOWER_FREQ, 200 / NQ);
72 	drc_set_param(drc, 1, PARAM_ENABLED, 1);
73 	drc_set_param(drc, 1, PARAM_THRESHOLD, -32);
74 	drc_set_param(drc, 1, PARAM_KNEE, 23);
75 	drc_set_param(drc, 1, PARAM_RATIO, 12);
76 	drc_set_param(drc, 1, PARAM_ATTACK, 0.02);
77 	drc_set_param(drc, 1, PARAM_RELEASE, 0.2);
78 	drc_set_param(drc, 1, PARAM_POST_GAIN, 0.7);
79 
80 	drc_set_param(drc, 2, PARAM_CROSSOVER_LOWER_FREQ, 1200 / NQ);
81 	drc_set_param(drc, 2, PARAM_ENABLED, 1);
82 	drc_set_param(drc, 2, PARAM_THRESHOLD, -24);
83 	drc_set_param(drc, 2, PARAM_KNEE, 30);
84 	drc_set_param(drc, 2, PARAM_RATIO, 1);
85 	drc_set_param(drc, 2, PARAM_ATTACK, 0.001);
86 	drc_set_param(drc, 2, PARAM_RELEASE, 1);
87 	drc_set_param(drc, 2, PARAM_POST_GAIN, 0);
88 
89 	drc_init(drc);
90 	buf = read_raw(argv[1], &frames);
91 	process(drc, buf, frames);
92 	write_raw(argv[2], buf, frames);
93 	drc_free(drc);
94 	free(buf);
95 	dsp_util_print_fp_exceptions();
96 	return 0;
97 }
98