UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
AudioCaptureProtocol.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4#include "CoreMinimal.h"
5#include "Engine/Engine.h"
8#include "AudioDevice.h"
12#include "AudioCaptureProtocol.generated.h"
13
18UCLASS(meta = (DisplayName = "No Audio", CommandLineID = "NullAudio"), MinimalAPI)
23
30UCLASS(MinimalApi, meta = (DisplayName = "Master Audio Submix", CommandLineID = "MasterAudioSubmix"))
32{
33public:
35
38 , TotalGameRecordingTime(0.0)
39 , TotalPlatformRecordingTime(0.0)
40 , GameRecordingStartTime(0.0)
41 , PlatformRecordingStartTime(0.0)
42 , bHasSetup(false)
43 {
44 // Match default format for video captures.
45 FileName = TEXT("{world}");
46 }
47
49 virtual bool SetupImpl() override
50 {
51
52 return true;
53 }
54
55 virtual bool StartCaptureImpl() override
56 {
57 if (!bHasSetup)
58 {
59 // This is called every time we want to resume capturing audio.
60 if (GetWorld() && GetWorld()->GetGameViewport())
61 {
62 // Disable rendering so we save all the render thread/GPU overhead
63 GetWorld()->GetGameViewport()->bDisableWorldRendering = true;
64 }
65
66 UAudioMixerBlueprintLibrary::StartRecordingOutput(GetWorld(), CaptureHost->GetEstimatedCaptureDurationSeconds());
67 bHasSetup = true;
68 }
69 else
70 {
71 UE_LOG(LogTemp, Warning, TEXT("Audio Recording Resumed"));
73 }
74
75
76 GameRecordingStartTime = GetWorld()->TimeSeconds;
77 PlatformRecordingStartTime = FPlatformTime::Seconds();
78 return true;
79 }
80
81 virtual void PauseCaptureImpl() override
82 {
83 // Pause the audio capture so we don't incorrectly capture audio for durations that we're not capturing frames for.
85
86 // Stop all sounds currently playing on the Audio Device. This helps kill looping or long audio clips. When the sequence evaluates again,
87 // these clips will resume play at the correct location.
89 {
90 AudioDevice->StopAllSounds(true);
91 }
92
93 // Subtract the current time from our start time and add it to our running total.
94 // This allows us to keep track of how much recording has actually been done, not counting paused time.
95 TotalGameRecordingTime += GetWorld()->TimeSeconds - GameRecordingStartTime;
96 TotalPlatformRecordingTime += FPlatformTime::Seconds() - PlatformRecordingStartTime;
97
98 UE_LOG(LogTemp, Warning, TEXT("Audio Recording Paused. Adding: %f seconds to GameRecording. Adding: %f seconds to Platform Recording."),
99 GetWorld()->TimeSeconds - GameRecordingStartTime, FPlatformTime::Seconds() - PlatformRecordingStartTime);
100
101 GameRecordingStartTime = -1.0;
102 PlatformRecordingStartTime = -1.0;
103 }
104
105 virtual void BeginFinalizeImpl() override
106 {
107 if (GetWorld() && GetWorld()->GetGameViewport())
108 {
109 // Re-enable rendering
110 GetWorld()->GetGameViewport()->bDisableWorldRendering = false;
111 }
112
113 // Convert it to absolute as the Audio Recorder wants to save relative to a different directory.
114 FString FormattedFileName = CaptureHost->ResolveFileFormat(FileName, FFrameMetrics());
115 FString AbsoluteDirectory = FPaths::ConvertRelativePathToFull(CaptureHost->GetSettings().OutputDirectory.Path);
117
118 // Now we can compare the two to see how close they are to each other to try and warn users about potential de-syncs caused by rendering.
119 double Difference = TotalGameRecordingTime - TotalPlatformRecordingTime;
121 {
122 // @todo-sequencer: This doesn't seem to correctly calculate the difference between UWorld time and platform time. It will report only a ~0.1s offset, but the wav file is ~28 seconds longer (platform time).
123 UE_LOG(LogMovieSceneCapture, Warning, TEXT("Game Time is out of sync with Platform Time during audio recording. This is usually an indication that the sequence could not play back at full speed, and audio will most likely be desynchronized. Platform Time took %f seconds longer than Game Time."), Difference);
124 }
125 }
128protected:
129 UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category = "Audio Options")
130 FString FileName;
131
132 double TotalGameRecordingTime;
133 double TotalPlatformRecordingTime;
134 double GameRecordingStartTime;
135 double PlatformRecordingStartTime;
136 bool bHasSetup;
137};
ENGINE_API class UEngine * GEngine
Definition UnrealEngine.cpp:427
#define TEXT(x)
Definition Platform.h:1272
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
void Init()
Definition LockFreeList.h:4
#define UE_LOG(CategoryName, Verbosity, Format,...)
Definition LogMacros.h:270
#define UPROPERTY(...)
UObject definition macros.
Definition ObjectMacros.h:744
#define GENERATED_BODY(...)
Definition ObjectMacros.h:765
#define UCLASS(...)
Definition ObjectMacros.h:776
Definition AudioDeviceHandle.h:28
Definition UObjectGlobals.h:1292
static CORE_API FString ConvertRelativePathToFull(const FString &InPath)
Definition Paths.cpp:1586
static AUDIOMIXER_API void StartRecordingOutput(const UObject *WorldContextObject, float ExpectedDuration, USoundSubmix *SubmixToRecord=nullptr)
Definition AudioMixerBlueprintLibrary.cpp:246
static AUDIOMIXER_API USoundWave * StopRecordingOutput(const UObject *WorldContextObject, EAudioRecordingExportType ExportType, const FString &Name, FString Path, USoundSubmix *SubmixToRecord=nullptr, USoundWave *ExistingSoundWaveToOverwrite=nullptr)
Definition AudioMixerBlueprintLibrary.cpp:258
static AUDIOMIXER_API void ResumeRecordingOutput(const UObject *WorldContextObject, USoundSubmix *SubmixToPause=nullptr)
Definition AudioMixerBlueprintLibrary.cpp:329
static AUDIOMIXER_API void PauseRecordingOutput(const UObject *WorldContextObject, USoundSubmix *SubmixToPause=nullptr)
Definition AudioMixerBlueprintLibrary.cpp:317
ENGINE_API FAudioDeviceHandle GetActiveAudioDevice()
Definition UnrealEngine.cpp:3922
Definition AudioCaptureProtocol.h:32
virtual bool SetupImpl() override
Definition AudioCaptureProtocol.h:49
virtual void PauseCaptureImpl() override
Definition AudioCaptureProtocol.h:81
virtual void BeginFinalizeImpl() override
Definition AudioCaptureProtocol.h:105
virtual bool StartCaptureImpl() override
Definition AudioCaptureProtocol.h:55
Definition MovieSceneCaptureProtocolBase.h:298
Definition AudioCaptureProtocol.h:20
@ false
Definition radaudio_common.h:23
static double Seconds()
Definition AndroidPlatformTime.h:20
Definition MovieSceneCaptureProtocolBase.h:310
static UE_FORCEINLINE_HINT bool IsNearlyZero(float Value, float ErrorTolerance=UE_SMALL_NUMBER)
Definition UnrealMathUtility.h:407