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.

Kinks.cpp 3.6KB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "plugin.hpp"
  2. struct Kinks : Module {
  3. enum ParamIds {
  4. NUM_PARAMS
  5. };
  6. enum InputIds {
  7. SIGN_INPUT,
  8. LOGIC_A_INPUT,
  9. LOGIC_B_INPUT,
  10. SH_INPUT,
  11. TRIG_INPUT,
  12. NUM_INPUTS
  13. };
  14. enum OutputIds {
  15. INVERT_OUTPUT,
  16. HALF_RECTIFY_OUTPUT,
  17. FULL_RECTIFY_OUTPUT,
  18. MAX_OUTPUT,
  19. MIN_OUTPUT,
  20. NOISE_OUTPUT,
  21. SH_OUTPUT,
  22. NUM_OUTPUTS
  23. };
  24. enum LightIds {
  25. SIGN_POS_LIGHT, SIGN_NEG_LIGHT,
  26. LOGIC_POS_LIGHT, LOGIC_NEG_LIGHT,
  27. SH_POS_LIGHT, SH_NEG_LIGHT,
  28. NUM_LIGHTS
  29. };
  30. dsp::SchmittTrigger trigger;
  31. float sample = 0.0;
  32. Kinks() {
  33. config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
  34. }
  35. void process(const ProcessArgs& args) override {
  36. // Gaussian noise generator
  37. float noise = 2.0 * random::normal();
  38. // S&H
  39. if (trigger.process(inputs[TRIG_INPUT].getVoltage() / 0.7)) {
  40. sample = inputs[SH_INPUT].getNormalVoltage(noise);
  41. }
  42. // lights
  43. lights[SIGN_POS_LIGHT].setSmoothBrightness(fmaxf(0.0, inputs[SIGN_INPUT].getVoltage() / 5.0), args.sampleTime);
  44. lights[SIGN_NEG_LIGHT].setSmoothBrightness(fmaxf(0.0, -inputs[SIGN_INPUT].getVoltage() / 5.0), args.sampleTime);
  45. float logicSum = inputs[LOGIC_A_INPUT].getVoltage() + inputs[LOGIC_B_INPUT].getVoltage();
  46. lights[LOGIC_POS_LIGHT].setSmoothBrightness(fmaxf(0.0, logicSum / 5.0), args.sampleTime);
  47. lights[LOGIC_NEG_LIGHT].setSmoothBrightness(fmaxf(0.0, -logicSum / 5.0), args.sampleTime);
  48. lights[SH_POS_LIGHT].setBrightness(fmaxf(0.0, sample / 5.0));
  49. lights[SH_NEG_LIGHT].setBrightness(fmaxf(0.0, -sample / 5.0));
  50. // outputs
  51. outputs[INVERT_OUTPUT].setVoltage(-inputs[SIGN_INPUT].getVoltage());
  52. outputs[HALF_RECTIFY_OUTPUT].setVoltage(fmaxf(0.0, inputs[SIGN_INPUT].getVoltage()));
  53. outputs[FULL_RECTIFY_OUTPUT].setVoltage(fabsf(inputs[SIGN_INPUT].getVoltage()));
  54. outputs[MAX_OUTPUT].setVoltage(fmaxf(inputs[LOGIC_A_INPUT].getVoltage(), inputs[LOGIC_B_INPUT].getVoltage()));
  55. outputs[MIN_OUTPUT].setVoltage(fminf(inputs[LOGIC_A_INPUT].getVoltage(), inputs[LOGIC_B_INPUT].getVoltage()));
  56. outputs[NOISE_OUTPUT].setVoltage(noise);
  57. outputs[SH_OUTPUT].setVoltage(sample);
  58. }
  59. };
  60. struct KinksWidget : ModuleWidget {
  61. KinksWidget(Kinks* module) {
  62. setModule(module);
  63. setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/Kinks.svg")));
  64. addChild(createWidget<ScrewSilver>(Vec(15, 0)));
  65. addChild(createWidget<ScrewSilver>(Vec(15, 365)));
  66. addInput(createInput<PJ301MPort>(Vec(4, 75), module, Kinks::SIGN_INPUT));
  67. addOutput(createOutput<PJ301MPort>(Vec(31, 75), module, Kinks::INVERT_OUTPUT));
  68. addOutput(createOutput<PJ301MPort>(Vec(4, 113), module, Kinks::HALF_RECTIFY_OUTPUT));
  69. addOutput(createOutput<PJ301MPort>(Vec(31, 113), module, Kinks::FULL_RECTIFY_OUTPUT));
  70. addInput(createInput<PJ301MPort>(Vec(4, 177), module, Kinks::LOGIC_A_INPUT));
  71. addInput(createInput<PJ301MPort>(Vec(31, 177), module, Kinks::LOGIC_B_INPUT));
  72. addOutput(createOutput<PJ301MPort>(Vec(4, 214), module, Kinks::MAX_OUTPUT));
  73. addOutput(createOutput<PJ301MPort>(Vec(31, 214), module, Kinks::MIN_OUTPUT));
  74. addInput(createInput<PJ301MPort>(Vec(4, 278), module, Kinks::SH_INPUT));
  75. addInput(createInput<PJ301MPort>(Vec(31, 278), module, Kinks::TRIG_INPUT));
  76. addOutput(createOutput<PJ301MPort>(Vec(4, 316), module, Kinks::NOISE_OUTPUT));
  77. addOutput(createOutput<PJ301MPort>(Vec(31, 316), module, Kinks::SH_OUTPUT));
  78. addChild(createLight<SmallLight<GreenRedLight>>(Vec(11, 59), module, Kinks::SIGN_POS_LIGHT));
  79. addChild(createLight<SmallLight<GreenRedLight>>(Vec(11, 161), module, Kinks::LOGIC_POS_LIGHT));
  80. addChild(createLight<SmallLight<GreenRedLight>>(Vec(11, 262), module, Kinks::SH_POS_LIGHT));
  81. }
  82. };
  83. Model* modelKinks = createModel<Kinks, KinksWidget>("Kinks");