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.

132 lines
3.8KB

  1. // Copyright 2014 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. // Follower.
  28. #ifndef STREAMS_FOLLOWER_H_
  29. #define STREAMS_FOLLOWER_H_
  30. #include "stmlib/stmlib.h"
  31. #include "streams/meta_parameters.h"
  32. #include "streams/resources.h"
  33. #include "streams/svf.h"
  34. namespace streams {
  35. const uint16_t kNumBands = 3;
  36. class Follower {
  37. public:
  38. Follower() { }
  39. ~Follower() { }
  40. void Init();
  41. void Process(
  42. int16_t audio,
  43. int16_t excite,
  44. uint16_t* gain,
  45. uint16_t* frequency);
  46. void Configure(bool alternate, int32_t* parameters, int32_t* globals) {
  47. uint16_t attack_time;
  48. uint16_t decay_time;
  49. if (globals) {
  50. // Attack: 1ms to 100ms
  51. attack_time = globals[0] >> 8;
  52. // Decay: 10ms to 1000ms
  53. decay_time = 128 + (globals[2] >> 8);
  54. ComputeAmountOffset(
  55. parameters[1],
  56. &target_frequency_amount_,
  57. &target_frequency_offset_);
  58. } else {
  59. uint16_t shape = parameters[0];
  60. if (shape < 32768) {
  61. // attack: 1ms to 2ms.
  62. attack_time = (shape * 39 >> 15);
  63. // decay: 10ms to 100ms.
  64. decay_time = 128 + (shape * 128 >> 15);
  65. } else {
  66. shape -= 32768;
  67. // attack: 2ms to 20ms.
  68. attack_time = 39 + (shape * 128 >> 15);
  69. // decay: 100ms to 200ms.
  70. decay_time = 128 + 128 + (shape * 39 >> 15);
  71. }
  72. ComputeAmountOffset(
  73. parameters[1],
  74. &target_frequency_amount_,
  75. &target_frequency_offset_);
  76. }
  77. // Slow down the attack detection on low frequencies.
  78. attack_coefficient_[0] = lut_lp_coefficients[attack_time + 39];
  79. attack_coefficient_[1] = lut_lp_coefficients[attack_time + 19];
  80. attack_coefficient_[2] = lut_lp_coefficients[attack_time + 0];
  81. // Slow down the decay detection on high frequencies as there is more noise.
  82. decay_coefficient_[0] = lut_lp_coefficients[decay_time + 39];
  83. decay_coefficient_[1] = lut_lp_coefficients[decay_time + 19];
  84. decay_coefficient_[2] = lut_lp_coefficients[decay_time + 99];
  85. only_filter_ = alternate;
  86. }
  87. private:
  88. Svf analysis_low_;
  89. Svf analysis_medium_;
  90. int32_t energy_[kNumBands][2];
  91. int64_t follower_[kNumBands];
  92. int64_t attack_coefficient_[kNumBands];
  93. int64_t decay_coefficient_[kNumBands];
  94. int64_t follower_lp_[kNumBands];
  95. int32_t spectrum_[kNumBands];
  96. int32_t centroid_;
  97. int32_t frequency_offset_;
  98. int32_t frequency_amount_;
  99. int32_t target_frequency_offset_;
  100. int32_t target_frequency_amount_;
  101. int32_t naive_;
  102. bool only_filter_;
  103. DISALLOW_COPY_AND_ASSIGN(Follower);
  104. };
  105. } // namespace streams
  106. #endif // STREAMS_FOLLOWER_H_