UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Osc.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/Dsp.h"
8#include "DSP/Noise.h"
9
10namespace Audio
11{
12 // Struct which wraps all factors which contribute to pitch of the oscillator
14 {
15 // A factor which directly scales the frequency output (used for FM modulation)
16 float Scale;
17
18 // External modulation source
20
21 // The modulated frequency value
22 float Mod;
23
24 // Exponential frequency modulation
25 float Detune;
26
27 // Pitch bend modulation (i.e. from mod wheel)
28 float PitchBend;
29
30 // Octave frequency modulation
31 float Octave;
32
33 // Semitones frequency modulation
34 float Semitones;
35
36 // cents frequency modulation
37 float Cents;
38
40 : Scale(1.0f)
41 , ExternalMod(0.0f)
42 , Mod(0.0f)
43 , Detune(0.0f)
44 , PitchBend(0.0f)
45 , Octave(0.0f)
46 , Semitones(0.0f)
47 , Cents(0.0f)
48 {}
49 };
50
51 // Oscillator base class
53 {
54 public:
58
59 // Initializes the oscillator
60 SIGNALPROCESSING_API virtual void Init(const float InSampleRate, const int32 InVoiceId = 0, FModulationMatrix* InMatrix = nullptr, const int32 ModMatrixStage = 1);
61
62 // Starts the oscillator
63 virtual void Start() = 0;
64
65 // Stops the oscillator
66 virtual void Stop() = 0;
67
68 // Generates the next sample of audio, optionally passes out the auxillary output (supported in some oscillators)
69 virtual float Generate(float* AuxOutput = nullptr) = 0;
70
71 // Updates oscillator state
72 SIGNALPROCESSING_API virtual void Update();
73
74 // Resets the oscillator
75 SIGNALPROCESSING_API virtual void Reset();
76
77 // Resets the phase of this oscillator to 0.0f
78 SIGNALPROCESSING_API virtual void ResetPhase();
79
80 // Sets the gain of the oscillator
81 void SetGain(const float InGain) { Gain = InGain; }
82
83 // Sets the gain modulator of the oscillator
85
86 // Sets the base frequency of the oscillator
88
89 // Sets a frequency modulation value
91
92 // Sets the base frequency of the oscillator from the midi note number
93 SIGNALPROCESSING_API void SetNote(const float InNote);
94
95 // Returns the frequency of the oscillator
96 float GetFrequency() const { return BaseFreq; }
97
98 // Returns the frequency of the oscillator
99 float GetGain() const { return Gain; }
100
101 SIGNALPROCESSING_API void SetCents(const float InCents);
102 SIGNALPROCESSING_API void SetOctave(const float InOctave);
105 SIGNALPROCESSING_API void SetDetune(const float InDetune);
108
109 // Sets the LFO pulse width for square-wave type oscillators
111
112 // Returns whether or not this oscillator is playing
113 bool IsPlaying() const { return bIsPlaying; }
114
115 // Returns if this oscillator should be synced to a leader oscillator
116 bool IsSync() const { return bIsSync; }
117
118 // Sets whether or not this oscillator should be synced to a leader oscillator. leader oscillator needs to have set this oscillator as its follower.
119 void SetSync(const bool bInSync) { bIsSync = bInSync; }
120
121 // Deprecated: use SetFollowerOsc
122 UE_DEPRECATED(5.1, "SetSlaveOsc is deprecated, please use SetFollowerOsc instead.")
124
125 // Sets the input oscillator as the follower of this oscillator
127
128 // Return patch destinations for various modulatable parameters
134
135 protected:
136
137 // Updates the phase based on the phase increment
139 {
140 Phase += PhaseInc;
141 }
142
143 // Returns true if there was a phase wrap this update
145 {
146 bool Result = false;
147 if (PhaseInc > 0.0f && Phase >= 1.0f)
148 {
149 Phase = FMath::Fmod(Phase, 1.0f);
150 Result = true;
151 }
152 else if (PhaseInc < 0.0f && Phase <= 0.0f)
153 {
154 Phase = FMath::Fmod(Phase, 1.0f) + 1.0f;
155 Result = true;
156 }
157
158 if (Result && FollowerOsc && FollowerOsc->IsSync())
159 {
161 }
162
163 return Result;
164 }
165
166 // Returns the current phase of the oscillator
167 inline float GetPhase() const
168 {
169 return Phase;
170 }
171
172 // Returns the quadrature phase, wrapped
173 inline float GetQuadPhase() const
174 {
175 return FMath::Fmod(Phase + 0.25f, 1.0f);
176 }
177
178 protected:
179
180 // The voice ID that this oscillator belongs to
182
183 // Sample rate of the oscillator
185
186 // Maximum frequency allowed
187 float Nyquist;
188
189 // The final frequency of the oscillator after computing all factors contributing to frequency
190 float Freq;
191
192 // The base frequency of the oscillator
193 float BaseFreq;
194
195 // Holds all frequency data
197
198 // Linear gain of the oscillator
199 float Gain;
200
201 // Linear gain modulation of the oscillator (used in amplitude modulation)
203
204 // The current phase of oscillator (between 0.0 and 1.0)
205 float Phase;
206
207 // How much to increment the phase each update
208 float PhaseInc;
209
210 // Pulse width used in square LFOs
212
213 // Pulse width modulator factor
215
216 // The final pulse width
218
219 // Modulation matrix to use for this oscillator
221
227
228 // Ptr to a follower oscillator that can be triggered to 0 phase if it is synced.
230
231 // Whether or not the oscillator is on or off
235 };
236
237 namespace EOsc
238 {
248 }
249
250 // Pitched oscillator
251 class FOsc : public IOscBase
252 {
253 public:
255 SIGNALPROCESSING_API virtual ~FOsc();
256
257 //~ Begin FOscBase
258 SIGNALPROCESSING_API virtual void Start() override;
259 SIGNALPROCESSING_API virtual void Stop() override;
260 SIGNALPROCESSING_API virtual void Reset() override;
261 SIGNALPROCESSING_API virtual void Update() override;
262 SIGNALPROCESSING_API virtual float Generate(float* AuxOutput = nullptr) override;
263 //~ End FOscBase
264
265 // Sets the oscillator type
267
268 // Gets the oscillator type
269 EOsc::Type GetType() const { return OscType; }
270
271 protected:
272
273 // Smooth out the saw-tooth discontinuities to improve aliasing
274 SIGNALPROCESSING_API float PolySmooth(const float InPhase, const float InPhaseInc);
275
276 // Current sign of the square mod, used for triangle wave generation
278
279 // Used to store state for triangular differentiator
280 float DPW_z1;
281
282 // The pulse width base lerped
284
285 // The type of the oscillator
287
288 // a noise generator
290 };
291
292}
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
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
Definition Dsp.h:505
Definition ModulationMatrix.h:102
Definition Osc.h:252
SIGNALPROCESSING_API FOsc()
Definition Osc.cpp:210
EOsc::Type OscType
Definition Osc.h:286
virtual SIGNALPROCESSING_API void Stop() override
Definition Osc.cpp:229
EOsc::Type GetType() const
Definition Osc.h:269
FExponentialEase PulseWidthLerped
Definition Osc.h:283
float TriangleSign
Definition Osc.h:277
SIGNALPROCESSING_API float PolySmooth(const float InPhase, const float InPhaseInc)
Definition Osc.cpp:356
FWhiteNoise Noise
Definition Osc.h:289
float DPW_z1
Definition Osc.h:280
virtual SIGNALPROCESSING_API ~FOsc()
Definition Osc.cpp:218
virtual SIGNALPROCESSING_API void Update() override
Definition Osc.cpp:248
virtual SIGNALPROCESSING_API void Start() override
Definition Osc.cpp:222
void SetType(const EOsc::Type InType)
Definition Osc.h:266
virtual SIGNALPROCESSING_API void Reset() override
Definition Osc.cpp:234
virtual SIGNALPROCESSING_API float Generate(float *AuxOutput=nullptr) override
Definition Osc.cpp:255
Definition Noise.h:15
Definition Osc.h:53
virtual SIGNALPROCESSING_API void Update()
Definition Osc.cpp:147
bool WrapPhase()
Definition Osc.h:144
bool bIsSync
Definition Osc.h:234
float Freq
Definition Osc.h:190
IOscBase * FollowerOsc
Definition Osc.h:229
FPatchDestination GetModDestScale() const
Definition Osc.h:133
float Phase
Definition Osc.h:205
SIGNALPROCESSING_API void SetFreqScale(const float InFreqScale)
Definition Osc.cpp:138
SIGNALPROCESSING_API void SetFrequencyMod(const float InFreqMod)
Definition Osc.cpp:67
SIGNALPROCESSING_API void SetPulseWidth(const float InPulseWidth)
Definition Osc.cpp:174
SIGNALPROCESSING_API void SetSampleRate(const float InSampleRate)
Definition Osc.cpp:100
float PulseWidthMod
Definition Osc.h:214
float SampleRate
Definition Osc.h:184
FPatchDestination ModScaleDest
Definition Osc.h:225
SIGNALPROCESSING_API IOscBase(const IOscBase &)
void SetSync(const bool bInSync)
Definition Osc.h:119
float PhaseInc
Definition Osc.h:208
SIGNALPROCESSING_API void SetSemitones(const float InSemiTone)
Definition Osc.cpp:111
float PulseWidthBase
Definition Osc.h:211
float Gain
Definition Osc.h:199
SIGNALPROCESSING_API void SetCents(const float InCents)
Definition Osc.cpp:82
bool bIsPlaying
Definition Osc.h:232
SIGNALPROCESSING_API void SetDetune(const float InDetune)
Definition Osc.cpp:120
float GetFrequency() const
Definition Osc.h:96
float GetPhase() const
Definition Osc.h:167
SIGNALPROCESSING_API IOscBase()
Definition Osc.cpp:8
virtual SIGNALPROCESSING_API ~IOscBase()
FOscFrequencyMod FreqData
Definition Osc.h:196
virtual SIGNALPROCESSING_API void Reset()
Definition Osc.cpp:198
FPatchDestination GetModDestFrequency() const
Definition Osc.h:129
FModulationMatrix * ModMatrix
Definition Osc.h:220
void SetGainMod(const float InGainMod)
Definition Osc.h:84
float PulseWidth
Definition Osc.h:217
FPatchDestination GetModDestPulseWidth() const
Definition Osc.h:130
FPatchDestination ModPulseWidthDest
Definition Osc.h:223
SIGNALPROCESSING_API void SetFollowerOsc(IOscBase *InFollowerOsc)
Definition Osc.cpp:193
float Nyquist
Definition Osc.h:187
float GetQuadPhase() const
Definition Osc.h:173
bool bChanged
Definition Osc.h:233
virtual float Generate(float *AuxOutput=nullptr)=0
float ExternalGainMod
Definition Osc.h:202
SIGNALPROCESSING_API void SetFrequency(const float InFreqBase)
Definition Osc.cpp:58
SIGNALPROCESSING_API void SetPitchBend(const float InPitchBend)
Definition Osc.cpp:129
float GetGain() const
Definition Osc.h:99
FPatchDestination GetModDestGain() const
Definition Osc.h:131
SIGNALPROCESSING_API void SetSlaveOsc(IOscBase *InSlaveOsc)
Definition Osc.cpp:188
void UpdatePhase()
Definition Osc.h:138
virtual void Start()=0
FPatchDestination ModFrequencyDest
Definition Osc.h:222
SIGNALPROCESSING_API void SetOctave(const float InOctave)
Definition Osc.cpp:91
virtual SIGNALPROCESSING_API void ResetPhase()
Definition Osc.cpp:183
FPatchDestination ModAddDest
Definition Osc.h:226
void SetGain(const float InGain)
Definition Osc.h:81
FPatchDestination ModGainDest
Definition Osc.h:224
float BaseFreq
Definition Osc.h:193
bool IsSync() const
Definition Osc.h:116
FPatchDestination GetModDestAdd() const
Definition Osc.h:132
bool IsPlaying() const
Definition Osc.h:113
SIGNALPROCESSING_API void SetNote(const float InNote)
Definition Osc.cpp:76
int32 VoiceId
Definition Osc.h:181
virtual void Stop()=0
Type
Definition Osc.h:240
@ Sine
Definition Osc.h:241
@ Noise
Definition Osc.h:245
@ NumOscTypes
Definition Osc.h:246
@ Saw
Definition Osc.h:242
@ Triangle
Definition Osc.h:243
@ Square
Definition Osc.h:244
NO_LOGGING.
Definition AudioMixerPlatformAndroid.cpp:53
Definition Osc.h:14
float Mod
Definition Osc.h:22
float Detune
Definition Osc.h:25
float ExternalMod
Definition Osc.h:19
float Cents
Definition Osc.h:37
float Octave
Definition Osc.h:31
float Semitones
Definition Osc.h:34
FOscFrequencyMod()
Definition Osc.h:39
float Scale
Definition Osc.h:16
float PitchBend
Definition Osc.h:28
Definition ModulationMatrix.h:37