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.

167 lines
4.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 "dsp/digital.hpp"
  21. #include "bsp.hpp"
  22. namespace rack_plugin_bsp {
  23. struct DownSampler : Module {
  24. enum ParamIds {
  25. RATE_PARAM,
  26. NUM_PARAMS
  27. };
  28. enum InputIds {
  29. AUDIO_INPUT,
  30. TRIG_INPUT,
  31. RATE_MOD_INPUT,
  32. NUM_INPUTS
  33. };
  34. enum OutputIds {
  35. AUDIO_OUTPUT,
  36. NUM_OUTPUTS
  37. };
  38. static const uint32_t BUFFER_SIZE = (512*1024u); // ~11.8sec @ 44.1kHz
  39. static const uint32_t BUFFER_SIZE_MASK = (BUFFER_SIZE - 1u);
  40. float *buf;
  41. uint32_t buf_write_idx;
  42. int32_t rate_read_left;
  43. uint32_t buf_read_idx;
  44. SchmittTrigger trigger;
  45. float sample_rate;
  46. DownSampler() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) {
  47. buf_read_idx = ~0u;
  48. rate_read_left = 0;
  49. buf_write_idx = 0u;
  50. buf = new float[BUFFER_SIZE];
  51. handleSampleRateChanged();
  52. }
  53. ~DownSampler() {
  54. delete [] buf;
  55. buf = NULL;
  56. }
  57. void handleSampleRateChanged(void) {
  58. sample_rate = engineGetSampleRate();
  59. }
  60. void onSampleRateChange() override {
  61. Module::onSampleRateChange();
  62. handleSampleRateChanged();
  63. }
  64. void step() override;
  65. };
  66. void DownSampler::step() {
  67. if(trigger.process(inputs[TRIG_INPUT].value))
  68. {
  69. // printf("xxx DownSampler: trig\n");
  70. buf_read_idx = ~0u;
  71. buf_write_idx = 0u;
  72. rate_read_left = 0;
  73. }
  74. // Append new input to ring buffer
  75. float inVal = inputs[AUDIO_INPUT].value;
  76. buf[buf_write_idx] = inVal;
  77. buf_write_idx = (buf_write_idx + 1u) & BUFFER_SIZE_MASK;
  78. if(--rate_read_left < 0)
  79. {
  80. buf_read_idx = (buf_read_idx + 1u) & BUFFER_SIZE_MASK;
  81. float rateF = params[RATE_PARAM].value;
  82. if(inputs[RATE_MOD_INPUT].active)
  83. {
  84. rateF += inputs[RATE_MOD_INPUT].value * (7.0f / 5.0f);
  85. }
  86. rate_read_left = int32_t(rateF);
  87. }
  88. float outVal = buf[buf_read_idx];
  89. outputs[AUDIO_OUTPUT].value = outVal;
  90. #if 0
  91. static int xxx = 0;
  92. if(0 == (++xxx & 32767))
  93. {
  94. printf("xxx readIdx=%u writeIdx=%u readLeft=%d\n", buf_read_idx, buf_write_idx, rate_read_left);
  95. }
  96. #endif
  97. }
  98. struct DownSamplerWidget : ModuleWidget {
  99. DownSamplerWidget(DownSampler *module);
  100. };
  101. DownSamplerWidget::DownSamplerWidget(DownSampler *module) : ModuleWidget(module) {
  102. setPanel(SVG::load(assetPlugin(plugin, "res/DownSampler.svg")));
  103. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  104. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  105. float cx;
  106. float cy;
  107. #define STY 60.0f
  108. cx = 12.0f;
  109. cy = 66.0f;
  110. addInput(Port::create<PJ301MPort>(Vec(11.0f, cy), Port::INPUT, module, DownSampler::AUDIO_INPUT));
  111. cy += STY;
  112. addInput(Port::create<PJ301MPort>(Vec(11.0f, cy), Port::INPUT, module, DownSampler::TRIG_INPUT));
  113. #undef STY
  114. #define STY 32.0f
  115. cx = 12.0f;
  116. cy = 200.0f;
  117. addParam(ParamWidget::create<RoundSmallBlackKnob>(Vec(cx, cy), module, DownSampler::RATE_PARAM, 0.0f, 7.0f, 1.0f));
  118. cy += STY;
  119. addInput(Port::create<PJ301MPort>(Vec(11.0f, cy), Port::INPUT, module, DownSampler::RATE_MOD_INPUT));
  120. #undef STX
  121. #undef STY
  122. addOutput(Port::create<PJ301MPort>(Vec(11, 325), Port::OUTPUT, module, DownSampler::AUDIO_OUTPUT));
  123. }
  124. } // namespace rack_plugin_bsp
  125. using namespace rack_plugin_bsp;
  126. RACK_PLUGIN_MODEL_INIT(bsp, DownSampler) {
  127. Model *modelDownSampler = Model::create<DownSampler, DownSamplerWidget>("bsp", "DownSampler", "DownSampler", SAMPLER_TAG);
  128. return modelDownSampler;
  129. }