blob: 1f9b96e2107dbc589b4d555f4e783794e8fa8beb [file] [log] [blame]
Dave Sparks56c99cd2009-08-24 17:35:45 -07001/*----------------------------------------------------------------------------
2 *
3 * File:
4 * eas_math.h
5 *
6 * Contents and purpose:
7 * Contains common math routines for the various audio engines.
8 *
9 *
10 * Copyright Sonic Network Inc. 2005
The Android Open Source Project7df30102009-03-03 19:30:38 -080011
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
Dave Sparks56c99cd2009-08-24 17:35:45 -070023 *
24 *----------------------------------------------------------------------------
25 * Revision Control:
26 * $Revision: 584 $
27 * $Date: 2007-03-08 09:49:24 -0800 (Thu, 08 Mar 2007) $
28 *----------------------------------------------------------------------------
29*/
30
31#ifndef _EAS_MATH_H
32#define _EAS_MATH_H
33
Marco Nelissend216e302015-07-30 12:35:14 -070034#include <stdint.h>
Dave Sparks56c99cd2009-08-24 17:35:45 -070035
36/** coefs for pan, generates sin, cos */
37#define COEFF_PAN_G2 -27146 /* -0.82842712474619 = 2 - 4/sqrt(2) */
38#define COEFF_PAN_G0 23170 /* 0.707106781186547 = 1/sqrt(2) */
39
40/*
41coefficients for approximating
422^x = gn2toX0 + gn2toX1*x + gn2toX2*x^2 + gn2toX3*x^3
43where x is a int.frac number representing number of octaves.
44Actually, we approximate only the 2^(frac) using the power series
45and implement the 2^(int) as a shift, so that
462^x == 2^(int.frac) == 2^(int) * 2^(fract)
47 == (gn2toX0 + gn2toX1*x + gn2toX2*x^2 + gn2toX3*x^3) << (int)
48
49The gn2toX.. were generated using a best fit for a 3rd
50order polynomial, instead of taking the coefficients from
51a truncated Taylor (or Maclaurin?) series.
52*/
53
54#define GN2_TO_X0 32768 /* 1 */
55#define GN2_TO_X1 22833 /* 0.696807861328125 */
56#define GN2_TO_X2 7344 /* 0.22412109375 */
57#define GN2_TO_X3 2588 /* 0.0789794921875 */
58
59/*----------------------------------------------------------------------------
60 * Fixed Point Math
61 *----------------------------------------------------------------------------
62 * These macros are used for fixed point multiplies. If the processor
63 * supports fixed point multiplies, replace these macros with inline
64 * assembly code to improve performance.
65 *----------------------------------------------------------------------------
66*/
67
68/* Fixed point multiply 0.15 x 0.15 = 0.15 returned as 32-bits */
69#define FMUL_15x15(a,b) \
70 /*lint -e(704) <avoid multiply for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -070071 (((int32_t)(a) * (int32_t)(b)) >> 15)
Dave Sparks56c99cd2009-08-24 17:35:45 -070072
73/* Fixed point multiply 0.7 x 0.7 = 0.15 returned as 32-bits */
74#define FMUL_7x7(a,b) \
75 /*lint -e(704) <avoid multiply for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -070076 (((int32_t)(a) * (int32_t)(b) ) << 1)
Dave Sparks56c99cd2009-08-24 17:35:45 -070077
78/* Fixed point multiply 0.8 x 0.8 = 0.15 returned as 32-bits */
79#define FMUL_8x8(a,b) \
80 /*lint -e(704) <avoid multiply for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -070081 (((int32_t)(a) * (int32_t)(b) ) >> 1)
Dave Sparks56c99cd2009-08-24 17:35:45 -070082
83/* Fixed point multiply 0.8 x 1.15 = 0.15 returned as 32-bits */
84#define FMUL_8x15(a,b) \
85 /*lint -e(704) <avoid divide for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -070086 (((int32_t)((a) << 7) * (int32_t)(b)) >> 15)
Dave Sparks56c99cd2009-08-24 17:35:45 -070087
88/* macros for fractional phase accumulator */
89/*
90Note: changed the _U32 to _I32 on 03/14/02. This should not
91affect the phase calculations, and should allow us to reuse these
92macros for other audio sample related math.
93*/
94#define HARDWARE_BIT_WIDTH 32
95
96#define NUM_PHASE_INT_BITS 1
97#define NUM_PHASE_FRAC_BITS 15
98
Marco Nelissend216e302015-07-30 12:35:14 -070099#define PHASE_FRAC_MASK (uint32_t) ((0x1L << NUM_PHASE_FRAC_BITS) -1)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700100
Marco Nelissend216e302015-07-30 12:35:14 -0700101#define GET_PHASE_INT_PART(x) (uint32_t)((uint32_t)(x) >> NUM_PHASE_FRAC_BITS)
102#define GET_PHASE_FRAC_PART(x) (uint32_t)((uint32_t)(x) & PHASE_FRAC_MASK)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700103
104#define DEFAULT_PHASE_FRAC 0
105#define DEFAULT_PHASE_INT 0
106
107/*
108Linear interpolation calculates:
109output = (1-frac) * sample[n] + (frac) * sample[n+1]
110
111where conceptually 0 <= frac < 1
112
113For a fixed point implementation, frac is actually an integer value
114with an implied binary point one position to the left. The value of
115one (unity) is given by PHASE_ONE
116one half and one quarter are useful for 4-point linear interp.
117*/
Marco Nelissend216e302015-07-30 12:35:14 -0700118#define PHASE_ONE (int32_t) (0x1L << NUM_PHASE_FRAC_BITS)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700119
120/*
121 Multiply the signed audio sample by the unsigned fraction.
122- a is the signed audio sample
123- b is the unsigned fraction (cast to signed int as long as coef
124 uses (n-1) or less bits, where n == hardware bit width)
125*/
126#define MULT_AUDIO_COEF(audio,coef) /*lint -e704 <avoid divide for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -0700127 (int32_t)( \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700128 ( \
Marco Nelissend216e302015-07-30 12:35:14 -0700129 ((int32_t)(audio)) * ((int32_t)(coef)) \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700130 ) \
131 >> NUM_PHASE_FRAC_BITS \
132 ) \
133 /* lint +704 <restore checking>*/
134
135/* wet / dry calculation macros */
136#define NUM_WET_DRY_FRAC_BITS 7 // 15
137#define NUM_WET_DRY_INT_BITS 9 // 1
138
139/* define a 1.0 */
Marco Nelissend216e302015-07-30 12:35:14 -0700140#define WET_DRY_ONE (int32_t) ((0x1L << NUM_WET_DRY_FRAC_BITS))
141#define WET_DRY_MINUS_ONE (int32_t) (~WET_DRY_ONE)
142#define WET_DRY_FULL_SCALE (int32_t) (WET_DRY_ONE - 1)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700143
144#define MULT_AUDIO_WET_DRY_COEF(audio,coef) /*lint -e(702) <avoid divide for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -0700145 (int32_t)( \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700146 ( \
Marco Nelissend216e302015-07-30 12:35:14 -0700147 ((int32_t)(audio)) * ((int32_t)(coef)) \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700148 ) \
149 >> NUM_WET_DRY_FRAC_BITS \
150 )
151
152/* Envelope 1 (EG1) calculation macros */
153#define NUM_EG1_INT_BITS 1
154#define NUM_EG1_FRAC_BITS 15
155
156/* the max positive gain used in the synth for EG1 */
157/* SYNTH_FULL_SCALE_EG1_GAIN must match the value in the dls2eas
158converter, otherwise, the values we read from the .eas file are bogus. */
Marco Nelissend216e302015-07-30 12:35:14 -0700159#define SYNTH_FULL_SCALE_EG1_GAIN (int32_t) ((0x1L << NUM_EG1_FRAC_BITS) -1)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700160
161/* define a 1.0 */
Marco Nelissend216e302015-07-30 12:35:14 -0700162#define EG1_ONE (int32_t) ((0x1L << NUM_EG1_FRAC_BITS))
163#define EG1_MINUS_ONE (int32_t) (~SYNTH_FULL_SCALE_EG1_GAIN)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700164
Marco Nelissend216e302015-07-30 12:35:14 -0700165#define EG1_HALF (int32_t) (EG1_ONE/2)
166#define EG1_MINUS_HALF (int32_t) (EG1_MINUS_ONE/2)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700167
168/*
169We implement the EG1 using a linear gain value, which means that the
170attack segment is handled by incrementing (adding) the linear gain.
171However, EG1 treats the Decay, Sustain, and Release differently than
172the Attack portion. For Decay, Sustain, and Release, the gain is
173linear on dB scale, which is equivalent to exponential damping on
174a linear scale. Because we use a linear gain for EG1, we implement
175the Decay and Release as multiplication (instead of incrementing
176as we did for the attack segment).
177Therefore, we need the following macro to implement the multiplication
178(i.e., exponential damping) during the Decay and Release segments of
179the EG1
180*/
181#define MULT_EG1_EG1(gain,damping) /*lint -e(704) <avoid divide for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -0700182 (int32_t)( \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700183 ( \
Marco Nelissend216e302015-07-30 12:35:14 -0700184 ((int32_t)(gain)) * ((int32_t)(damping)) \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700185 ) \
186 >> NUM_EG1_FRAC_BITS \
187 )
188
189// Use the following macro specifically for the filter, when multiplying
190// the b1 coefficient. The 0 <= |b1| < 2, which therefore might overflow
191// in certain conditions because we store b1 as a 1.15 value.
192// Instead, we could store b1 as b1p (b1' == b1 "prime") where
193// b1p == b1/2, thus ensuring no potential overflow for b1p because
194// 0 <= |b1p| < 1
195// However, during the filter calculation, we must account for the fact
196// that we are using b1p instead of b1, and thereby multiply by
197// an extra factor of 2. Rather than multiply by an extra factor of 2,
198// we can instead shift the result right by one less, hence the
199// modified shift right value of (NUM_EG1_FRAC_BITS -1)
200#define MULT_EG1_EG1_X2(gain,damping) /*lint -e(702) <avoid divide for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -0700201 (int32_t)( \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700202 ( \
Marco Nelissend216e302015-07-30 12:35:14 -0700203 ((int32_t)(gain)) * ((int32_t)(damping)) \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700204 ) \
205 >> (NUM_EG1_FRAC_BITS -1) \
206 )
207
208#define SATURATE_EG1(x) /*lint -e{734} saturation operation */ \
Marco Nelissend216e302015-07-30 12:35:14 -0700209 ((int32_t)(x) > SYNTH_FULL_SCALE_EG1_GAIN) ? (SYNTH_FULL_SCALE_EG1_GAIN) : \
210 ((int32_t)(x) < EG1_MINUS_ONE) ? (EG1_MINUS_ONE) : (x);
Dave Sparks56c99cd2009-08-24 17:35:45 -0700211
212
213/* use "digital cents" == "dents" instead of cents */
214/* we coudl re-use the phase frac macros, but if we do,
215we must change the phase macros to cast to _I32 instead of _U32,
216because using a _U32 cast causes problems when shifting the exponent
217for the 2^x calculation, because right shift a negative values MUST
218be sign extended, or else the 2^x calculation is wrong */
219
220/* use "digital cents" == "dents" instead of cents */
221#define NUM_DENTS_FRAC_BITS 12
222#define NUM_DENTS_INT_BITS (HARDWARE_BIT_WIDTH - NUM_DENTS_FRAC_BITS)
223
Marco Nelissend216e302015-07-30 12:35:14 -0700224#define DENTS_FRAC_MASK (int32_t) ((0x1L << NUM_DENTS_FRAC_BITS) -1)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700225
226#define GET_DENTS_INT_PART(x) /*lint -e(704) <avoid divide for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -0700227 (int32_t)((int32_t)(x) >> NUM_DENTS_FRAC_BITS)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700228
Marco Nelissend216e302015-07-30 12:35:14 -0700229#define GET_DENTS_FRAC_PART(x) (int32_t)((int32_t)(x) & DENTS_FRAC_MASK)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700230
Marco Nelissend216e302015-07-30 12:35:14 -0700231#define DENTS_ONE (int32_t) (0x1L << NUM_DENTS_FRAC_BITS)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700232
233/* use CENTS_TO_DENTS to convert a value in cents to dents */
Marco Nelissend216e302015-07-30 12:35:14 -0700234#define CENTS_TO_DENTS (int32_t) (DENTS_ONE * (0x1L << NUM_EG1_FRAC_BITS) / 1200L) \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700235
236
237/*
238For gain, the LFO generates a value that modulates in terms
239of dB. However, we use a linear gain value, so we must convert
240the LFO value in dB to a linear gain. Normally, we would use
241linear gain = 10^x, where x = LFO value in dB / 20.
242Instead, we implement 10^x using our 2^x approximation.
243because
244
245 10^x = 2^(log2(10^x)) = 2^(x * log2(10))
246
247so we need to multiply by log2(10) which is just a constant.
248Ah, but just wait -- our 2^x actually doesn't exactly implement
2492^x, but it actually assumes that the input is in cents, and within
250the 2^x approximation converts its input from cents to octaves
251by dividing its input by 1200.
252
253So, in order to convert the LFO gain value in dB to something
254that our existing 2^x approximation can use, multiply the LFO gain
255by log2(10) * 1200 / 20
256
257The divide by 20 helps convert dB to linear gain, and we might
258as well incorporate that operation into this conversion.
259Of course, we need to keep some fractional bits, so multiply
260the constant by NUM_EG1_FRAC_BITS
261*/
262
263/* use LFO_GAIN_TO_CENTS to convert the LFO gain value to cents */
264#if 0
265#define DOUBLE_LOG2_10 (double) (3.32192809488736) /* log2(10) */
266
267#define DOUBLE_LFO_GAIN_TO_CENTS (double) \
268 ( \
269 (DOUBLE_LOG2_10) * \
270 1200.0 / \
271 20.0 \
272 )
273
Marco Nelissend216e302015-07-30 12:35:14 -0700274#define LFO_GAIN_TO_CENTS (int32_t) \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700275 ( \
276 DOUBLE_LFO_GAIN_TO_CENTS * \
277 (0x1L << NUM_EG1_FRAC_BITS) \
278 )
279#endif
280
Marco Nelissend216e302015-07-30 12:35:14 -0700281#define LFO_GAIN_TO_CENTS (int32_t) (1671981156L >> (23 - NUM_EG1_FRAC_BITS))
Dave Sparks56c99cd2009-08-24 17:35:45 -0700282
283
284#define MULT_DENTS_COEF(dents,coef) /*lint -e704 <avoid divide for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -0700285 (int32_t)( \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700286 ( \
Marco Nelissend216e302015-07-30 12:35:14 -0700287 ((int32_t)(dents)) * ((int32_t)(coef)) \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700288 ) \
289 >> NUM_DENTS_FRAC_BITS \
290 ) \
291 /* lint +e704 <restore checking>*/
292
293/* we use 16-bits in the PC per audio sample */
294#define BITS_PER_AUDIO_SAMPLE 16
295
296/* we define 1 as 1.0 - 1 LSbit */
Marco Nelissend216e302015-07-30 12:35:14 -0700297#define DISTORTION_ONE (int32_t)((0x1L << (BITS_PER_AUDIO_SAMPLE-1)) -1)
298#define DISTORTION_MINUS_ONE (int32_t)(~DISTORTION_ONE)
Dave Sparks56c99cd2009-08-24 17:35:45 -0700299
300/* drive coef is given as int.frac */
301#define NUM_DRIVE_COEF_INT_BITS 1
302#define NUM_DRIVE_COEF_FRAC_BITS 4
303
304#define MULT_AUDIO_DRIVE(audio,drive) /*lint -e(702) <avoid divide for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -0700305 (int32_t) ( \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700306 ( \
Marco Nelissend216e302015-07-30 12:35:14 -0700307 ((int32_t)(audio)) * ((int32_t)(drive)) \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700308 ) \
309 >> NUM_DRIVE_COEF_FRAC_BITS \
310 )
311
312#define MULT_AUDIO_AUDIO(audio1,audio2) /*lint -e(702) <avoid divide for performance>*/ \
Marco Nelissend216e302015-07-30 12:35:14 -0700313 (int32_t) ( \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700314 ( \
Marco Nelissend216e302015-07-30 12:35:14 -0700315 ((int32_t)(audio1)) * ((int32_t)(audio2)) \
Dave Sparks56c99cd2009-08-24 17:35:45 -0700316 ) \
317 >> (BITS_PER_AUDIO_SAMPLE-1) \
318 )
319
320#define SATURATE(x) \
Marco Nelissend216e302015-07-30 12:35:14 -0700321 ((((int32_t)(x)) > DISTORTION_ONE) ? (DISTORTION_ONE) : \
322 (((int32_t)(x)) < DISTORTION_MINUS_ONE) ? (DISTORTION_MINUS_ONE) : ((int32_t)(x)));
Dave Sparks56c99cd2009-08-24 17:35:45 -0700323
324
325
326/*----------------------------------------------------------------------------
327 * EAS_Calculate2toX()
328 *----------------------------------------------------------------------------
329 * Purpose:
330 * Calculate 2^x
331 *
332 * Inputs:
333 * nCents - measured in cents
334 *
335 * Outputs:
336 * nResult - int.frac result (where frac has NUM_DENTS_FRAC_BITS)
337 *
338 * Side Effects:
339 *
340 *----------------------------------------------------------------------------
341*/
342EAS_I32 EAS_Calculate2toX (EAS_I32 nCents);
343
344/*----------------------------------------------------------------------------
345 * EAS_LogToLinear16()
346 *----------------------------------------------------------------------------
347 * Purpose:
348 * Transform log value to linear gain multiplier using piece-wise linear
349 * approximation
350 *
351 * Inputs:
352 * nGain - log scale value in 20.10 format. Even though gain is normally
353 * stored in 6.10 (16-bit) format we use 32-bit numbers here to eliminate
354 * the need for saturation checking when combining gain values.
355 *
356 * Outputs:
357 * Returns a 16-bit linear value approximately equal to 2^(nGain/1024)
358 *
359 * Side Effects:
360 *
361 *----------------------------------------------------------------------------
362*/
363EAS_U16 EAS_LogToLinear16 (EAS_I32 nGain);
364
365/*----------------------------------------------------------------------------
366 * EAS_VolumeToGain()
367 *----------------------------------------------------------------------------
368 * Purpose:
369 * Transform volume control in 1dB increments to gain multiplier
370 *
371 * Inputs:
372 * volume - 100 = 0dB, 99 = -1dB, 0 = -inf
373 *
374 * Outputs:
375 * Returns a 16-bit linear value
376 *----------------------------------------------------------------------------
377*/
378EAS_I16 EAS_VolumeToGain (EAS_INT volume);
379
380/*----------------------------------------------------------------------------
381 * EAS_fsqrt()
382 *----------------------------------------------------------------------------
383 * Purpose:
384 * Calculates the square root of a 32-bit fixed point value
385 *
386 * Inputs:
387 * n = value of interest
388 *
389 * Outputs:
390 * returns the square root of n
391 *
392 *----------------------------------------------------------------------------
393*/
394EAS_U16 EAS_fsqrt (EAS_U32 n);
395
396/*----------------------------------------------------------------------------
397 * EAS_flog2()
398 *----------------------------------------------------------------------------
399 * Purpose:
400 * Calculates the log2 of a 32-bit fixed point value
401 *
402 * Inputs:
403 * n = value of interest
404 *
405 * Outputs:
406 * returns the log2 of n
407 *
408 *----------------------------------------------------------------------------
409*/
410EAS_I32 EAS_flog2 (EAS_U32 n);
411
412#endif
413