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_H_ 12 #define DRC_H_ 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 #include "crossover2.h" 19 #include "drc_kernel.h" 20 #include "eq2.h" 21 22 /* DRC implements a flexible audio dynamics compression effect such as is 23 * commonly used in musical production and game audio. It lowers the volume of 24 * the loudest parts of the signal and raises the volume of the softest parts, 25 * making the sound richer, fuller, and more controlled. 26 * 27 * This is a three band stereo DRC. There are three compressor kernels, and each 28 * can have its own parameters. If a kernel is disabled, it only delays the 29 * signal and does not compress it. 30 * 31 * INPUT 32 * | 33 * +----------+ 34 * | emphasis | 35 * +----------+ 36 * | 37 * +------------+ 38 * | crossover | 39 * +------------+ 40 * / | \ 41 * (low band) (mid band) (high band) 42 * / | \ 43 * +------+ +------+ +------+ 44 * | drc | | drc | | drc | 45 * |kernel| |kernel| |kernel| 46 * +------+ +------+ +------+ 47 * \ | / 48 * \ | / 49 * +-------------+ 50 * | (+) | 51 * +-------------+ 52 * | 53 * +------------+ 54 * | deemphasis | 55 * +------------+ 56 * | 57 * OUTPUT 58 * 59 */ 60 61 /* The parameters of the DRC compressor. 62 * 63 * PARAM_THRESHOLD - The value above which the compression starts, in dB. 64 * PARAM_KNEE - The value above which the knee region starts, in dB. 65 * PARAM_RATIO - The input/output dB ratio after the knee region. 66 * PARAM_ATTACK - The time to reduce the gain by 10dB, in seconds. 67 * PARAM_RELEASE - The time to increase the gain by 10dB, in seconds. 68 * PARAM_PRE_DELAY - The lookahead time for the compressor, in seconds. 69 * PARAM_RELEASE_ZONE[1-4] - The adaptive release curve parameters. 70 * PARAM_POST_GAIN - The static boost value in output, in dB. 71 * PARAM_FILTER_STAGE_GAIN - The gain of each emphasis filter stage. 72 * PARAM_FILTER_STAGE_RATIO - The frequency ratio for each emphasis filter stage 73 * to the previous stage. 74 * PARAM_FILTER_ANCHOR - The frequency of the first emphasis filter, in 75 * normalized frequency (in [0, 1], relative to half of the sample rate). 76 * PARAM_CROSSOVER_LOWER_FREQ - The lower frequency of the band, in normalized 77 * frequency (in [0, 1], relative to half of the sample rate). 78 * PARAM_ENABLED - 1 to enable the compressor, 0 to disable it. 79 */ 80 enum { PARAM_THRESHOLD, 81 PARAM_KNEE, 82 PARAM_RATIO, 83 PARAM_ATTACK, 84 PARAM_RELEASE, 85 PARAM_PRE_DELAY, 86 PARAM_RELEASE_ZONE1, 87 PARAM_RELEASE_ZONE2, 88 PARAM_RELEASE_ZONE3, 89 PARAM_RELEASE_ZONE4, 90 PARAM_POST_GAIN, 91 PARAM_FILTER_STAGE_GAIN, 92 PARAM_FILTER_STAGE_RATIO, 93 PARAM_FILTER_ANCHOR, 94 PARAM_CROSSOVER_LOWER_FREQ, 95 PARAM_ENABLED, 96 PARAM_LAST }; 97 98 /* The number of compressor kernels (also the number of bands). */ 99 #define DRC_NUM_KERNELS 3 100 101 /* The maximum number of frames can be passed to drc_process() call. */ 102 #define DRC_PROCESS_MAX_FRAMES 2048 103 104 /* The default value of PARAM_PRE_DELAY in seconds. */ 105 #define DRC_DEFAULT_PRE_DELAY 0.006f 106 107 struct drc { 108 /* sample rate in Hz */ 109 float sample_rate; 110 111 /* 1 to disable the emphasis and deemphasis, 0 to enable it. */ 112 int emphasis_disabled; 113 114 /* parameters holds the tweakable compressor parameters. */ 115 float parameters[DRC_NUM_KERNELS][PARAM_LAST]; 116 117 /* The emphasis filter and deemphasis filter */ 118 struct eq2 *emphasis_eq; 119 struct eq2 *deemphasis_eq; 120 121 /* The crossover filter */ 122 struct crossover2 xo2; 123 124 /* The compressor kernels */ 125 struct drc_kernel kernel[DRC_NUM_KERNELS]; 126 127 /* Temporary buffer used during drc_process(). The mid and high band 128 * signal is stored in these buffers (the low band is stored in the 129 * original input buffer). */ 130 float *data1[DRC_NUM_CHANNELS]; 131 float *data2[DRC_NUM_CHANNELS]; 132 }; 133 134 /* DRC needs the parameters to be set before initialization. So drc_new() should 135 * be called first to allocated an instance, then drc_set_param() is called 136 * (multiple times) to set the parameters. Finally drc_init() is called to do 137 * the initialization. After that drc_process() can be used to process data. The 138 * sequence is: 139 * 140 * drc_new(); 141 * drc_set_param(); 142 * ... 143 * drc_set_param(); 144 * drc_init(); 145 * drc_process(); 146 * ... 147 * drc_process(); 148 * drc_free(); 149 */ 150 151 /* Allocates a DRC. */ 152 struct drc *drc_new(float sample_rate); 153 154 /* Initializes a DRC. */ 155 void drc_init(struct drc *drc); 156 157 /* Frees a DRC.*/ 158 void drc_free(struct drc *drc); 159 160 /* Processes input data using a DRC. 161 * Args: 162 * drc - The DRC we want to use. 163 * float **data - Pointers to input/output data. The input must be stereo 164 * and one channel is pointed by data[0], another pointed by data[1]. The 165 * output data is stored in the same place. 166 * frames - The number of frames to process. 167 */ 168 void drc_process(struct drc *drc, float **data, int frames); 169 170 /* Sets a parameter for the DRC. 171 * Args: 172 * drc - The DRC we want to use. 173 * index - The index of the kernel we want to set its parameter. 174 * paramID - One of the PARAM_* enum constant. 175 * value - The parameter value 176 */ 177 void drc_set_param(struct drc *drc, int index, unsigned paramID, float value); 178 179 #ifdef __cplusplus 180 } /* extern "C" */ 181 #endif 182 183 #endif /* DRC_H_ */ 184