swift
SimpleEnvelope.h
1 /*
2  * Simple Envelope Detectors (header)
3  *
4  * File : SimpleEnvelope.h
5  * Library : SimpleSource
6  * Version : 1.12
7  * Class : EnvelopeDetector, AttRelEnvelope
8  *
9  * 2006, ChunkWare Music Software, OPEN-SOURCE
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */
29 
30 
31 #ifndef __SIMPLE_ENVELOPE_H__
32 #define __SIMPLE_ENVELOPE_H__
33 
34 #include "SimpleHeader.h" // common header
35 
36 namespace chunkware_simple
37 {
38  //-------------------------------------------------------------
39  // DC offset (to prevent denormal)
40  //-------------------------------------------------------------
41 
42  // USE:
43  // 1. init envelope state to DC_OFFSET before processing
44  // 2. add to input before envelope runtime function
45  static constexpr double DC_OFFSET = 1.0E-25;
46 
49  {
50  public:
52  EnvelopeDetector(double ms = 1.0, double sampleRate = 44100.0);
53 
55  virtual ~EnvelopeDetector() {}
56 
58  virtual void setTc(double ms);
59 
61  virtual double getTc(void) const { return ms_; }
62 
64  virtual void setSampleRate(double sampleRate);
65 
67  virtual double getSampleRate(void) const { return sampleRate_; }
68 
70  INLINE void run(double in, double &state)
71  {
72  state = in + coef_ * (state - in);
73  }
74 
75  protected:
77  virtual void setCoef(void);
78 
79  double sampleRate_;
80  double ms_;
81  double coef_;
82  }; // end SimpleComp class
83 
86  {
87  public:
89  AttRelEnvelope(double att_ms = 10.0, double rel_ms = 100.0, double sampleRate = 44100.0);
90 
92  virtual ~AttRelEnvelope() {}
93 
96  virtual void setAttack(double ms);
97  virtual double getAttack(void) const { return att_.getTc(); }
99 
102  virtual void setRelease(double ms);
103  virtual double getRelease(void) const { return rel_.getTc(); }
105 
108  virtual void setSampleRate(double sampleRate);
109  virtual double getSampleRate(void) const { return att_.getSampleRate(); }
111 
113  INLINE void run(double in, double &state)
114  {
115 
116  /* assumes that:
117  * positive delta = attack
118  * negative delta = release
119  * good for linear & log values
120  */
121 
122  if (in > state)
123  att_.run(in, state); // attack
124  else
125  rel_.run(in, state); // release
126  }
127 
128  private:
129  EnvelopeDetector att_;
130  EnvelopeDetector rel_;
131 
132  }; // end AttRelEnvelope class
133 
134 } // end namespace chunkware_simple
135 
136 #endif // end __SIMPLE_ENVELOPE_H__
attack/release envelope
virtual void setAttack(double ms)
Attack time constant.
virtual double getRelease(void) const
Release time constant.
AttRelEnvelope(double att_ms=10.0, double rel_ms=100.0, double sampleRate=44100.0)
Ctor.
virtual double getSampleRate(void) const
Sample rate dependencies.
INLINE void run(double in, double &state)
Runtime function.
virtual double getAttack(void) const
Attack time constant.
virtual void setSampleRate(double sampleRate)
Sample rate dependencies.
virtual void setRelease(double ms)
Release time constant.
EnvelopeDetector(double ms=1.0, double sampleRate=44100.0)
Ctor.
INLINE void run(double in, double &state)
runtime function
virtual void setSampleRate(double sampleRate)
set sample rate
virtual double getTc(void) const
get time constant
virtual void setTc(double ms)
set time constant
virtual void setCoef(void)
Set coefficients.
double ms_
time constant in ms
double coef_
runtime coefficient
virtual double getSampleRate(void) const
get sample rate