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.

94 lines
2.9KB

  1. // Copyright 2015 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Noise burst generator for Karplus-Strong synthesis.
  28. #ifndef RINGS_DSP_PLUCKER_H_
  29. #define RINGS_DSP_PLUCKER_H_
  30. #include "stmlib/stmlib.h"
  31. #include <algorithm>
  32. #include "stmlib/dsp/filter.h"
  33. #include "stmlib/dsp/delay_line.h"
  34. #include "stmlib/utils/random.h"
  35. namespace rings {
  36. class Plucker {
  37. public:
  38. Plucker() { }
  39. ~Plucker() { }
  40. void Init() {
  41. svf_.Init();
  42. comb_filter_.Init();
  43. remaining_samples_ = 0;
  44. comb_filter_period_ = 0.0f;
  45. }
  46. void Trigger(float frequency, float cutoff, float position) {
  47. float ratio = position * 0.9f + 0.05f;
  48. float comb_period = 1.0f / frequency * ratio;
  49. remaining_samples_ = static_cast<size_t>(comb_period);
  50. while (comb_period >= 255.0f) {
  51. comb_period *= 0.5f;
  52. }
  53. comb_filter_period_ = comb_period;
  54. comb_filter_gain_ = (1.0f - position) * 0.8f;
  55. svf_.set_f_q<FREQUENCY_DIRTY>(std::min(cutoff, 0.499f), 1.0f);
  56. }
  57. void Process(float* out, size_t size) {
  58. const float comb_gain = comb_filter_gain_;
  59. const float comb_delay = comb_filter_period_;
  60. for (size_t i = 0; i < size; ++i) {
  61. float in = 0.0f;
  62. if (remaining_samples_) {
  63. in = 2.0f * Random::GetFloat() - 1.0f;
  64. --remaining_samples_;
  65. }
  66. out[i] = in + comb_gain * comb_filter_.Read(comb_delay);
  67. comb_filter_.Write(out[i]);
  68. }
  69. svf_.Process<FILTER_MODE_LOW_PASS>(out, out, size);
  70. }
  71. private:
  72. stmlib::Svf svf_;
  73. stmlib::DelayLine<float, 256> comb_filter_;
  74. size_t remaining_samples_;
  75. float comb_filter_period_;
  76. float comb_filter_gain_;
  77. DISALLOW_COPY_AND_ASSIGN(Plucker);
  78. };
  79. } // namespace rings
  80. #endif // RINGS_DSP_PLUCKER_H_