eDSP  0.0.1
A cross-platform DSP library written in C++.
envelope_follower.hpp
Go to the documentation of this file.
1 /*
2  * eDSP, A cross-platform Digital Signal Processing library written in modern C++.
3  * Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi, All rights reserved.
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation, either version 3 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along width
16  * this program. If not, see <http://www.gnu.org/licenses/>
17  *
18  * File: envelope_follower.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 14/6/2018
21  */
22 #ifndef EDSP_FEATURE_TEMPORAL_EnvelopeFollower_HPP
23 #define EDSP_FEATURE_TEMPORAL_EnvelopeFollower_HPP
24 
25 #include <edsp/math/numeric.hpp>
26 #include <numeric>
27 #include <cmath>
28 
29 namespace edsp {
30 
43  template <typename T>
45  public:
46  using value_type = T;
47  using size_type = std::size_t;
48 
56  constexpr envelope_follower(value_type samplerate, value_type attackTime, value_type releaseTime,
57  bool rectify = false) noexcept;
58 
63  constexpr value_type samplerate() const noexcept;
64 
69  constexpr void set_samplerate(value_type samplerate) noexcept;
70 
75  constexpr value_type attack_time() const noexcept;
76 
81  constexpr void set_attack_time(value_type attackTime) noexcept;
82 
87  constexpr value_type release_time() const noexcept;
88 
93  constexpr void set_release_time(value_type releaseTime) noexcept;
94 
99  constexpr bool rectification() const noexcept;
100 
107  constexpr void set_rectification(bool enabled) noexcept;
108 
112  constexpr void reset() noexcept;
113 
122  template <typename InIterator, typename OutputIt>
123  constexpr void apply(InIterator first, InIterator last, OutputIt d_first);
124 
125  private:
126  value_type samplerate_{44100};
127  value_type attack_time_{10};
128  value_type attack_gain_{0};
129  value_type release_time_{1500};
130  value_type release_gain_{0};
131  value_type last_{0};
132  bool rectification_{false};
133  };
134 
135  template <typename T>
137  value_type release_time, bool rectify) noexcept :
138  samplerate_(samplerate),
139  attack_time_(attack_time),
140  release_time_(release_time),
141  rectification_(rectify) {
142  reset();
143  }
144 
145  template <typename T>
147  return samplerate_;
148  }
149 
150  template <typename T>
152  samplerate_ = samplerate;
153  reset();
154  }
155 
156  template <typename T>
158  return attack_time_;
159  }
160 
161  template <typename T>
163  attack_time_ = attack_time;
164  reset();
165  }
166 
167  template <typename T>
169  return release_time_;
170  }
171 
172  template <typename T>
174  release_time_ = release_time;
175  reset();
176  }
177 
178  template <typename T>
179  constexpr bool envelope_follower<T>::rectification() const noexcept {
180  return rectification_;
181  }
182 
183  template <typename T>
184  constexpr void envelope_follower<T>::set_rectification(bool enabled) noexcept {
185  rectification_ = enabled;
186  }
187 
188  template <typename T>
189  constexpr void envelope_follower<T>::reset() noexcept {
190  attack_gain_ = (attack_gain_ > 0) ? static_cast<value_type>(std::exp(-1 / (attack_time_ * samplerate_))) : 0;
191  release_gain_ = (release_gain_ > 0) ? static_cast<value_type>(std::exp(-1 / (release_time_ * samplerate_))) : 0;
192  last_ = 0;
193  }
194 
195  template <typename T>
196  template <typename InIterator, typename OutputIt>
197  constexpr void envelope_follower<T>::apply(InIterator first, InIterator last, OutputIt d_first) {
198  for (; first != last; ++first, ++d_first) {
199  const auto rectified = rectification_ ? std::abs(*first) : *first;
200  const auto current = (last_ < rectified) ? (1 - attack_gain_) * rectified + attack_gain_ * last_
201  : (1 - release_gain_) * rectified + release_gain_ * last_;
202  last_ = math::is_denormal(current) ? 0 : current;
203  *d_first = last_;
204  }
205  }
206 
207 } // namespace edsp
208 
209 #endif // EDSP_FEATURE_TEMPORAL_EnvelopeFollower_HPP
This class implements a basic envelope-follower.
Definition: envelope_follower.hpp:44
constexpr bool rectification() const noexcept
Checks if the rectification is enabled.
Definition: envelope_follower.hpp:179
constexpr value_type attack_time() const noexcept
Returns the attack time of the first order lowpass in the attack phase.
Definition: envelope_follower.hpp:157
std::size_t size_type
Definition: envelope_follower.hpp:47
constexpr bool is_denormal(T x)
Determines if the number is denormal floating-point.
Definition: numeric.hpp:80
constexpr void apply(InIterator first, InIterator last, OutputIt d_first)
Computes the envelope of the element&#39;s value in the range [first, last), and stores the result in ano...
Definition: envelope_follower.hpp:197
constexpr void set_attack_time(value_type attackTime) noexcept
Set the the attack time of the first order lowpass in the attack phase.
Definition: envelope_follower.hpp:162
constexpr void reset() noexcept
Resets the temporal an internal data.
Definition: envelope_follower.hpp:189
constexpr void rectify(InputIt first, InputIt last, OutputIt d_first)
For each element in the range [first, last) computes the absolute value not and stores the result in ...
Definition: rectify.hpp:41
constexpr value_type samplerate() const noexcept
Returns the sample rate in Hz.
Definition: envelope_follower.hpp:146
constexpr void set_release_time(value_type releaseTime) noexcept
Set the the release time of the first order lowpass in the attack phase.
Definition: envelope_follower.hpp:173
constexpr void set_rectification(bool enabled) noexcept
Enables the rectification of the output signal.
Definition: envelope_follower.hpp:184
constexpr value_type release_time() const noexcept
Returns the release time of the first order lowpass in the release phase.
Definition: envelope_follower.hpp:168
constexpr envelope_follower(value_type samplerate, value_type attackTime, value_type releaseTime, bool rectify=false) noexcept
Creates an EnvelopeFollower object class.
Definition: envelope_follower.hpp:136
Definition: amplifier.hpp:29
T value_type
Definition: envelope_follower.hpp:46
constexpr void set_samplerate(value_type samplerate) noexcept
Sets the sample rate in Hz and resets the internal parameters.
Definition: envelope_follower.hpp:151