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.

125 lines
3.3KB

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