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.

118 lines
2.3KB

  1. /*
  2. Copyright 2013 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __CONTROLLERS_H
  14. #define __CONTROLLERS_H
  15. #include <Arduino.h>
  16. #include "synth.h"
  17. #include <stdio.h>
  18. #include <string.h>
  19. #ifdef _WIN32
  20. #define snprintf _snprintf
  21. #endif
  22. // State of MIDI controllers
  23. const int kControllerPitch = 0;
  24. const int kControllerPitchRange = 1;
  25. const int kControllerPitchStep = 2;
  26. class FmCore;
  27. struct FmMod {
  28. uint8_t range;
  29. bool pitch;
  30. bool amp;
  31. bool eg;
  32. FmMod() {
  33. range = 0;
  34. pitch = false;
  35. amp = false;
  36. eg = false;
  37. }
  38. void setRange(uint8_t r) {
  39. range = r < 0 && r > 127 ? 0 : r;
  40. }
  41. void setTarget(uint8_t assign) {
  42. assign = assign < 0 && assign > 7 ? 0 : assign;
  43. pitch = assign & 1; // AMP
  44. amp = assign & 2; // PITCH
  45. eg = assign & 4; // EG
  46. }
  47. };
  48. class Controllers {
  49. void applyMod(int cc, FmMod &mod) {
  50. float range = 0.01 * mod.range;
  51. uint8_t total = (float)cc * range;
  52. if (mod.amp)
  53. amp_mod = max(amp_mod, total);
  54. if (mod.pitch)
  55. pitch_mod = max(pitch_mod, total);
  56. if (mod.eg)
  57. eg_mod = max(eg_mod, total);
  58. }
  59. public:
  60. int32_t values_[3];
  61. uint8_t amp_mod;
  62. uint8_t pitch_mod;
  63. uint8_t eg_mod;
  64. uint8_t aftertouch_cc;
  65. uint8_t breath_cc;
  66. uint8_t foot_cc;
  67. uint8_t modwheel_cc;
  68. int masterTune;
  69. uint8_t opSwitch;
  70. FmMod wheel;
  71. FmMod foot;
  72. FmMod breath;
  73. FmMod at;
  74. Controllers() {
  75. amp_mod = 0;
  76. pitch_mod = 0;
  77. eg_mod = 0;
  78. }
  79. void refresh() {
  80. amp_mod = pitch_mod = eg_mod = 0;
  81. applyMod(modwheel_cc, wheel);
  82. applyMod(breath_cc, breath);
  83. applyMod(foot_cc, foot);
  84. applyMod(aftertouch_cc, at);
  85. if ( ! ((wheel.eg || foot.eg) || (breath.eg || at.eg)) )
  86. eg_mod = 127;
  87. }
  88. FmCore *core;
  89. };
  90. #endif // __CONTROLLERS_H