UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SoundWaveDecoder.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 "AudioDevice.h"
7#include "Sound/SoundWave.h"
8#include "DSP/SinOsc.h"
10#include "Misc/ScopeLock.h"
11
12
13
14class FAudioDevice;
15
16#define AUDIO_SOURCE_DECODER_DEBUG 0
17
18namespace Audio
19{
20 class FMixerBuffer;
21 class FMixerSourceBuffer;
22
32
50
52 {
53 public:
54 FDecodingSoundSource(FAudioDevice* AudioDevice, const FSourceDecodeInit& InitData);
56
57 // Called before we initialize
58 bool PreInit(int32 OutputSampleRate);
59
60 // Queries if we're ready to initialize
61 bool IsReadyToInit();
62
63 // Initializes the decoding source
64 void Init();
65
66 // Returns if we've been initialized
67 bool IsInitialized() const { return bInitialized; }
68
69 // If the sound source finished playing all its source. Will only return true for non-looping sources.
70 bool IsFinished() const { return !bInitialized || SourceInfo.bIsLastBuffer; }
71
72 // Sets the pitch scale
73 void SetPitchScale(float InPitchScale, uint32 NumFrames = 512);
74
75 // Sets the volume scale
76 void SetVolumeScale(float InVolumeScale, uint32 NumFrames = 512);
77
78 // Sets the ForceSyncDecode flag. (Decodes for this soundwave will not happen in an async task if true)
80
81 // Get audio buffer
83
84 // Return the underlying sound wave
85 USoundWave* GetSoundWave() { return SoundWave; }
86
87 TObjectPtr<USoundWave>& GetSoundWavePtr() { return SoundWave; }
88
89 private:
90
91 void ReadFrame();
92 void GetAudioBufferInternal(const int32 InNumFrames, const int32 InNumChannels, FAlignedFloatBuffer& OutAudioBuffer);
93
94 // Handle to the decoding source
96
97 FDeviceId AudioDeviceID;
98
99 // The sound wave object with which this sound is generating
100 TObjectPtr<USoundWave> SoundWave;
101
102 // Mixer buffer object which is a convenience wrapper around some buffer initialization and management
103 FMixerBuffer* MixerBuffer;
104
105 // Critical Section around mixer source buffer access
106 FCriticalSection MixerSourceBufferCritSec;
107
108 // Object which handles bulk of decoding operations
110
111 // Scratch buffer used for upmixing and downmixing the audio
112 FAlignedFloatBuffer ScratchBuffer;
113
114 // Sample rate of the source
115 int32 SampleRate;
116
117 // Current seek time
118 float SeekTime;
119
120 // If we've initialized
121 FThreadSafeBool bInitialized;
122
123 // force the decoding of this sound to be synchronous
124 bool bForceSyncDecode{false};
125
126 // Object used for source generation from decoded buffers
127 struct FSourceInfo
128 {
129 // Number of channels of source file
130 int32 NumSourceChannels;
131
132 // Total number of frames of source file
133 uint32 TotalNumFrames;
134
135 // Total number of frames played (or read from decoded buffers) so far. Will always be less than TotalNumFrames
136 uint32 NumFramesRead;
137
138 // Total number of frames generated (could be larger or smaller than number of frames read)
139 uint32 NumFramesGenerated;
140
141 // The current frame alpha (how far we are between current and next frame)
142 float CurrentFrameAlpha;
143
144 // The current frame index
145 int32 CurrentFrameIndex;
146
147 // Number of frames of current decoded chunk
148 int32 CurrentAudioChunkNumFrames;
149
150 // The pitch scale to use to account for sample rate differences of source to output sample rate
151 float BasePitchScale;
152 float PitchScale;
153
154 // The pitch param object, allows easy pitch interpolation
155 FParam PitchParam;
156
157 // The frame count (from frames generated) to reset the pitch param
158 uint32 PitchResetFrame;
159
160 // The volume param object, allows easy volume interpolation
161 FParam VolumeParam;
162
163 // The frame count (from frames generated) to reset the volume param
164 uint32 VolumeResetFrame;
165
166 // Buffer to store current decoded audio frame
167 TArray<float> CurrentFrameValues;
168
169 // Buffer to store next decoded audio frame
170 TArray<float> NextFrameValues;
171
172 // The current decoded PCM buffer we are reading from
174
175 // If this sound is done (has decoded all data)
176 bool bIsDone;
177
178 // If this sound hasn't yet started rendering audio
179 bool bHasStarted;
180
181 // If this is the last decoded buffer
182 bool bIsLastBuffer;
183
184 FSourceInfo()
185 : NumSourceChannels(0)
186 , TotalNumFrames(0)
187 , NumFramesRead(0)
188 , NumFramesGenerated(0)
189 , CurrentFrameAlpha(0.0f)
190 , CurrentFrameIndex(0)
191 , CurrentAudioChunkNumFrames(0)
192 , BasePitchScale(1.0f)
193 , PitchScale(1.0f)
194 , PitchResetFrame(0)
195 , VolumeResetFrame(0)
196 , bIsDone(false)
197 , bHasStarted(false)
198 , bIsLastBuffer(false)
199 {}
200
201 };
202
203 FSourceInfo SourceInfo;
204
205#if AUDIO_SOURCE_DECODER_DEBUG
206 FSineOsc SineTone[2];
207#endif
208
209 };
210
212
214 {
215 public:
218
219 //~ Begin FGCObject
220 AUDIOMIXER_API virtual void AddReferencedObjects(FReferenceCollector & Collector) override;
221 virtual FString GetReferencerName() const override
222 {
223 return TEXT("Audio::FSoundSourceDecoder");
224 }
225 //~ End FGCObject
226
227 // Initialize the source decoder at the given output sample rate
228 // Sources will automatically sample rate convert to match this output
230
231 // Creates a new decoding sound source handle
233
234 // Called from the audio thread
235 AUDIOMIXER_API void Update();
236
237 // Called from the audio render thread
239
240 // Initialize a decoding instance of this sound wave object. Call only from game thread.
242
243 // Removes the decoding source from the decoder
245
246 // Resets internal state of decoder
247 AUDIOMIXER_API void Reset();
248
249 // Sets the source pitch scale
251
252 // Sets the source volume scale
254
255 // Get a decoded buffer for the given decoding sound wave handle. Call only from audio render thread or audio render thread task.
257
258 // Queries if the decoding source is finished
260
262
263 private:
264 // Sends a command to the audio render thread from audio thread
265 AUDIOMIXER_API void EnqueueDecoderCommand(TFunction<void()> Command);
266 AUDIOMIXER_API void PumpDecoderCommandQueue();
267 AUDIOMIXER_API bool InitDecodingSourceInternal(const FSourceDecodeInit& InitData);
268
269 int32 AudioThreadId;
270 FAudioDevice* AudioDevice;
271 int32 SampleRate;
272 mutable FCriticalSection DecodingSourcesCritSec;
273 TMap<int32, FDecodingSoundSourcePtr> InitializingDecodingSources;
275 TMap<int32, FSourceDecodeInit> PrecachingSources;
276
277 TQueue<TFunction<void()>> CommandQueue;
278 };
279
280}
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
@ INDEX_NONE
Definition CoreMiscDefines.h:150
#define TEXT(x)
Definition Platform.h:1272
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
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
void Init()
Definition LockFreeList.h:4
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition SoundWaveDecoder.h:52
TObjectPtr< USoundWave > & GetSoundWavePtr()
Definition SoundWaveDecoder.h:87
bool IsReadyToInit()
Definition SoundWaveDecoder.cpp:84
bool GetAudioBuffer(const int32 InNumFrames, const int32 InNumChannels, FAlignedFloatBuffer &OutAudioBuffer)
Definition SoundWaveDecoder.cpp:368
void SetVolumeScale(float InVolumeScale, uint32 NumFrames=512)
Definition SoundWaveDecoder.cpp:172
USoundWave * GetSoundWave()
Definition SoundWaveDecoder.h:85
bool PreInit(int32 OutputSampleRate)
Definition SoundWaveDecoder.cpp:46
void SetForceSyncDecode(bool bShouldForceSyncDecode)
Definition SoundWaveDecoder.cpp:178
~FDecodingSoundSource()
Definition SoundWaveDecoder.cpp:35
void SetPitchScale(float InPitchScale, uint32 NumFrames=512)
Definition SoundWaveDecoder.cpp:166
bool IsInitialized() const
Definition SoundWaveDecoder.h:67
void Init()
Definition SoundWaveDecoder.cpp:130
bool IsFinished() const
Definition SoundWaveDecoder.h:70
Definition AudioMixerBuffer.h:28
Definition SoundWaveDecoder.h:214
AUDIOMIXER_API FSoundSourceDecoder()
Definition SoundWaveDecoder.cpp:432
AUDIOMIXER_API void Update()
Definition SoundWaveDecoder.cpp:607
AUDIOMIXER_API void UpdateRenderThread()
Definition SoundWaveDecoder.cpp:653
virtual AUDIOMIXER_API void AddReferencedObjects(FReferenceCollector &Collector) override
Definition SoundWaveDecoder.cpp:444
AUDIOMIXER_API bool GetSourceBuffer(const FDecodingSoundSourceHandle &InHandle, const int32 NumOutFrames, const int32 NumOutChannels, FAlignedFloatBuffer &OutAudioBuffer)
Definition SoundWaveDecoder.cpp:685
AUDIOMIXER_API FDecodingSoundSourceHandle CreateSourceHandle(USoundWave *InSoundWave)
Definition SoundWaveDecoder.cpp:472
AUDIOMIXER_API bool InitDecodingSource(const FSourceDecodeInit &InitData)
Definition SoundWaveDecoder.cpp:524
AUDIOMIXER_API void Reset()
Definition SoundWaveDecoder.cpp:580
virtual AUDIOMIXER_API ~FSoundSourceDecoder()
Definition SoundWaveDecoder.cpp:439
virtual FString GetReferencerName() const override
Definition SoundWaveDecoder.h:221
AUDIOMIXER_API void RemoveDecodingSource(const FDecodingSoundSourceHandle &Handle)
Definition SoundWaveDecoder.cpp:574
AUDIOMIXER_API void SetSourcePitchScale(const FDecodingSoundSourceHandle &Handle, float InPitchScale)
Definition SoundWaveDecoder.cpp:591
AUDIOMIXER_API bool IsFinished(const FDecodingSoundSourceHandle &InHandle) const
Definition SoundWaveDecoder.cpp:658
AUDIOMIXER_API void SetSourceVolumeScale(const FDecodingSoundSourceHandle &Handle, float InVolumeScale)
Definition SoundWaveDecoder.cpp:596
Definition AudioDevice.h:417
Definition GCObject.h:128
Definition NameTypes.h:617
Definition UObjectGlobals.h:2492
Definition ThreadSafeBool.h:17
Definition AndroidPlatformMisc.h:14
Definition UnrealString.h.inl:34
Definition Queue.h:48
Definition SharedPointer.h:692
Definition SoundWave.h:417
NO_LOGGING.
Definition AudioMixerPlatformAndroid.cpp:53
TSharedPtr< FDecodingSoundSource > FDecodingSoundSourcePtr
Definition SoundWaveDecoder.h:211
uint32 FDeviceId
Definition AudioDefines.h:66
@ false
Definition radaudio_common.h:23
Definition SoundWaveDecoder.h:24
FDecodingSoundSourceHandle()
Definition SoundWaveDecoder.h:25
int32 Id
Definition SoundWaveDecoder.h:29
FName SoundWaveName
Definition SoundWaveDecoder.h:30
Definition SoundWaveDecoder.h:34
float VolumeScale
Definition SoundWaveDecoder.h:46
float SeekTime
Definition SoundWaveDecoder.h:44
TObjectPtr< USoundWave > SoundWave
Definition SoundWaveDecoder.h:43
FSourceDecodeInit()
Definition SoundWaveDecoder.h:35
FDecodingSoundSourceHandle Handle
Definition SoundWaveDecoder.h:48
bool bForceSyncDecode
Definition SoundWaveDecoder.h:47
float PitchScale
Definition SoundWaveDecoder.h:45
Definition ObjectPtr.h:488