-
Notifications
You must be signed in to change notification settings - Fork 1
/
synth-envelope.h
119 lines (98 loc) · 2.1 KB
/
synth-envelope.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
FM. BISON hybrid FM synthesis -- Flexible analog style ADSR envelope.
(C) njdewit technologies (visualizers.nl) & bipolaraudio.nl
MIT license applies, please see https://en.wikipedia.org/wiki/MIT_License or LICENSE in the project root!
*/
#pragma once
#include "synth-global.h"
#include "3rdparty/JUCE/ADSR.h"
namespace SFM
{
class Envelope
{
public:
Envelope()
{
Reset();
}
// Full ADSR
struct Parameters
{
// In seconds
float preAttack;
// [0..1]
float attack;
float decay;
float sustain; // Level
float release;
// In seconds
float globalMul; // [kEnvMulMin..kEnvMulMin+kEnvMulRange] (synth-global.h or Arturia DX V manual)
// Curvature
float attackCurve;
float decayCurve;
float releaseCurve;
};
void Start(const Parameters ¶meters, unsigned sampleRate, bool isCarrier, float keyTracking, float velScaling);
SFM_INLINE void Stop()
{
// Kill pre-attack
m_preAttackSamples = 0;
if (false == m_isInfinite)
{
// Release
m_ADSR.noteOff();
}
else
{
// Sustain forever
m_ADSR.sustain();
}
}
SFM_INLINE void Reset()
{
m_preAttackSamples = 0;
m_ADSR.reset();
m_isInfinite = false;
}
SFM_INLINE float Sample()
{
if (0 != m_preAttackSamples)
{
// Wait for it..
--m_preAttackSamples;
SFM_ASSERT(0.f == m_ADSR.getSample());
return 0.f;
}
else
{
return m_ADSR.getNextSample();
}
}
// Use to get value without sampling
SFM_INLINE float Get() const
{
return m_ADSR.getSample();
}
SFM_INLINE bool IsReleasing() const
{
const bool isReleasing = m_ADSR.isReleasing();
return isReleasing;
}
SFM_INLINE bool IsIdle() const
{
const bool isIdle = false == m_ADSR.isActive();
SFM_ASSERT(false == isIdle || (true == isIdle && 0.f == m_ADSR.getSample()));
return isIdle;
}
SFM_INLINE bool IsInfinite()
{
return m_isInfinite;
}
// To emulate piano-style sustain pedal behaviour
void OnPianoSustain(float falloff, float pedalReleaseMul);
private:
unsigned m_preAttackSamples;
ADSR m_ADSR;
bool m_isInfinite;
};
}