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.

107 lines
3.8KB

  1. #include "AudibleInstruments.hpp"
  2. #include "dsp/digital.hpp"
  3. struct Kinks : Module {
  4. enum ParamIds {
  5. NUM_PARAMS
  6. };
  7. enum InputIds {
  8. SIGN_INPUT,
  9. LOGIC_A_INPUT,
  10. LOGIC_B_INPUT,
  11. SH_INPUT,
  12. TRIG_INPUT,
  13. NUM_INPUTS
  14. };
  15. enum OutputIds {
  16. INVERT_OUTPUT,
  17. HALF_RECTIFY_OUTPUT,
  18. FULL_RECTIFY_OUTPUT,
  19. MAX_OUTPUT,
  20. MIN_OUTPUT,
  21. NOISE_OUTPUT,
  22. SH_OUTPUT,
  23. NUM_OUTPUTS
  24. };
  25. enum LightIds {
  26. SIGN_POS_LIGHT, SIGN_NEG_LIGHT,
  27. LOGIC_POS_LIGHT, LOGIC_NEG_LIGHT,
  28. SH_POS_LIGHT, SH_NEG_LIGHT,
  29. NUM_LIGHTS
  30. };
  31. SchmittTrigger trigger;
  32. float sample = 0.0;
  33. Kinks() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  34. void step() override;
  35. };
  36. void Kinks::step() {
  37. // Gaussian noise generator
  38. float noise = 2.0 * randomNormal();
  39. // S&H
  40. if (trigger.process(inputs[TRIG_INPUT].value / 0.7)) {
  41. sample = inputs[SH_INPUT].normalize(noise);
  42. }
  43. // lights
  44. lights[SIGN_POS_LIGHT].setBrightnessSmooth(fmaxf(0.0, inputs[SIGN_INPUT].value / 5.0));
  45. lights[SIGN_NEG_LIGHT].setBrightnessSmooth(fmaxf(0.0, -inputs[SIGN_INPUT].value / 5.0));
  46. float logicSum = inputs[LOGIC_A_INPUT].value + inputs[LOGIC_B_INPUT].value;
  47. lights[LOGIC_POS_LIGHT].setBrightnessSmooth(fmaxf(0.0, logicSum / 5.0));
  48. lights[LOGIC_NEG_LIGHT].setBrightnessSmooth(fmaxf(0.0, -logicSum / 5.0));
  49. lights[SH_POS_LIGHT].setBrightness(fmaxf(0.0, sample / 5.0));
  50. lights[SH_NEG_LIGHT].setBrightness(fmaxf(0.0, -sample / 5.0));
  51. // outputs
  52. outputs[INVERT_OUTPUT].value = -inputs[SIGN_INPUT].value;
  53. outputs[HALF_RECTIFY_OUTPUT].value = fmaxf(0.0, inputs[SIGN_INPUT].value);
  54. outputs[FULL_RECTIFY_OUTPUT].value = fabsf(inputs[SIGN_INPUT].value);
  55. outputs[MAX_OUTPUT].value = fmaxf(inputs[LOGIC_A_INPUT].value, inputs[LOGIC_B_INPUT].value);
  56. outputs[MIN_OUTPUT].value = fminf(inputs[LOGIC_A_INPUT].value, inputs[LOGIC_B_INPUT].value);
  57. outputs[NOISE_OUTPUT].value = noise;
  58. outputs[SH_OUTPUT].value = sample;
  59. }
  60. struct KinksWidget : ModuleWidget {
  61. KinksWidget(Kinks *module) : ModuleWidget(module) {
  62. box.size = Vec(15*4, 380);
  63. {
  64. Panel *panel = new LightPanel();
  65. panel->backgroundImage = Image::load(assetPlugin(plugin, "res/Kinks.png"));
  66. panel->box.size = box.size;
  67. addChild(panel);
  68. }
  69. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  70. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  71. addInput(Port::create<PJ301MPort>(Vec(4, 75), Port::INPUT, module, Kinks::SIGN_INPUT));
  72. addOutput(Port::create<PJ301MPort>(Vec(31, 75), Port::OUTPUT, module, Kinks::INVERT_OUTPUT));
  73. addOutput(Port::create<PJ301MPort>(Vec(4, 113), Port::OUTPUT, module, Kinks::HALF_RECTIFY_OUTPUT));
  74. addOutput(Port::create<PJ301MPort>(Vec(31, 113), Port::OUTPUT, module, Kinks::FULL_RECTIFY_OUTPUT));
  75. addInput(Port::create<PJ301MPort>(Vec(4, 177), Port::INPUT, module, Kinks::LOGIC_A_INPUT));
  76. addInput(Port::create<PJ301MPort>(Vec(31, 177), Port::INPUT, module, Kinks::LOGIC_B_INPUT));
  77. addOutput(Port::create<PJ301MPort>(Vec(4, 214), Port::OUTPUT, module, Kinks::MAX_OUTPUT));
  78. addOutput(Port::create<PJ301MPort>(Vec(31, 214), Port::OUTPUT, module, Kinks::MIN_OUTPUT));
  79. addInput(Port::create<PJ301MPort>(Vec(4, 278), Port::INPUT, module, Kinks::SH_INPUT));
  80. addInput(Port::create<PJ301MPort>(Vec(31, 278), Port::INPUT, module, Kinks::TRIG_INPUT));
  81. addOutput(Port::create<PJ301MPort>(Vec(4, 316), Port::OUTPUT, module, Kinks::NOISE_OUTPUT));
  82. addOutput(Port::create<PJ301MPort>(Vec(31, 316), Port::OUTPUT, module, Kinks::SH_OUTPUT));
  83. addChild(ModuleLightWidget::create<SmallLight<GreenRedLight>>(Vec(11, 59), module, Kinks::SIGN_POS_LIGHT));
  84. addChild(ModuleLightWidget::create<SmallLight<GreenRedLight>>(Vec(11, 161), module, Kinks::LOGIC_POS_LIGHT));
  85. addChild(ModuleLightWidget::create<SmallLight<GreenRedLight>>(Vec(11, 262), module, Kinks::SH_POS_LIGHT));
  86. }
  87. };
  88. Model *modelKinks = Model::create<Kinks, KinksWidget>("Audible Instruments", "Kinks", "Utilities", UTILITY_TAG, NOISE_TAG);