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.

226 lines
8.7KB

  1. /*
  2. * Copyright (C) 2014 Pascal Gauthier.
  3. * Copyright (C) 2012 Steffen Ohrendorf <steffen.ohrendorf@gmx.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Original Java Code: Copyright (C) 2008 Robson Cozendey <robson@cozendey.com>
  20. *
  21. * Some code based on forum posts in: http://forums.submarine.org.uk/phpBB/viewforum.php?f=9,
  22. * Copyright (C) 2010-2013 by carbon14 and opl3
  23. *
  24. */
  25. #include "EngineOpl.h"
  26. #ifdef _WIN32
  27. __declspec(align(16)) const int zeros[N] = {0};
  28. #else
  29. const int32_t __attribute__ ((aligned(16))) zeros[_N_] = {0};
  30. #endif
  31. uint16_t SignBit = 0x8000;
  32. uint16_t sinLogTable[256] = {
  33. 2137, 1731, 1543, 1419, 1326, 1252, 1190, 1137, 1091, 1050, 1013, 979, 949, 920, 894, 869,
  34. 846, 825, 804, 785, 767, 749, 732, 717, 701, 687, 672, 659, 646, 633, 621, 609,
  35. 598, 587, 576, 566, 556, 546, 536, 527, 518, 509, 501, 492, 484, 476, 468, 461,
  36. 453, 446, 439, 432, 425, 418, 411, 405, 399, 392, 386, 380, 375, 369, 363, 358,
  37. 352, 347, 341, 336, 331, 326, 321, 316, 311, 307, 302, 297, 293, 289, 284, 280,
  38. 276, 271, 267, 263, 259, 255, 251, 248, 244, 240, 236, 233, 229, 226, 222, 219,
  39. 215, 212, 209, 205, 202, 199, 196, 193, 190, 187, 184, 181, 178, 175, 172, 169,
  40. 167, 164, 161, 159, 156, 153, 151, 148, 146, 143, 141, 138, 136, 134, 131, 129,
  41. 127, 125, 122, 120, 118, 116, 114, 112, 110, 108, 106, 104, 102, 100, 98, 96,
  42. 94, 92, 91, 89, 87, 85, 83, 82, 80, 78, 77, 75, 74, 72, 70, 69,
  43. 67, 66, 64, 63, 62, 60, 59, 57, 56, 55, 53, 52, 51, 49, 48, 47,
  44. 46, 45, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30,
  45. 29, 28, 27, 26, 25, 24, 23, 23, 22, 21, 20, 20, 19, 18, 17, 17,
  46. 16, 15, 15, 14, 13, 13, 12, 12, 11, 10, 10, 9, 9, 8, 8, 7,
  47. 7, 7, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2,
  48. 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0
  49. };
  50. uint16_t sinExpTable[256] = {
  51. 0, 3, 6, 8, 11, 14, 17, 20, 22, 25, 28, 31, 34, 37, 40, 42,
  52. 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90,
  53. 93, 96, 99, 102, 105, 108, 111, 114, 117, 120, 123, 126, 130, 133, 136, 139,
  54. 142, 145, 148, 152, 155, 158, 161, 164, 168, 171, 174, 177, 181, 184, 187, 190,
  55. 194, 197, 200, 204, 207, 210, 214, 217, 220, 224, 227, 231, 234, 237, 241, 244,
  56. 248, 251, 255, 258, 262, 265, 268, 272, 276, 279, 283, 286, 290, 293, 297, 300,
  57. 304, 308, 311, 315, 318, 322, 326, 329, 333, 337, 340, 344, 348, 352, 355, 359,
  58. 363, 367, 370, 374, 378, 382, 385, 389, 393, 397, 401, 405, 409, 412, 416, 420,
  59. 424, 428, 432, 436, 440, 444, 448, 452, 456, 460, 464, 468, 472, 476, 480, 484,
  60. 488, 492, 496, 501, 505, 509, 513, 517, 521, 526, 530, 534, 538, 542, 547, 551,
  61. 555, 560, 564, 568, 572, 577, 581, 585, 590, 594, 599, 603, 607, 612, 616, 621,
  62. 625, 630, 634, 639, 643, 648, 652, 657, 661, 666, 670, 675, 680, 684, 689, 693,
  63. 698, 703, 708, 712, 717, 722, 726, 731, 736, 741, 745, 750, 755, 760, 765, 770,
  64. 774, 779, 784, 789, 794, 799, 804, 809, 814, 819, 824, 829, 834, 839, 844, 849,
  65. 854, 859, 864, 869, 874, 880, 885, 890, 895, 900, 906, 911, 916, 921, 927, 932,
  66. 937, 942, 948, 953, 959, 964, 969, 975, 980, 986, 991, 996, 1002, 1007, 1013, 1018
  67. };
  68. inline uint16_t sinLog(uint16_t phi) {
  69. const uint8_t index = (phi & 0xff);
  70. switch( ( phi & 0x0300 ) ) {
  71. case 0x0000:
  72. // rising quarter wave Shape A
  73. return sinLogTable[index];
  74. case 0x0100:
  75. // falling quarter wave Shape B
  76. return sinLogTable[index ^ 0xFF];
  77. case 0x0200:
  78. // rising quarter wave -ve Shape C
  79. return sinLogTable[index] | SignBit;
  80. default:
  81. // falling quarter wave -ve Shape D
  82. return sinLogTable[index ^ 0xFF] | SignBit;
  83. }
  84. }
  85. // 16 env units are ~3dB and halve the output
  86. /**
  87. * @brief OPL Sine Wave calculation
  88. * @param[in] phase Wave phase (0..1023)
  89. * @param[in] env Envelope value (0..511)
  90. * @warning @a env will not be checked for correct values.
  91. */
  92. inline int16_t oplSin( uint16_t phase, uint16_t env ) {
  93. uint16_t expVal = sinLog(phase) + (env << 3);
  94. const bool isSigned = expVal & SignBit;
  95. expVal &= ~SignBit;
  96. // expVal: 0..2137+511*8 = 0..6225
  97. // result: 0..1018+1024
  98. uint32_t result = 0x0400 + sinExpTable[( expVal & 0xff ) ^ 0xFF];
  99. result <<= 1;
  100. result >>= ( expVal >> 8 ); // exp
  101. if( isSigned ) {
  102. // -1 for one's complement
  103. return -result - 1;
  104. } else {
  105. return result;
  106. }
  107. }
  108. void EngineOpl::compute(int32_t *output, const int32_t *input, int32_t phase0, int32_t freq, int32_t gain1, int32_t gain2, bool add) {
  109. int32_t dgain = (gain2 - gain1 + (_N_ >> 1)) >> LG_N;
  110. int32_t gain = gain1;
  111. int32_t phase = phase0;
  112. const int32_t *adder = add ? output : zeros;
  113. for (int i = 0; i < _N_; i++) {
  114. gain += dgain;
  115. int32_t y = oplSin((phase+input[i]) >> 14, gain);
  116. output[i] = (y << 14) + adder[i];
  117. phase += freq;
  118. }
  119. }
  120. void EngineOpl::compute_pure(int32_t *output, int32_t phase0, int32_t freq, int32_t gain1, int32_t gain2, bool add) {
  121. int32_t dgain = (gain2 - gain1 + (_N_ >> 1)) >> LG_N;
  122. int32_t gain = gain1;
  123. int32_t phase = phase0;
  124. const int32_t *adder = add ? output : zeros;
  125. for (int i = 0; i < _N_; i++) {
  126. gain += dgain;
  127. int32_t y = oplSin(phase >> 14, gain);
  128. output[i] = (y << 14) + adder[i];
  129. phase += freq;
  130. }
  131. }
  132. void EngineOpl::compute_fb(int32_t *output, int32_t phase0, int32_t freq,
  133. int32_t gain1, int32_t gain2,
  134. int32_t *fb_buf, int fb_shift, bool add) {
  135. int32_t dgain = (gain2 - gain1 + (_N_ >> 1)) >> LG_N;
  136. int32_t gain = gain1;
  137. int32_t phase = phase0;
  138. const int32_t *adder = add ? output : zeros;
  139. int32_t y0 = fb_buf[0];
  140. int32_t y = fb_buf[1];
  141. for (int i = 0; i < _N_; i++) {
  142. gain += dgain;
  143. int32_t scaled_fb = (y0 + y) >> (fb_shift + 1);
  144. y0 = y;
  145. y = oplSin((phase+scaled_fb) >> 14, gain) << 14;
  146. output[i] = y + adder[i];
  147. phase += freq;
  148. }
  149. fb_buf[0] = y0;
  150. fb_buf[1] = y;
  151. }
  152. void EngineOpl::render(int32_t *output, FmOpParams *params, int algorithm,int32_t *fb_buf, int32_t feedback_shift) {
  153. const int kLevelThresh = 507; // really ????
  154. const FmAlgorithm alg = algorithms[algorithm];
  155. bool has_contents[3] = { true, false, false };
  156. for (int op = 0; op < 6; op++) {
  157. int flags = alg.ops[op];
  158. bool add = (flags & OUT_BUS_ADD) != 0;
  159. FmOpParams &param = params[op];
  160. int inbus = (flags >> 4) & 3;
  161. int outbus = flags & 3;
  162. int32_t *outptr = (outbus == 0) ? output : buf_[outbus - 1].get();
  163. int32_t gain1 = param.gain_out == 0 ? 511 : param.gain_out;
  164. int32_t gain2 = 512-(param.level_in >> 19);
  165. param.gain_out = gain2;
  166. if (gain1 <= kLevelThresh || gain2 <= kLevelThresh) {
  167. if (!has_contents[outbus]) {
  168. add = false;
  169. }
  170. if (inbus == 0 || !has_contents[inbus]) {
  171. // todo: more than one op in a feedback loop
  172. if ((flags & 0xc0) == 0xc0 && feedback_shift < 16) {
  173. // cout << op << " fb " << inbus << outbus << add << endl;
  174. compute_fb(outptr, param.phase, param.freq,
  175. gain1, gain2,
  176. fb_buf, feedback_shift, add);
  177. } else {
  178. // cout << op << " pure " << inbus << outbus << add << endl;
  179. compute_pure(outptr, param.phase, param.freq,
  180. gain1, gain2, add);
  181. }
  182. } else {
  183. // cout << op << " normal " << inbus << outbus << " " << param.freq << add << endl;
  184. compute(outptr, buf_[inbus - 1].get(),
  185. param.phase, param.freq, gain1, gain2, add);
  186. }
  187. has_contents[outbus] = true;
  188. } else if (!add) {
  189. has_contents[outbus] = false;
  190. }
  191. param.phase += param.freq << LG_N;
  192. }
  193. }