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 /* Copyright (C) 2011 Google Inc. All rights reserved.
7  * Use of this source code is governed by a BSD-style license that can be
8  * found in the LICENSE.WEBKIT file.
9  */
10 
11 #ifndef DRC_KERNEL_H_
12 #define DRC_KERNEL_H_
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #define DRC_NUM_CHANNELS 2
19 
20 struct drc_kernel {
21 	float sample_rate;
22 
23 	/* The detector_average is the target gain obtained by looking at the
24 	 * future samples in the lookahead buffer and applying the compression
25 	 * curve on them. compressor_gain is the gain applied to the current
26 	 * samples. compressor_gain moves towards detector_average with the
27 	 * speed envelope_rate which is calculated once for each division (32
28 	 * frames). */
29 	float detector_average;
30 	float compressor_gain;
31 	int enabled;
32 	int processed;
33 
34 	/* Lookahead section. */
35 	unsigned last_pre_delay_frames;
36 	float *pre_delay_buffers[DRC_NUM_CHANNELS];
37 	int pre_delay_read_index;
38 	int pre_delay_write_index;
39 
40 	float max_attack_compression_diff_db;
41 
42 	/* Amount of input change in dB required for 1 dB of output change.
43 	 * This applies to the portion of the curve above knee_threshold
44 	 * (see below).
45 	 */
46 	float ratio;
47 	float slope; /* Inverse ratio. */
48 
49 	/* The input to output change below the threshold is 1:1. */
50 	float linear_threshold;
51 	float db_threshold;
52 
53 	/* db_knee is the number of dB above the threshold before we enter the
54 	 * "ratio" portion of the curve.  The portion between db_threshold and
55 	 * (db_threshold + db_knee) is the "soft knee" portion of the curve
56 	 * which transitions smoothly from the linear portion to the ratio
57 	 * portion. knee_threshold is db_to_linear(db_threshold + db_knee).
58 	 */
59 	float db_knee;
60 	float knee_threshold;
61 	float ratio_base;
62 
63 	/* Internal parameter for the knee portion of the curve. */
64 	float K;
65 
66 	/* The release frames coefficients */
67 	float kA, kB, kC, kD, kE;
68 
69 	/* Calculated parameters */
70 	float master_linear_gain;
71 	float attack_frames;
72 	float sat_release_frames_inv_neg;
73 	float sat_release_rate_at_neg_two_db;
74 	float knee_alpha, knee_beta;
75 
76 	/* envelope for the current division */
77 	float envelope_rate;
78 	float scaled_desired_gain;
79 };
80 
81 /* Initializes a drc kernel */
82 void dk_init(struct drc_kernel *dk, float sample_rate);
83 
84 /* Frees a drc kernel */
85 void dk_free(struct drc_kernel *dk);
86 
87 /* Sets the parameters of a drc kernel. See drc.h for details */
88 void dk_set_parameters(struct drc_kernel *dk,
89 		       float db_threshold,
90 		       float db_knee,
91 		       float ratio,
92 		       float attack_time,
93 		       float release_time,
94 		       float pre_delay_time,
95 		       float db_post_gain,
96 		       float releaseZone1,
97 		       float releaseZone2,
98 		       float releaseZone3,
99 		       float releaseZone4
100 		       );
101 
102 /* Enables or disables a drc kernel */
103 void dk_set_enabled(struct drc_kernel *dk, int enabled);
104 
105 /* Performs stereo-linked compression.
106  * Args:
107  *    dk - The DRC kernel.
108  *    data - The pointers to the audio sample buffer. One pointer per channel.
109  *    count - The number of audio samples per channel.
110  */
111 void dk_process(struct drc_kernel *dk, float *data_channels[], unsigned count);
112 
113 #ifdef __cplusplus
114 } /* extern "C" */
115 #endif
116 
117 #endif /* DRC_KERNEL_H_ */
118