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.

163 lines
5.7KB

  1. #include "Fundamental.hpp"
  2. #include "dsp/vumeter.hpp"
  3. struct Unity : Module {
  4. enum ParamIds {
  5. AVG1_PARAM,
  6. AVG2_PARAM,
  7. NUM_PARAMS
  8. };
  9. enum InputIds {
  10. IN1_INPUT,
  11. IN2_INPUT = IN1_INPUT + 6,
  12. NUM_INPUTS = IN2_INPUT + 6
  13. };
  14. enum OutputIds {
  15. MIX1_OUTPUT,
  16. INV1_OUTPUT,
  17. MIX2_OUTPUT,
  18. INV2_OUTPUT,
  19. NUM_OUTPUTS
  20. };
  21. enum LightIds {
  22. VU1_LIGHT,
  23. VU2_LIGHT = VU1_LIGHT + 5,
  24. NUM_LIGHTS = VU2_LIGHT + 5
  25. };
  26. bool merge = false;
  27. Unity() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  28. void step() override;
  29. void onReset() override {
  30. merge = false;
  31. }
  32. json_t *dataToJson() override {
  33. json_t *rootJ = json_object();
  34. // merge
  35. json_object_set_new(rootJ, "merge", json_boolean(merge));
  36. return rootJ;
  37. }
  38. void dataFromJson(json_t *rootJ) override {
  39. // merge
  40. json_t *mergeJ = json_object_get(rootJ, "merge");
  41. if (mergeJ)
  42. merge = json_boolean_value(mergeJ);
  43. }
  44. };
  45. void Unity::step() {
  46. float mix[2] = {};
  47. int count[2] = {};
  48. for (int i = 0; i < 2; i++) {
  49. // Inputs
  50. for (int j = 0; j < 6; j++) {
  51. mix[i] += inputs[IN1_INPUT + 6*i + j].value;
  52. if (inputs[IN1_INPUT + 6*i + j].active)
  53. count[i]++;
  54. }
  55. }
  56. // Combine
  57. if (merge) {
  58. mix[0] += mix[1];
  59. mix[1] = mix[0];
  60. count[0] += count[1];
  61. count[1] = count[0];
  62. }
  63. for (int i = 0; i < 2; i++) {
  64. // Params
  65. if ((int) params[AVG1_PARAM + i].value == 1 && count[i] > 0)
  66. mix[i] /= count[i];
  67. // Outputs
  68. outputs[MIX1_OUTPUT + 2*i].value = mix[i];
  69. outputs[INV1_OUTPUT + 2*i].value = -mix[i];
  70. // Lights
  71. VUMeter vuMeter;
  72. vuMeter.dBInterval = 6.0f;
  73. vuMeter.setValue(mix[i] / 10.0f);
  74. for (int j = 0; j < 5; j++) {
  75. lights[VU1_LIGHT + 5*i + j].setBrightnessSmooth(vuMeter.getBrightness(j));
  76. }
  77. }
  78. }
  79. struct UnityWidget : ModuleWidget {
  80. UnityWidget(Unity *module);
  81. void appendContextMenu(Menu *menu) override;
  82. };
  83. UnityWidget::UnityWidget(Unity *module) : ModuleWidget(module) {
  84. setPanel(SVG::load(assetPlugin(plugin, "res/Unity.svg")));
  85. addChild(createWidget<ScrewSilver>(Vec(15, 0)));
  86. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 30, 0)));
  87. addChild(createWidget<ScrewSilver>(Vec(15, 365)));
  88. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 30, 365)));
  89. addParam(createParam<CKSS>(mm2px(Vec(12.867, 52.961)), module, Unity::AVG1_PARAM, 0.0f, 1.0f, 0.0f));
  90. addParam(createParam<CKSS>(mm2px(Vec(12.867, 107.006)), module, Unity::AVG2_PARAM, 0.0f, 1.0f, 0.0f));
  91. addInput(createPort<PJ301MPort>(mm2px(Vec(2.361, 17.144)), PortWidget::INPUT, module, Unity::IN1_INPUT + 0));
  92. addInput(createPort<PJ301MPort>(mm2px(Vec(19.907, 17.144)), PortWidget::INPUT, module, Unity::IN1_INPUT + 1));
  93. addInput(createPort<PJ301MPort>(mm2px(Vec(2.361, 28.145)), PortWidget::INPUT, module, Unity::IN1_INPUT + 2));
  94. addInput(createPort<PJ301MPort>(mm2px(Vec(19.907, 28.145)), PortWidget::INPUT, module, Unity::IN1_INPUT + 3));
  95. addInput(createPort<PJ301MPort>(mm2px(Vec(2.361, 39.145)), PortWidget::INPUT, module, Unity::IN1_INPUT + 4));
  96. addInput(createPort<PJ301MPort>(mm2px(Vec(19.907, 39.145)), PortWidget::INPUT, module, Unity::IN1_INPUT + 5));
  97. addInput(createPort<PJ301MPort>(mm2px(Vec(2.361, 71.145)), PortWidget::INPUT, module, Unity::IN2_INPUT + 0));
  98. addInput(createPort<PJ301MPort>(mm2px(Vec(19.907, 71.145)), PortWidget::INPUT, module, Unity::IN2_INPUT + 1));
  99. addInput(createPort<PJ301MPort>(mm2px(Vec(2.361, 82.145)), PortWidget::INPUT, module, Unity::IN2_INPUT + 2));
  100. addInput(createPort<PJ301MPort>(mm2px(Vec(19.907, 82.145)), PortWidget::INPUT, module, Unity::IN2_INPUT + 3));
  101. addInput(createPort<PJ301MPort>(mm2px(Vec(2.361, 93.144)), PortWidget::INPUT, module, Unity::IN2_INPUT + 4));
  102. addInput(createPort<PJ301MPort>(mm2px(Vec(19.907, 93.144)), PortWidget::INPUT, module, Unity::IN2_INPUT + 5));
  103. addOutput(createPort<PJ301MPort>(mm2px(Vec(2.361, 54.15)), PortWidget::OUTPUT, module, Unity::MIX1_OUTPUT));
  104. addOutput(createPort<PJ301MPort>(mm2px(Vec(19.907, 54.15)), PortWidget::OUTPUT, module, Unity::INV1_OUTPUT));
  105. addOutput(createPort<PJ301MPort>(mm2px(Vec(2.361, 108.144)), PortWidget::OUTPUT, module, Unity::MIX2_OUTPUT));
  106. addOutput(createPort<PJ301MPort>(mm2px(Vec(19.907, 108.144)), PortWidget::OUTPUT, module, Unity::INV2_OUTPUT));
  107. addChild(createLight<MediumLight<RedLight>>(mm2px(Vec(13.652, 19.663)), module, Unity::VU1_LIGHT + 0));
  108. addChild(createLight<MediumLight<YellowLight>>(mm2px(Vec(13.652, 25.163)), module, Unity::VU1_LIGHT + 1));
  109. addChild(createLight<MediumLight<GreenLight>>(mm2px(Vec(13.652, 30.663)), module, Unity::VU1_LIGHT + 2));
  110. addChild(createLight<MediumLight<GreenLight>>(mm2px(Vec(13.652, 36.162)), module, Unity::VU1_LIGHT + 3));
  111. addChild(createLight<MediumLight<GreenLight>>(mm2px(Vec(13.652, 41.662)), module, Unity::VU1_LIGHT + 4));
  112. addChild(createLight<MediumLight<RedLight>>(mm2px(Vec(13.652, 73.663)), module, Unity::VU2_LIGHT + 0));
  113. addChild(createLight<MediumLight<YellowLight>>(mm2px(Vec(13.652, 79.163)), module, Unity::VU2_LIGHT + 1));
  114. addChild(createLight<MediumLight<GreenLight>>(mm2px(Vec(13.652, 84.663)), module, Unity::VU2_LIGHT + 2));
  115. addChild(createLight<MediumLight<GreenLight>>(mm2px(Vec(13.652, 90.162)), module, Unity::VU2_LIGHT + 3));
  116. addChild(createLight<MediumLight<GreenLight>>(mm2px(Vec(13.652, 95.662)), module, Unity::VU2_LIGHT + 4));
  117. }
  118. struct UnityMergeItem : MenuItem {
  119. Unity *unity;
  120. void onAction(const event::Action &e) override {
  121. unity->merge ^= true;
  122. }
  123. void step() override {
  124. rightText = CHECKMARK(unity->merge);
  125. }
  126. };
  127. void UnityWidget::appendContextMenu(Menu *menu) {
  128. menu->addChild(new MenuEntry);
  129. Unity *unity = dynamic_cast<Unity*>(module);
  130. assert(unity);
  131. UnityMergeItem *mergeItem = createMenuItem<UnityMergeItem>("Merge channels 1 & 2");
  132. mergeItem->unity = unity;
  133. menu->addChild(mergeItem);
  134. }
  135. Model *modelUnity = createModel<Unity, UnityWidget>("Unity");