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.

207 lines
6.5KB

  1. /*
  2. Copyright (c) 2018 bsp
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. #include <math.h>
  20. #include "bsp.hpp"
  21. namespace rack_plugin_bsp {
  22. typedef union fi_u {
  23. float f;
  24. unsigned int u;
  25. int s;
  26. } fi_t;
  27. struct AttenuMixer : Module {
  28. enum ParamIds {
  29. IN_1_SCL_PARAM,
  30. IN_1_OFF_PARAM,
  31. IN_2_SCL_PARAM,
  32. IN_2_OFF_PARAM,
  33. IN_3_SCL_PARAM,
  34. IN_3_OFF_PARAM,
  35. IN_4_SCL_PARAM,
  36. IN_4_OFF_PARAM,
  37. BIPOLAR_PARAM,
  38. FINEOFF_PARAM,
  39. NUM_PARAMS
  40. };
  41. enum InputIds {
  42. IN_1_INPUT,
  43. IN_2_INPUT,
  44. IN_3_INPUT,
  45. IN_4_INPUT,
  46. NUM_INPUTS
  47. };
  48. enum OutputIds {
  49. CTL_OUTPUT,
  50. NUM_OUTPUTS
  51. };
  52. AttenuMixer() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) {
  53. }
  54. void step() override;
  55. };
  56. void AttenuMixer::step() {
  57. float outVal = 0.0f;
  58. if(params[FINEOFF_PARAM].value >= 0.5f)
  59. {
  60. if(params[BIPOLAR_PARAM].value >= 0.5f)
  61. {
  62. for(int i = 0; i < 4; i++)
  63. {
  64. fi_t scl; scl.f = params[IN_1_SCL_PARAM + (i<<1)].value;
  65. scl.f = (2.0f * scl.f) - 1.0f;
  66. uint32_t sclSign = scl.u & 0x80000000u;
  67. scl.f *= scl.f;
  68. scl.f *= scl.f;
  69. scl.u |= sclSign;
  70. fi_t off; off.f = params[IN_1_OFF_PARAM + (i<<1)].value;
  71. uint32_t offSign = off.u & 0x80000000u;
  72. off.f /= 5.0f;
  73. off.f *= off.f;
  74. off.f *= off.f;
  75. off.u |= offSign;
  76. off.f *= 5.0f;
  77. outVal += inputs[IN_1_INPUT + i].value * scl.f + off.f;
  78. }
  79. }
  80. else
  81. {
  82. for(int i = 0; i < 4; i++)
  83. {
  84. float scl = params[IN_1_SCL_PARAM + (i<<1)].value;
  85. scl *= scl;
  86. scl *= scl;
  87. fi_t off; off.f = params[IN_1_OFF_PARAM + (i<<1)].value;
  88. uint32_t offSign = off.u & 0x80000000u;
  89. off.f /= 5.0f;
  90. off.f *= off.f;
  91. off.f *= off.f;
  92. off.u |= offSign;
  93. off.f *= 5.0f;
  94. outVal += inputs[IN_1_INPUT + i].value * scl + off.f;
  95. }
  96. }
  97. }
  98. else
  99. {
  100. if(params[BIPOLAR_PARAM].value >= 0.5f)
  101. {
  102. for(int i = 0; i < 4; i++)
  103. {
  104. fi_t scl; scl.f = params[IN_1_SCL_PARAM + (i<<1)].value;
  105. scl.f = (2.0f * scl.f) - 1.0f;
  106. uint32_t sclSign = scl.u & 0x80000000u;
  107. scl.f *= scl.f;
  108. scl.f *= scl.f;
  109. scl.u |= sclSign;
  110. float off = params[IN_1_OFF_PARAM + (i<<1)].value;
  111. outVal += inputs[IN_1_INPUT + i].value * scl.f + off;
  112. }
  113. }
  114. else
  115. {
  116. for(int i = 0; i < 4; i++)
  117. {
  118. float scl = params[IN_1_SCL_PARAM + (i<<1)].value;
  119. scl *= scl;
  120. scl *= scl;
  121. float off = params[IN_1_OFF_PARAM + (i<<1)].value;
  122. outVal += inputs[IN_1_INPUT + i].value * scl + off;
  123. }
  124. }
  125. }
  126. outputs[CTL_OUTPUT].value = outVal;
  127. #if 0
  128. static int xxx = 0;
  129. if(0 == (++xxx & 32767))
  130. {
  131. printf("xxx params[IN_2_SCL_PARAM].value=%f\n", params[IN_2_SCL_PARAM].value);
  132. }
  133. #endif
  134. }
  135. struct AttenuMixerWidget : ModuleWidget {
  136. AttenuMixerWidget(AttenuMixer *module);
  137. };
  138. AttenuMixerWidget::AttenuMixerWidget(AttenuMixer *module) : ModuleWidget(module) {
  139. setPanel(SVG::load(assetPlugin(plugin, "res/AttenuMixer.svg")));
  140. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  141. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  142. addParam(ParamWidget::create<CKSS>(Vec(box.size.x - 19, 18), module, AttenuMixer::BIPOLAR_PARAM, 0.0f, 1.0f, 0.0f));
  143. addParam(ParamWidget::create<CKSS>(Vec(2, 12), module, AttenuMixer::FINEOFF_PARAM, 0.0f, 1.0f, 0.0f));
  144. #define STY 42
  145. #define OFX 17
  146. #define OFY 20
  147. float cx = 2.0f;
  148. float cy = 34.0f;
  149. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, AttenuMixer::IN_1_SCL_PARAM, 0.0f, 1.0f, 1.0f));
  150. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx + OFX, cy + OFY), module, AttenuMixer::IN_1_OFF_PARAM, -5.0f, 5.0f, 0.0f));
  151. cy += STY;
  152. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, AttenuMixer::IN_2_SCL_PARAM, 0.0f, 1.0f, 0.796f));
  153. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx + OFX, cy + OFY), module, AttenuMixer::IN_2_OFF_PARAM, -5.0f, 5.0f, 0.0f));
  154. cy += STY;
  155. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, AttenuMixer::IN_3_SCL_PARAM, 0.0f, 1.0f, 0.5f));
  156. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx + OFX, cy + OFY), module, AttenuMixer::IN_3_OFF_PARAM, -5.0f, 5.0f, 0.0f));
  157. cy += STY;
  158. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, AttenuMixer::IN_4_SCL_PARAM, 0.0f, 1.0f, 0.25f));
  159. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx + OFX, cy + OFY), module, AttenuMixer::IN_4_OFF_PARAM, -5.0f, 5.0f, 0.0f));
  160. #undef STX
  161. #undef STY
  162. #define STY 28.0f
  163. cx = 11.0f;
  164. cy = 208.0f;
  165. addInput(Port::create<PJ301MPort>(Vec(cx, cy), Port::INPUT, module, AttenuMixer::IN_1_INPUT));
  166. cy += STY;
  167. addInput(Port::create<PJ301MPort>(Vec(cx, cy), Port::INPUT, module, AttenuMixer::IN_2_INPUT));
  168. cy += STY;
  169. addInput(Port::create<PJ301MPort>(Vec(cx, cy), Port::INPUT, module, AttenuMixer::IN_3_INPUT));
  170. cy += STY;
  171. addInput(Port::create<PJ301MPort>(Vec(cx, cy), Port::INPUT, module, AttenuMixer::IN_4_INPUT));
  172. addOutput(Port::create<PJ301MPort>(Vec(11, 325), Port::OUTPUT, module, AttenuMixer::CTL_OUTPUT));
  173. }
  174. } // namespace rack_plugin_bsp
  175. using namespace rack_plugin_bsp;
  176. RACK_PLUGIN_MODEL_INIT(bsp, AttenuMixer) {
  177. Model *modelAttenuMixer = Model::create<AttenuMixer, AttenuMixerWidget>("bsp", "AttenuMixer", "AttenuMixer", ATTENUATOR_TAG, MIXER_TAG);
  178. return modelAttenuMixer;
  179. }