blob: 5af316d0f0cb14433775e52eb10a2b59d9fb0b99 [file] [log] [blame]
Eric Laurent135ad072010-05-21 06:05:13 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_EFFECTREVERB_H_
18#define ANDROID_EFFECTREVERB_H_
19
Eric Laurentcb281022010-07-08 15:32:51 -070020#include <media/EffectEnvironmentalReverbApi.h>
21#include <media/EffectPresetReverbApi.h>
Eric Laurent135ad072010-05-21 06:05:13 -070022
23
24/*------------------------------------
25 * defines
26 *------------------------------------
27*/
28
29/*
30CIRCULAR() calculates the array index using modulo arithmetic.
31The "trick" is that modulo arithmetic is simplified by masking
32the effective address where the mask is (2^n)-1. This only works
33if the buffer size is a power of two.
34*/
35#define CIRCULAR(base,offset,size) (uint32_t)( \
36 ( \
37 ((int32_t)(base)) + ((int32_t)(offset)) \
38 ) \
39 & size \
40 )
41
42#define NUM_OUTPUT_CHANNELS 2
43#define OUTPUT_CHANNELS CHANNEL_STEREO
44
45#define REVERB_BUFFER_SIZE_IN_SAMPLES_MAX 16384
46
Eric Laurentcb281022010-07-08 15:32:51 -070047#define REVERB_NUM_PRESETS REVERB_PRESET_PLATE // REVERB_PRESET_NONE is not included
Eric Laurent135ad072010-05-21 06:05:13 -070048#define REVERB_MAX_NUM_REFLECTIONS 5 // max num reflections per channel
49
50
51// xfade parameters
52#define REVERB_XFADE_PERIOD_IN_SECONDS (double) (100.0 / 1000.0) // xfade once every this many seconds
53
54
55/**********/
56/* the entire synth uses various flags in a bit field */
57
58/* if flag is set, synth reset has been requested */
59#define REVERB_FLAG_RESET_IS_REQUESTED 0x01 /* bit 0 */
60#define MASK_REVERB_RESET_IS_REQUESTED 0x01
61#define MASK_REVERB_RESET_IS_NOT_REQUESTED (uint32_t)(~MASK_REVERB_RESET_IS_REQUESTED)
62
63/*
64by default, we always want to update ALL channel parameters
65when we reset the synth (e.g., during GM ON)
66*/
67#define DEFAULT_REVERB_FLAGS 0x0
68
69/* coefficients for generating sin, cos */
70#define REVERB_PAN_G2 4294940151 /* -0.82842712474619 = 2 - 4/sqrt(2) */
71/*
72int32_t nPanG1 = +1.0 for sin
73int32_t nPanG1 = -1.0 for cos
74*/
75#define REVERB_PAN_G0 23170 /* 0.707106781186547 = 1/sqrt(2) */
76
77/*************************************************************/
78// define the input injection points
79#define GUARD 5 // safety guard of this many samples
80
81#define MAX_AP_TIME (int) ((20*65536)/1000) // delay time in time units (65536th of sec)
82#define MAX_DELAY_TIME (int) ((65*65536)/1000) // delay time in time units
83#define MAX_EARLY_TIME (int) ((65*65536)/1000) // delay time in time units
84
85#define AP0_IN 0
86
87
88#define REVERB_DEFAULT_ROOM_NUMBER 1 // default preset number
89#define DEFAULT_AP0_GAIN 19400
90#define DEFAULT_AP1_GAIN -19400
91
92#define REVERB_DEFAULT_WET 32767
93#define REVERB_DEFAULT_DRY 0
94
95#define REVERB_WET_MAX 32767
96#define REVERB_WET_MIN 0
97#define REVERB_DRY_MAX 32767
98#define REVERB_DRY_MIN 0
99
100// constants for reverb density
101// The density expressed in permilles changes the Allpass delay in a linear manner in the range defined by
102// AP0_TIME_BASE to AP0_TIME_BASE + AP0_TIME_RANGE
103#define AP0_TIME_BASE (int)((9*65536)/1000)
104#define AP0_TIME_RANGE (int)((4*65536)/1000)
105#define AP1_TIME_BASE (int)((12*65536)/1000)
106#define AP1_TIME_RANGE (int)((8*65536)/1000)
107
108// constants for reverb diffusion
109// The diffusion expressed in permilles changes the Allpass gain in a linear manner in the range defined by
110// AP0_GAIN_BASE to AP0_GAIN_BASE + AP0_GAIN_RANGE
111#define AP0_GAIN_BASE (int)(9830)
112#define AP0_GAIN_RANGE (int)(19660-9830)
113#define AP1_GAIN_BASE (int)(6553)
114#define AP1_GAIN_RANGE (int)(22936-6553)
115
116
117/* parameters for each allpass */
118typedef struct
119{
120 uint16_t m_zApOut; // delay offset for ap out
121
122 int16_t m_nApGain; // gain for ap
123
124 uint16_t m_zApIn; // delay offset for ap in
125
126} allpass_object_t;
127
128
129/* parameters for early reflections */
130typedef struct
131{
132 uint16_t m_zDelay[REVERB_MAX_NUM_REFLECTIONS]; // delay offset for ap out
133
134 int16_t m_nGain[REVERB_MAX_NUM_REFLECTIONS]; // gain for ap
135
136} early_reflection_object_t;
137
138//demo
139typedef struct
140{
141 int16_t m_nRvbLpfFbk;
142 int16_t m_nRvbLpfFwd;
143 int16_t m_nRoomLpfFbk;
144 int16_t m_nRoomLpfFwd;
145
146 int16_t m_nEarlyGain;
147 int16_t m_nEarlyDelay;
148 int16_t m_nLateGain;
149 int16_t m_nLateDelay;
150
151 early_reflection_object_t m_sEarlyL;
152 early_reflection_object_t m_sEarlyR;
153
154 uint16_t m_nMaxExcursion; //28
155 int16_t m_nXfadeInterval;
156
157 int16_t m_nAp0_ApGain; //30
158 int16_t m_nAp0_ApOut;
159 int16_t m_nAp1_ApGain;
160 int16_t m_nAp1_ApOut;
161 int16_t m_nDiffusion;
162
163 int16_t m_rfu4;
164 int16_t m_rfu5;
165 int16_t m_rfu6;
166 int16_t m_rfu7;
167 int16_t m_rfu8;
168 int16_t m_rfu9;
169 int16_t m_rfu10; //43
170
171} reverb_preset_t;
172
173typedef struct
174{
Eric Laurentcb281022010-07-08 15:32:51 -0700175 reverb_preset_t m_sPreset[REVERB_NUM_PRESETS]; // array of presets(does not include REVERB_PRESET_NONE)
Eric Laurent135ad072010-05-21 06:05:13 -0700176
177} reverb_preset_bank_t;
178
179
180/* parameters for each reverb */
181typedef struct
182{
183 /* update counter keeps track of when synth params need updating */
184 /* only needs to be as large as REVERB_UPDATE_PERIOD_IN_SAMPLES */
185 int16_t m_nUpdateCounter;
186
187 uint16_t m_nBaseIndex; // base index for circular buffer
188
189 // reverb delay line offsets, allpass parameters, etc:
190
191 short m_nRevFbkR; // combine feedback reverb right out with dry left in
192 short m_zOutLpfL; // left reverb output
193
194 allpass_object_t m_sAp0; // allpass 0 (left channel)
195
196 uint16_t m_zD0In; // delay offset for delay line D0 in
197
198 short m_nRevFbkL; // combine feedback reverb left out with dry right in
199 short m_zOutLpfR; // right reverb output
200
201 allpass_object_t m_sAp1; // allpass 1 (right channel)
202
203 uint16_t m_zD1In; // delay offset for delay line D1 in
204
205 // delay output taps, notice criss cross order
206 uint16_t m_zD0Self; // self feeds forward d0 --> d0
207
208 uint16_t m_zD1Cross; // cross feeds across d1 --> d0
209
210 uint16_t m_zD1Self; // self feeds forward d1 --> d1
211
212 uint16_t m_zD0Cross; // cross feeds across d0 --> d1
213
214 int16_t m_nSin; // gain for self taps
215
216 int16_t m_nCos; // gain for cross taps
217
218 int16_t m_nSinIncrement; // increment for gain
219
220 int16_t m_nCosIncrement; // increment for gain
221
222 int16_t m_nRvbLpfFwd; // reverb feedback lpf forward gain (includes scaling for mixer)
223
224 int16_t m_nRvbLpfFbk; // reverb feedback lpf feedback gain
225
226 int16_t m_nRoomLpfFwd; // room lpf forward gain (includes scaling for mixer)
227
228 int16_t m_nRoomLpfFbk; // room lpf feedback gain
229
230 uint16_t m_nXfadeInterval; // update/xfade after this many samples
231
232 uint16_t m_nXfadeCounter; // keep track of when to xfade
233
234 int16_t m_nPhase; // -1 <= m_nPhase < 1
235 // but during sin,cos calculations
236 // use m_nPhase/2
237
238 int16_t m_nPhaseIncrement; // add this to m_nPhase each frame
239
240 int16_t m_nNoise; // random noise sample
241
242 uint16_t m_nMaxExcursion; // the taps can excurse +/- this amount
243
244 uint16_t m_bUseNoise; // if TRUE, use noise as input signal
245
246 uint16_t m_bBypass; // if TRUE, then bypass reverb and copy input to output
247
248 int16_t m_nCurrentRoom; // preset number for current room
249
250 int16_t m_nNextRoom; // preset number for next room
251
252 int16_t m_nEarlyGain; // gain for early (widen) signal
253 int16_t m_nEarlyDelay; // initial dealy for early (widen) signal
254 int16_t m_nEarly0in;
255 int16_t m_nEarly1in;
256 int16_t m_nLateGain; // gain for late reverb
257 int16_t m_nLateDelay;
258
259 int16_t m_nDiffusion;
260
261 early_reflection_object_t m_sEarlyL; // left channel early reflections
262 early_reflection_object_t m_sEarlyR; // right channel early reflections
263
264 short m_nDelayLine[REVERB_BUFFER_SIZE_IN_SAMPLES_MAX]; // one large delay line for all reverb elements
265
266 reverb_preset_t pPreset;
267
268 reverb_preset_bank_t m_sPreset;
269
270 //int8_t preset;
271 uint32_t m_nSamplingRate;
272 int32_t m_nUpdatePeriodInBits;
273 int32_t m_nBufferMask;
274 int32_t m_nUpdatePeriodInSamples;
275 int32_t m_nDelay0Out;
276 int32_t m_nDelay1Out;
277 int16_t m_nCosWT_5KHz;
278
279 uint16_t m_Aux; // if TRUE, is connected as auxiliary effect
280 uint16_t m_Preset; // if TRUE, expose preset revert interface
281
282} reverb_object_t;
283
284
285
286typedef struct reverb_module_s {
287 const struct effect_interface_s *itfe;
288 effect_config_t config;
289 reverb_object_t context;
290} reverb_module_t;
291
292/*------------------------------------
293 * Effect API
294 *------------------------------------
295*/
Eric Laurentbe916aa2010-06-01 23:49:17 -0700296int EffectQueryNumberEffects(uint32_t *pNumEffects);
Eric Laurentffe9c252010-06-23 17:38:20 -0700297int EffectQueryEffect(uint32_t index, effect_descriptor_t *pDescriptor);
298int EffectCreate(effect_uuid_t *effectUID, int32_t sessionId, int32_t ioId, effect_interface_t *pInterface);
Eric Laurent135ad072010-05-21 06:05:13 -0700299int EffectRelease(effect_interface_t interface);
300
301static int Reverb_Process(effect_interface_t self, audio_buffer_t *inBuffer, audio_buffer_t *outBuffer);
302static int Reverb_Command(effect_interface_t self, int cmdCode, int cmdSize, void *pCmdData, int *replySize, void *pReplyData);
303
304
305/*------------------------------------
306 * internal functions
307 *------------------------------------
308*/
309
310int Reverb_Init(reverb_module_t *pRvbModule, int aux, int preset);
311int Reverb_Configure(reverb_module_t *pRvbModule, effect_config_t *pConfig, bool init);
312void Reverb_Reset(reverb_object_t *pReverb, bool init);
313
314int Reverb_setParameter (reverb_object_t *pReverb, int32_t param, size_t size, void *pValue);
315int Reverb_getParameter(reverb_object_t *pReverb, int32_t param, size_t *pSize, void *pValue);
316
317/*----------------------------------------------------------------------------
318 * ReverbUpdateXfade
319 *----------------------------------------------------------------------------
320 * Purpose:
321 * Update the xfade parameters as required
322 *
323 * Inputs:
324 * nNumSamplesToAdd - number of samples to write to buffer
325 *
326 * Outputs:
327 *
328 *
329 * Side Effects:
330 * - xfade parameters will be changed
331 *
332 *----------------------------------------------------------------------------
333*/
334static int ReverbUpdateXfade(reverb_object_t* pReverbData, int nNumSamplesToAdd);
335
336/*----------------------------------------------------------------------------
337 * ReverbCalculateNoise
338 *----------------------------------------------------------------------------
339 * Purpose:
340 * Calculate a noise sample and limit its value
341 *
342 * Inputs:
343 * Pointer to reverb context
344 *
345 * Outputs:
346 * new limited noise value
347 *
348 * Side Effects:
349 * - pReverbData->m_nNoise value is updated
350 *
351 *----------------------------------------------------------------------------
352*/
353static uint16_t ReverbCalculateNoise(reverb_object_t *pReverbData);
354
355/*----------------------------------------------------------------------------
356 * ReverbCalculateSinCos
357 *----------------------------------------------------------------------------
358 * Purpose:
359 * Calculate a new sin and cosine value based on the given phase
360 *
361 * Inputs:
362 * nPhase - phase angle
363 * pnSin - input old value, output new value
364 * pnCos - input old value, output new value
365 *
366 * Outputs:
367 *
368 * Side Effects:
369 * - *pnSin, *pnCos are updated
370 *
371 *----------------------------------------------------------------------------
372*/
373static int ReverbCalculateSinCos(int16_t nPhase, int16_t *pnSin, int16_t *pnCos);
374
375/*----------------------------------------------------------------------------
376 * Reverb
377 *----------------------------------------------------------------------------
378 * Purpose:
379 * apply reverb to the given signal
380 *
381 * Inputs:
382 * nNu
383 * pnSin - input old value, output new value
384 * pnCos - input old value, output new value
385 *
386 * Outputs:
387 * number of samples actually reverberated
388 *
389 * Side Effects:
390 *
391 *----------------------------------------------------------------------------
392*/
393static int Reverb(reverb_object_t* pReverbData, int nNumSamplesToAdd, short *pOutputBuffer, short *pInputBuffer);
394
395/*----------------------------------------------------------------------------
396 * ReverbReadInPresets()
397 *----------------------------------------------------------------------------
398 * Purpose: sets global reverb preset bank to defaults
399 *
400 * Inputs:
401 *
402 * Outputs:
403 *
404 *----------------------------------------------------------------------------
405*/
406static int ReverbReadInPresets(reverb_object_t* pReverbData);
407
408
409/*----------------------------------------------------------------------------
410 * ReverbUpdateRoom
411 *----------------------------------------------------------------------------
412 * Purpose:
413 * Update the room's preset parameters as required
414 *
415 * Inputs:
416 *
417 * Outputs:
418 *
419 *
420 * Side Effects:
421 * - reverb paramters (fbk, fwd, etc) will be changed
422 * - m_nCurrentRoom := m_nNextRoom
423 *----------------------------------------------------------------------------
424*/
425static int ReverbUpdateRoom(reverb_object_t* pReverbData, bool fullUpdate);
426
427
428static int ReverbComputeConstants(reverb_object_t *pReverbData, uint32_t samplingRate);
429
430#endif /*ANDROID_EFFECTREVERB_H_*/