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.

219 lines
5.6KB

  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. namespace rack_plugin_bsp {
  22. struct Sway : Module {
  23. enum ParamIds {
  24. MIN_T_PARAM,
  25. MAX_T_PARAM,
  26. MIN_A_PARAM,
  27. MAX_A_PARAM,
  28. SCALE_A_PARAM,
  29. OFFSET_A_PARAM,
  30. NUM_PARAMS
  31. };
  32. enum InputIds {
  33. NUM_INPUTS
  34. };
  35. enum OutputIds {
  36. CTL_OUTPUT,
  37. NUM_OUTPUTS
  38. };
  39. static const int32_t SCALE_T_SEC = 60;
  40. static const int32_t SCALE_MAX = 5;
  41. float sampleRate;
  42. float cur_rand_val_step;
  43. float cur_rand_val;
  44. int cur_rand_val_countdown;
  45. float last_min_t;
  46. float last_max_t;
  47. #if 0
  48. void onRandomize() override {
  49. cur_rand_val_countdown = -1;
  50. }
  51. #endif
  52. static float randf(float _max) {
  53. return ((rand()*(0.999999999999f / float(RAND_MAX))) * _max);
  54. }
  55. void handleSampleRateChanged(void) {
  56. sampleRate = engineGetSampleRate();
  57. }
  58. Sway() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) {
  59. handleSampleRateChanged();
  60. cur_rand_val_countdown = -1;
  61. }
  62. void step() override;
  63. void onReset() override {
  64. handleSampleRateChanged();
  65. }
  66. void onSampleRateChange() override {
  67. Module::onSampleRateChange();
  68. handleSampleRateChanged();
  69. }
  70. };
  71. void Sway::step() {
  72. if(params[MIN_T_PARAM].value != last_min_t)
  73. {
  74. last_min_t = params[MIN_T_PARAM].value;
  75. cur_rand_val_countdown = -1;
  76. }
  77. else if(params[MAX_T_PARAM].value != last_max_t)
  78. {
  79. last_max_t = params[MAX_T_PARAM].value;
  80. cur_rand_val_countdown = -1;
  81. }
  82. if(cur_rand_val_countdown < 0)
  83. {
  84. // First sample after reset / init
  85. float minA = params[MIN_A_PARAM].value;
  86. float maxA = params[MAX_A_PARAM].value;
  87. if(minA > maxA)
  88. {
  89. float t = minA;
  90. minA = maxA;
  91. maxA = t;
  92. }
  93. cur_rand_val = randf(maxA - minA) + minA;
  94. }
  95. if(--cur_rand_val_countdown <= 0)
  96. {
  97. // Next target val
  98. float minT = params[MIN_T_PARAM].value;
  99. float maxT = params[MAX_T_PARAM].value;
  100. float minA = params[MIN_A_PARAM].value;
  101. float maxA = params[MAX_A_PARAM].value;
  102. // Sort min / max
  103. if(minT > maxT)
  104. {
  105. float t = minT;
  106. minT = maxT;
  107. maxT = t;
  108. }
  109. if(minA > maxA)
  110. {
  111. float t = minA;
  112. minA = maxA;
  113. maxA = t;
  114. }
  115. // Bias towards slow modulation
  116. minT = minT * minT;
  117. minT = minT * minT;
  118. maxT = maxT * maxT;
  119. maxT = maxT * maxT;
  120. cur_rand_val_countdown = int((randf(maxT - minT) + minT) * (sampleRate * float(SCALE_T_SEC)));
  121. if(cur_rand_val_countdown < 1)
  122. cur_rand_val_countdown = 1;
  123. float nextVal = randf(maxA - minA) + minA;
  124. cur_rand_val_step = (nextVal - cur_rand_val) / cur_rand_val_countdown;
  125. }
  126. cur_rand_val += cur_rand_val_step;
  127. // Set output
  128. float scaleA = params[SCALE_A_PARAM].value;
  129. const float offA = params[OFFSET_A_PARAM].value;
  130. // Bias towards subtle modulation
  131. scaleA = scaleA * scaleA;
  132. scaleA = scaleA * scaleA;
  133. scaleA *= float(SCALE_MAX);
  134. outputs[CTL_OUTPUT].value = cur_rand_val * scaleA + offA;
  135. }
  136. struct SwayWidget : ModuleWidget {
  137. SwayWidget(Sway *module);
  138. #if 0
  139. void randomize() override {
  140. module->onRandomize();
  141. }
  142. #endif
  143. };
  144. SwayWidget::SwayWidget(Sway *module) : ModuleWidget(module) {
  145. setPanel(SVG::load(assetPlugin(plugin, "res/Sway.svg")));
  146. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  147. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  148. // addParam(ParamWidget::create<CKSS>(Vec(15, 50), module, Obxd_VCF::FOURPOLE_PARAM, 0.0f, 1.0f, 0.0f));
  149. #define HL 55
  150. #define HS 40
  151. float cx = 9.0f;
  152. float cy = 50.0f;
  153. addParam(ParamWidget::create<RoundBlackKnob>(Vec(cx, cy), module, Sway::MIN_T_PARAM, 0.0f, 1.0f, 0.2f));
  154. cy += HS;
  155. addParam(ParamWidget::create<RoundBlackKnob>(Vec(cx, cy), module, Sway::MAX_T_PARAM, 0.0f, 1.0f, 0.3f));
  156. cy += HL;
  157. addParam(ParamWidget::create<RoundBlackKnob>(Vec(cx, cy), module, Sway::MIN_A_PARAM, -1.0f, 1.0f, -1.0f));
  158. cy += HS;
  159. addParam(ParamWidget::create<RoundBlackKnob>(Vec(cx, cy), module, Sway::MAX_A_PARAM, -1.0f, 1.0f, 1.0f));
  160. cy += HL;
  161. addParam(ParamWidget::create<RoundBlackKnob>(Vec(cx, cy), module, Sway::SCALE_A_PARAM, 0.0f, 1.0f, 0.1f));
  162. cy += HS;
  163. addParam(ParamWidget::create<RoundBlackKnob>(Vec(cx, cy), module, Sway::OFFSET_A_PARAM,-5.0f, 5.0f, 0.0f));
  164. addOutput(Port::create<PJ301MPort>(Vec(11, 325), Port::OUTPUT, module, Sway::CTL_OUTPUT));
  165. }
  166. } // namespace rack_plugin_bsp
  167. using namespace rack_plugin_bsp;
  168. RACK_PLUGIN_MODEL_INIT(bsp, Sway) {
  169. Model *modelSway = Model::create<Sway, SwayWidget>("bsp", "Sway", "Sway", NOISE_TAG, OSCILLATOR_TAG, LFO_TAG);
  170. return modelSway;
  171. }