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.

200 lines
5.1KB

  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 <stdlib.h> // memset
  21. #include "bsp.hpp"
  22. namespace rack_plugin_bsp {
  23. typedef union fi_u {
  24. float f;
  25. unsigned int u;
  26. int s;
  27. } fi_t;
  28. struct RMS : Module {
  29. enum ParamIds {
  30. IN_AMP_PARAM,
  31. WIN_SIZE_PARAM,
  32. SMOOTH_RISE_PARAM,
  33. SMOOTH_FALL_PARAM,
  34. OUT_AMP_PARAM,
  35. NUM_PARAMS
  36. };
  37. enum InputIds {
  38. AUDIO_INPUT,
  39. NUM_INPUTS
  40. };
  41. enum OutputIds {
  42. RMS_OUTPUT,
  43. NUM_OUTPUTS
  44. };
  45. static const uint32_t MAX_WIN_SIZE = (1024u);
  46. float buf[MAX_WIN_SIZE];
  47. uint32_t buf_idx;
  48. double integrated_val;
  49. double smoothed_sign;
  50. double last_smoothed_val;
  51. double smoothed_val;
  52. uint32_t last_win_size;
  53. RMS() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) {
  54. last_win_size = 0u;
  55. buf_idx = 0u;
  56. }
  57. void step() override;
  58. };
  59. void RMS::step() {
  60. #if 0
  61. outputs[RMS_OUTPUT].value = 0.0f;
  62. return;
  63. #endif
  64. uint32_t winSize = (1u << uint32_t(params[WIN_SIZE_PARAM].value));
  65. uint32_t winSizeMask = (winSize - 1u);
  66. if(winSize != last_win_size)
  67. {
  68. last_win_size = winSize;
  69. ::memset((void*)buf, 0, winSize * sizeof(float));
  70. buf_idx = 0u;
  71. integrated_val = 0.0;
  72. smoothed_sign = 0.0;
  73. last_smoothed_val = 0.0;
  74. smoothed_val = 0.0;
  75. }
  76. float inAmp = params[IN_AMP_PARAM].value;
  77. inAmp *= inAmp;
  78. inAmp *= inAmp;
  79. // amp is now in range 0..1000
  80. // Read new input and calc square
  81. float inValOrig = inputs[AUDIO_INPUT].value;
  82. float inVal = inValOrig * inAmp;
  83. inVal *= inVal;
  84. // Integrate new input
  85. integrated_val += inVal;
  86. // Subtract oldest input
  87. integrated_val -= buf[(buf_idx - winSize + 1u) & winSizeMask];
  88. buf[buf_idx] = inVal;
  89. buf_idx = (buf_idx + 1u) & winSizeMask;
  90. double outVal = integrated_val / double(winSize);
  91. if(outVal > 0.0)
  92. outVal = sqrt(outVal);
  93. // Smoothing
  94. double smoothAmt;
  95. if(smoothed_sign >= 0.0)
  96. {
  97. smoothAmt = params[SMOOTH_RISE_PARAM].value;
  98. }
  99. else
  100. {
  101. smoothAmt = params[SMOOTH_FALL_PARAM].value;
  102. }
  103. smoothAmt = (1.0 - smoothAmt);
  104. smoothAmt *= smoothAmt;
  105. smoothAmt *= smoothAmt;
  106. smoothAmt *= smoothAmt;
  107. smoothed_val = smoothed_val + (outVal - smoothed_val) * smoothAmt;
  108. smoothed_sign = (smoothed_val - last_smoothed_val);
  109. last_smoothed_val = smoothed_val;
  110. // Output
  111. float outAmp = params[OUT_AMP_PARAM].value;
  112. outAmp *= outAmp;
  113. outAmp *= outAmp;
  114. // out amp is now in range 0..1000
  115. outputs[RMS_OUTPUT].value = float(smoothed_val * outAmp);
  116. #if 0
  117. static int xxx = 0;
  118. if(0 == (++xxx & 32767))
  119. {
  120. printf("xxx winSize=%u winSizeMask=%u bufIdx=%u smoothAmt=%f\n", winSize, winSizeMask, buf_idx, smoothAmt);
  121. }
  122. #endif
  123. }
  124. struct RMSWidget : ModuleWidget {
  125. RMSWidget(RMS *module);
  126. };
  127. RMSWidget::RMSWidget(RMS *module) : ModuleWidget(module) {
  128. setPanel(SVG::load(assetPlugin(plugin, "res/RMS.svg")));
  129. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  130. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  131. float cx;
  132. float cy;
  133. #define STY 33.0f
  134. cx = 12.0f;
  135. cy = 50.0f;
  136. addInput(Port::create<PJ301MPort>(Vec(11.0f, cy), Port::INPUT, module, RMS::AUDIO_INPUT));
  137. cy += STY;
  138. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, RMS::IN_AMP_PARAM, 0.0f, 3.0f, 0.562341325191f));
  139. #undef STY
  140. #define STY 50.0f
  141. cx = 12.0f;
  142. cy = 140.0f;
  143. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, RMS::WIN_SIZE_PARAM, 1.0f, 10.0f, 6.0f));
  144. cy += STY;
  145. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, RMS::SMOOTH_RISE_PARAM, 0.0f, 1.0f, 0.447f));
  146. cy += STY;
  147. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, RMS::SMOOTH_FALL_PARAM, 0.0f, 1.0f, 0.52f));
  148. #undef STX
  149. #undef STY
  150. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(12.0f, 290.0f), module, RMS::OUT_AMP_PARAM, 0.0f, 3.0f, 1.8f));
  151. addOutput(Port::create<PJ301MPort>(Vec(11, 325), Port::OUTPUT, module, RMS::RMS_OUTPUT));
  152. }
  153. } // namespace rack_plugin_bsp
  154. using namespace rack_plugin_bsp;
  155. RACK_PLUGIN_MODEL_INIT(bsp, RMS) {
  156. Model *modelRMS = Model::create<RMS, RMSWidget>("bsp", "RMS", "RMS", ENVELOPE_FOLLOWER_TAG, UTILITY_TAG);
  157. return modelRMS;
  158. }