To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / CollidoscopeApp / include / EnvASR.h @ 16:4dad0b810f18
History | View | Annotate | Download (4.41 KB)
| 1 | 5:75b744078d66 | f | /*
|
|---|---|---|---|
| 2 | |||
| 3 | Copyright (C) 2016 Queen Mary University of London
|
||
| 4 | 16:4dad0b810f18 | f | Author: Fiore Martin, based on CCRMA STK ADSR.h (https://ccrma.stanford.edu/software/stk/classstk_1_1ADSR.html)
|
| 5 | 5:75b744078d66 | f | |
| 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 | 16:4dad0b810f18 | f | |
| 21 | This file incorporates work covered by the following copyright and permission notice:
|
||
| 22 | |||
| 23 | The Synthesis ToolKit in C++ (STK)
|
||
| 24 | |||
| 25 | Copyright (c) 1995--2016 Perry R. Cook and Gary P. Scavone
|
||
| 26 | |||
| 27 | Permission is hereby granted, free of charge, to any person obtaining
|
||
| 28 | a copy of this software and associated documentation files (the
|
||
| 29 | "Software"), to deal in the Software without restriction, including
|
||
| 30 | without limitation the rights to use, copy, modify, merge, publish,
|
||
| 31 | distribute, sublicense, and/or sell copies of the Software, and to
|
||
| 32 | permit persons to whom the Software is furnished to do so, subject to
|
||
| 33 | the following conditions:
|
||
| 34 | |||
| 35 | The above copyright notice and this permission notice shall be
|
||
| 36 | included in all copies or substantial portions of the Software.
|
||
| 37 | |||
| 38 | Any person wishing to distribute modifications to the Software is
|
||
| 39 | asked to send the modifications to the original developer so that they
|
||
| 40 | can be incorporated into the canonical version. This is, however, not
|
||
| 41 | a binding provision of this license.
|
||
| 42 | |||
| 43 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||
| 44 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||
| 45 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||
| 46 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
||
| 47 | ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
||
| 48 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||
| 49 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
| 50 | |||
| 51 | 5:75b744078d66 | f | */
|
| 52 | |||
| 53 | 0:02467299402e | f | #pragma once
|
| 54 | |||
| 55 | namespace collidoscope {
|
||
| 56 | |||
| 57 | 2:dd889fff8423 | f | |
| 58 | /*
|
||
| 59 | * An ASR envelope with linear shape. It is modeled after the STK envelope classes.
|
||
| 60 | * The tick() method advances the computation of the envelope one sample and returns the computed sample
|
||
| 61 | * The class is templated for the type of the samples that each tick of the envelope produces.
|
||
| 62 | *
|
||
| 63 | * Client classes can set/get the current state of the envelope with the
|
||
| 64 | * respective getter/setter methods
|
||
| 65 | *
|
||
| 66 | */
|
||
| 67 | 0:02467299402e | f | template <typename T> |
| 68 | class EnvASR |
||
| 69 | {
|
||
| 70 | public:
|
||
| 71 | |||
| 72 | 3:7fb593d53361 | f | /** Possible states of the envelope. Idle means the envelope ouputs 0 */
|
| 73 | 0:02467299402e | f | enum class State {
|
| 74 | eAttack, |
||
| 75 | eSustain, |
||
| 76 | eRelease, |
||
| 77 | eIdle // before attack after release
|
||
| 78 | }; |
||
| 79 | |||
| 80 | EnvASR( T sustainLevel, T attackTime, T releaseTime, std::size_t sampleRate ) : |
||
| 81 | mSustainLevel( sustainLevel ), |
||
| 82 | mState( State::eIdle ), |
||
| 83 | mValue( 0 )
|
||
| 84 | |||
| 85 | {
|
||
| 86 | if ( attackTime <= 0 ) |
||
| 87 | attackTime = T( 0.001 ); |
||
| 88 | |||
| 89 | if ( releaseTime <= 0 ) |
||
| 90 | releaseTime = T( 0.001 ); |
||
| 91 | |||
| 92 | mAttackRate = T( 1.0 ) / (attackTime * sampleRate); |
||
| 93 | mReleaseRate = T( 1.0 ) / (releaseTime * sampleRate); |
||
| 94 | } |
||
| 95 | |||
| 96 | 3:7fb593d53361 | f | /** Produces one sample worth of envelope */
|
| 97 | 0:02467299402e | f | T tick() |
| 98 | {
|
||
| 99 | |||
| 100 | switch ( mState )
|
||
| 101 | {
|
||
| 102 | |||
| 103 | case State::eIdle: {
|
||
| 104 | mValue = 0;
|
||
| 105 | }; |
||
| 106 | break;
|
||
| 107 | |||
| 108 | case State::eAttack: {
|
||
| 109 | mValue += mAttackRate; |
||
| 110 | if ( mValue >= mSustainLevel ){
|
||
| 111 | mValue = mSustainLevel; |
||
| 112 | mState = State::eSustain; |
||
| 113 | } |
||
| 114 | }; |
||
| 115 | break;
|
||
| 116 | |||
| 117 | case State::eRelease:
|
||
| 118 | mValue -= mReleaseRate; |
||
| 119 | if ( mValue <= 0 ){ |
||
| 120 | mValue = 0;
|
||
| 121 | mState = State::eIdle; |
||
| 122 | } |
||
| 123 | break;
|
||
| 124 | default:
|
||
| 125 | break;
|
||
| 126 | } |
||
| 127 | |||
| 128 | return mValue;
|
||
| 129 | |||
| 130 | } |
||
| 131 | |||
| 132 | State getState() const
|
||
| 133 | {
|
||
| 134 | return mState;
|
||
| 135 | } |
||
| 136 | |||
| 137 | void setState( State state )
|
||
| 138 | {
|
||
| 139 | mState = state; |
||
| 140 | } |
||
| 141 | |||
| 142 | private:
|
||
| 143 | T mSustainLevel; |
||
| 144 | T mAttackRate; |
||
| 145 | T mReleaseRate; |
||
| 146 | |||
| 147 | // output
|
||
| 148 | T mValue; |
||
| 149 | |||
| 150 | State mState; |
||
| 151 | |||
| 152 | }; |
||
| 153 | |||
| 154 | |||
| 155 | 2:dd889fff8423 | f | } |