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.

138 lines
4.2KB

  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. #include "streams/follower.h"
  29. #include "stmlib/utils/dsp.h"
  30. #include "streams/gain.h"
  31. #include "streams/resources.h"
  32. namespace streams {
  33. using namespace stmlib;
  34. void Follower::Init() {
  35. analysis_low_.Init();
  36. analysis_low_.set_frequency(45 << 7);
  37. analysis_low_.set_resonance(0);
  38. analysis_medium_.Init();
  39. analysis_medium_.set_frequency(86 << 7);
  40. analysis_medium_.set_resonance(0);
  41. for (uint8_t i = 0; i < 3; ++i) {
  42. energy_[i][0] = energy_[i][1] = 0;
  43. follower_[i] = 0;
  44. follower_lp_[i] = 0;
  45. spectrum_[i] = 0;
  46. }
  47. centroid_ = 0;
  48. }
  49. void Follower::Process(
  50. int16_t audio,
  51. int16_t excite,
  52. uint16_t* gain,
  53. uint16_t* frequency) {
  54. // Smooth frequency amount parameters.
  55. frequency_amount_ += (target_frequency_amount_ - frequency_amount_) >> 8;
  56. frequency_offset_ += (target_frequency_offset_ - frequency_offset_) >> 8;
  57. analysis_low_.Process(excite);
  58. analysis_medium_.Process(analysis_low_.hp());
  59. int32_t channel[3];
  60. channel[0] = analysis_low_.lp();
  61. channel[1] = analysis_medium_.lp();
  62. channel[2] = analysis_medium_.hp();
  63. int32_t envelope = 0;
  64. int32_t centroid_numerator = 0;
  65. int32_t centroid_denominator = 0;
  66. for (int32_t i = 0; i < 3; ++i) {
  67. int32_t energy = channel[i];
  68. energy *= energy;
  69. // Ride an ascending peak.
  70. if (energy_[i][0] < energy_[i][1] && energy_[i][1] < energy &&
  71. energy > follower_[i]) {
  72. follower_[i] = energy;
  73. }
  74. // Otherwise, hold and snap on local maxima.
  75. if (energy_[i][0] <= energy_[i][1] && energy_[i][1] >= energy) {
  76. follower_[i] = energy_[i][1];
  77. }
  78. energy_[i][0] = energy_[i][1];
  79. energy_[i][1] = energy;
  80. // Then let a low-pass filter smooth things out.
  81. int64_t error = follower_[i] - follower_lp_[i];
  82. if (error > 0) {
  83. follower_lp_[i] += error * attack_coefficient_[i] >> 31;
  84. } else {
  85. follower_lp_[i] += error * decay_coefficient_[i] >> 31;
  86. }
  87. envelope += follower_lp_[i] >> 13;
  88. // Integrate more slowly for spectrum estimation.
  89. if (only_filter_) {
  90. error = follower_lp_[i] - spectrum_[i];
  91. spectrum_[i] += error >> 6;
  92. } else {
  93. error = follower_[i] - spectrum_[i];
  94. spectrum_[i] += error >> 10;
  95. }
  96. centroid_numerator += i * (spectrum_[i] >> 1) >> 16;
  97. centroid_denominator += spectrum_[i] >> 16;
  98. }
  99. if (envelope > 65535) {
  100. envelope = 65535;
  101. } else if (envelope < 0) {
  102. envelope = 0;
  103. }
  104. uint16_t gain_mod = Interpolate824(lut_square_root, envelope << 16) >> 1;
  105. int32_t centroid = (centroid_numerator << 15) / (centroid_denominator + 1);
  106. if (gain_mod > 4096) {
  107. centroid_ = centroid;
  108. } else if (gain_mod > 2048) {
  109. centroid_ += (centroid - centroid_) >> 8;
  110. }
  111. *gain = gain_mod * kUnityGain >> 15;
  112. *frequency = frequency_offset_ + (centroid_ * frequency_amount_ >> 15);
  113. if (only_filter_) {
  114. *gain = *frequency;
  115. *frequency = 65535;
  116. }
  117. }
  118. } // namespace streams