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.

145 lines
4.2KB

  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. #define _USE_MATH_DEFINES
  17. #include <math.h>
  18. #include "synth.h"
  19. #include "sin.h"
  20. #define R (1 << 29)
  21. #ifdef SIN_DELTA
  22. int32_t sintab[SIN_N_SAMPLES << 1];
  23. #else
  24. int32_t sintab[SIN_N_SAMPLES + 1];
  25. #endif
  26. void Sin::init() {
  27. FRAC_NUM dphase = 2 * M_PI / SIN_N_SAMPLES;
  28. //int32_t c = (int32_t)floor(cos(dphase) * (1 << 30) + 0.5);
  29. int32_t c = (int32_t)floor(COS_FUNC(dphase) * (1 << 30) + 0.5);
  30. //int32_t s = (int32_t)floor(sin(dphase) * (1 << 30) + 0.5);
  31. int32_t s = (int32_t)floor(SIN_FUNC(dphase) * (1 << 30) + 0.5);
  32. int32_t u = 1 << 30;
  33. int32_t v = 0;
  34. for (int i = 0; i < SIN_N_SAMPLES / 2; i++) {
  35. #ifdef SIN_DELTA
  36. sintab[(i << 1) + 1] = (v + 32) >> 6;
  37. sintab[((i + SIN_N_SAMPLES / 2) << 1) + 1] = -((v + 32) >> 6);
  38. #else
  39. sintab[i] = (v + 32) >> 6;
  40. sintab[i + SIN_N_SAMPLES / 2] = -((v + 32) >> 6);
  41. #endif
  42. int32_t t = ((int64_t)u * (int64_t)s + (int64_t)v * (int64_t)c + R) >> 30;
  43. u = ((int64_t)u * (int64_t)c - (int64_t)v * (int64_t)s + R) >> 30;
  44. v = t;
  45. }
  46. #ifdef SIN_DELTA
  47. for (int i = 0; i < SIN_N_SAMPLES - 1; i++) {
  48. sintab[i << 1] = sintab[(i << 1) + 3] - sintab[(i << 1) + 1];
  49. }
  50. sintab[(SIN_N_SAMPLES << 1) - 2] = -sintab[(SIN_N_SAMPLES << 1) - 1];
  51. #else
  52. sintab[SIN_N_SAMPLES] = 0;
  53. #endif
  54. }
  55. #ifndef SIN_INLINE
  56. int32_t Sin::lookup(int32_t phase) {
  57. const int SHIFT = 24 - SIN_LG_N_SAMPLES;
  58. int lowbits = phase & ((1 << SHIFT) - 1);
  59. #ifdef SIN_DELTA
  60. int phase_int = (phase >> (SHIFT - 1)) & ((SIN_N_SAMPLES - 1) << 1);
  61. int dy = sintab[phase_int];
  62. int y0 = sintab[phase_int + 1];
  63. return y0 + (((int64_t)dy * (int64_t)lowbits) >> SHIFT);
  64. #else
  65. int phase_int = (phase >> SHIFT) & (SIN_N_SAMPLES - 1);
  66. int y0 = sintab[phase_int];
  67. int y1 = sintab[phase_int + 1];
  68. return y0 + (((int64_t)(y1 - y0) * (int64_t)lowbits) >> SHIFT);
  69. #endif
  70. }
  71. #endif
  72. #if 0
  73. // The following is an implementation designed not to use any lookup tables,
  74. // based on the following implementation by Basile Graf:
  75. // http://www.rossbencina.com/static/code/sinusoids/even_polynomial_sin_approximation.txt
  76. #define C0 (1 << 24)
  77. #define C1 (331121857 >> 2)
  78. #define C2 (1084885537 >> 4)
  79. #define C3 (1310449902 >> 6)
  80. int32_t Sin::compute(int32_t phase) {
  81. int32_t x = (phase & ((1 << 23) - 1)) - (1 << 22);
  82. int32_t x2 = ((int64_t)x * (int64_t)x) >> 22;
  83. int32_t x4 = ((int64_t)x2 * (int64_t)x2) >> 24;
  84. int32_t x6 = ((int64_t)x2 * (int64_t)x4) >> 24;
  85. int32_t y = C0 -
  86. (((int64_t)C1 * (int64_t)x2) >> 24) +
  87. (((int64_t)C2 * (int64_t)x4) >> 24) -
  88. (((int64_t)C3 * (int64_t)x6) >> 24);
  89. y ^= -((phase >> 23) & 1);
  90. return y;
  91. }
  92. #endif
  93. #if 1
  94. // coefficients are Chebyshev polynomial, computed by compute_cos_poly.py
  95. #define C8_0 16777216
  96. #define C8_2 -331168742
  97. #define C8_4 1089453524
  98. #define C8_6 -1430910663
  99. #define C8_8 950108533
  100. int32_t Sin::compute(int32_t phase) {
  101. int32_t x = (phase & ((1 << 23) - 1)) - (1 << 22);
  102. int32_t x2 = ((int64_t)x * (int64_t)x) >> 16;
  103. int32_t y = (((((((((((((int64_t)C8_8
  104. * (int64_t)x2) >> 32) + C8_6)
  105. * (int64_t)x2) >> 32) + C8_4)
  106. * (int64_t)x2) >> 32) + C8_2)
  107. * (int64_t)x2) >> 32) + C8_0);
  108. y ^= -((phase >> 23) & 1);
  109. return y;
  110. }
  111. #endif
  112. #define C10_0 (1 << 30)
  113. #define C10_2 -1324675874 // scaled * 4
  114. #define C10_4 1089501821
  115. #define C10_6 -1433689867
  116. #define C10_8 1009356886
  117. #define C10_10 -421101352
  118. int32_t Sin::compute10(int32_t phase) {
  119. int32_t x = (phase & ((1 << 29) - 1)) - (1 << 28);
  120. int32_t x2 = ((int64_t)x * (int64_t)x) >> 26;
  121. int32_t y = ((((((((((((((((int64_t)C10_10
  122. * (int64_t)x2) >> 34) + C10_8)
  123. * (int64_t)x2) >> 34) + C10_6)
  124. * (int64_t)x2) >> 34) + C10_4)
  125. * (int64_t)x2) >> 32) + C10_2)
  126. * (int64_t)x2) >> 30) + C10_0);
  127. y ^= -((phase >> 29) & 1);
  128. return y;
  129. }