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.

189 lines
7.1KB

  1. #include "plugin.hpp"
  2. // Only valid for <1, 4> and <4, 1>
  3. template <int INPUTS, int OUTPUTS>
  4. struct SequentialSwitch : Module {
  5. enum ParamIds {
  6. STEPS_PARAM,
  7. NUM_PARAMS
  8. };
  9. enum InputIds {
  10. CLOCK_INPUT,
  11. RESET_INPUT,
  12. ENUMS(IN_INPUTS, INPUTS),
  13. NUM_INPUTS
  14. };
  15. enum OutputIds {
  16. ENUMS(OUT_OUTPUTS, OUTPUTS),
  17. NUM_OUTPUTS
  18. };
  19. enum LightIds {
  20. ENUMS(CHANNEL_LIGHTS, 4),
  21. NUM_LIGHTS
  22. };
  23. dsp::SchmittTrigger clockTrigger;
  24. dsp::SchmittTrigger resetTrigger;
  25. int index = 0;
  26. dsp::ClockDivider lightDivider;
  27. dsp::SlewLimiter clickFilters[4];
  28. SequentialSwitch() {
  29. config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
  30. configSwitch(STEPS_PARAM, 0.0, 2.0, 0.0, "Steps", {"4", "3", "2"});
  31. configInput(CLOCK_INPUT, "Clock");
  32. configInput(RESET_INPUT, "Reset");
  33. if (INPUTS == 1) {
  34. configInput(IN_INPUTS + 0, "Main");
  35. }
  36. else {
  37. for (int i = 0; i < INPUTS; i++)
  38. configInput(IN_INPUTS + i, string::f("Channel %d", i + 1));
  39. }
  40. if (OUTPUTS == 1) {
  41. configOutput(OUT_OUTPUTS + 0, "Main");
  42. }
  43. else {
  44. for (int i = 0; i < OUTPUTS; i++)
  45. configOutput(OUT_OUTPUTS + i, string::f("Channel %d", i + 1));
  46. }
  47. for (int i = 0; i < 4; i++) {
  48. clickFilters[i].rise = 400.f; // Hz
  49. clickFilters[i].fall = 400.f; // Hz
  50. }
  51. lightDivider.setDivision(512);
  52. }
  53. void process(const ProcessArgs& args) override {
  54. // Determine current index
  55. if (clockTrigger.process(rescale(inputs[CLOCK_INPUT].getVoltage(), 0.1f, 2.f, 0.f, 1.f))) {
  56. index++;
  57. }
  58. if (resetTrigger.process(rescale(inputs[RESET_INPUT].getVoltage(), 0.1f, 2.f, 0.f, 1.f))) {
  59. index = 0;
  60. }
  61. int length = 4 - (int) std::round(params[STEPS_PARAM].getValue());
  62. if (index >= length)
  63. index = 0;
  64. // Use first input to get number of channels
  65. int channels = std::max(inputs[IN_INPUTS + 0].getChannels(), 1);
  66. if (INPUTS == 1) {
  67. // <1, 4>
  68. // Get input
  69. float* in = inputs[IN_INPUTS + 0].getVoltages();
  70. // Set output
  71. for (int i = 0; i < OUTPUTS; i++) {
  72. float gain = clickFilters[i].process(args.sampleTime, index == i);
  73. outputs[OUT_OUTPUTS + i].setChannels(channels);
  74. if (gain != 0.f) {
  75. for (int c = 0; c < channels; c++) {
  76. float out = in[c] * gain;
  77. outputs[OUT_OUTPUTS + i].setVoltage(out, c);
  78. }
  79. }
  80. else {
  81. outputs[OUT_OUTPUTS + i].clearVoltages();
  82. }
  83. }
  84. }
  85. else {
  86. // <4, 1>
  87. // Get input
  88. float out[16] = {};
  89. for (int i = 0; i < INPUTS; i++) {
  90. float gain = clickFilters[i].process(args.sampleTime, index == i);
  91. if (gain != 0.f) {
  92. for (int c = 0; c < channels; c++) {
  93. float in = inputs[IN_INPUTS + i].getVoltage(c);
  94. out[c] += in * gain;
  95. }
  96. }
  97. }
  98. // Set output
  99. outputs[OUT_OUTPUTS + 0].setChannels(channels);
  100. outputs[OUT_OUTPUTS + 0].writeVoltages(out);
  101. }
  102. // Set lights
  103. if (lightDivider.process()) {
  104. for (int i = 0; i < 4; i++) {
  105. lights[CHANNEL_LIGHTS + i].setBrightness(index == i);
  106. }
  107. }
  108. }
  109. };
  110. struct SequentialSwitch1Widget : ModuleWidget {
  111. typedef SequentialSwitch<1, 4> TSequentialSwitch;
  112. SequentialSwitch1Widget(TSequentialSwitch* module) {
  113. setModule(module);
  114. setPanel(createPanel(asset::plugin(pluginInstance, "res/SequentialSwitch1.svg")));
  115. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  116. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
  117. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  118. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  119. addParam(createParamCentered<CKSSThree>(mm2px(Vec(7.555, 20.942)), module, TSequentialSwitch::STEPS_PARAM));
  120. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.555, 33.831)), module, TSequentialSwitch::CLOCK_INPUT));
  121. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.555, 50.126)), module, TSequentialSwitch::RESET_INPUT));
  122. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.555, 66.379)), module, TSequentialSwitch::IN_INPUTS + 0));
  123. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(7.555, 82.607)), module, TSequentialSwitch::OUT_OUTPUTS + 0));
  124. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(7.555, 92.767)), module, TSequentialSwitch::OUT_OUTPUTS + 1));
  125. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(7.555, 102.927)), module, TSequentialSwitch::OUT_OUTPUTS + 2));
  126. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(7.555, 113.087)), module, TSequentialSwitch::OUT_OUTPUTS + 3));
  127. addChild(createLightCentered<TinyLight<RedLight>>(mm2px(Vec(11.28, 78.863)), module, TSequentialSwitch::CHANNEL_LIGHTS + 0));
  128. addChild(createLightCentered<TinyLight<RedLight>>(mm2px(Vec(11.28, 89.023)), module, TSequentialSwitch::CHANNEL_LIGHTS + 1));
  129. addChild(createLightCentered<TinyLight<RedLight>>(mm2px(Vec(11.28, 99.183)), module, TSequentialSwitch::CHANNEL_LIGHTS + 2));
  130. addChild(createLightCentered<TinyLight<RedLight>>(mm2px(Vec(11.28, 109.343)), module, TSequentialSwitch::CHANNEL_LIGHTS + 3));
  131. }
  132. };
  133. Model* modelSequentialSwitch1 = createModel<SequentialSwitch<1, 4>, SequentialSwitch1Widget>("SequentialSwitch1");
  134. struct SequentialSwitch2Widget : ModuleWidget {
  135. typedef SequentialSwitch<4, 1> TSequentialSwitch;
  136. SequentialSwitch2Widget(TSequentialSwitch* module) {
  137. setModule(module);
  138. setPanel(createPanel(asset::plugin(pluginInstance, "res/SequentialSwitch2.svg")));
  139. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  140. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
  141. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  142. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  143. addParam(createParamCentered<CKSSThree>(mm2px(Vec(7.8, 20.942)), module, TSequentialSwitch::STEPS_PARAM));
  144. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.8, 33.831)), module, TSequentialSwitch::CLOCK_INPUT));
  145. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.8, 50.126)), module, TSequentialSwitch::RESET_INPUT));
  146. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.8, 66.379)), module, TSequentialSwitch::IN_INPUTS + 0));
  147. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.8, 76.539)), module, TSequentialSwitch::IN_INPUTS + 1));
  148. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.8, 86.699)), module, TSequentialSwitch::IN_INPUTS + 2));
  149. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(7.8, 96.859)), module, TSequentialSwitch::IN_INPUTS + 3));
  150. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(7.8, 113.115)), module, TSequentialSwitch::OUT_OUTPUTS + 0));
  151. addChild(createLightCentered<TinyLight<RedLight>>(mm2px(Vec(11.526, 63.259)), module, TSequentialSwitch::CHANNEL_LIGHTS + 0));
  152. addChild(createLightCentered<TinyLight<RedLight>>(mm2px(Vec(11.526, 72.795)), module, TSequentialSwitch::CHANNEL_LIGHTS + 1));
  153. addChild(createLightCentered<TinyLight<RedLight>>(mm2px(Vec(11.526, 82.955)), module, TSequentialSwitch::CHANNEL_LIGHTS + 2));
  154. addChild(createLightCentered<TinyLight<RedLight>>(mm2px(Vec(11.526, 93.115)), module, TSequentialSwitch::CHANNEL_LIGHTS + 3));
  155. }
  156. };
  157. Model* modelSequentialSwitch2 = createModel<SequentialSwitch<4, 1>, SequentialSwitch2Widget>("SequentialSwitch2");