UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SampleBufferIO.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 "SampleBuffer.h"
7#include "Misc/Paths.h"
9#include "Async/AsyncWork.h"
11#include "UObject/GCObject.h"
12#include "Tickable.h"
13
14class USoundWave;
15class FAudioDevice;
16
17namespace Audio
18{
19 /************************************************************************/
20 /* FSoundWavePCMLoader */
21 /* This class loads and decodes a USoundWave asset into a TSampleBuffer.*/
22 /* To use, call LoadSoundWave with the sound wave you'd like to load */
23 /* and call Update on every tick until it returns true, at which point */
24 /* you may call GetSampleBuffer to get the decoded audio. */
25 /************************************************************************/
27 {
28 public:
30
31 // Loads a USoundWave, call on game thread. Unless called with bSynchnous set to true, this class will require Update() to be called on the game thread.
33
34
35 // Update the loading state, should be called on the game thread.
36 ENGINE_API void Update();
37
38 //~ GCObject Interface
39 ENGINE_API virtual void AddReferencedObjects(FReferenceCollector& Collector) override;
40 ENGINE_API virtual FString GetReferencerName() const override;
41 //~ GCObject Interface
42
43 private:
44
45 struct FLoadingSoundWaveInfo
46 {
47 // The sound wave which is loading PCM data
49
50 // The lambda function to call when the sound wave finishes loading
52
53
54
55 enum class LoadStatus : uint8
56 {
57 // No request to load has been issued (default)
58 None = 0,
59
60 // The sound wave load/decode is in-flight
61 Loading,
62
63 // The sound wave has already been loaded
64 Loaded,
65 };
66
67 LoadStatus Status;
68
69 FLoadingSoundWaveInfo()
70 : SoundWave(nullptr)
71 , Status(LoadStatus::None)
72 {
73 }
74 };
75
76 // Reference to current loading sound wave
77 TArray<FLoadingSoundWaveInfo> LoadingSoundWaves;
78
79 // MERGE-REVIEW - should be in object or moved into loading info?
80 // Whether or not this object is tickable. I.e. a sound wave has been asked to load.
81 bool bCanBeTicked;
82 };
83
84 // Enum used to express the current state of a FSoundWavePCMWriter's current operation.
86 {
87 Idle,
91 Failed,
93 };
94
95 // Enum used internally by the FSoundWavePCMWriter.
103
104 /************************************************************************/
105 /* FAsyncSoundWavePCMWriteWorker */
106 /* This class is used by FSoundWavePCMWriter to handle async writing. */
107 /************************************************************************/
140
142
143 // This is the default chunk size, in bytes that FSoundWavePCMWriter writes to the disk at once.
144 static const int32 WriterDefaultChunkSize = 8192;
145
146
147 /************************************************************************/
148 /* FSoundWavePCMWriter */
149 /* This class can be used to save a TSampleBuffer to either a wav file */
150 /* or a USoundWave using BeginGeneratingSoundWaveFromBuffer, */
151 /* BeginWriteToSoundWave, or BeginWriteToWavFile on the game thread. */
152 /* This class uses an async task to generate and write the file to disk.*/
153 /************************************************************************/
155 {
156 public:
158
159 ENGINE_API FSoundWavePCMWriter(int32 InChunkSize = WriterDefaultChunkSize);
161
162 // This kicks off an operation to write InSampleBuffer to SoundWaveToSaveTo.
163 // If InSoundWave is not nullptr, the audio will be written directly into
164 // Returns true on a successful start, false otherwise.
166
167 // This kicks off an operation to write InSampleBuffer to a USoundWave asset
168 // at the specified file path relative to the project directory.
169 // This function should only be used in the editor.
170 // If a USoundWave asset already exists
171 // InSoundWaveTransformationTarget is used to overwrite SoundWave data with its own transformations. (ex. marker data)
172 ENGINE_API bool BeginWriteToSoundWave(const FString& FileName, const TSampleBuffer<>& InSampleBuffer, FString InPath, TFunction<void(const USoundWave*)> OnSuccess = [](const USoundWave* ResultingWave) {}, TObjectPtr<USoundWave> InSoundWaveTransformationTarget = nullptr);
173
174 // This writes out the InSampleBuffer as a wav file at the path specified by FilePath and FileName.
175 // If FilePath is a relative path, it will be relative to the /Saved/BouncedWavFiles folder, otherwise specified absolute path will be used.
176 // FileName should not contain the extension. This can be used in non-editor builds.
177 ENGINE_API bool BeginWriteToWavFile(const TSampleBuffer<>& InSampleBuffer, const FString& FileName, const FString& FilePath, TFunction<void()> OnSuccess = []() {});
178
179 // This is a blocking call that will return the SoundWave generated from InSampleBuffer.
180 // Optionally, if you're using the editor, you can also write the resulting soundwave out to the content browser using the FileName and FilePath parameters.
181 ENGINE_API USoundWave* SynchronouslyWriteSoundWave(const TSampleBuffer<>& InSampleBuffer, const FString* FileName = nullptr, const FString* FilePath = nullptr);
182
183 // This blocking call writes out the InSampleBuffer as a wav file at the path specified by FilePath and FileName.
184 // If FilePath is a relative path, it will be relative to the /Saved/BouncedWavFiles folder, otherwise specified absolute path will be used.
185 // FileName should not contain the extension. This can be used in non-editor builds.
186 ENGINE_API bool SynchronouslyWriteToWavFile(const TSampleBuffer<>& InSampleBuffer, const FString& FileName, const FString& FilePath, FString* OutFilePathName = nullptr);
187
188 // Call this on the game thread to continue the write operation. Optionally provide a pointer
189 // to an ESoundWavePCMWriterState which will be written to with the current state of the write operation.
190 // Returns a float value from 0 to 1 indicating how complete the write operation is.
192
193 // Aborts the current write operation.
194 ENGINE_API void CancelWrite();
195
196 // Whether we have finished the write operation, by either succeeding, failing, or being cancelled.
197 ENGINE_API bool IsDone();
198
199 // Clean up all resources used.
200 ENGINE_API void Reset();
201
202 // Used to grab the a handle to the soundwave.
204
205 // This function can be used after generating a USoundWave by calling BeginGeneratingSoundWaveFromBuffer
206 // to save the generated soundwave to an asset.
207 // This is handy if you'd like to preview or edit the USoundWave before saving it to disk.
208 ENGINE_API void SaveFinishedSoundWaveToPath(const FString& FileName, FString InPath = FPaths::EngineContentDir());
209
210 private:
211 // Current pending buffer.
212 TSampleBuffer<> CurrentBuffer;
213
214 // Sound wave currently being written to.
215 USoundWave* CurrentSoundWave;
216
217 // Current state of the buffer.
218 ESoundWavePCMWriterState CurrentState;
219
220 // Current Absolute File Path we are writing to.
221 FString AbsoluteFilePath;
222
223 bool bWasPreviouslyAddedToRoot;
224
226
227 // Internal buffer for holding the serialized wav file in memory.
228 TArray<uint8> SerializedWavData;
229
230 // Internal progress
231 FThreadSafeCounter Progress;
232
233 int32 ChunkSize;
234
235 UPackage* CurrentPackage;
236
237 private:
238
239 // This is used to emplace CurrentBuffer in CurrentSoundWave.
240 ENGINE_API void ApplyBufferToSoundWave();
241
242 // This is used to save CurrentSoundWave within CurrentPackage.
243 ENGINE_API void SerializeSoundWaveToAsset();
244
245 // This is used to write a WavFile in disk.
246 ENGINE_API void SerializeBufferToWavFile();
247
248 // This checks to see if a directory exists and, if it does not, recursively adds the directory.
249 ENGINE_API bool CreateDirectoryIfNeeded(FString& DirectoryPath);
250
251 // Do some shared prep steps for WAV file export
252 bool PrepWavFileOutput(const TSampleBuffer<>& InSampleBuffer, const FString& FileName, const FString& FilePath);
253 };
254
255 /************************************************************************/
256 /* FAudioRecordingData */
257 /* This is used by USoundSubmix and the AudioMixerBlueprintLibrary */
258 /* to contain FSoundWavePCMWriter operations. */
259 /************************************************************************/
267
268}
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define RETURN_QUICK_DECLARE_CYCLE_STAT(StatId, GroupId)
Definition Stats.h:655
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition SampleBufferIO.h:109
ENGINE_API ~FAsyncSoundWavePCMWriteWorker()
Definition SampleBufferIO.cpp:660
TFunction< void(const USoundWave *)> CallbackOnSuccess
Definition SampleBufferIO.h:116
ENGINE_API void DoWork()
Definition SampleBufferIO.cpp:666
class FSoundWavePCMWriter * Writer
Definition SampleBufferIO.h:111
TStatId GetStatId() const
Definition SampleBufferIO.h:135
FCriticalSection NonAbandonableSection
Definition SampleBufferIO.h:114
ESoundWavePCMWriteTaskType TaskType
Definition SampleBufferIO.h:112
ENGINE_API void Abandon()
Definition SampleBufferIO.cpp:761
bool CanAbandon()
Definition SampleBufferIO.h:128
Definition SampleBufferIO.h:27
virtual ENGINE_API void AddReferencedObjects(FReferenceCollector &Collector) override
Definition SampleBufferIO.cpp:91
ENGINE_API void Update()
Definition SampleBufferIO.cpp:66
ENGINE_API void LoadSoundWave(USoundWave *InSoundWave, TFunction< void(const USoundWave *SoundWave, const Audio::FSampleBuffer &OutSampleBuffer)> OnLoaded, bool bSynchrounous=false)
Definition SampleBufferIO.cpp:19
virtual ENGINE_API FString GetReferencerName() const override
Definition SampleBufferIO.cpp:100
ENGINE_API FSoundWavePCMLoader()
Definition SampleBufferIO.cpp:15
Definition SampleBufferIO.h:155
ENGINE_API bool BeginWriteToWavFile(const TSampleBuffer<> &InSampleBuffer, const FString &FileName, const FString &FilePath, TFunction< void()> OnSuccess=[]() {})
Definition SampleBufferIO.cpp:273
ENGINE_API bool BeginGeneratingSoundWaveFromBuffer(const TSampleBuffer<> &InSampleBuffer, USoundWave *InSoundWave=nullptr, TFunction< void(const USoundWave *)> OnSuccess=[](const USoundWave *ResultingWave){})
Definition SampleBufferIO.cpp:120
ENGINE_API bool SynchronouslyWriteToWavFile(const TSampleBuffer<> &InSampleBuffer, const FString &FileName, const FString &FilePath, FString *OutFilePathName=nullptr)
Definition SampleBufferIO.cpp:298
ENGINE_API bool IsDone()
Definition SampleBufferIO.cpp:419
ENGINE_API ~FSoundWavePCMWriter()
Definition SampleBufferIO.cpp:115
ENGINE_API void SaveFinishedSoundWaveToPath(const FString &FileName, FString InPath=FPaths::EngineContentDir())
Definition SampleBufferIO.cpp:480
ENGINE_API void Reset()
Definition SampleBufferIO.cpp:433
ENGINE_API bool BeginWriteToSoundWave(const FString &FileName, const TSampleBuffer<> &InSampleBuffer, FString InPath, TFunction< void(const USoundWave *)> OnSuccess=[](const USoundWave *ResultingWave) {}, TObjectPtr< USoundWave > InSoundWaveTransformationTarget=nullptr)
Definition SampleBufferIO.cpp:163
ENGINE_API USoundWave * SynchronouslyWriteSoundWave(const TSampleBuffer<> &InSampleBuffer, const FString *FileName=nullptr, const FString *FilePath=nullptr)
Definition SampleBufferIO.cpp:322
ENGINE_API void CancelWrite()
Definition SampleBufferIO.cpp:405
ENGINE_API USoundWave * GetFinishedSoundWave()
Definition SampleBufferIO.cpp:449
ENGINE_API float CheckStatus(ESoundWavePCMWriterState *OutCurrentState=nullptr)
Definition SampleBufferIO.cpp:395
Definition SampleBuffer.h:24
Definition AsyncWork.h:585
Definition AudioDevice.h:417
Definition GCObject.h:128
Definition AsyncWork.h:663
static CORE_API FString EngineContentDir()
Definition Paths.cpp:228
Definition UObjectGlobals.h:2492
Definition ThreadSafeCounter.h:14
Definition Array.h:670
Definition AndroidPlatformMisc.h:14
Definition UniquePtr.h:107
Definition Package.h:216
Definition SoundWave.h:417
NO_LOGGING.
Definition AudioMixerPlatformAndroid.cpp:53
ESoundWavePCMWriteTaskType
Definition SampleBufferIO.h:97
ESoundWavePCMWriterState
Definition SampleBufferIO.h:86
FAsyncTask< FAsyncSoundWavePCMWriteWorker > FAsyncSoundWavePCMWriterTask
Definition SampleBufferIO.h:141
Definition SampleBufferIO.h:261
~FAudioRecordingData()
Definition SampleBufferIO.h:265
FSoundWavePCMWriter Writer
Definition SampleBufferIO.h:263
TSampleBuffer< int16 > InputBuffer
Definition SampleBufferIO.h:262
Definition ObjectPtr.h:488
Definition LightweightStats.h:416