UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MediaSoundComponent.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6
7#include "Containers/Array.h"
8#include "MediaSampleQueue.h"
9#include "Misc/Timespan.h"
10#include "Templates/Atomic.h"
17#include "Sound/SoundClass.h"
19#include "MediaAudioResampler.h"
21
22#include "MediaSoundComponent.generated.h"
23
28class IMediaPlayer;
29class UMediaPlayer;
30
31
35UENUM()
37{
39 Mono,
40
42 Stereo,
43
46};
47
48UENUM(BlueprintType)
56
57USTRUCT(BlueprintType)
59{
61
62 // The frequency hz of the spectrum value
63 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SpectralData")
64 float FrequencyHz = 0.0f;
65
66 // The magnitude of the spectrum at this frequency
67 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SpectralData")
68 float Magnitude = 0.0f;
69};
70
71// Class implements an ISoundGenerator to feed decoded audio to audio renderering async tasks
73{
74public:
76 {
77 int32 SampleRate = 0;
78 int32 NumChannels = 0;
79
81 uint32 PreviousSampleQueueFlushCount = 0;
82
83 bool bSpectralAnalysisEnabled = false;
84 bool bEnvelopeFollowingEnabled = false;
85
86 int32 EnvelopeFollowerAttackTime = 0;
87 int32 EnvelopeFollowerReleaseTime = 0;
88
91
92 float CachedRate = 0.0f;
95 };
96
98
99 virtual ~FMediaSoundGenerator();
100
101 virtual void OnEndGenerate() override;
102
103 virtual int32 OnGenerateAudio(float* OutAudio, int32 NumSamples) override;
104
105 void SetCachedData(float InCachedRate, const FTimespan& InCachedTime);
106 void SetLastPlaySampleTime(const FTimespan& InLastPlaySampleTime);
107
108 void SetEnableSpectralAnalysis(bool bInSpectralAnlaysisEnabled);
109 void SetEnableEnvelopeFollowing(bool bInEnvelopeFollowingEnabled);
110
112 void SetEnvelopeFollowingSettings(int32 InAttackTimeMsec, int32 InReleaseTimeMsec);
113
115
116 TArray<FMediaSoundComponentSpectralData> GetSpectralData() const;
117 TArray<FMediaSoundComponentSpectralData> GetNormalizedSpectralData() const;
118 float GetCurrentEnvelopeValue() const { return CurrentEnvelopeValue; }
119
120 FTimespan GetLastPlayTime() const { return LastPlaySampleTime.Load(); }
121
122private:
123
124 FSoundGeneratorParams Params;
125
127 FMediaAudioResampler Resampler;
128
130 Audio::AlignedFloatBuffer AudioScratchBuffer;
131
133 mutable Audio::FAsyncSpectrumAnalyzer SpectrumAnalyzer;
134
135 Audio::FEnvelopeFollower EnvelopeFollower;
136
137 TAtomic<float> CachedRate;
138 TAtomic<FTimespan> CachedTime;
139 TAtomic<FTimespan> LastPlaySampleTime;
140
141 float CurrentEnvelopeValue = 0.0f;
142 bool bEnvelopeFollowerSettingsChanged = false;
143
144 mutable FCriticalSection AnalysisCritSect;
145 mutable FCriticalSection SampleQueueCritSect;
146};
147
151UCLASS(ClassGroup=Media, editinlinenew, meta=(BlueprintSpawnableComponent), MinimalAPI)
154{
156
157public:
158
160 UPROPERTY(EditAnywhere, Category="Media")
162
164 UPROPERTY(EditAnywhere, Category="Media", AdvancedDisplay)
165 bool DynamicRateAdjustment;
166
174 UPROPERTY(EditAnywhere, Category="Media", AdvancedDisplay)
175 float RateAdjustmentFactor;
176
183 UPROPERTY(EditAnywhere, Category="Media", AdvancedDisplay)
184 FFloatRange RateAdjustmentRange;
185
186public:
187
194
197
198public:
199
206 UFUNCTION(BlueprintCallable, Category="Media|MediaSoundComponent", meta=(DisplayName="Get Attenuation Settings To Apply", ScriptName="GetAttenuationSettingsToApply"))
207 MEDIAASSETS_API bool BP_GetAttenuationSettingsToApply(FSoundAttenuationSettings& OutAttenuationSettings);
208
215 UFUNCTION(BlueprintCallable, Category="Media|MediaSoundComponent")
216 MEDIAASSETS_API UMediaPlayer* GetMediaPlayer() const;
217
218 virtual USoundClass* GetSoundClass() override
219 {
220 if (SoundClass)
221 {
222 return SoundClass;
223 }
224
226 {
227 if (USoundClass* DefaultSoundClass = AudioSettings->GetDefaultMediaSoundClass())
228 {
229 return DefaultSoundClass;
230 }
231
232 if (USoundClass* DefaultSoundClass = AudioSettings->GetDefaultSoundClass())
233 {
234 return DefaultSoundClass;
235 }
236 }
237
238 return nullptr;
239 }
240
247 UFUNCTION(BlueprintCallable, Category="Media|MediaSoundComponent")
248 MEDIAASSETS_API void SetMediaPlayer(UMediaPlayer* NewMediaPlayer);
249
251 UFUNCTION(BlueprintCallable, Category = "Media|MediaSoundComponent")
252 MEDIAASSETS_API void SetEnableSpectralAnalysis(bool bInSpectralAnalysisEnabled);
253
255 UFUNCTION(BlueprintCallable, Category = "Media|MediaSoundComponent")
257
259 UFUNCTION(BlueprintCallable, Category = "TimeSynth")
261
263 UFUNCTION(BlueprintCallable, Category = "TimeSynth")
264 MEDIAASSETS_API TArray<FMediaSoundComponentSpectralData> GetNormalizedSpectralData();
265
267 UFUNCTION(BlueprintCallable, Category = "Media|MediaSoundComponent")
268 MEDIAASSETS_API void SetEnableEnvelopeFollowing(bool bInEnvelopeFollowing);
269
271 UFUNCTION(BlueprintCallable, Category = "Media|MediaSoundComponent")
272 MEDIAASSETS_API void SetEnvelopeFollowingsettings(int32 AttackTimeMsec, int32 ReleaseTimeMsec);
273
275 UFUNCTION(BlueprintCallable, Category = "TimeSynth")
276 MEDIAASSETS_API float GetEnvelopeValue() const;
277
278public:
279
281 MEDIAASSETS_API void AddClockSink();
282
284 MEDIAASSETS_API void RemoveClockSink();
285
286 MEDIAASSETS_API void UpdatePlayer();
287
288#if WITH_EDITOR
296#endif
297
298public:
299
300 //~ TAttenuatedComponentVisualizer interface
301
303
304public:
305
306 //~ UActorComponent interface
307
308 MEDIAASSETS_API virtual void OnRegister() override;
309 MEDIAASSETS_API virtual void OnUnregister() override;
310 MEDIAASSETS_API virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;
311
312public:
313
314 //~ USceneComponent interface
315
316 MEDIAASSETS_API virtual void Activate(bool bReset = false) override;
317 MEDIAASSETS_API virtual void Deactivate() override;
318
319public:
320
321 //~ UObject interface
322 MEDIAASSETS_API virtual void PostLoad() override;
323
324#if WITH_EDITOR
325 MEDIAASSETS_API virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
326#endif
327
328protected:
329
335 MEDIAASSETS_API const FSoundAttenuationSettings* GetSelectedAttenuationSettings() const;
336
337protected:
338
339 //~ USynthComponent interface
340
341 MEDIAASSETS_API virtual bool Init(int32& SampleRate) override;
342
343 MEDIAASSETS_API virtual ISoundGeneratorPtr CreateSoundGenerator(const FSoundGeneratorInitParams& InParams) override;
344
345protected:
346
355 UPROPERTY(EditAnywhere, Category="Media")
357
358private:
359
361 float CachedRate;
362
364 FTimespan CachedTime;
365
368
370 TWeakObjectPtr<UMediaPlayer> CurrentPlayer;
371
373 TWeakPtr<FMediaPlayerFacade, ESPMode::ThreadSafe> CurrentPlayerFacade;
374
376 float RateAdjustment;
377
380
381 /* Time of last sample played. */
382 FTimespan LastPlaySampleTime;
383
385 TArray<float> FrequenciesToAnalyze;
387 Audio::FSpectrumAnalyzerSettings SpectrumAnalyzerSettings;
388 int32 EnvelopeFollowerAttackTime;
389 int32 EnvelopeFollowerReleaseTime;
390
392 bool bSpectralAnalysisEnabled;
393
395 bool bEnvelopeFollowingEnabled;
396
399
401 ISoundGeneratorPtr MediaSoundGenerator;
402};
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
ELevelTick
Definition EngineBaseTypes.h:70
void Init()
Definition LockFreeList.h:4
EMediaSoundChannels
Definition MediaSoundComponent.h:37
EMediaSoundComponentFFTSize
Definition MediaSoundComponent.h:50
#define UPROPERTY(...)
UObject definition macros.
Definition ObjectMacros.h:744
#define GENERATED_BODY(...)
Definition ObjectMacros.h:765
#define UFUNCTION(...)
Definition ObjectMacros.h:745
#define UCLASS(...)
Definition ObjectMacros.h:776
#define UENUM(...)
Definition ObjectMacros.h:749
#define USTRUCT(...)
Definition ObjectMacros.h:746
#define GENERATED_USTRUCT_BODY(...)
Definition ObjectMacros.h:767
ESPMode
Definition SharedPointerFwd.h:12
USkinnedMeshComponent float
Definition SkinnedMeshComponent.h:60
::FCriticalSection FTransactionallySafeCriticalSection
Definition TransactionallySafeCriticalSection.h:16
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition SpectrumAnalyzer.h:408
Definition EnvelopeFollower.h:226
Definition MediaAudioResampler.h:20
Definition MediaSampleQueue.h:455
Definition MediaPlayerFacade.h:70
Definition MediaSoundComponent.cpp:36
Definition MediaSoundComponent.h:73
FTimespan GetLastPlayTime() const
Definition MediaSoundComponent.h:120
float GetCurrentEnvelopeValue() const
Definition MediaSoundComponent.h:118
Definition UObjectGlobals.h:1292
Definition IMediaAudioSample.h:41
Definition IMediaPlayer.h:35
Definition SoundGenerator.h:31
Definition Array.h:670
Definition Atomic.h:538
Definition SharedPointer.h:692
Definition SharedPointer.h:1295
Definition AudioSettings.h:125
Definition MediaPlayer.h:176
Definition MediaSoundComponent.h:154
Definition SoundClass.h:209
Definition SynthComponent.h:80
NO_LOGGING.
Definition AudioMixerPlatformAndroid.cpp:53
Definition SpectrumAnalyzer.h:18
EFFTSize
Definition SpectrumAnalyzer.h:21
Definition EngineBaseTypes.h:571
Definition MediaSoundComponent.h:59
Definition MediaSoundComponent.h:76
TSharedPtr< FMediaAudioSampleQueue, ESPMode::ThreadSafe > SampleQueue
Definition MediaSoundComponent.h:80
Audio::FSpectrumAnalyzerSettings SpectrumAnalyzerSettings
Definition MediaSoundComponent.h:89
FTimespan CachedTime
Definition MediaSoundComponent.h:93
TArray< float > FrequenciesToAnalyze
Definition MediaSoundComponent.h:90
FTimespan LastPlaySampleTime
Definition MediaSoundComponent.h:94
Definition UnrealType.h:6865
Definition SoundAttenuation.h:139
Definition SoundGenerator.h:17
Definition Timespan.h:76
Definition ObjectPtr.h:488
Definition WeakObjectPtrTemplates.h:25