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