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.

171 lines
5.6KB

  1. #include "dsp/digital.hpp"
  2. #include <iostream>
  3. #include "RJModules.hpp"
  4. namespace rack_plugin_RJModules {
  5. struct LowFrequencyOscillator {
  6. float phase = 0.0;
  7. float pw = 0.5;
  8. float freq = 1.0;
  9. bool offset = false;
  10. bool invert = false;
  11. SchmittTrigger resetTrigger;
  12. LowFrequencyOscillator() {}
  13. void setPitch(float pitch) {
  14. pitch = fminf(pitch, 8.0);
  15. freq = powf(2.0, pitch);
  16. }
  17. float getFreq() {
  18. return freq;
  19. }
  20. void setReset(float reset) {
  21. if (resetTrigger.process(reset)) {
  22. phase = 0.0;
  23. }
  24. }
  25. void step(float dt) {
  26. //float deltaPhase = fminf(freq * dt, 0.5);
  27. float deltaPhase = fminf(freq * dt, 0.5);
  28. phase += deltaPhase;
  29. if (phase >= 1.0)
  30. phase -= 1.0;
  31. }
  32. float sin() {
  33. if (offset)
  34. return 1.0 - cosf(2*M_PI * phase) * (invert ? -1.0 : 1.0);
  35. else
  36. return sinf(2*M_PI * phase) * (invert ? -1.0 : 1.0);
  37. }
  38. float saw(float x) {
  39. return 2.0 * (x - roundf(x));
  40. }
  41. float saw() {
  42. if (offset)
  43. return invert ? 2.0 * (1.0 - phase) : 2.0 * phase;
  44. else
  45. return saw(phase) * (invert ? -1.0 : 1.0);
  46. }
  47. float light() {
  48. return sinf(2*M_PI * phase);
  49. }
  50. };
  51. struct TwinLFO : Module {
  52. enum ParamIds {
  53. OFFSET_PARAM,
  54. INVERT_PARAM,
  55. FREQ_PARAM,
  56. FREQ_PARAM_2,
  57. DETUNE_PARAM,
  58. SHAPE_PARAM,
  59. THREE_OSC_PARAM,
  60. NUM_PARAMS
  61. };
  62. enum InputIds {
  63. FREQ_CV_INPUT,
  64. FREQ_CV_INPUT_2,
  65. SHAPE_CV_INPUT,
  66. RESET_INPUT,
  67. PW_INPUT,
  68. NUM_INPUTS
  69. };
  70. enum OutputIds {
  71. SAW_OUTPUT,
  72. NUM_OUTPUTS
  73. };
  74. enum LightIds {
  75. PHASE_POS_LIGHT,
  76. PHASE_NEG_LIGHT,
  77. PHASE_POS_LIGHT2,
  78. PHASE_NEG_LIGHT2,
  79. NUM_LIGHTS
  80. };
  81. LowFrequencyOscillator oscillator;
  82. LowFrequencyOscillator oscillator2;
  83. TwinLFO() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  84. void step() override;
  85. };
  86. void TwinLFO::step() {
  87. float root_pitch2 = params[FREQ_PARAM_2].value * clamp(inputs[FREQ_CV_INPUT_2].normalize(10.0f) / 10.0f, 0.0f, 1.0f);
  88. oscillator2.setPitch(root_pitch2);
  89. oscillator2.offset = (params[OFFSET_PARAM].value > 0.0);
  90. oscillator2.invert = (params[INVERT_PARAM].value <= 0.0);
  91. oscillator2.step(0.3 / engineGetSampleRate());
  92. oscillator2.setReset(inputs[RESET_INPUT].value);
  93. float root_pitch = params[FREQ_PARAM].value * clamp(inputs[FREQ_CV_INPUT].normalize(10.0f) / 10.0f, 0.0f, 1.0f) * clamp(oscillator2.sin(), 0.0f, 1.0f);
  94. oscillator.setPitch(root_pitch);
  95. oscillator.offset = (params[OFFSET_PARAM].value > 0.0);
  96. oscillator.invert = (params[INVERT_PARAM].value <= 0.0);
  97. oscillator.step(0.3 / engineGetSampleRate());
  98. oscillator.setReset(inputs[RESET_INPUT].value);
  99. float shape_percent = params[SHAPE_PARAM].value * clamp(inputs[SHAPE_CV_INPUT].normalize(10.0f) / 10.0f, 0.0f, 1.0f);
  100. float mixed = ((oscillator.sin() * shape_percent)) + (oscillator.saw() * (1-shape_percent));
  101. // This is from 0 to 2V. Should be mapped?
  102. outputs[SAW_OUTPUT].value = mixed;
  103. lights[PHASE_POS_LIGHT].setBrightnessSmooth(fmaxf(0.0, oscillator.light()));
  104. lights[PHASE_NEG_LIGHT].setBrightnessSmooth(fmaxf(0.0, -oscillator.light()));
  105. lights[PHASE_POS_LIGHT2].setBrightnessSmooth(fmaxf(0.0, oscillator2.light()));
  106. lights[PHASE_NEG_LIGHT2].setBrightnessSmooth(fmaxf(0.0, -oscillator2.light()));
  107. }
  108. struct TwinLFOWidget: ModuleWidget {
  109. TwinLFOWidget(TwinLFO *module);
  110. };
  111. TwinLFOWidget::TwinLFOWidget(TwinLFO *module) : ModuleWidget(module) {
  112. box.size = Vec(15*10, 380);
  113. {
  114. SVGPanel *panel = new SVGPanel();
  115. panel->box.size = box.size;
  116. panel->setBackground(SVG::load(assetPlugin(plugin, "res/TwinLFO.svg")));
  117. addChild(panel);
  118. }
  119. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  120. addChild(Widget::create<ScrewSilver>(Vec(box.size.x-30, 0)));
  121. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  122. addChild(Widget::create<ScrewSilver>(Vec(box.size.x-30, 365)));
  123. addParam(ParamWidget::create<CKSS>(Vec(119, 100), module, TwinLFO::OFFSET_PARAM, 0.0, 1.0, 1.0));
  124. addParam(ParamWidget::create<CKSS>(Vec(119, 180), module, TwinLFO::INVERT_PARAM, 0.0, 1.0, 1.0));
  125. addParam(ParamWidget::create<RoundHugeBlackKnob>(Vec(47, 61), module, TwinLFO::FREQ_PARAM, 0.0, 8.0, 5.0));
  126. addParam(ParamWidget::create<RoundHugeBlackKnob>(Vec(47, 143), module, TwinLFO::FREQ_PARAM_2, 0.0, 8.0, 0.5));
  127. addParam(ParamWidget::create<RoundHugeBlackKnob>(Vec(47, 228), module, TwinLFO::SHAPE_PARAM, 0.0, 1.0, 1.0));
  128. addInput(Port::create<PJ301MPort>(Vec(22, 100), Port::INPUT, module, TwinLFO::FREQ_CV_INPUT));
  129. addInput(Port::create<PJ301MPort>(Vec(22, 190), Port::INPUT, module, TwinLFO::FREQ_CV_INPUT_2));
  130. addInput(Port::create<PJ301MPort>(Vec(22, 270), Port::INPUT, module, TwinLFO::SHAPE_CV_INPUT));
  131. addInput(Port::create<PJ301MPort>(Vec(38, 310), Port::INPUT, module, TwinLFO::RESET_INPUT));
  132. addOutput(Port::create<PJ301MPort>(Vec(100, 310), Port::OUTPUT, module, TwinLFO::SAW_OUTPUT));
  133. addChild(ModuleLightWidget::create<SmallLight<GreenRedLight>>(Vec(99, 60), module, TwinLFO::PHASE_POS_LIGHT));
  134. addChild(ModuleLightWidget::create<SmallLight<GreenRedLight>>(Vec(99, 140), module, TwinLFO::PHASE_POS_LIGHT2));
  135. }
  136. } // namespace rack_plugin_RJModules
  137. using namespace rack_plugin_RJModules;
  138. RACK_PLUGIN_MODEL_INIT(RJModules, TwinLFO) {
  139. Model *modelTwinLFO = Model::create<TwinLFO, TwinLFOWidget>("RJModules", "TwinLFO", "[GEN] TwinLFO", LFO_TAG);
  140. return modelTwinLFO;
  141. }