You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
[FYI] phil.popp #jira UE-118903 #rb trivial #lockdown jeff.farris #ROBOMERGE-SOURCE: CL 16775810 via CL 16775845 #ROBOMERGE-BOT: STARSHIP (Main -> Release-Engine-Test) (v836-16769935) [CL 16775861 by rob gay in ue5-release-engine-test branch]
310 lines
8.9 KiB
C++
310 lines
8.9 KiB
C++
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
#include "MetasoundSource.h"
|
|
|
|
#include "AssetRegistryModule.h"
|
|
#include "CoreMinimal.h"
|
|
#include "Internationalization/Text.h"
|
|
#include "MetasoundAssetBase.h"
|
|
#include "MetasoundAudioFormats.h"
|
|
#include "MetasoundEngineArchetypes.h"
|
|
#include "MetasoundEngineEnvironment.h"
|
|
#include "MetasoundEnvironment.h"
|
|
#include "MetasoundFrontendController.h"
|
|
#include "MetasoundFrontendQuery.h"
|
|
#include "MetasoundFrontendQuerySteps.h"
|
|
#include "MetasoundFrontendSearchEngine.h"
|
|
#include "MetasoundFrontendTransform.h"
|
|
#include "MetasoundGenerator.h"
|
|
#include "MetasoundInstanceTransmitter.h"
|
|
#include "MetasoundLog.h"
|
|
#include "MetasoundOperatorSettings.h"
|
|
#include "MetasoundPrimitives.h"
|
|
#include "MetasoundReceiveNode.h"
|
|
#include "MetasoundTrigger.h"
|
|
#include "MetasoundUObjectRegistry.h"
|
|
#include "UObject/ObjectSaveContext.h"
|
|
|
|
#if WITH_EDITORONLY_DATA
|
|
#include "EdGraph/EdGraph.h"
|
|
#endif // WITH_EDITORONLY_DATA
|
|
|
|
#define LOCTEXT_NAMESPACE "MetaSoundSource"
|
|
|
|
|
|
FAutoConsoleVariableRef CVarMetaSoundBlockRate(
|
|
TEXT("au.MetaSound.BlockRate"),
|
|
Metasound::ConsoleVariables::BlockRate,
|
|
TEXT("Sets block rate (blocks per second) of MetaSounds.\n")
|
|
TEXT("Default: 100.0f"),
|
|
ECVF_Default);
|
|
|
|
UMetaSoundSource::UMetaSoundSource(const FObjectInitializer& ObjectInitializer)
|
|
: Super(ObjectInitializer)
|
|
, FMetasoundAssetBase()
|
|
{
|
|
bRequiresStopFade = true;
|
|
NumChannels = 1;
|
|
Duration = INDEFINITELY_LOOPING_DURATION;
|
|
bLooping = true;
|
|
|
|
// todo: ensure that we have a method so that the audio engine can be authoritative over the sample rate the UMetaSoundSource runs at.
|
|
SampleRate = 48000.f;
|
|
|
|
}
|
|
|
|
#if WITH_EDITOR
|
|
|
|
void UMetaSoundSource::PreSave(FObjectPreSaveContext SaveContext)
|
|
{
|
|
Super::PreSave(SaveContext);
|
|
|
|
// TODO: Move these to be run anytime the interface changes or
|
|
// the node is versioned
|
|
|
|
// TODO: Enable Composition
|
|
// RegisterGraphWithFrontend();
|
|
}
|
|
|
|
void UMetaSoundSource::PostEditUndo()
|
|
{
|
|
Super::PostEditUndo();
|
|
|
|
if (Graph)
|
|
{
|
|
Graph->Synchronize();
|
|
}
|
|
}
|
|
|
|
void UMetaSoundSource::PostEditChangeProperty(FPropertyChangedEvent& InEvent)
|
|
{
|
|
using namespace Metasound;
|
|
using namespace Metasound::Frontend;
|
|
|
|
Super::PostEditChangeProperty(InEvent);
|
|
Metasound::PostEditChangeProperty(*this, InEvent);
|
|
|
|
if (InEvent.GetPropertyName() == GET_MEMBER_NAME_CHECKED(UMetaSoundSource, OutputFormat))
|
|
{
|
|
ConvertFromPreset();
|
|
|
|
bool bDidModifyDocument = false;
|
|
switch (OutputFormat)
|
|
{
|
|
case EMetasoundSourceAudioFormat::Mono:
|
|
{
|
|
NumChannels = 1;
|
|
// TODO: utilize latest version `MetasoudnSourceMono`
|
|
bDidModifyDocument = Metasound::Frontend::FMatchRootGraphToArchetype(Metasound::Engine::MetasoundSourceMono::GetVersion()).Transform(GetDocumentHandle());
|
|
}
|
|
break;
|
|
|
|
case EMetasoundSourceAudioFormat::Stereo:
|
|
{
|
|
NumChannels = 2;
|
|
bDidModifyDocument = Metasound::Frontend::FMatchRootGraphToArchetype(Metasound::Engine::MetasoundSourceStereo::GetVersion()).Transform(GetDocumentHandle());
|
|
}
|
|
break;
|
|
|
|
default:
|
|
{
|
|
static_assert(static_cast<int32>(EMetasoundSourceAudioFormat::COUNT) == 2, "Possible missing format switch case coverage.");
|
|
}
|
|
break;
|
|
}
|
|
|
|
if (bDidModifyDocument)
|
|
{
|
|
MarkMetasoundDocumentDirty();
|
|
}
|
|
}
|
|
}
|
|
#endif // WITH_EDITOR
|
|
|
|
#if WITH_EDITORONLY_DATA
|
|
UEdGraph* UMetaSoundSource::GetGraph()
|
|
{
|
|
return Graph;
|
|
}
|
|
|
|
const UEdGraph* UMetaSoundSource::GetGraph() const
|
|
{
|
|
return Graph;
|
|
}
|
|
|
|
UEdGraph& UMetaSoundSource::GetGraphChecked()
|
|
{
|
|
check(Graph);
|
|
return *Graph;
|
|
}
|
|
|
|
const UEdGraph& UMetaSoundSource::GetGraphChecked() const
|
|
{
|
|
check(Graph);
|
|
return *Graph;
|
|
}
|
|
|
|
FText UMetaSoundSource::GetDisplayName() const
|
|
{
|
|
FString TypeName = UMetaSoundSource::StaticClass()->GetName();
|
|
return FMetasoundAssetBase::GetDisplayName(MoveTemp(TypeName));
|
|
}
|
|
|
|
void UMetaSoundSource::SetRegistryAssetClassInfo(const Metasound::Frontend::FNodeClassInfo& InNodeInfo)
|
|
{
|
|
Metasound::SetMetaSoundRegistryAssetClassInfo(*this, InNodeInfo);
|
|
}
|
|
#endif // WITH_EDITORONLY_DATA
|
|
|
|
Metasound::Frontend::FNodeClassInfo UMetaSoundSource::GetAssetClassInfo() const
|
|
{
|
|
return { GetDocumentChecked().RootGraph, *GetPathName() };
|
|
}
|
|
|
|
bool UMetaSoundSource::IsPlayable() const
|
|
{
|
|
// todo: cache off whether this metasound is buildable to an operator.
|
|
return true;
|
|
}
|
|
|
|
bool UMetaSoundSource::SupportsSubtitles() const
|
|
{
|
|
return Super::SupportsSubtitles();
|
|
}
|
|
|
|
const FMetasoundFrontendVersion& UMetaSoundSource::GetDefaultArchetypeVersion() const
|
|
{
|
|
static const FMetasoundFrontendVersion DefaultVersion = Metasound::Engine::MetasoundSourceMono::GetVersion();
|
|
|
|
return DefaultVersion;
|
|
}
|
|
|
|
float UMetaSoundSource::GetDuration()
|
|
{
|
|
// eh? this is kind of a weird field anyways.
|
|
return Super::GetDuration();
|
|
}
|
|
|
|
ISoundGeneratorPtr UMetaSoundSource::CreateSoundGenerator(const FSoundGeneratorInitParams& InParams)
|
|
{
|
|
using namespace Metasound;
|
|
using namespace Metasound::Frontend;
|
|
using namespace Metasound::Engine;
|
|
|
|
Duration = INDEFINITELY_LOOPING_DURATION;
|
|
bLooping = true;
|
|
VirtualizationMode = EVirtualizationMode::PlayWhenSilent;
|
|
|
|
SampleRate = InParams.SampleRate;
|
|
FOperatorSettings InSettings = GetOperatorSettings(static_cast<FSampleRate>(SampleRate));
|
|
FMetasoundEnvironment Environment;
|
|
|
|
// Add audio device ID to environment.
|
|
FAudioDeviceHandle DeviceHandle;
|
|
if (UWorld* World = GetWorld())
|
|
{
|
|
DeviceHandle = World->GetAudioDevice();
|
|
}
|
|
if (!DeviceHandle.IsValid())
|
|
{
|
|
if (FAudioDeviceManager* DeviceManager = FAudioDeviceManager::Get())
|
|
{
|
|
DeviceHandle = DeviceManager->GetMainAudioDeviceHandle();
|
|
}
|
|
}
|
|
Environment.SetValue<FAudioDeviceHandle>(MetasoundSource::GetAudioDeviceHandleVariableName(), DeviceHandle);
|
|
Environment.SetValue<uint32>(MetasoundSource::GetSoundUniqueIdName(), GetUniqueID());
|
|
Environment.SetValue<bool>(MetasoundSource::GetIsPreviewSoundName(), InParams.bIsPreviewSound);
|
|
|
|
const FMetasoundFrontendDocument* OriginalDoc = GetDocument().Get();
|
|
if (nullptr == OriginalDoc)
|
|
{
|
|
UE_LOG(LogMetaSound, Error, TEXT("Cannot create sound generator. Null Metasound document in UMetaSoundSource [Name:%s]"), *GetName());
|
|
return ISoundGeneratorPtr(nullptr);
|
|
}
|
|
|
|
// Check version and attempt to version up if out-of-date
|
|
if (VersionAsset(false /* bMarkDirty */))
|
|
{
|
|
const FMetasoundFrontendDocumentMetadata& Metadata = GetDocumentHandle()->GetMetadata();
|
|
UE_LOG(LogMetaSound, Display,
|
|
TEXT("Dynamically versioned UMetaSoundSource asset [Name:%s] to %s (Resave to avoid update at runtime)."),
|
|
*GetName(),
|
|
*Metadata.Version.Number.ToString());
|
|
}
|
|
|
|
// Inject receive nodes for unused transmittable inputs. Perform edits on a copy
|
|
// of the document to avoid altering document.
|
|
// TODO: Use a light wrapper of the document instead of a copy.
|
|
FMetasoundFrontendDocument DocumentWithInjectedReceives;
|
|
ensure(CopyDocumentAndInjectReceiveNodes(InParams.InstanceID, *OriginalDoc, DocumentWithInjectedReceives));
|
|
|
|
// Create handles for new root graph
|
|
FConstDocumentHandle NewDocumentHandle = IDocumentController::CreateDocumentHandle(MakeAccessPtr<FConstDocumentAccessPtr>(DocumentWithInjectedReceives.AccessPoint, DocumentWithInjectedReceives));
|
|
|
|
FMetasoundGeneratorInitParams InitParams =
|
|
{
|
|
InSettings,
|
|
MoveTemp(DocumentWithInjectedReceives),
|
|
Environment,
|
|
GetName(),
|
|
{}, // Channel names still need to be set.
|
|
MetasoundSource::GetOnPlayInputName(),
|
|
MetasoundSource::GetIsFinishedOutputName()
|
|
};
|
|
|
|
// Set init params dependent upon output audio format.
|
|
switch (OutputFormat)
|
|
{
|
|
case EMetasoundSourceAudioFormat::Mono:
|
|
{
|
|
InitParams.AudioOutputNames = { MetasoundSourceMono::GetAudioOutputName() };
|
|
}
|
|
break;
|
|
|
|
case EMetasoundSourceAudioFormat::Stereo:
|
|
{
|
|
InitParams.AudioOutputNames = { MetasoundSourceStereo::GetLeftAudioOutputName(), MetasoundSourceStereo::GetRightAudioOutputName() };
|
|
}
|
|
break;
|
|
|
|
default:
|
|
{
|
|
static_assert(static_cast<int32>(EMetasoundSourceAudioFormat::COUNT) == 2, "Possible missing format switch case coverage.");
|
|
}
|
|
break;
|
|
}
|
|
|
|
return ISoundGeneratorPtr(new FMetasoundGenerator(MoveTemp(InitParams)));
|
|
}
|
|
|
|
TUniquePtr<IAudioInstanceTransmitter> UMetaSoundSource::CreateInstanceTransmitter(const FAudioInstanceTransmitterInitParams& InParams) const
|
|
{
|
|
Metasound::FMetasoundInstanceTransmitter::FInitParams InitParams(GetOperatorSettings(InParams.SampleRate), InParams.InstanceID);
|
|
|
|
for (const FSendInfoAndVertexName& InfoAndName : FMetasoundAssetBase::GetSendInfos(InParams.InstanceID))
|
|
{
|
|
InitParams.Infos.Add(InfoAndName.SendInfo);
|
|
}
|
|
|
|
return MakeUnique<Metasound::FMetasoundInstanceTransmitter>(InitParams);
|
|
}
|
|
|
|
Metasound::FOperatorSettings UMetaSoundSource::GetOperatorSettings(Metasound::FSampleRate InSampleRate) const
|
|
{
|
|
const float BlockRate = FMath::Clamp(Metasound::ConsoleVariables::BlockRate, 1.0f, 1000.0f);
|
|
return Metasound::FOperatorSettings(InSampleRate, BlockRate);
|
|
}
|
|
|
|
const TArray<FMetasoundFrontendVersion>& UMetaSoundSource::GetSupportedArchetypeVersions() const
|
|
{
|
|
static const TArray<FMetasoundFrontendVersion> Supported(
|
|
{
|
|
Metasound::Engine::MetasoundSourceMono::GetVersion(),
|
|
Metasound::Engine::MetasoundSourceStereo::GetVersion()
|
|
});
|
|
|
|
return Supported;
|
|
}
|
|
|
|
#undef LOCTEXT_NAMESPACE // MetaSoundSource
|