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.

150 lines
5.4KB

  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. #ifdef VERBOSE
  17. #include <iostream>
  18. #endif
  19. #include "synth.h"
  20. #include "exp2.h"
  21. #include "fm_op_kernel.h"
  22. #include "fm_core.h"
  23. //using namespace std;
  24. const FmAlgorithm FmCore::algorithms[32] = {
  25. { { 0xc1, 0x11, 0x11, 0x14, 0x01, 0x14 } }, // 1
  26. { { 0x01, 0x11, 0x11, 0x14, 0xc1, 0x14 } }, // 2
  27. { { 0xc1, 0x11, 0x14, 0x01, 0x11, 0x14 } }, // 3
  28. { { 0xc1, 0x11, 0x94, 0x01, 0x11, 0x14 } }, // 4
  29. { { 0xc1, 0x14, 0x01, 0x14, 0x01, 0x14 } }, // 5
  30. { { 0xc1, 0x94, 0x01, 0x14, 0x01, 0x14 } }, // 6
  31. { { 0xc1, 0x11, 0x05, 0x14, 0x01, 0x14 } }, // 7
  32. { { 0x01, 0x11, 0xc5, 0x14, 0x01, 0x14 } }, // 8
  33. { { 0x01, 0x11, 0x05, 0x14, 0xc1, 0x14 } }, // 9
  34. { { 0x01, 0x05, 0x14, 0xc1, 0x11, 0x14 } }, // 10
  35. { { 0xc1, 0x05, 0x14, 0x01, 0x11, 0x14 } }, // 11
  36. { { 0x01, 0x05, 0x05, 0x14, 0xc1, 0x14 } }, // 12
  37. { { 0xc1, 0x05, 0x05, 0x14, 0x01, 0x14 } }, // 13
  38. { { 0xc1, 0x05, 0x11, 0x14, 0x01, 0x14 } }, // 14
  39. { { 0x01, 0x05, 0x11, 0x14, 0xc1, 0x14 } }, // 15
  40. { { 0xc1, 0x11, 0x02, 0x25, 0x05, 0x14 } }, // 16
  41. { { 0x01, 0x11, 0x02, 0x25, 0xc5, 0x14 } }, // 17
  42. { { 0x01, 0x11, 0x11, 0xc5, 0x05, 0x14 } }, // 18
  43. { { 0xc1, 0x14, 0x14, 0x01, 0x11, 0x14 } }, // 19
  44. { { 0x01, 0x05, 0x14, 0xc1, 0x14, 0x14 } }, // 20
  45. { { 0x01, 0x14, 0x14, 0xc1, 0x14, 0x14 } }, // 21
  46. { { 0xc1, 0x14, 0x14, 0x14, 0x01, 0x14 } }, // 22
  47. { { 0xc1, 0x14, 0x14, 0x01, 0x14, 0x04 } }, // 23
  48. { { 0xc1, 0x14, 0x14, 0x14, 0x04, 0x04 } }, // 24
  49. { { 0xc1, 0x14, 0x14, 0x04, 0x04, 0x04 } }, // 25
  50. { { 0xc1, 0x05, 0x14, 0x01, 0x14, 0x04 } }, // 26
  51. { { 0x01, 0x05, 0x14, 0xc1, 0x14, 0x04 } }, // 27
  52. { { 0x04, 0xc1, 0x11, 0x14, 0x01, 0x14 } }, // 28
  53. { { 0xc1, 0x14, 0x01, 0x14, 0x04, 0x04 } }, // 29
  54. { { 0x04, 0xc1, 0x11, 0x14, 0x04, 0x04 } }, // 30
  55. { { 0xc1, 0x14, 0x04, 0x04, 0x04, 0x04 } }, // 31
  56. { { 0xc4, 0x04, 0x04, 0x04, 0x04, 0x04 } }, // 32
  57. };
  58. int n_out(const FmAlgorithm &alg) {
  59. int count = 0;
  60. for (int i = 0; i < 6; i++) {
  61. if ((alg.ops[i] & 7) == OUT_BUS_ADD) count++;
  62. }
  63. return count;
  64. }
  65. uint8_t FmCore::get_carrier_operators(uint8_t algorithm)
  66. {
  67. uint8_t op_out=0;
  68. FmAlgorithm alg=algorithms[algorithm];
  69. for(uint8_t i=0; i<6;i++)
  70. {
  71. if((alg.ops[i]&OUT_BUS_ADD)==OUT_BUS_ADD)
  72. op_out|=1<<i;
  73. }
  74. return op_out;
  75. }
  76. void FmCore::dump() {
  77. #ifdef VERBOSE
  78. for (int i = 0; i < 32; i++) {
  79. cout << (i + 1) << ":";
  80. const FmAlgorithm &alg = algorithms[i];
  81. for (int j = 0; j < 6; j++) {
  82. int flags = alg.ops[j];
  83. cout << " ";
  84. if (flags & FB_IN) cout << "[";
  85. cout << (flags & IN_BUS_ONE ? "1" : flags & IN_BUS_TWO ? "2" : "0") << "->";
  86. cout << (flags & OUT_BUS_ONE ? "1" : flags & OUT_BUS_TWO ? "2" : "0");
  87. if (flags & OUT_BUS_ADD) cout << "+";
  88. //cout << alg.ops[j].in << "->" << alg.ops[j].out;
  89. if (flags & FB_OUT) cout << "]";
  90. }
  91. cout << " " << n_out(alg);
  92. cout << endl;
  93. }
  94. #endif
  95. }
  96. void FmCore::render(int32_t *output, FmOpParams *params, int algorithm, int32_t *fb_buf, int feedback_shift) {
  97. const int kLevelThresh = 1120;
  98. const FmAlgorithm alg = algorithms[algorithm];
  99. bool has_contents[3] = { true, false, false };
  100. for (int op = 0; op < 6; op++) {
  101. int flags = alg.ops[op];
  102. bool add = (flags & OUT_BUS_ADD) != 0;
  103. FmOpParams &param = params[op];
  104. int inbus = (flags >> 4) & 3;
  105. int outbus = flags & 3;
  106. int32_t *outptr = (outbus == 0) ? output : buf_[outbus - 1].get();
  107. int32_t gain1 = param.gain_out;
  108. int32_t gain2 = Exp2::lookup(param.level_in - (14 * (1 << 24)));
  109. param.gain_out = gain2;
  110. if (gain1 >= kLevelThresh || gain2 >= kLevelThresh) {
  111. if (!has_contents[outbus]) {
  112. add = false;
  113. }
  114. if (inbus == 0 || !has_contents[inbus]) {
  115. // todo: more than one op in a feedback loop
  116. if ((flags & 0xc0) == 0xc0 && feedback_shift < 16) {
  117. // cout << op << " fb " << inbus << outbus << add << endl;
  118. FmOpKernel::compute_fb(outptr, param.phase, param.freq,
  119. gain1, gain2,
  120. fb_buf, feedback_shift, add);
  121. } else {
  122. // cout << op << " pure " << inbus << outbus << add << endl;
  123. FmOpKernel::compute_pure(outptr, param.phase, param.freq,
  124. gain1, gain2, add);
  125. }
  126. } else {
  127. // cout << op << " normal " << inbus << outbus << " " << param.freq << add << endl;
  128. FmOpKernel::compute(outptr, buf_[inbus - 1].get(),
  129. param.phase, param.freq, gain1, gain2, add);
  130. }
  131. has_contents[outbus] = true;
  132. } else if (!add) {
  133. has_contents[outbus] = false;
  134. }
  135. param.phase += param.freq << LG_N;
  136. }
  137. }