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