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.8KB

  1. #include <stdint.h>
  2. #include "DrumKit.hpp"
  3. #include "dmx.h"
  4. namespace rack_plugin_DrumKit {
  5. struct DMXContainer {
  6. float *sample;
  7. unsigned int length;
  8. int current;
  9. };
  10. struct DMXContainer dmxsamples[ 12 ] = {
  11. { (float *)dmx1, dmx1_len, 0 },
  12. { (float *)dmx2, dmx2_len, 1 },
  13. { (float *)dmx3, dmx3_len, 2 },
  14. { (float *)dmx4, dmx4_len, 3 },
  15. { (float *)dmx5, dmx5_len, 4 },
  16. { (float *)dmx6, dmx6_len, 5 },
  17. { (float *)dmx7, dmx7_len, 6 },
  18. { (float *)dmx8, dmx8_len, 7 },
  19. { (float *)dmx9, dmx9_len, 8 },
  20. { (float *)dmx10, dmx10_len, 9 },
  21. { (float *)dmx11, dmx11_len, 10 },
  22. { (float *)dmx12, dmx12_len, 11 },
  23. };
  24. float dmxnotes[12] = { 0.08, 0.17, 0.25, 0.33, 0.42, 0.5, 0.58, 0.67, 0.75, 0.83, 0.92, 1.0 };
  25. struct DMXContainer *getNote(float current) {
  26. for (int i = 0; i < 12; i++) {
  27. if ((dmxnotes[i] - 0.02) <= current && (dmxnotes[i] + 0.02) >= current) {
  28. return &dmxsamples[i];
  29. }
  30. }
  31. return NULL;
  32. }
  33. struct DMXModule : Module {
  34. enum ParamIds { NUM_PARAMS };
  35. enum InputIds { NOTE1_INPUT, NUM_INPUTS };
  36. enum OutputIds { AUDIO1_OUTPUT, NUM_OUTPUTS };
  37. enum LightIds { NUM_LIGHTS };
  38. DMXModule( ) : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {
  39. currentStep = 0;
  40. last = -1;
  41. }
  42. void step( ) override;
  43. uint32_t currentStep;
  44. int last;
  45. };
  46. void DMXModule::step( ) {
  47. float in1 = inputs[ NOTE1_INPUT ].value;
  48. struct DMXContainer *note;
  49. // check the first note
  50. note = getNote(in1);
  51. if (note == NULL) {
  52. currentStep = 0;
  53. outputs[ AUDIO1_OUTPUT ].value = 0;
  54. last = -1;
  55. } else {
  56. if (last != note->current) {
  57. last = note->current;
  58. currentStep = 0;
  59. }
  60. if (currentStep >= note->length) {
  61. outputs[ AUDIO1_OUTPUT ].value = 0;
  62. } else {
  63. outputs[ AUDIO1_OUTPUT ].value = note->sample[currentStep];
  64. currentStep++;
  65. }
  66. }
  67. }
  68. struct DMXWidget : ModuleWidget {
  69. DMXWidget(DMXModule *module);
  70. };
  71. DMXWidget::DMXWidget(DMXModule *module) : ModuleWidget(module) {
  72. box.size = Vec(3 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
  73. {
  74. SVGPanel *panel = new SVGPanel( );
  75. panel->box.size = box.size;
  76. panel->setBackground(SVG::load(assetPlugin(plugin, "res/DMX.svg")));
  77. addChild(panel);
  78. }
  79. addChild(Widget::create<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  80. addChild(Widget::create<ScrewSilver>(
  81. Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  82. addInput(Port::create<PJ301MPort>(Vec(10, 45), Port::INPUT, module,
  83. DMXModule::NOTE1_INPUT));
  84. addOutput(Port::create<PJ301MPort>(Vec(10, 92), Port::OUTPUT, module,
  85. DMXModule::AUDIO1_OUTPUT));
  86. }
  87. } // namespace rack_plugin_DrumKit
  88. using namespace rack_plugin_DrumKit;
  89. RACK_PLUGIN_MODEL_INIT(DrumKit, DMX) {
  90. Model *modelDMX = Model::create<DMXModule, DMXWidget>("DrumKit", "DMX", "DMX", DRUM_TAG);
  91. return modelDMX;
  92. }