eas_math.h 16 KB

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