• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
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_EFFECTSMATH_H_
18  #define ANDROID_EFFECTSMATH_H_
19  
20  #include <stdint.h>
21  
22  #if __cplusplus
23  extern "C" {
24  #endif
25  
26  /** coefs for pan, generates sin, cos */
27  #define COEFF_PAN_G2    -27146    /* -0.82842712474619 = 2 - 4/sqrt(2) */
28  #define COEFF_PAN_G0    23170     /* 0.707106781186547 = 1/sqrt(2) */
29  
30  /*
31  coefficients for approximating
32  2^x = gn2toX0 + gn2toX1*x + gn2toX2*x^2 + gn2toX3*x^3
33  where x is a int.frac number representing number of octaves.
34  Actually, we approximate only the 2^(frac) using the power series
35  and implement the 2^(int) as a shift, so that
36  2^x == 2^(int.frac) == 2^(int) * 2^(fract)
37      == (gn2toX0 + gn2toX1*x + gn2toX2*x^2 + gn2toX3*x^3) << (int)
38  
39  The gn2toX.. were generated using a best fit for a 3rd
40  order polynomial, instead of taking the coefficients from
41  a truncated Taylor (or Maclaurin?) series.
42  */
43  
44  #define GN2_TO_X0    32768    /*    1                    */
45  #define GN2_TO_X1    22833    /*    0.696807861328125    */
46  #define GN2_TO_X2    7344    /*    0.22412109375        */
47  #define GN2_TO_X3    2588    /*    0.0789794921875        */
48  
49  /*----------------------------------------------------------------------------
50   * Fixed Point Math
51   *----------------------------------------------------------------------------
52   * These macros are used for fixed point multiplies. If the processor
53   * supports fixed point multiplies, replace these macros with inline
54   * assembly code to improve performance.
55   *----------------------------------------------------------------------------
56  */
57  
58  /* Fixed point multiply 0.15 x 0.15 = 0.15 returned as 32-bits */
59  #define FMUL_15x15(a,b) \
60      /*lint -e(704) <avoid multiply for performance>*/ \
61      (((int32_t)(a) * (int32_t)(b)) >> 15)
62  
63  /* Fixed point multiply 0.7 x 0.7 = 0.15 returned as 32-bits */
64  #define FMUL_7x7(a,b) \
65      /*lint -e(704) <avoid multiply for performance>*/ \
66      (((int32_t)(a) * (int32_t)(b) ) << 1)
67  
68  /* Fixed point multiply 0.8 x 0.8 = 0.15 returned as 32-bits */
69  #define FMUL_8x8(a,b) \
70      /*lint -e(704) <avoid multiply for performance>*/ \
71      (((int32_t)(a) * (int32_t)(b) ) >> 1)
72  
73  /* Fixed point multiply 0.8 x 1.15 = 0.15 returned as 32-bits */
74  #define FMUL_8x15(a,b) \
75      /*lint -e(704) <avoid divide for performance>*/ \
76      (((int32_t)((a) << 7) * (int32_t)(b)) >> 15)
77  
78  /* macros for fractional phase accumulator */
79  /*
80  Note: changed the _U32 to _I32 on 03/14/02. This should not
81  affect the phase calculations, and should allow us to reuse these
82  macros for other audio sample related math.
83  */
84  #define HARDWARE_BIT_WIDTH        32
85  
86  #define NUM_PHASE_INT_BITS        1
87  #define NUM_PHASE_FRAC_BITS       15
88  
89  #define PHASE_FRAC_MASK           (uint32_t) ((0x1L << NUM_PHASE_FRAC_BITS) -1)
90  
91  #define GET_PHASE_INT_PART(x)     (uint32_t)((uint32_t)(x) >> NUM_PHASE_FRAC_BITS)
92  #define GET_PHASE_FRAC_PART(x)    (uint32_t)((uint32_t)(x) & PHASE_FRAC_MASK)
93  
94  #define DEFAULT_PHASE_FRAC        0
95  #define DEFAULT_PHASE_INT         0
96  
97  /*
98  Linear interpolation calculates:
99  output = (1-frac) * sample[n] + (frac) * sample[n+1]
100  
101  where conceptually    0 <= frac < 1
102  
103  For a fixed point implementation, frac is actually an integer value
104  with an implied binary point one position to the left. The value of
105  one (unity) is given by PHASE_ONE
106  one half and one quarter are useful for 4-point linear interp.
107  */
108  #define PHASE_ONE                (int32_t) (0x1L << NUM_PHASE_FRAC_BITS)
109  
110  /*
111   Multiply the signed audio sample by the unsigned fraction.
112  -  a is the signed audio sample
113  -  b is the unsigned fraction (cast to signed int as long as coef
114      uses (n-1) or less bits, where n == hardware bit width)
115  */
116  #define MULT_AUDIO_COEF(audio,coef)         /*lint -e704 <avoid divide for performance>*/ \
117              (int32_t)(                                    \
118              (                                            \
119                  ((int32_t)(audio)) * ((int32_t)(coef))    \
120              )                                            \
121              >> NUM_PHASE_FRAC_BITS                        \
122                                          )                \
123                                          /* lint +704 <restore checking>*/
124  
125  /* wet / dry calculation macros */
126  #define NUM_WET_DRY_FRAC_BITS       7    // 15
127  #define NUM_WET_DRY_INT_BITS        9    // 1
128  
129  /* define a 1.0 */
130  #define WET_DRY_ONE                 (int32_t) ((0x1L << NUM_WET_DRY_FRAC_BITS))
131  #define WET_DRY_MINUS_ONE           (int32_t) (~WET_DRY_ONE)
132  #define WET_DRY_FULL_SCALE          (int32_t) (WET_DRY_ONE - 1)
133  
134  #define MULT_AUDIO_WET_DRY_COEF(audio,coef) /*lint -e(702) <avoid divide for performance>*/ \
135              (int32_t)(                                        \
136              (                                                \
137                  ((int32_t)(audio)) * ((int32_t)(coef))        \
138              )                                                \
139              >> NUM_WET_DRY_FRAC_BITS                        \
140                                                       )
141  
142  /* Envelope 1 (EG1) calculation macros */
143  #define    NUM_EG1_INT_BITS          1
144  #define NUM_EG1_FRAC_BITS            15
145  
146  /* the max positive gain used in the synth for EG1 */
147  /* SYNTH_FULL_SCALE_EG1_GAIN must match the value in the dls2eas
148  converter, otherwise, the values we read from the .eas file are bogus. */
149  #define SYNTH_FULL_SCALE_EG1_GAIN    (int32_t) ((0x1L << NUM_EG1_FRAC_BITS) -1)
150  
151  /* define a 1.0 */
152  #define EG1_ONE                      (int32_t) ((0x1L << NUM_EG1_FRAC_BITS))
153  #define EG1_MINUS_ONE                (int32_t) (~SYNTH_FULL_SCALE_EG1_GAIN)
154  
155  #define EG1_HALF                     (int32_t) (EG1_ONE/2)
156  #define EG1_MINUS_HALF               (int32_t) (EG1_MINUS_ONE/2)
157  
158  /*
159  We implement the EG1 using a linear gain value, which means that the
160  attack segment is handled by incrementing (adding) the linear gain.
161  However, EG1 treats the Decay, Sustain, and Release differently than
162  the Attack portion. For Decay, Sustain, and Release, the gain is
163  linear on dB scale, which is equivalent to exponential damping on
164  a linear scale. Because we use a linear gain for EG1, we implement
165  the Decay and Release as multiplication (instead of incrementing
166  as we did for the attack segment).
167  Therefore, we need the following macro to implement the multiplication
168  (i.e., exponential damping) during the Decay and Release segments of
169  the EG1
170  */
171  #define MULT_EG1_EG1(gain,damping)        /*lint -e(704) <avoid divide for performance>*/ \
172              (int32_t)(                                        \
173              (                                                \
174                  ((int32_t)(gain)) * ((int32_t)(damping))    \
175              )                                                \
176              >> NUM_EG1_FRAC_BITS                            \
177                                          )
178  
179  // Use the following macro specifically for the filter, when multiplying
180  // the b1 coefficient. The 0 <= |b1| < 2, which therefore might overflow
181  // in certain conditions because we store b1 as a 1.15 value.
182  // Instead, we could store b1 as b1p (b1' == b1 "prime") where
183  // b1p == b1/2, thus ensuring no potential overflow for b1p because
184  // 0 <= |b1p| < 1
185  // However, during the filter calculation, we must account for the fact
186  // that we are using b1p instead of b1, and thereby multiply by
187  // an extra factor of 2. Rather than multiply by an extra factor of 2,
188  // we can instead shift the result right by one less, hence the
189  // modified shift right value of (NUM_EG1_FRAC_BITS -1)
190  #define MULT_EG1_EG1_X2(gain,damping)         /*lint -e(702) <avoid divide for performance>*/ \
191              (int32_t)(                                        \
192              (                                                \
193                  ((int32_t)(gain)) * ((int32_t)(damping))    \
194              )                                                \
195              >> (NUM_EG1_FRAC_BITS -1)                        \
196                                          )
197  
198  #define SATURATE_EG1(x)        /*lint -e{734} saturation operation */                \
199      ((int32_t)(x) > SYNTH_FULL_SCALE_EG1_GAIN)    ? (SYNTH_FULL_SCALE_EG1_GAIN) :    \
200      ((int32_t)(x) < EG1_MINUS_ONE)                ? (EG1_MINUS_ONE) :    (x);
201  
202  
203  /* use "digital cents" == "dents" instead of cents */
204  /* we coudl re-use the phase frac macros, but if we do,
205  we must change the phase macros to cast to _I32 instead of _U32,
206  because using a _U32 cast causes problems when shifting the exponent
207  for the 2^x calculation, because right shift a negative values MUST
208  be sign extended, or else the 2^x calculation is wrong */
209  
210  /* use "digital cents" == "dents" instead of cents */
211  #define NUM_DENTS_FRAC_BITS        12
212  #define NUM_DENTS_INT_BITS         (HARDWARE_BIT_WIDTH - NUM_DENTS_FRAC_BITS)
213  
214  #define DENTS_FRAC_MASK            (int32_t) ((0x1L << NUM_DENTS_FRAC_BITS) -1)
215  
216  #define GET_DENTS_INT_PART(x)        /*lint -e(704) <avoid divide for performance>*/    \
217                              (int32_t)((int32_t)(x) >> NUM_DENTS_FRAC_BITS)
218  
219  #define GET_DENTS_FRAC_PART(x)     (int32_t)((int32_t)(x) & DENTS_FRAC_MASK)
220  
221  #define DENTS_ONE                  (int32_t) (0x1L << NUM_DENTS_FRAC_BITS)
222  
223  /* use CENTS_TO_DENTS to convert a value in cents to dents */
224  #define CENTS_TO_DENTS (int32_t) (DENTS_ONE * (0x1L << NUM_EG1_FRAC_BITS) / 1200L)                            \
225  
226  
227  /*
228  For gain, the LFO generates a value that modulates in terms
229  of dB. However, we use a linear gain value, so we must convert
230  the LFO value in dB to a linear gain. Normally, we would use
231  linear gain = 10^x, where x = LFO value in dB / 20.
232  Instead, we implement 10^x using our 2^x approximation.
233  because
234  
235    10^x = 2^(log2(10^x)) = 2^(x * log2(10))
236  
237  so we need to multiply by log2(10) which is just a constant.
238  Ah, but just wait -- our 2^x actually doesn't exactly implement
239  2^x, but it actually assumes that the input is in cents, and within
240  the 2^x approximation converts its input from cents to octaves
241  by dividing its input by 1200.
242  
243  So, in order to convert the LFO gain value in dB to something
244  that our existing 2^x approximation can use, multiply the LFO gain
245  by log2(10) * 1200 / 20
246  
247  The divide by 20 helps convert dB to linear gain, and we might
248  as well incorporate that operation into this conversion.
249  Of course, we need to keep some fractional bits, so multiply
250  the constant by NUM_EG1_FRAC_BITS
251  */
252  
253  /* use LFO_GAIN_TO_CENTS to convert the LFO gain value to cents */
254  #if 0
255  #define    DOUBLE_LOG2_10    (double) (3.32192809488736)    /* log2(10) */
256  
257  #define    DOUBLE_LFO_GAIN_TO_CENTS    (double)                \
258      (                                                        \
259                  (DOUBLE_LOG2_10) *                            \
260                  1200.0    /                                    \
261                  20.0                                        \
262      )
263  
264  #define    LFO_GAIN_TO_CENTS    (int32_t)                        \
265      (                                                        \
266                  DOUBLE_LFO_GAIN_TO_CENTS *                    \
267                  (0x1L << NUM_EG1_FRAC_BITS)                    \
268      )
269  #endif
270  
271  #define LFO_GAIN_TO_CENTS (int32_t) (1671981156L >> (23 - NUM_EG1_FRAC_BITS))
272  
273  
274  #define MULT_DENTS_COEF(dents,coef)     /*lint -e704 <avoid divide for performance>*/    \
275              (int32_t)(                                    \
276              (                                            \
277                  ((int32_t)(dents)) * ((int32_t)(coef))    \
278              )                                            \
279              >> NUM_DENTS_FRAC_BITS                        \
280                                          )                \
281                                          /* lint +e704 <restore checking>*/
282  
283  
284  /* we use 16-bits in the PC per audio sample */
285  #define BITS_PER_AUDIO_SAMPLE    16
286  
287  /* we define 1 as 1.0 - 1 LSbit */
288  #define DISTORTION_ONE           (int32_t)((0x1L << (BITS_PER_AUDIO_SAMPLE-1)) -1)
289  #define DISTORTION_MINUS_ONE     (int32_t)(~DISTORTION_ONE)
290  
291  /* drive coef is given as int.frac */
292  #define NUM_DRIVE_COEF_INT_BITS  1
293  #define NUM_DRIVE_COEF_FRAC_BITS 4
294  
295  #define MULT_AUDIO_DRIVE(audio,drive)         /*lint -e(702) <avoid divide for performance>*/ \
296              (int32_t)    (                                \
297              (                                            \
298                  ((int32_t)(audio)) * ((int32_t)(drive))    \
299              )                                            \
300              >> NUM_DRIVE_COEF_FRAC_BITS                    \
301                                                  )
302  
303  #define MULT_AUDIO_AUDIO(audio1,audio2)         /*lint -e(702) <avoid divide for performance>*/ \
304              (int32_t)    (                                    \
305              (                                                \
306                  ((int32_t)(audio1)) * ((int32_t)(audio2))    \
307              )                                                \
308              >> (BITS_PER_AUDIO_SAMPLE-1)                    \
309                                                      )
310  
311  #define SATURATE(x)                                                            \
312      ((((int32_t)(x)) > DISTORTION_ONE)        ? (DISTORTION_ONE) :            \
313      (((int32_t)(x)) < DISTORTION_MINUS_ONE)    ? (DISTORTION_MINUS_ONE) :    ((int32_t)(x)));
314  
315  
316  /*----------------------------------------------------------------------------
317   * Effects_log2()
318   *----------------------------------------------------------------------------
319   * Purpose:
320   * Fixed-point log2 function.
321   *
322   * Inputs:
323   * Input is interpreted as an integer (should not be 0).
324   *
325   * Outputs:
326   * Output is in 15-bit precision.
327   *
328   * Side Effects:
329   *
330   *----------------------------------------------------------------------------
331  */
332  int32_t Effects_log2(uint32_t x);
333  
334  /*----------------------------------------------------------------------------
335   * Effects_exp2()
336   *----------------------------------------------------------------------------
337   * Purpose:
338   * Fixed-point radix-2 exponent.
339   *
340   * Inputs:
341   * Input is in 15-bit precision. Must be non-negative and less than 32.
342   *
343   * Outputs:
344   * Output is an integer.
345   *
346   * Side Effects:
347   *
348   *----------------------------------------------------------------------------
349  */
350  uint32_t Effects_exp2(int32_t x);
351  
352  /*----------------------------------------------------------------------------
353   * Effects_MillibelsToLinear16()
354   *----------------------------------------------------------------------------
355   * Purpose:
356   * Transform gain in millibels to linear gain multiplier:
357   *
358   * mB = 2000*log(lin/32767)
359   *    => lin = 2^((mB+2000*log(32767))/2000*log(2))
360   *    => lin = Effects_exp2(((mB + K1) << 15) / K2)
361   * with:
362   *    K1 = 2000*log(32767) and K2 = 2000*log(2)
363   *
364   * Inputs:
365   * nGain - log scale value in millibels.
366   *
367   * Outputs:
368   * Returns a 16-bit linear value approximately equal to 2^(nGain/1024)
369   *
370   * Side Effects:
371   *
372   *----------------------------------------------------------------------------
373  */
374  #define MB_TO_LIN_K1 9031
375  #define MB_TO_LIN_K2 602
376  int16_t Effects_MillibelsToLinear16 (int32_t nGain);
377  
378  /*----------------------------------------------------------------------------
379   * Effects_Linear16ToMillibels()
380   *----------------------------------------------------------------------------
381   * Purpose:
382   * Transform linear gain multiplier to millibels
383   *  mB = 2000*log(lin/32767)
384   *     = 2000*log(2)*log2(lin)-2000*log(32767)
385   *    => mB = K1*Effects_log2(lin) + K2
386   * with:
387   *    K1 = 2000*log(2) and K2 = -2000*log(32767)
388   *
389   * Inputs:
390   * nGain - linear multiplier ranging form 0 to 32767 (corresponding to [0 1] gain range).
391   *
392   * Outputs:
393   * Returns a 16-bit log value expressed in milllibels.
394   *
395   * Side Effects:
396   *
397   *----------------------------------------------------------------------------
398  */
399  int16_t Effects_Linear16ToMillibels (int32_t nGain);
400  
401  /*----------------------------------------------------------------------------
402   * Effects_Sqrt()
403   *----------------------------------------------------------------------------
404   * Purpose:
405   * Returns the square root of the argument given.
406   *
407   * Inputs:
408   * in - positive number in the range 0 - 2^28
409   *
410   * Outputs:
411   * Returned value: square root of in.
412   *
413   * Side Effects:
414   *
415   *----------------------------------------------------------------------------
416  */
417  int32_t Effects_Sqrt(int32_t in);
418  
419  #if __cplusplus
420  }  // extern "C"
421  #endif
422  
423  #endif /*ANDROID_EFFECTSMATH_H_*/
424  
425