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.

113 lines
3.2KB

  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. // Envelope / centroid follower for FM voice.
  28. #ifndef RINGS_DSP_FOLLOWER_H_
  29. #define RINGS_DSP_FOLLOWER_H_
  30. #include "stmlib/stmlib.h"
  31. #include <algorithm>
  32. #include "stmlib/dsp/dsp.h"
  33. #include "stmlib/dsp/filter.h"
  34. namespace rings {
  35. using namespace stmlib;
  36. class Follower {
  37. public:
  38. Follower() { }
  39. ~Follower() { }
  40. void Init(float low, float low_mid, float mid_high) {
  41. low_mid_filter_.Init();
  42. mid_high_filter_.Init();
  43. low_mid_filter_.set_f_q<FREQUENCY_DIRTY>(low_mid, 0.5f);
  44. mid_high_filter_.set_f_q<FREQUENCY_DIRTY>(mid_high, 0.5f);
  45. attack_[0] = low_mid;
  46. decay_[0] = Sqrt(low_mid * low);
  47. attack_[1] = Sqrt(low_mid * mid_high);
  48. decay_[1] = low_mid;
  49. attack_[2] = Sqrt(mid_high * 0.5f);
  50. decay_[2] = Sqrt(mid_high * low_mid);
  51. std::fill(&detector_[0], &detector_[3], 0.0f);
  52. centroid_ = 0.0f;
  53. }
  54. void Process(
  55. float sample,
  56. float* envelope,
  57. float* centroid) {
  58. float bands[3] = { 0.0f, 0.0f, 0.0f };
  59. bands[2] = mid_high_filter_.Process<FILTER_MODE_HIGH_PASS>(sample);
  60. bands[1] = low_mid_filter_.Process<FILTER_MODE_HIGH_PASS>(
  61. mid_high_filter_.lp());
  62. bands[0] = low_mid_filter_.lp();
  63. float weighted = 0.0f;
  64. float total = 0.0f;
  65. float frequency = 0.0f;
  66. for (int32_t i = 0; i < 3; ++i) {
  67. SLOPE(detector_[i], fabs(bands[i]), attack_[i], decay_[i]);
  68. weighted += detector_[i] * frequency;
  69. total += detector_[i];
  70. frequency += 0.5f;
  71. }
  72. float error = weighted / (total + 0.001f) - centroid_;
  73. float coefficient = error > 0.0f ? 0.05f : 0.001f;
  74. centroid_ += error * coefficient;
  75. *envelope = total;
  76. *centroid = centroid_;
  77. }
  78. private:
  79. NaiveSvf low_mid_filter_;
  80. NaiveSvf mid_high_filter_;
  81. float attack_[3];
  82. float decay_[3];
  83. float detector_[3];
  84. float centroid_;
  85. DISALLOW_COPY_AND_ASSIGN(Follower);
  86. };
  87. } // namespace rings
  88. #endif // RINGS_DSP_FOLLOWER_H_