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.

198 lines
5.3KB

  1. /*
  2. Copyright (c) 2018 bsp
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. #include <math.h>
  20. #include "bsp.hpp"
  21. #include "dsp/digital.hpp"
  22. namespace rack_plugin_bsp {
  23. struct Legato : Module {
  24. enum ParamIds {
  25. DECAY_RATE_PARAM,
  26. SMOOTH_MIN_RISE_PARAM,
  27. SMOOTH_MIN_FALL_PARAM,
  28. SMOOTH_MAX_RISE_PARAM,
  29. SMOOTH_MAX_FALL_PARAM,
  30. NUM_PARAMS
  31. };
  32. enum InputIds {
  33. CTL_INPUT,
  34. TRIG_INPUT,
  35. RATE_MOD_INPUT,
  36. NUM_INPUTS
  37. };
  38. enum OutputIds {
  39. CTL_OUTPUT,
  40. NUM_OUTPUTS
  41. };
  42. double smoothed_sign;
  43. double last_smoothed_val;
  44. double smoothed_val;
  45. double decay_t;
  46. SchmittTrigger trigger;
  47. double rcp_sample_rate;
  48. Legato() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) {
  49. smoothed_sign = 0.0;
  50. last_smoothed_val = 0.0;
  51. smoothed_val = 0.0;
  52. decay_t = 0.0;
  53. handleSampleRateChanged();
  54. }
  55. void handleSampleRateChanged(void) {
  56. rcp_sample_rate = 1.0 / double(engineGetSampleRate());
  57. }
  58. void onSampleRateChange() override {
  59. Module::onSampleRateChange();
  60. handleSampleRateChanged();
  61. }
  62. void step() override;
  63. };
  64. void Legato::step() {
  65. // Read ctl input
  66. float inVal = inputs[CTL_INPUT].value;
  67. if(trigger.process(inputs[TRIG_INPUT].value))
  68. {
  69. decay_t = 0.0;
  70. smoothed_sign = 0.0f;
  71. }
  72. else
  73. {
  74. double dcyR = params[DECAY_RATE_PARAM].value;
  75. if(inputs[RATE_MOD_INPUT].active)
  76. {
  77. dcyR += inputs[RATE_MOD_INPUT].value * (1.0 / 5);
  78. if(dcyR < 0.0)
  79. dcyR = 0.0;
  80. else if(dcyR > 1.0)
  81. dcyR = 1.0;
  82. }
  83. dcyR *= dcyR;
  84. dcyR *= dcyR;
  85. dcyR *= 4000.0f;
  86. dcyR += 1.0f;
  87. dcyR *= rcp_sample_rate; // divide by sample rate
  88. decay_t += dcyR;
  89. if(decay_t >= 1.0)
  90. decay_t = 1.0;
  91. }
  92. double smoothAmt;
  93. if(smoothed_sign >= 0.0f)
  94. {
  95. smoothAmt = params[SMOOTH_MIN_RISE_PARAM].value + (params[SMOOTH_MAX_RISE_PARAM].value - params[SMOOTH_MIN_RISE_PARAM].value) * decay_t;
  96. }
  97. else
  98. {
  99. smoothAmt = params[SMOOTH_MIN_FALL_PARAM].value + (params[SMOOTH_MAX_FALL_PARAM].value - params[SMOOTH_MIN_FALL_PARAM].value) * decay_t;
  100. }
  101. smoothAmt = (1.0 - smoothAmt);
  102. smoothAmt *= smoothAmt;
  103. smoothAmt *= smoothAmt;
  104. smoothAmt *= smoothAmt;
  105. smoothed_val = smoothed_val + (inVal - smoothed_val) * smoothAmt;
  106. smoothed_sign = (smoothed_val - last_smoothed_val);
  107. last_smoothed_val = smoothed_val;
  108. outputs[CTL_OUTPUT].value = float(smoothed_val);
  109. #if 0
  110. static int xxx = 0;
  111. if(0 == (++xxx & 32767))
  112. {
  113. printf("xxx smoothAmt=%g decay_t=%g\n", smoothAmt, decay_t);
  114. }
  115. #endif
  116. }
  117. struct LegatoWidget : ModuleWidget {
  118. LegatoWidget(Legato *module);
  119. };
  120. LegatoWidget::LegatoWidget(Legato *module) : ModuleWidget(module) {
  121. setPanel(SVG::load(assetPlugin(plugin, "res/Legato.svg")));
  122. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  123. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  124. float cx;
  125. float cy;
  126. #define STY 30.0f
  127. cx = 12.0f;
  128. cy = 50.0f;
  129. addInput(Port::create<PJ301MPort>(Vec(11.0f, cy), Port::INPUT, module, Legato::CTL_INPUT));
  130. cy += STY;
  131. addInput(Port::create<PJ301MPort>(Vec(11.0f, cy), Port::INPUT, module, Legato::TRIG_INPUT));
  132. cy += STY;
  133. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, Legato::DECAY_RATE_PARAM, 0.0f, 1.0f, 0.2f));
  134. cy += STY;
  135. addInput(Port::create<PJ301MPort>(Vec(11.0f, cy), Port::INPUT, module, Legato::RATE_MOD_INPUT));
  136. #undef STY
  137. #define STY 30.0f
  138. cx = 12.0f;
  139. cy = 185.0f;
  140. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, Legato::SMOOTH_MIN_RISE_PARAM, 0.0f, 1.0f, 0.0f));
  141. cy += STY;
  142. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, Legato::SMOOTH_MIN_FALL_PARAM, 0.0f, 1.0f, 0.0f));
  143. cy += 10.0f;
  144. cy += STY;
  145. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, Legato::SMOOTH_MAX_RISE_PARAM, 0.0f, 1.0f, 0.6f));
  146. cy += STY;
  147. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, Legato::SMOOTH_MAX_FALL_PARAM, 0.0f, 1.0f, 0.6f));
  148. #undef STX
  149. #undef STY
  150. addOutput(Port::create<PJ301MPort>(Vec(11, 325), Port::OUTPUT, module, Legato::CTL_OUTPUT));
  151. }
  152. } // namespace rack_plugin_bsp
  153. using namespace rack_plugin_bsp;
  154. RACK_PLUGIN_MODEL_INIT(bsp, Legato) {
  155. Model *modelLegato = Model::create<Legato, LegatoWidget>("bsp", "Legato", "Legato", SLEW_LIMITER_TAG, UTILITY_TAG);
  156. return modelLegato;
  157. }