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 main_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, float db_threshold, float db_knee, 89 float ratio, float attack_time, float release_time, 90 float pre_delay_time, float db_post_gain, 91 float releaseZone1, float releaseZone2, 92 float releaseZone3, float releaseZone4); 93 94 /* Enables or disables a drc kernel */ 95 void dk_set_enabled(struct drc_kernel *dk, int enabled); 96 97 /* Performs stereo-linked compression. 98 * Args: 99 * dk - The DRC kernel. 100 * data - The pointers to the audio sample buffer. One pointer per channel. 101 * count - The number of audio samples per channel. 102 */ 103 void dk_process(struct drc_kernel *dk, float *data_channels[], unsigned count); 104 105 #ifdef __cplusplus 106 } /* extern "C" */ 107 #endif 108 109 #endif /* DRC_KERNEL_H_ */ 110