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.

276 lines
6.1KB

  1. #include <list>
  2. #include <algorithm>
  3. #include "core.hpp"
  4. #include "midi.hpp"
  5. #include "dsp/digital.hpp"
  6. /*
  7. * MIDIToCVInterface converts midi note on/off events, velocity , channel aftertouch, pitch wheel and mod wheel to
  8. * CV
  9. */
  10. struct MidiValue {
  11. int val = 0; // Controller value
  12. // TransitionSmoother tSmooth;
  13. bool changed = false; // Value has been changed by midi message (only if it is in sync!)
  14. };
  15. struct MIDIToCVInterface : Module {
  16. enum ParamIds {
  17. RESET_PARAM,
  18. NUM_PARAMS
  19. };
  20. enum InputIds {
  21. NUM_INPUTS
  22. };
  23. enum OutputIds {
  24. PITCH_OUTPUT = 0,
  25. GATE_OUTPUT,
  26. VELOCITY_OUTPUT,
  27. MOD_OUTPUT,
  28. PITCHWHEEL_OUTPUT,
  29. CHANNEL_AFTERTOUCH_OUTPUT,
  30. NUM_OUTPUTS
  31. };
  32. enum LightIds {
  33. RESET_LIGHT,
  34. NUM_LIGHTS
  35. };
  36. MidiInputQueue midiInput;
  37. std::list<int> notes;
  38. bool pedal = false;
  39. int note = 60; // C4, most modules should use 261.626 Hz
  40. int vel = 0;
  41. MidiValue mod;
  42. MidiValue afterTouch;
  43. MidiValue pitchWheel;
  44. bool gate = false;
  45. SchmittTrigger resetTrigger;
  46. MIDIToCVInterface() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {
  47. pitchWheel.val = 64;
  48. // pitchWheel.tSmooth.set(0, 0);
  49. }
  50. ~MIDIToCVInterface() {
  51. };
  52. void step() override;
  53. void pressNote(int note);
  54. void releaseNote(int note);
  55. void processMidi(std::vector<unsigned char> msg);
  56. json_t *toJson() override {
  57. json_t *rootJ = json_object();
  58. // addBaseJson(rootJ);
  59. return rootJ;
  60. }
  61. void fromJson(json_t *rootJ) override {
  62. // baseFromJson(rootJ);
  63. }
  64. void onReset() override {
  65. // resetMidi();
  66. }
  67. // void resetMidi() override;
  68. };
  69. /*
  70. void MIDIToCVInterface::resetMidi() {
  71. mod.val = 0;
  72. mod.tSmooth.set(0, 0);
  73. pitchWheel.val = 64;
  74. pitchWheel.tSmooth.set(0, 0);
  75. afterTouch.val = 0;
  76. afterTouch.tSmooth.set(0, 0);
  77. vel = 0;
  78. gate = false;
  79. notes.clear();
  80. }
  81. */
  82. void MIDIToCVInterface::step() {
  83. /*
  84. if (isPortOpen()) {
  85. std::vector<unsigned char> message;
  86. // midiIn->getMessage returns empty vector if there are no messages in the queue
  87. getMessage(&message);
  88. if (message.size() > 0) {
  89. processMidi(message);
  90. }
  91. }
  92. outputs[PITCH_OUTPUT].value = ((note - 60)) / 12.0;
  93. if (resetTrigger.process(params[RESET_PARAM].value)) {
  94. resetMidi();
  95. return;
  96. }
  97. lights[RESET_LIGHT].value -= lights[RESET_LIGHT].value / 0.55 / engineGetSampleRate(); // fade out light
  98. outputs[GATE_OUTPUT].value = gate ? 10.0 : 0.0;
  99. outputs[VELOCITY_OUTPUT].value = vel / 127.0 * 10.0;
  100. int steps = int(engineGetSampleRate() / 32);
  101. if (mod.changed) {
  102. mod.tSmooth.set(outputs[MOD_OUTPUT].value, (mod.val / 127.0 * 10.0), steps);
  103. mod.changed = false;
  104. }
  105. outputs[MOD_OUTPUT].value = mod.tSmooth.next();
  106. if (pitchWheel.changed) {
  107. pitchWheel.tSmooth.set(outputs[PITCHWHEEL_OUTPUT].value, (pitchWheel.val - 64) / 64.0 * 10.0, steps);
  108. pitchWheel.changed = false;
  109. }
  110. outputs[PITCHWHEEL_OUTPUT].value = pitchWheel.tSmooth.next();
  111. outputs[CHANNEL_AFTERTOUCH_OUTPUT].value = afterTouch.val / 127.0 * 10.0;
  112. */
  113. }
  114. void MIDIToCVInterface::pressNote(int note) {
  115. // Remove existing similar note
  116. auto it = std::find(notes.begin(), notes.end(), note);
  117. if (it != notes.end())
  118. notes.erase(it);
  119. // Push note
  120. notes.push_back(note);
  121. this->note = note;
  122. gate = true;
  123. }
  124. void MIDIToCVInterface::releaseNote(int note) {
  125. // Remove the note
  126. auto it = std::find(notes.begin(), notes.end(), note);
  127. if (it != notes.end())
  128. notes.erase(it);
  129. if (pedal) {
  130. // Don't release if pedal is held
  131. gate = true;
  132. } else if (!notes.empty()) {
  133. // Play previous note
  134. auto it2 = notes.end();
  135. it2--;
  136. this->note = *it2;
  137. gate = true;
  138. } else {
  139. gate = false;
  140. }
  141. }
  142. void MIDIToCVInterface::processMidi(std::vector<unsigned char> msg) {
  143. /*
  144. int channel = msg[0] & 0xf;
  145. int status = (msg[0] >> 4) & 0xf;
  146. int data1 = msg[1];
  147. int data2 = msg[2];
  148. // fprintf(stderr, "channel %d status %d data1 %d data2 %d\n", channel, status, data1, data2);
  149. // Filter channels
  150. if (this->channel >= 0 && this->channel != channel)
  151. return;
  152. switch (status) {
  153. // note off
  154. case 0x8: {
  155. releaseNote(data1);
  156. }
  157. break;
  158. case 0x9: // note on
  159. if (data2 > 0) {
  160. pressNote(data1);
  161. this->vel = data2;
  162. } else {
  163. // For some reason, some keyboards send a "note on" event with a velocity of 0 to signal that the key has been released.
  164. releaseNote(data1);
  165. }
  166. break;
  167. case 0xb: // cc
  168. switch (data1) {
  169. case 0x01: // mod
  170. mod.val = data2;
  171. mod.changed = true;
  172. break;
  173. case 0x40: // sustain
  174. pedal = (data2 >= 64);
  175. if (!pedal) {
  176. releaseNote(-1);
  177. }
  178. break;
  179. }
  180. break;
  181. case 0xe: // pitch wheel
  182. pitchWheel.val = data2;
  183. pitchWheel.changed = true;
  184. break;
  185. case 0xd: // channel aftertouch
  186. afterTouch.val = data1;
  187. afterTouch.changed = true;
  188. break;
  189. }
  190. */
  191. }
  192. MidiToCVWidget::MidiToCVWidget() {
  193. MIDIToCVInterface *module = new MIDIToCVInterface();
  194. setModule(module);
  195. box.size = Vec(15 * 9, 380);
  196. {
  197. Panel *panel = new LightPanel();
  198. panel->box.size = box.size;
  199. addChild(panel);
  200. }
  201. float margin = 5;
  202. float labelHeight = 15;
  203. float yPos = margin;
  204. float yGap = 35;
  205. addChild(createScrew<ScrewSilver>(Vec(15, 0)));
  206. addChild(createScrew<ScrewSilver>(Vec(box.size.x - 30, 0)));
  207. addChild(createScrew<ScrewSilver>(Vec(15, 365)));
  208. addChild(createScrew<ScrewSilver>(Vec(box.size.x - 30, 365)));
  209. {
  210. Label *label = new Label();
  211. label->box.pos = Vec(box.size.x - margin - 7 * 15, margin);
  212. label->text = "MIDI to CV";
  213. addChild(label);
  214. yPos = labelHeight * 2;
  215. }
  216. addParam(createParam<LEDButton>(Vec(7 * 15, labelHeight), module, MIDIToCVInterface::RESET_PARAM, 0.0, 1.0, 0.0));
  217. addChild(createLight<SmallLight<RedLight>>(Vec(7 * 15 + 5, labelHeight + 5), module, MIDIToCVInterface::RESET_LIGHT));
  218. std::string labels[MIDIToCVInterface::NUM_OUTPUTS] = {"1V/oct", "Gate", "Velocity", "Mod Wheel", "Pitch Wheel", "Aftertouch"};
  219. for (int i = 0; i < MIDIToCVInterface::NUM_OUTPUTS; i++) {
  220. Label *label = new Label();
  221. label->box.pos = Vec(margin, yPos);
  222. label->text = labels[i];
  223. addChild(label);
  224. addOutput(createOutput<PJ3410Port>(Vec(15 * 6, yPos - 5), module, i));
  225. yPos += yGap + margin;
  226. }
  227. MidiWidget *midiWidget = construct<MIDI_DIN_MidiWidget>();
  228. midiWidget->midiIO = &module->midiInput;
  229. addChild(midiWidget);
  230. }