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.

117 lines
3.2KB

  1. #include "HetrickCV.hpp"
  2. namespace rack_plugin_HetrickCV {
  3. struct Exponent : Module
  4. {
  5. enum ParamIds
  6. {
  7. AMOUNT_PARAM,
  8. SCALE_PARAM,
  9. RANGE_PARAM,
  10. NUM_PARAMS
  11. };
  12. enum InputIds
  13. {
  14. MAIN_INPUT,
  15. AMOUNT_INPUT,
  16. NUM_INPUTS
  17. };
  18. enum OutputIds
  19. {
  20. MAIN_OUTPUT,
  21. NUM_OUTPUTS
  22. };
  23. Exponent() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS)
  24. {
  25. }
  26. void step() override;
  27. // For more advanced Module features, read Rack's engine.hpp header file
  28. // - toJson, fromJson: serialization of internal data
  29. // - onSampleRateChange: event triggered by a change of sample rate
  30. // - reset, randomize: implements special behavior when user clicks these from the context menu
  31. };
  32. void Exponent::step()
  33. {
  34. float input = inputs[MAIN_INPUT].value;
  35. const bool negativeInput = input < 0.0f;
  36. bool mode5V = (params[RANGE_PARAM].value == 0.0f);
  37. if(mode5V) input = clampf(input, -5.0f, 5.0f) * 0.2f;
  38. else input = clampf(input, -10.0f, 10.0f) * 0.1f;
  39. input = std::abs(input);
  40. float exponent = params[AMOUNT_PARAM].value + (inputs[AMOUNT_INPUT].value * params[SCALE_PARAM].value);
  41. exponent = clampf(exponent, -5.0f, 5.0f) * 0.2f;
  42. if(exponent < 0)
  43. {
  44. exponent = 1.0f - (exponent * -0.5f);
  45. }
  46. else exponent += 1.0f;
  47. float output = powf(input, exponent);
  48. if (negativeInput) output *= -1.0f;
  49. if(mode5V) output *= 5.0f;
  50. else output *= 10.0f;
  51. outputs[MAIN_OUTPUT].value = output;
  52. }
  53. struct CKSSRot : SVGSwitch, ToggleSwitch {
  54. CKSSRot() {
  55. addFrame(SVG::load(assetPlugin(plugin, "res/CKSS_rot_0.svg")));
  56. addFrame(SVG::load(assetPlugin(plugin, "res/CKSS_rot_1.svg")));
  57. sw->wrap();
  58. box.size = sw->box.size;
  59. }
  60. };
  61. struct ExponentWidget : ModuleWidget { ExponentWidget(Exponent *module); };
  62. ExponentWidget::ExponentWidget(Exponent *module) : ModuleWidget(module)
  63. {
  64. box.size = Vec(6 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
  65. {
  66. auto *panel = new SVGPanel();
  67. panel->box.size = box.size;
  68. panel->setBackground(SVG::load(assetPlugin(plugin, "res/Exponent.svg")));
  69. addChild(panel);
  70. }
  71. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  72. addChild(Widget::create<ScrewSilver>(Vec(box.size.x - 30, 0)));
  73. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  74. addChild(Widget::create<ScrewSilver>(Vec(box.size.x - 30, 365)));
  75. //////PARAMS//////
  76. addParam(ParamWidget::create<Davies1900hBlackKnob>(Vec(27, 62), module, Exponent::AMOUNT_PARAM, -5.0, 5.0, 0.0));
  77. addParam(ParamWidget::create<Trimpot>(Vec(36, 112), module, Exponent::SCALE_PARAM, -1.0, 1.0, 1.0));
  78. addParam(ParamWidget::create<CKSSRot>(Vec(35, 200), module, Exponent::RANGE_PARAM, 0.0, 1.0, 0.0));
  79. //////INPUTS//////
  80. addInput(Port::create<PJ301MPort>(Vec(33, 235), Port::INPUT, module, Exponent::MAIN_INPUT));
  81. addInput(Port::create<PJ301MPort>(Vec(33, 145), Port::INPUT, module, Exponent::AMOUNT_INPUT));
  82. //////OUTPUTS//////
  83. addOutput(Port::create<PJ301MPort>(Vec(33, 285), Port::OUTPUT, module, Exponent::MAIN_OUTPUT));
  84. }
  85. } // namespace rack_plugin_HetrickCV
  86. using namespace rack_plugin_HetrickCV;
  87. RACK_PLUGIN_MODEL_INIT(HetrickCV, Exponent) {
  88. Model *modelExponent = Model::create<Exponent, ExponentWidget>("HetrickCV", "Exponent", "Exponent", WAVESHAPER_TAG);
  89. return modelExponent;
  90. }