swift
SimpleEnvelope.cpp
1 /*
2  * Simple Envelope Detectors (source)
3  *
4  * File : SimpleEnvelope.cpp
5  * Library : SimpleSource
6  * Version : 1.12
7  * Implements : 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 #include "SimpleEnvelope.h"
31 
32 namespace chunkware_simple
33 {
34  //-------------------------------------------------------------
35  // envelope detector
36  //-------------------------------------------------------------
37  EnvelopeDetector::EnvelopeDetector(double ms, double sampleRate)
38  {
39  assert(sampleRate > 0.0);
40  assert(ms > 0.0);
41  sampleRate_ = sampleRate;
42  ms_ = ms;
43 
44  // setCoef();
45  // TODO: KB avoid virtual function call in ctor
46  coef_ = exp(-1000.0 / (ms_ * sampleRate_));
47  }
48 
49  //-------------------------------------------------------------
50  void EnvelopeDetector::setTc(double ms)
51  {
52  assert(ms > 0.0);
53  ms_ = ms;
54  setCoef();
55  }
56 
57  //-------------------------------------------------------------
58  void EnvelopeDetector::setSampleRate(double sampleRate)
59  {
60  assert(sampleRate > 0.0);
61  sampleRate_ = sampleRate;
62  setCoef();
63  }
64 
65  //-------------------------------------------------------------
67  {
68  coef_ = exp(-1000.0 / (ms_ * sampleRate_));
69  }
70 
71  //-------------------------------------------------------------
72  // attack/release envelope
73  //-------------------------------------------------------------
74  AttRelEnvelope::AttRelEnvelope(double att_ms, double rel_ms, double sampleRate)
75  : att_(att_ms, sampleRate)
76  , rel_(rel_ms, sampleRate)
77  {
78  }
79 
80  //-------------------------------------------------------------
81  void AttRelEnvelope::setAttack(double ms)
82  {
83  att_.setTc(ms);
84  }
85 
86  //-------------------------------------------------------------
88  {
89  rel_.setTc(ms);
90  }
91 
92  //-------------------------------------------------------------
93  void AttRelEnvelope::setSampleRate(double sampleRate)
94  {
95  att_.setSampleRate(sampleRate);
96  rel_.setSampleRate(sampleRate);
97  }
98 
99 } // end namespace chunkware_simple
virtual void setAttack(double ms)
Attack time constant.
AttRelEnvelope(double att_ms=10.0, double rel_ms=100.0, double sampleRate=44100.0)
Ctor.
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.
virtual void setSampleRate(double sampleRate)
set sample rate
virtual void setTc(double ms)
set time constant
virtual void setCoef(void)
Set coefficients.
double ms_
time constant in ms
double coef_
runtime coefficient