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.

140 lines
3.4KB

  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. // Vocoder.
  28. #ifndef WARPS_DSP_VOCODER_H_
  29. #define WARPS_DSP_VOCODER_H_
  30. #include "stmlib/stmlib.h"
  31. #include "warps/dsp/filter_bank.h"
  32. #include "warps/dsp/limiter.h"
  33. namespace warps {
  34. const float kFollowerGain = sqrtf(kNumBands);
  35. class EnvelopeFollower {
  36. public:
  37. EnvelopeFollower() { }
  38. ~EnvelopeFollower() { }
  39. void Init() {
  40. envelope_ = 0.0f;
  41. freeze_ = false;
  42. attack_ = decay_ = 0.1f;
  43. peak_ = 0.0f;
  44. };
  45. void set_attack(float attack) {
  46. attack_ = attack;
  47. }
  48. void set_decay(float decay) {
  49. decay_ = decay;
  50. }
  51. void set_freeze(bool freeze) {
  52. freeze_ = freeze;
  53. }
  54. void Process(const float* in, float* out, size_t size) {
  55. float envelope = envelope_;
  56. float attack = freeze_ ? 0.0f : attack_;
  57. float decay = freeze_ ? 0.0f : decay_;
  58. float peak = 0.0f;
  59. while (size--) {
  60. float error = fabs(*in++ * kFollowerGain) - envelope;
  61. envelope += (error > 0.0f ? attack : decay) * error;
  62. if (envelope > peak) {
  63. peak = envelope;
  64. }
  65. *out++ = envelope;
  66. }
  67. envelope_ = envelope;
  68. float error = peak - peak_;
  69. peak_ += (error > 0.0f ? 0.5f : 0.1f) * error;
  70. }
  71. inline float peak() const { return peak_; }
  72. private:
  73. float attack_;
  74. float decay_;
  75. float envelope_;
  76. float peak_;
  77. float freeze_;
  78. DISALLOW_COPY_AND_ASSIGN(EnvelopeFollower);
  79. };
  80. struct BandGain {
  81. float carrier;
  82. float vocoder;
  83. };
  84. class Vocoder {
  85. public:
  86. Vocoder() { }
  87. ~Vocoder() { }
  88. void Init(float sample_rate);
  89. void Process(
  90. const float* modulator,
  91. const float* carrier,
  92. float* out,
  93. size_t size);
  94. void set_release_time(float release_time) {
  95. release_time_ = release_time;
  96. }
  97. void set_formant_shift(float formant_shift) {
  98. formant_shift_ = formant_shift;
  99. }
  100. private:
  101. float release_time_;
  102. float formant_shift_;
  103. BandGain previous_gain_[kNumBands];
  104. BandGain gain_[kNumBands];
  105. float tmp_[kMaxFilterBankBlockSize];
  106. FilterBank modulator_filter_bank_;
  107. FilterBank carrier_filter_bank_;
  108. Limiter limiter_;
  109. EnvelopeFollower follower_[kNumBands];
  110. DISALLOW_COPY_AND_ASSIGN(Vocoder);
  111. };
  112. } // namespace warps
  113. #endif // WARPS_DSP_VOCODER_H_