To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / CollidoscopeApp / include / EnvASR.h @ 7:a4a336624f5a

History | View | Annotate | Download (2.81 KB)

1
/*
2

3
 Copyright (C) 2016  Queen Mary University of London 
4
 Author: Fiore Martin
5

6
 This file is part of Collidoscope.
7
 
8
 Collidoscope is free software: you can redistribute it and/or modify
9
 it under the terms of the GNU General Public License as published by
10
 the Free Software Foundation, either version 3 of the License, or
11
 (at your option) any later version.
12

13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 GNU General Public License for more details.
17

18
 You should have received a copy of the GNU General Public License
19
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
*/
21

    
22
#pragma once 
23

    
24
namespace collidoscope {
25

    
26

    
27
/* 
28
 * An ASR envelope with linear shape. It is modeled after the STK envelope classes.
29
 * The tick() method advances the computation of the envelope one sample and returns the computed sample
30
 * The class is templated for the type of the samples that each tick of the envelope produces. 
31
 *
32
 * Client classes can set/get the current state of the envelope with the
33
 * respective getter/setter methods
34
 *
35
 */
36
template <typename T>
37
class EnvASR
38
{
39
public:
40

    
41
    /** Possible states of the envelope. Idle means the envelope ouputs 0 */
42
    enum class State {
43
        eAttack,
44
        eSustain,
45
        eRelease,
46
        eIdle // before attack after release  
47
    };
48

    
49
    EnvASR( T sustainLevel, T attackTime, T releaseTime, std::size_t sampleRate ) :
50
        mSustainLevel( sustainLevel ),
51
        mState( State::eIdle ),
52
        mValue( 0 )
53
        
54
    {
55
        if ( attackTime <= 0 )
56
            attackTime = T( 0.001 );
57

    
58
        if ( releaseTime <= 0 )
59
            releaseTime = T( 0.001 );
60
        
61
        mAttackRate =  T( 1.0 ) / (attackTime * sampleRate);
62
        mReleaseRate = T( 1.0 ) / (releaseTime * sampleRate);
63
    }
64

    
65
    /** Produces one sample worth of envelope */
66
    T tick()
67
    {
68

    
69
        switch ( mState )
70
        {
71

    
72
        case State::eIdle: {
73
            mValue = 0;
74
        };
75
            break;
76

    
77
        case State::eAttack: {
78
            mValue += mAttackRate;
79
            if ( mValue >= mSustainLevel ){
80
                mValue = mSustainLevel;
81
                mState = State::eSustain;
82
            }
83
        };
84
            break;
85

    
86
        case State::eRelease:
87
            mValue -= mReleaseRate;
88
            if ( mValue <= 0 ){
89
                mValue = 0;
90
                mState = State::eIdle;
91
            }
92
            break;
93
        default:
94
            break;
95
        }
96

    
97
        return mValue;
98

    
99
    }
100

    
101
    State getState() const
102
    {
103
        return mState;
104
    }
105

    
106
    void setState( State state )
107
    {
108
        mState = state;
109
    }
110

    
111
private:
112
    T mSustainLevel;
113
    T mAttackRate;
114
    T mReleaseRate;
115

    
116
    // output
117
    T mValue;
118

    
119
    State mState;
120

    
121
};
122

    
123

    
124
}