MicroDexed is a compatible 6-operator-FM-synth based on the Teensy(-3.6/-4.0) Microcontroller. https://www.parasitstudio.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0KB

  1. /*
  2. * Copyright 2012 Google Inc.
  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. class Exp2 {
  17. public:
  18. Exp2();
  19. static void init();
  20. // Q24 in, Q24 out
  21. static int32_t lookup(int32_t x);
  22. };
  23. #define EXP2_LG_N_SAMPLES 10
  24. #define EXP2_N_SAMPLES (1 << EXP2_LG_N_SAMPLES)
  25. #define EXP2_INLINE
  26. extern int32_t exp2tab[EXP2_N_SAMPLES << 1];
  27. #ifdef EXP2_INLINE
  28. inline
  29. int32_t Exp2::lookup(int32_t x) {
  30. const int SHIFT = 24 - EXP2_LG_N_SAMPLES;
  31. int lowbits = x & ((1 << SHIFT) - 1);
  32. int x_int = (x >> (SHIFT - 1)) & ((EXP2_N_SAMPLES - 1) << 1);
  33. int dy = exp2tab[x_int];
  34. int y0 = exp2tab[x_int + 1];
  35. int y = y0 + (((int64_t)dy * (int64_t)lowbits) >> SHIFT);
  36. return y >> (6 - (x >> 24));
  37. }
  38. #endif
  39. class Tanh {
  40. public:
  41. static void init();
  42. // Q24 in, Q24 out
  43. static int32_t lookup(int32_t x);
  44. };
  45. #define TANH_LG_N_SAMPLES 10
  46. #define TANH_N_SAMPLES (1 << TANH_LG_N_SAMPLES)
  47. extern int32_t tanhtab[TANH_N_SAMPLES << 1];
  48. inline
  49. int32_t Tanh::lookup(int32_t x) {
  50. int32_t signum = x >> 31;
  51. x ^= signum;
  52. if (x >= (4 << 24)) {
  53. if (x >= (17 << 23)) {
  54. return signum ^ (1 << 24);
  55. }
  56. int32_t sx = ((int64_t)-48408812 * (int64_t)x) >> 24;
  57. return signum ^ ((1 << 24) - 2 * Exp2::lookup(sx));
  58. } else {
  59. const int SHIFT = 26 - TANH_LG_N_SAMPLES;
  60. int lowbits = x & ((1 << SHIFT) - 1);
  61. int x_int = (x >> (SHIFT - 1)) & ((TANH_N_SAMPLES - 1) << 1);
  62. int dy = tanhtab[x_int];
  63. int y0 = tanhtab[x_int + 1];
  64. int y = y0 + (((int64_t)dy * (int64_t)lowbits) >> SHIFT);
  65. return y ^ signum;
  66. }
  67. }