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.

307 lines
7.1KB

  1. #include <list>
  2. #include <algorithm>
  3. #include "rtmidi/RtMidi.h"
  4. #include "core.hpp"
  5. #include "MidiIO.hpp"
  6. #include "dsp/digital.hpp"
  7. /*
  8. * MIDIToCVInterface converts midi note on/off events, velocity , channel aftertouch, pitch wheel and mod wheel to
  9. * CV
  10. */
  11. struct MidiValue {
  12. int val = 0; // Controller value
  13. TransitionSmoother tSmooth;
  14. bool changed = false; // Value has been changed by midi message (only if it is in sync!)
  15. };
  16. struct MIDIToCVInterface : MidiIO, Module {
  17. enum ParamIds {
  18. RESET_PARAM,
  19. NUM_PARAMS
  20. };
  21. enum InputIds {
  22. NUM_INPUTS
  23. };
  24. enum OutputIds {
  25. PITCH_OUTPUT = 0,
  26. GATE_OUTPUT,
  27. VELOCITY_OUTPUT,
  28. MOD_OUTPUT,
  29. PITCHWHEEL_OUTPUT,
  30. CHANNEL_AFTERTOUCH_OUTPUT,
  31. NUM_OUTPUTS
  32. };
  33. enum LightIds {
  34. RESET_LIGHT,
  35. NUM_LIGHTS
  36. };
  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() : MidiIO(), 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 reset() override {
  65. resetMidi();
  66. }
  67. void resetMidi() override;
  68. };
  69. void MIDIToCVInterface::resetMidi() {
  70. mod.val = 0;
  71. mod.tSmooth.set(0, 0);
  72. pitchWheel.val = 64;
  73. pitchWheel.tSmooth.set(0, 0);
  74. afterTouch.val = 0;
  75. afterTouch.tSmooth.set(0, 0);
  76. vel = 0;
  77. gate = false;
  78. notes.clear();
  79. }
  80. void MIDIToCVInterface::step() {
  81. if (isPortOpen()) {
  82. std::vector<unsigned char> message;
  83. // midiIn->getMessage returns empty vector if there are no messages in the queue
  84. getMessage(&message);
  85. while (message.size() > 0) {
  86. processMidi(message);
  87. getMessage(&message);
  88. }
  89. }
  90. outputs[PITCH_OUTPUT].value = ((note - 60)) / 12.0;
  91. if (resetTrigger.process(params[RESET_PARAM].value)) {
  92. resetMidi();
  93. return;
  94. }
  95. lights[RESET_LIGHT].value -= lights[RESET_LIGHT].value / 0.55 / engineGetSampleRate(); // fade out light
  96. outputs[GATE_OUTPUT].value = gate ? 10.0 : 0.0;
  97. outputs[VELOCITY_OUTPUT].value = vel / 127.0 * 10.0;
  98. int steps = int(engineGetSampleRate() / 32);
  99. if (mod.changed) {
  100. mod.tSmooth.set(outputs[MOD_OUTPUT].value, (mod.val / 127.0 * 10.0), steps);
  101. mod.changed = false;
  102. }
  103. outputs[MOD_OUTPUT].value = mod.tSmooth.next();
  104. if (pitchWheel.changed) {
  105. pitchWheel.tSmooth.set(outputs[PITCHWHEEL_OUTPUT].value, (pitchWheel.val - 64) / 64.0 * 10.0, steps);
  106. pitchWheel.changed = false;
  107. }
  108. outputs[PITCHWHEEL_OUTPUT].value = pitchWheel.tSmooth.next();
  109. /* NOTE: I'll leave out value smoothing for after touch for now. I currently don't
  110. * have an after touch capable device around and I assume it would require different
  111. * smoothing*/
  112. outputs[CHANNEL_AFTERTOUCH_OUTPUT].value = afterTouch.val / 127.0 * 10.0;
  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. int channel = msg[0] & 0xf;
  144. int status = (msg[0] >> 4) & 0xf;
  145. int data1 = msg[1];
  146. int data2 = msg[2];
  147. // fprintf(stderr, "channel %d status %d data1 %d data2 %d\n", channel, status, data1, data2);
  148. // Filter channels
  149. if (this->channel >= 0 && this->channel != channel)
  150. return;
  151. switch (status) {
  152. // note off
  153. case 0x8: {
  154. releaseNote(data1);
  155. }
  156. break;
  157. case 0x9: // note on
  158. if (data2 > 0) {
  159. pressNote(data1);
  160. this->vel = data2;
  161. } else {
  162. // For some reason, some keyboards send a "note on" event with a velocity of 0 to signal that the key has been released.
  163. releaseNote(data1);
  164. }
  165. break;
  166. case 0xb: // cc
  167. switch (data1) {
  168. case 0x01: // mod
  169. mod.val = data2;
  170. mod.changed = true;
  171. break;
  172. case 0x40: // sustain
  173. pedal = (data2 >= 64);
  174. if (!pedal) {
  175. releaseNote(-1);
  176. }
  177. break;
  178. }
  179. break;
  180. case 0xe: // pitch wheel
  181. pitchWheel.val = data2;
  182. pitchWheel.changed = true;
  183. break;
  184. case 0xd: // channel aftertouch
  185. afterTouch.val = data1;
  186. afterTouch.changed = true;
  187. break;
  188. }
  189. }
  190. MidiToCVWidget::MidiToCVWidget() {
  191. MIDIToCVInterface *module = new MIDIToCVInterface();
  192. setModule(module);
  193. box.size = Vec(15 * 9, 380);
  194. {
  195. Panel *panel = new LightPanel();
  196. panel->box.size = box.size;
  197. addChild(panel);
  198. }
  199. float margin = 5;
  200. float labelHeight = 15;
  201. float yPos = margin;
  202. float yGap = 35;
  203. addChild(createScrew<ScrewSilver>(Vec(15, 0)));
  204. addChild(createScrew<ScrewSilver>(Vec(box.size.x - 30, 0)));
  205. addChild(createScrew<ScrewSilver>(Vec(15, 365)));
  206. addChild(createScrew<ScrewSilver>(Vec(box.size.x - 30, 365)));
  207. {
  208. Label *label = new Label();
  209. label->box.pos = Vec(box.size.x - margin - 7 * 15, margin);
  210. label->text = "MIDI to CV";
  211. addChild(label);
  212. yPos = labelHeight * 2;
  213. }
  214. addParam(createParam<LEDButton>(Vec(7 * 15, labelHeight), module, MIDIToCVInterface::RESET_PARAM, 0.0, 1.0, 0.0));
  215. addChild(createLight<SmallLight<RedLight>>(Vec(7 * 15 + 5, labelHeight + 5), module,
  216. MIDIToCVInterface::RESET_LIGHT));
  217. {
  218. Label *label = new Label();
  219. label->box.pos = Vec(margin, yPos);
  220. label->text = "MIDI Interface";
  221. addChild(label);
  222. yPos += labelHeight + margin;
  223. MidiChoice *midiChoice = new MidiChoice();
  224. midiChoice->midiModule = dynamic_cast<MidiIO *>(module);
  225. midiChoice->box.pos = Vec(margin, yPos);
  226. midiChoice->box.size.x = box.size.x - 10;
  227. addChild(midiChoice);
  228. yPos += midiChoice->box.size.y + margin;
  229. }
  230. {
  231. Label *label = new Label();
  232. label->box.pos = Vec(margin, yPos);
  233. label->text = "Channel";
  234. addChild(label);
  235. yPos += labelHeight + margin;
  236. ChannelChoice *channelChoice = new ChannelChoice();
  237. channelChoice->midiModule = dynamic_cast<MidiIO *>(module);
  238. channelChoice->box.pos = Vec(margin, yPos);
  239. channelChoice->box.size.x = box.size.x - 10;
  240. addChild(channelChoice);
  241. yPos += channelChoice->box.size.y + margin + 15;
  242. }
  243. std::string labels[MIDIToCVInterface::NUM_OUTPUTS] = {"1V/oct", "Gate", "Velocity", "Mod Wheel", "Pitch Wheel",
  244. "Aftertouch"};
  245. for (int i = 0; i < MIDIToCVInterface::NUM_OUTPUTS; i++) {
  246. Label *label = new Label();
  247. label->box.pos = Vec(margin, yPos);
  248. label->text = labels[i];
  249. addChild(label);
  250. addOutput(createOutput<PJ3410Port>(Vec(15 * 6, yPos - 5), module, i));
  251. yPos += yGap + margin;
  252. }
  253. }
  254. void MidiToCVWidget::step() {
  255. ModuleWidget::step();
  256. }