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.

124 lines
3.4KB

  1. #include "UMix.hpp"
  2. #define SUM "sum"
  3. json_t* UMix::toJson() {
  4. json_t* root = json_object();
  5. json_object_set_new(root, SUM, json_boolean(_sum));
  6. return root;
  7. }
  8. void UMix::fromJson(json_t* root) {
  9. json_t* ll = json_object_get(root, SUM);
  10. if (ll) {
  11. _sum = json_is_true(ll);
  12. }
  13. }
  14. void UMix::step() {
  15. if (!outputs[OUT_OUTPUT].active) {
  16. return;
  17. }
  18. if (_sum) {
  19. float out = 0.0f;
  20. for (int i = 0; i < 8; ++i) {
  21. out += inputs[IN1_INPUT + i].value;
  22. }
  23. outputs[OUT_OUTPUT].value = _saturator.next(params[LEVEL_PARAM].value * out);
  24. }
  25. else {
  26. float out = 0.0f;
  27. int active = 0;
  28. for (int i = 0; i < 8; ++i) {
  29. if (inputs[IN1_INPUT + i].active) {
  30. out += inputs[IN1_INPUT + i].value;
  31. ++active;
  32. }
  33. }
  34. if (active > 0) {
  35. out /= (float)active;
  36. outputs[OUT_OUTPUT].value = _saturator.next(params[LEVEL_PARAM].value * out);
  37. }
  38. else {
  39. outputs[OUT_OUTPUT].value = 0.0f;
  40. }
  41. }
  42. }
  43. struct AverageMenuItem : MenuItem {
  44. UMix* _module;
  45. AverageMenuItem(UMix* module, const char* label)
  46. : _module(module)
  47. {
  48. this->text = label;
  49. }
  50. void onAction(EventAction &e) override {
  51. _module->_sum = !_module->_sum;
  52. }
  53. void step() override {
  54. rightText = !_module->_sum ? "?" : "";
  55. }
  56. };
  57. struct UMixWidget : ModuleWidget {
  58. static constexpr int hp = 3;
  59. UMixWidget(UMix* module) : ModuleWidget(module) {
  60. box.size = Vec(RACK_GRID_WIDTH * hp, RACK_GRID_HEIGHT);
  61. {
  62. SVGPanel *panel = new SVGPanel();
  63. panel->box.size = box.size;
  64. panel->setBackground(SVG::load(assetPlugin(plugin, "res/UMix.svg")));
  65. addChild(panel);
  66. }
  67. addChild(Widget::create<ScrewSilver>(Vec(0, 0)));
  68. addChild(Widget::create<ScrewSilver>(Vec(box.size.x - 15, 365)));
  69. // generated by svg_widgets.rb
  70. auto levelParamPosition = Vec(14.5, 320.5);
  71. auto in1InputPosition = Vec(10.5, 23.0);
  72. auto in2InputPosition = Vec(10.5, 53.0);
  73. auto in3InputPosition = Vec(10.5, 83.0);
  74. auto in4InputPosition = Vec(10.5, 113.0);
  75. auto in5InputPosition = Vec(10.5, 143.0);
  76. auto in6InputPosition = Vec(10.5, 173.0);
  77. auto in7InputPosition = Vec(10.5, 203.0);
  78. auto in8InputPosition = Vec(10.5, 233.0);
  79. auto outOutputPosition = Vec(10.5, 263.0);
  80. // end generated by svg_widgets.rb
  81. addParam(ParamWidget::create<Knob16>(levelParamPosition, module, UMix::LEVEL_PARAM, 0.0, 1.0, 1.0));
  82. addInput(Port::create<Port24>(in1InputPosition, Port::INPUT, module, UMix::IN1_INPUT));
  83. addInput(Port::create<Port24>(in2InputPosition, Port::INPUT, module, UMix::IN2_INPUT));
  84. addInput(Port::create<Port24>(in3InputPosition, Port::INPUT, module, UMix::IN3_INPUT));
  85. addInput(Port::create<Port24>(in4InputPosition, Port::INPUT, module, UMix::IN4_INPUT));
  86. addInput(Port::create<Port24>(in5InputPosition, Port::INPUT, module, UMix::IN5_INPUT));
  87. addInput(Port::create<Port24>(in6InputPosition, Port::INPUT, module, UMix::IN6_INPUT));
  88. addInput(Port::create<Port24>(in7InputPosition, Port::INPUT, module, UMix::IN7_INPUT));
  89. addInput(Port::create<Port24>(in8InputPosition, Port::INPUT, module, UMix::IN8_INPUT));
  90. addOutput(Port::create<Port24>(outOutputPosition, Port::OUTPUT, module, UMix::OUT_OUTPUT));
  91. }
  92. void appendContextMenu(Menu* menu) override {
  93. UMix* umix = dynamic_cast<UMix*>(module);
  94. assert(umix);
  95. menu->addChild(new MenuLabel());
  96. menu->addChild(new AverageMenuItem(umix, "Average"));
  97. }
  98. };
  99. RACK_PLUGIN_MODEL_INIT(Bogaudio, UMix) {
  100. Model* modelUMix = createModel<UMix, UMixWidget>("Bogaudio-UMix", "UMix", "unity mixer", MIXER_TAG);
  101. return modelUMix;
  102. }