UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Envelope.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "DSP/Osc.h"
9
10namespace Audio
11{
12 // Envelope class generates ADSR style envelope
14 {
15 public:
16 // States for the envelope state machine
17 enum class EEnvelopeState
18 {
19 Off,
20 Attack,
21 Decay,
22 Sustain,
23 Release,
25 };
26
29
30 // Initialize the envelope with the given sample rate
31 SIGNALPROCESSING_API void Init(const float InSampleRate, const int32 InVoiceId = 0, FModulationMatrix* InModMatrix = nullptr, const bool bInSimulateAnalog = true);
32
33 // Sets the envelope mode
35
36 // Sets whether the envelope is in legato mode. Legato mode doesn't restart the envelope if it's already playing
38
39 // Sets whether or not the envelope is zero'd when reset
41 bool IsRetrigger() const { return bIsRetriggerMode; }
42
43 // Start the envelope, puts envelope in attack state
44 SIGNALPROCESSING_API virtual void Start();
45
46 // For truly legato envelope logic, we need to know the new sustain gain (if its being changed).
47 SIGNALPROCESSING_API virtual void StartLegato(const float InNewDepth);
48 virtual void StartLegato() { StartLegato(Depth); }
49
50 // Stop the envelope, puts in the release state. Can optionally force to off state.
51 SIGNALPROCESSING_API virtual void Stop();
52
53 // Puts envelope into shutdown mode, which is a much faster cutoff than release, but avoids pops
54 SIGNALPROCESSING_API virtual void Shutdown();
55
56 // Kills the envelope (will cause discontinuity)
57 SIGNALPROCESSING_API virtual void Kill();
58
59 // Queries if the envelope has finished
60 SIGNALPROCESSING_API virtual bool IsDone() const;
61
62 // Resets the envelope
63 SIGNALPROCESSING_API virtual void Reset();
64
65 // Update the state of the envelope
66 SIGNALPROCESSING_API virtual void Update();
67
68 // Generate the next output value of the envelope.
69 // Optionally outputs the bias output (i.e. -1.0 to 1.0)
70 SIGNALPROCESSING_API virtual float Generate(float* BiasedOutput = nullptr);
71
73
74 // Sets the envelope attack time in msec
75 SIGNALPROCESSING_API virtual void SetAttackTime(const float InAttackTimeMsec);
76
77 // Sets the envelope decay time in msec
78 SIGNALPROCESSING_API virtual void SetDecayTime(const float InDecayTimeMsec);
79
80 // Sets the envelope sustain gain in linear gain values
81 SIGNALPROCESSING_API virtual void SetSustainGain(const float InSustainGain);
82
83 // Sets the envelope release time in msec
85
86 // Inverts the value of envelope output
87 SIGNALPROCESSING_API virtual void SetInvert(const bool bInInvert);
88
89 // Inverts the value of the biased envelope output
90 SIGNALPROCESSING_API virtual void SetBiasInvert(const bool bInBiasInvert);
91
92 // Sets the envelope depth.
93 SIGNALPROCESSING_API virtual void SetDepth(const float InDepth);
94
95 // Sets the depth of the bias output.
96 SIGNALPROCESSING_API virtual void SetBiasDepth(const float InDepth);
97
98 // Get the envelope's patch nodes
99 const FPatchSource GetModSourceEnv() const { return EnvSource; }
101
102 protected:
103 struct FEnvData
104 {
106 float Offset;
107 float TCO;
109
111 : Coefficient(0.0f)
112 , Offset(0.0f)
113 , TCO(0.0f)
114 , TimeSamples(0.0f)
115 {}
116 };
117
118 // The current envelope value, used to compute exponential envelope curves
129 float Depth;
132
136
138
139 // Mod matrix
141
144
145 // Whether or not we want to simulate analog behavior
147
148 // Whether or not the envelope is reset back to attack when started
150
151 // Whether or not the envelope value is set to zero when finished
153
154 // Whether or not this envelope has changed and needs to have values recomputed
156
157 // Inverts the output
159
160 // Bias output inversions
162
163 // tracks if the current envelope was started with sustain at 0.0
164 // (avoids bug where sustain being turned up during decay phase makes note hang)
166 };
167
168 // sample accurate attack-decay style envelope generator
170 {
171 public:
174
176
181
182 void SetLooping(bool bInIsLooping) { bIsLooping = bInIsLooping; }
183 bool IsLooping() const { return bIsLooping; }
184
185 // Call function to trigger a new attack-phase of the envelope generator
187
188 // Generates an output audio buffer (used for audio-rate envelopes)
190
191 // Generates a single float value (used for control-rate envelopes)
194
195 private:
196 int32 CurrentSampleIndex = INDEX_NONE;
197 float StartingEnvelopeValue = 0.0f;
198 float CurrentEnvelopeValue = 0.0f;
199 float AttackTimeSeconds = 0.0f;
200 float DecayTimeSeconds = 0.1f;
201 int32 AttackSampleCount = 0;
202 int32 DecaySampleCount = 0;
203 float AttackCurveFactor = 1.0f;
204 float DecayCurveFactor = 1.0f;
205 float SampleRate = 0.0f;
206 bool bIsLooping = false;
207
208 };
209}
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
void Init()
Definition LockFreeList.h:4
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition Envelope.h:170
void SetLooping(bool bInIsLooping)
Definition Envelope.h:182
~FADEnvelope()
Definition Envelope.h:173
SIGNALPROCESSING_API void SetDecayTimeSeconds(float InReleaseTimeSeconds)
Definition Envelope.cpp:443
bool IsLooping() const
Definition Envelope.h:183
SIGNALPROCESSING_API void SetDecayCurveFactor(float InDecayCurve)
Definition Envelope.cpp:454
SIGNALPROCESSING_API void SetAttackTimeSeconds(float InAttackTimeSeconds)
Definition Envelope.cpp:437
SIGNALPROCESSING_API void SetAttackCurveFactor(float InAttackCurve)
Definition Envelope.cpp:449
FADEnvelope()
Definition Envelope.h:172
SIGNALPROCESSING_API void Attack()
Definition Envelope.cpp:459
SIGNALPROCESSING_API void GetNextEnvelopeOut(int32 StartFrame, int32 EndFrame, TArray< int32 > &OutFinishedFrames, Audio::AlignedFloatBuffer &OutEnvelope)
Definition Envelope.cpp:466
Definition Envelope.h:14
float CurrentEnvelopeBiasValue
Definition Envelope.h:121
virtual SIGNALPROCESSING_API void Kill()
Definition Envelope.cpp:203
virtual SIGNALPROCESSING_API void Stop()
Definition Envelope.cpp:163
EEnvelopeState
Definition Envelope.h:18
uint8 bChanged
Definition Envelope.h:155
virtual void StartLegato()
Definition Envelope.h:48
uint8 bInvert
Definition Envelope.h:158
float SampleRate
Definition Envelope.h:122
virtual SIGNALPROCESSING_API bool IsDone() const
Definition Envelope.cpp:208
uint8 bIsRetriggerMode
Definition Envelope.h:152
virtual SIGNALPROCESSING_API void Reset()
Definition Envelope.cpp:213
virtual SIGNALPROCESSING_API EEnvelopeState GetState() const
Definition Envelope.cpp:56
uint8 bIsLegatoMode
Definition Envelope.h:149
virtual SIGNALPROCESSING_API void SetDepth(const float InDepth)
Definition Envelope.cpp:419
EEnvelopeState CurrentState
Definition Envelope.h:137
uint8 bCurrentCycleIsADOnly
Definition Envelope.h:165
float ReleaseTimeMsec
Definition Envelope.h:126
virtual SIGNALPROCESSING_API void SetAttackTime(const float InAttackTimeMsec)
Definition Envelope.cpp:385
const FPatchSource GetModSourceEnv() const
Definition Envelope.h:99
FEnvData AttackData
Definition Envelope.h:133
virtual SIGNALPROCESSING_API void Start()
Definition Envelope.cpp:67
FModulationMatrix * ModMatrix
Definition Envelope.h:140
float Depth
Definition Envelope.h:129
virtual SIGNALPROCESSING_API void Update()
Definition Envelope.cpp:231
float DecayTimeMsec
Definition Envelope.h:124
virtual SIGNALPROCESSING_API void Shutdown()
Definition Envelope.cpp:182
bool IsRetrigger() const
Definition Envelope.h:41
virtual SIGNALPROCESSING_API ~FEnvelope()
Definition Envelope.cpp:32
virtual SIGNALPROCESSING_API void SetInvert(const bool bInInvert)
Definition Envelope.cpp:409
FPatchSource EnvSource
Definition Envelope.h:142
int32 VoiceId
Definition Envelope.h:119
virtual SIGNALPROCESSING_API float Generate(float *BiasedOutput=nullptr)
Definition Envelope.cpp:268
virtual SIGNALPROCESSING_API void SetSustainGain(const float InSustainGain)
Definition Envelope.cpp:397
float BiasDepth
Definition Envelope.h:130
SIGNALPROCESSING_API void SetSimulateAnalog(const bool bInSimulatingAnalog)
Definition Envelope.cpp:61
FPatchSource BiasedEnvSource
Definition Envelope.h:143
FEnvData ReleaseData
Definition Envelope.h:135
virtual SIGNALPROCESSING_API void SetDecayTime(const float InDecayTimeMsec)
Definition Envelope.cpp:391
float CurrentEnvelopeValue
Definition Envelope.h:120
float AttackTimeMSec
Definition Envelope.h:123
uint8 bBiasInvert
Definition Envelope.h:161
uint8 bIsSimulatingAnalog
Definition Envelope.h:146
float OutputGain
Definition Envelope.h:131
float ShutdownTimeMsec
Definition Envelope.h:127
float SustainGain
Definition Envelope.h:125
virtual SIGNALPROCESSING_API void SetBiasInvert(const bool bInBiasInvert)
Definition Envelope.cpp:414
float ShutdownDelta
Definition Envelope.h:128
void SetLegato(const bool bInLegatoMode)
Definition Envelope.h:37
FEnvData DecayData
Definition Envelope.h:134
virtual SIGNALPROCESSING_API void SetBiasDepth(const float InDepth)
Definition Envelope.cpp:424
virtual SIGNALPROCESSING_API void SetReleaseTime(const float InReleaseTimeMsec)
Definition Envelope.cpp:403
void SetRetrigger(const bool bInRetrigger)
Definition Envelope.h:40
SIGNALPROCESSING_API FEnvelope()
Definition Envelope.cpp:8
const FPatchSource GetModSourceBiasEnv() const
Definition Envelope.h:100
Definition ModulationMatrix.h:102
Definition Array.h:670
NO_LOGGING.
Definition AudioMixerPlatformAndroid.cpp:53
Definition Envelope.h:104
float Offset
Definition Envelope.h:106
FEnvData()
Definition Envelope.h:110
float Coefficient
Definition Envelope.h:105
float TimeSamples
Definition Envelope.h:108
float TCO
Definition Envelope.h:107
Definition ModulationMatrix.h:12