hydrogen 1.2.3
Adsr.h
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program 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 2 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 https://www.gnu.org/licenses
20 *
21 */
22
23#ifndef H2C_ADSR_H
24#define H2C_ADSR_H
25
26#include <core/Object.h>
27
28#include <memory>
29
30namespace H2Core
31{
32
37class ADSR : public Object<ADSR>
38{
40 public:
41
49 ADSR ( unsigned int attack = 0, unsigned int decay = 0, float sustain = 1.0, unsigned int release = 1000 );
50
52 ADSR( const std::shared_ptr<ADSR> other );
53
55 ~ADSR();
56
57 void setAttack( unsigned int value );
58 unsigned int getAttack();
59 void setDecay( unsigned int value );
60 unsigned int getDecay();
61 void setSustain( float value );
62 float getSustain();
63 void setRelease( unsigned int value );
64 unsigned int getRelease();
65
66
70 void attack();
77 float release();
78
107 bool applyADSR( float *pLeft, float *pRight, int nFinalBufferPos, int nReleaseFrame, float fStep );
108
110 enum class State {
111 Attack = 0,
112 Decay,
113 Sustain,
114 Release,
115 Idle
116 };
117 static QString StateToQString( State state );
118
119 State getState() const;
120
129 QString toQString( const QString& sPrefix = "", bool bShort = true ) const override;
130 private:
131
132 unsigned int m_nAttack;
133 unsigned int m_nDecay;
135 unsigned int m_nRelease;
149 float m_fValue;
151
152 double m_fQ;
153
154 void normalise();
155};
156
157// DEFINITIONS
158
159inline void ADSR::setAttack( unsigned int value )
160{
161 m_nAttack = value;
162}
163
164inline unsigned int ADSR::getAttack()
165{
166 return m_nAttack;
167}
168
169inline void ADSR::setDecay( unsigned int value )
170{
171 m_nDecay = value;
172}
173
174inline unsigned int ADSR::getDecay()
175{
176 return m_nDecay;
177}
178
179inline void ADSR::setSustain( float value )
180{
181 m_fSustain = value;
182}
183
184inline float ADSR::getSustain()
185{
186 return m_fSustain;
187}
188
189inline void ADSR::setRelease( unsigned int value )
190{
191 m_nRelease = value;
192}
193
194inline unsigned int ADSR::getRelease()
195{
196 return m_nRelease;
197}
199 return m_state;
200}
201
202};
203
204#endif // H2C_ADRS_H
205
206/* vim: set softtabstop=4 noexpandtab: */
#define H2_OBJECT(name)
Definition Object.h:224
Attack Decay Sustain Release envelope.
Definition Adsr.h:38
unsigned int getDecay()
Definition Adsr.h:174
void attack()
Sets m_state to State::Attack.
Definition Adsr.cpp:275
~ADSR()
destructor
Definition Adsr.cpp:63
static QString StateToQString(State state)
Definition Adsr.cpp:298
ADSR(unsigned int attack=0, unsigned int decay=0, float sustain=1.0, unsigned int release=1000)
constructor
Definition Adsr.cpp:36
float m_fValue
current value
Definition Adsr.h:149
float release()
Sets m_state to State::Release and return the current m_fReleaseValue.
Definition Adsr.cpp:282
unsigned int getRelease()
Definition Adsr.h:194
unsigned int m_nDecay
Decay phase duration in frames.
Definition Adsr.h:133
unsigned int m_nAttack
Attack phase duration in frames.
Definition Adsr.h:132
State
possible states
Definition Adsr.h:110
void setSustain(float value)
Definition Adsr.h:179
double m_fQ
exponential decay state
Definition Adsr.h:152
void setAttack(unsigned int value)
Definition Adsr.h:159
void setRelease(unsigned int value)
Definition Adsr.h:189
float m_fSustain
Sustain level.
Definition Adsr.h:134
State m_state
current state
Definition Adsr.h:136
State getState() const
Definition Adsr.h:198
QString toQString(const QString &sPrefix="", bool bShort=true) const override
Formatted string version for debugging purposes.
Definition Adsr.cpp:315
bool applyADSR(float *pLeft, float *pRight, int nFinalBufferPos, int nReleaseFrame, float fStep)
Compute and apply successive ADSR values to stereo buffers.
Definition Adsr.cpp:175
float getSustain()
Definition Adsr.h:184
unsigned int m_nRelease
Release phase duration in frames.
Definition Adsr.h:135
unsigned int getAttack()
Definition Adsr.h:164
float m_fReleaseValue
value when the release state was entered
Definition Adsr.h:150
void normalise()
Definition Adsr.cpp:65
void setDecay(unsigned int value)
Definition Adsr.h:169
float m_fFramesInState
Tracks the number of frames passed in the current m_state.
Definition Adsr.h:148