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.

371 lines
8.8KB

  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. using namespace rack;
  8. struct MIDIClockToCVInterface : MidiIO, Module {
  9. enum ParamIds {
  10. NUM_PARAMS
  11. };
  12. enum InputIds {
  13. CLOCK1_RATIO,
  14. CLOCK2_RATIO,
  15. NUM_INPUTS
  16. };
  17. enum OutputIds {
  18. CLOCK1_PULSE,
  19. CLOCK2_PULSE,
  20. CONTINUE_PULSE,
  21. START_PULSE,
  22. STOP_PULSE,
  23. NUM_OUTPUTS
  24. };
  25. int clock1ratio = 0;
  26. int clock2ratio = 0;
  27. PulseGenerator clock1Pulse;
  28. PulseGenerator clock2Pulse;
  29. PulseGenerator continuePulse;
  30. PulseGenerator startPulse;
  31. PulseGenerator stopPulse;
  32. bool tick = false;
  33. bool running = false;
  34. bool start = false;
  35. bool stop = false;
  36. bool cont = false;
  37. int c_bar = 0;
  38. /* Note this is in relation to the Midi clock's Tick (6x per 16th note).
  39. * Therefore, e.g. the 2:3 is calculated:
  40. *
  41. * 24 (Ticks per quarter note) * 2 / 3 = 16
  42. *
  43. * Implying that every 16 midi clock ticks we need to send a pulse
  44. * */
  45. const int ratios[9] = {6, 8, 12, 16, 24, 32, 48, 96, 192};
  46. const int numratios = 9;
  47. /*
  48. * Length of clock pulse
  49. */
  50. const float pulseTime = 0.005;
  51. MIDIClockToCVInterface() : MidiIO(), Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) {
  52. }
  53. ~MIDIClockToCVInterface() {
  54. }
  55. void step() override;
  56. void processMidi(std::vector<unsigned char> msg);
  57. void onDeviceChange() override;
  58. void resetMidi() override;
  59. json_t *toJson() override{
  60. json_t *rootJ = json_object();
  61. addBaseJson(rootJ);
  62. json_object_set_new(rootJ, "clock1ratio", json_integer(clock1ratio));
  63. json_object_set_new(rootJ, "clock2ratio", json_integer(clock2ratio));
  64. return rootJ;
  65. }
  66. void fromJson(json_t *rootJ) override{
  67. baseFromJson(rootJ);
  68. json_t *c1rJ = json_object_get(rootJ, "clock1ratio");
  69. if (c1rJ) {
  70. clock1ratio = json_integer_value(c1rJ);
  71. }
  72. json_t *c2rJ = json_object_get(rootJ, "clock2ratio");
  73. if (c2rJ) {
  74. clock2ratio = json_integer_value(c2rJ);
  75. }
  76. }
  77. };
  78. void MIDIClockToCVInterface::step() {
  79. float sampleRate = engineGetSampleRate();
  80. if (isPortOpen()) {
  81. std::vector<unsigned char> message;
  82. // midiIn->getMessage returns empty vector if there are no messages in the queue
  83. getMessage(&message);
  84. while (message.size() > 0) {
  85. processMidi(message);
  86. getMessage(&message);
  87. }
  88. }
  89. if (inputs[CLOCK1_RATIO].active) {
  90. clock1ratio = int(clampf(inputs[CLOCK1_RATIO].value, 0.0, 10.0) * (numratios - 1) / 10);
  91. }
  92. if (inputs[CLOCK2_RATIO].active) {
  93. clock2ratio = int(clampf(inputs[CLOCK2_RATIO].value, 0.0, 10.0) * (numratios - 1) / 10);
  94. }
  95. if (start) {
  96. start = false;
  97. running = true;
  98. startPulse.trigger(pulseTime);
  99. c_bar = 0;
  100. }
  101. if (stop) {
  102. stop = false;
  103. running = false;
  104. stopPulse.trigger(pulseTime);
  105. }
  106. if (cont) {
  107. cont = false;
  108. running = true;
  109. continuePulse.trigger(pulseTime);
  110. }
  111. if (tick) {
  112. tick = false;
  113. /* Note: At least for my midi clock, the clock ticks are sent
  114. * even if the midi clock is stopped.
  115. * Therefore, we need to keep track of ticks even when the clock
  116. * is stopped. Otherwise we can run into weird timing issues.
  117. */
  118. if (running) {
  119. if (c_bar % ratios[clock1ratio] == 0) {
  120. clock1Pulse.trigger(pulseTime);
  121. }
  122. if (c_bar % ratios[clock2ratio] == 0) {
  123. clock2Pulse.trigger(pulseTime);
  124. }
  125. }
  126. c_bar++;
  127. // One "midi bar" = 4 whole notes = (6 ticks per 16th) 6 * 16 *4 = 384
  128. if (c_bar >= 384) {
  129. c_bar = 0;
  130. }
  131. }
  132. bool pulse = clock1Pulse.process(1.0 / sampleRate);
  133. outputs[CLOCK1_PULSE].value = pulse ? 10.0 : 0.0;
  134. pulse = clock2Pulse.process(1.0 / sampleRate);
  135. outputs[CLOCK2_PULSE].value = pulse ? 10.0 : 0.0;
  136. pulse = continuePulse.process(1.0 / sampleRate);
  137. outputs[CONTINUE_PULSE].value = pulse ? 10.0 : 0.0;
  138. pulse = startPulse.process(1.0 / sampleRate);
  139. outputs[START_PULSE].value = pulse ? 10.0 : 0.0;
  140. pulse = stopPulse.process(1.0 / sampleRate);
  141. outputs[STOP_PULSE].value = pulse ? 10.0 : 0.0;
  142. }
  143. void MIDIClockToCVInterface::resetMidi() {
  144. outputs[CLOCK1_PULSE].value = 0.0;
  145. outputs[CLOCK2_PULSE].value = 0.0;
  146. }
  147. void MIDIClockToCVInterface::processMidi(std::vector<unsigned char> msg) {
  148. switch (msg[0]) {
  149. case 0xfa:
  150. start = true;
  151. break;
  152. case 0xfb:
  153. cont = true;
  154. break;
  155. case 0xfc:
  156. stop = true;
  157. break;
  158. case 0xf8:
  159. tick = true;
  160. break;
  161. }
  162. }
  163. void MIDIClockToCVInterface::onDeviceChange() {
  164. setIgnores(true, false);
  165. }
  166. struct ClockRatioItem : MenuItem {
  167. int ratio;
  168. int *clockRatio;
  169. void onAction(EventAction &e) override {
  170. *clockRatio = ratio;
  171. }
  172. };
  173. struct ClockRatioChoice : ChoiceButton {
  174. int *clockRatio;
  175. const std::vector<std::string> ratioNames = {"Sixteenth note (1:4 ratio)", "Eighth note triplet (1:3 ratio)",
  176. "Eighth note (1:2 ratio)", "Quarter note triplet (2:3 ratio)",
  177. "Quarter note (tap speed)", "Half note triplet (4:3 ratio)",
  178. "Half note (2:1 ratio)", "Whole note (4:1 ratio)",
  179. "Two whole notes (8:1 ratio)"};
  180. const std::vector<std::string> ratioNames_short = {"1:4 ratio", "1:3 ratio", "1:2 ratio", "2:3 ratio", "1:1 ratio",
  181. "4:3", "2:1 ratio", "4:1 ratio", "8:1 ratio"};
  182. void onAction(EventAction &e) override {
  183. Menu *menu = gScene->createMenu();
  184. menu->box.pos = getAbsoluteOffset(Vec(0, box.size.y)).round();
  185. menu->box.size.x = box.size.x;
  186. for (unsigned long ratio = 0; ratio < ratioNames.size(); ratio++) {
  187. ClockRatioItem *clockRatioItem = new ClockRatioItem();
  188. clockRatioItem->ratio = ratio;
  189. clockRatioItem->clockRatio = clockRatio;
  190. clockRatioItem->text = ratioNames[ratio];
  191. menu->pushChild(clockRatioItem);
  192. }
  193. }
  194. void step() override {
  195. text = ratioNames_short[*clockRatio];
  196. }
  197. };
  198. MIDIClockToCVWidget::MIDIClockToCVWidget() {
  199. MIDIClockToCVInterface *module = new MIDIClockToCVInterface();
  200. setModule(module);
  201. box.size = Vec(15 * 9, 380);
  202. {
  203. Panel *panel = new LightPanel();
  204. panel->box.size = box.size;
  205. addChild(panel);
  206. }
  207. float margin = 5;
  208. float labelHeight = 15;
  209. float yPos = margin;
  210. addChild(createScrew<ScrewSilver>(Vec(15, 0)));
  211. addChild(createScrew<ScrewSilver>(Vec(box.size.x - 30, 0)));
  212. addChild(createScrew<ScrewSilver>(Vec(15, 365)));
  213. addChild(createScrew<ScrewSilver>(Vec(box.size.x - 30, 365)));
  214. {
  215. Label *label = new Label();
  216. label->box.pos = Vec(box.size.x - margin - 7 * 15, margin);
  217. label->text = "MIDI Clk-CV";
  218. addChild(label);
  219. yPos = labelHeight*2;
  220. }
  221. {
  222. Label *label = new Label();
  223. label->box.pos = Vec(margin, yPos);
  224. label->text = "MIDI Interface";
  225. addChild(label);
  226. yPos += labelHeight + margin;
  227. MidiChoice *midiChoice = new MidiChoice();
  228. midiChoice->midiModule = dynamic_cast<MidiIO *>(module);
  229. midiChoice->box.pos = Vec(margin, yPos);
  230. midiChoice->box.size.x = box.size.x - 10;
  231. addChild(midiChoice);
  232. yPos += midiChoice->box.size.y + margin * 4;
  233. }
  234. {
  235. Label *label = new Label();
  236. label->box.pos = Vec(margin, yPos);
  237. label->text = "Start";
  238. addChild(label);
  239. addOutput(createOutput<PJ3410Port>(Vec(15 * 6, yPos - 5), module, MIDIClockToCVInterface::START_PULSE));
  240. yPos += labelHeight + margin * 4;
  241. }
  242. {
  243. Label *label = new Label();
  244. label->box.pos = Vec(margin, yPos);
  245. label->text = "Stop";
  246. addChild(label);
  247. addOutput(createOutput<PJ3410Port>(Vec(15 * 6, yPos - 5), module, MIDIClockToCVInterface::STOP_PULSE));
  248. yPos += labelHeight + margin * 4;
  249. }
  250. {
  251. Label *label = new Label();
  252. label->box.pos = Vec(margin, yPos);
  253. label->text = "Continue";
  254. addChild(label);
  255. addOutput(createOutput<PJ3410Port>(Vec(15 * 6, yPos - 5), module, MIDIClockToCVInterface::CONTINUE_PULSE));
  256. yPos += labelHeight + margin * 6;
  257. }
  258. {
  259. addInput(createInput<PJ3410Port>(Vec(margin, yPos - 5), module, MIDIClockToCVInterface::CLOCK1_RATIO));
  260. ClockRatioChoice *ratioChoice = new ClockRatioChoice();
  261. ratioChoice->clockRatio = &module->clock1ratio;
  262. ratioChoice->box.pos = Vec(int(box.size.x/3), yPos);
  263. ratioChoice->box.size.x = int(box.size.x/1.5 - margin);
  264. addChild(ratioChoice);
  265. yPos += ratioChoice->box.size.y + margin * 3;
  266. }
  267. {
  268. Label *label = new Label();
  269. label->box.pos = Vec(margin, yPos);
  270. label->text = "C1 Pulse";
  271. addChild(label);
  272. addOutput(createOutput<PJ3410Port>(Vec(15 * 6, yPos - 5), module, MIDIClockToCVInterface::CLOCK1_PULSE));
  273. yPos += margin * 10;
  274. }
  275. {
  276. addInput(createInput<PJ3410Port>(Vec(margin, yPos - 5), module, MIDIClockToCVInterface::CLOCK2_RATIO));
  277. ClockRatioChoice *ratioChoice = new ClockRatioChoice();
  278. ratioChoice->clockRatio = &module->clock2ratio;
  279. ratioChoice->box.pos = Vec(int(box.size.x/3), yPos);
  280. ratioChoice->box.size.x = int(box.size.x/1.5 - margin);
  281. addChild(ratioChoice);
  282. yPos += ratioChoice->box.size.y + margin * 3;
  283. }
  284. {
  285. Label *label = new Label();
  286. label->box.pos = Vec(margin, yPos);
  287. label->text = "C2 Pulse";
  288. addChild(label);
  289. addOutput(createOutput<PJ3410Port>(Vec(15 * 6, yPos - 5), module, MIDIClockToCVInterface::CLOCK2_PULSE));
  290. yPos += labelHeight + margin * 3;
  291. }
  292. }
  293. void MIDIClockToCVWidget::step() {
  294. ModuleWidget::step();
  295. }