2021-02-23 20:16:28 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#include "Internationalization/Text.h"
|
|
|
|
|
#include "MetasoundExecutableOperator.h"
|
|
|
|
|
#include "MetasoundNodeRegistrationMacro.h"
|
2022-03-17 13:14:50 -04:00
|
|
|
#include "MetasoundParamHelper.h"
|
2021-02-23 20:16:28 -04:00
|
|
|
#include "MetasoundPrimitives.h"
|
|
|
|
|
#include "MetasoundStandardNodesNames.h"
|
|
|
|
|
#include "MetasoundTrigger.h"
|
|
|
|
|
#include "MetasoundTime.h"
|
|
|
|
|
#include "MetasoundAudioBuffer.h"
|
|
|
|
|
#include "DSP/Delay.h"
|
2021-04-03 18:40:14 -04:00
|
|
|
#include "MetasoundStandardNodesCategories.h"
|
|
|
|
|
#include "MetasoundFacade.h"
|
2021-02-23 20:16:28 -04:00
|
|
|
|
2022-01-18 17:44:56 -05:00
|
|
|
#define LOCTEXT_NAMESPACE "MetasoundStandardNodes"
|
2021-02-23 20:16:28 -04:00
|
|
|
|
|
|
|
|
namespace Metasound
|
|
|
|
|
{
|
|
|
|
|
namespace Delay
|
|
|
|
|
{
|
2022-03-17 13:14:50 -04:00
|
|
|
METASOUND_PARAM(InParamAudioInput, "In", "Audio input.")
|
2022-08-17 13:54:50 -04:00
|
|
|
METASOUND_PARAM(InParamDelayTime, "Delay Time", "The amount of time to delay the audio.")
|
2022-03-17 13:14:50 -04:00
|
|
|
METASOUND_PARAM(InParamDryLevel, "Dry Level", "The dry level of the delay.")
|
|
|
|
|
METASOUND_PARAM(InParamWetLevel, "Wet Level", "The wet level of the delay.")
|
|
|
|
|
METASOUND_PARAM(InParamFeedbackAmount, "Feedback", "Feedback amount.")
|
2022-08-17 13:54:50 -04:00
|
|
|
METASOUND_PARAM(InParamMaxDelayTime, "Max Delay Time", "The maximum amount of time to delay the audio.")
|
2022-03-17 13:14:50 -04:00
|
|
|
METASOUND_PARAM(OutParamAudio, "Out", "Audio output.")
|
2021-02-23 20:16:28 -04:00
|
|
|
|
2022-08-17 13:54:50 -04:00
|
|
|
static constexpr float MinMaxDelaySeconds = 0.001f;
|
|
|
|
|
static constexpr float MaxMaxDelaySeconds = 1000.f;
|
|
|
|
|
static constexpr float DefaultMaxDelaySeconds = 5.0f;
|
2021-02-23 20:16:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class FDelayOperator : public TExecutableOperator<FDelayOperator>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
static const FNodeClassMetadata& GetNodeInfo();
|
|
|
|
|
static const FVertexInterface& GetVertexInterface();
|
2022-08-17 13:54:50 -04:00
|
|
|
static TUniquePtr<IOperator> CreateOperator(const FBuildOperatorParams& InParams, FBuildResults& OutResults);
|
2021-02-23 20:16:28 -04:00
|
|
|
|
2023-03-02 14:40:35 -05:00
|
|
|
FDelayOperator(const FBuildOperatorParams& InParams,
|
2021-02-23 20:16:28 -04:00
|
|
|
const FAudioBufferReadRef& InAudioInput,
|
|
|
|
|
const FTimeReadRef& InDelayTime,
|
|
|
|
|
const FFloatReadRef& InDryLevel,
|
|
|
|
|
const FFloatReadRef& InWetLevel,
|
2022-08-17 13:54:50 -04:00
|
|
|
const FFloatReadRef& InFeedback,
|
|
|
|
|
float InMaxDelayTimeSeconds);
|
2021-02-23 20:16:28 -04:00
|
|
|
|
2023-05-22 13:28:27 -04:00
|
|
|
virtual void BindInputs(FInputVertexInterfaceData& InOutVertexData) override;
|
|
|
|
|
virtual void BindOutputs(FOutputVertexInterfaceData& InOutVertexData) override;
|
2021-02-23 20:16:28 -04:00
|
|
|
virtual FDataReferenceCollection GetInputs() const override;
|
|
|
|
|
virtual FDataReferenceCollection GetOutputs() const override;
|
2023-03-02 14:40:35 -05:00
|
|
|
void Reset(const IOperator::FResetParams& InParams);
|
2021-02-23 20:16:28 -04:00
|
|
|
void Execute();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
float GetInputDelayTimeMsec() const;
|
|
|
|
|
|
|
|
|
|
// The input audio buffer
|
|
|
|
|
FAudioBufferReadRef AudioInput;
|
|
|
|
|
|
|
|
|
|
// The amount of delay time
|
|
|
|
|
FTimeReadRef DelayTime;
|
|
|
|
|
|
2021-12-13 13:14:24 -05:00
|
|
|
// The dry level
|
2021-02-23 20:16:28 -04:00
|
|
|
FFloatReadRef DryLevel;
|
|
|
|
|
|
2021-12-13 13:14:24 -05:00
|
|
|
// The wet level
|
2021-02-23 20:16:28 -04:00
|
|
|
FFloatReadRef WetLevel;
|
|
|
|
|
|
|
|
|
|
// The feedback amount
|
|
|
|
|
FFloatReadRef Feedback;
|
|
|
|
|
|
|
|
|
|
// The audio output
|
|
|
|
|
FAudioBufferWriteRef AudioOutput;
|
|
|
|
|
|
|
|
|
|
// The internal delay buffer
|
|
|
|
|
Audio::FDelay DelayBuffer;
|
|
|
|
|
|
2021-12-13 13:14:24 -05:00
|
|
|
// The previous delay time
|
2022-08-17 13:54:50 -04:00
|
|
|
float PrevDelayTimeMsec = 0.f;
|
2021-02-23 20:16:28 -04:00
|
|
|
|
|
|
|
|
// Feedback sample
|
2022-08-17 13:54:50 -04:00
|
|
|
float FeedbackSample = 0.f;
|
|
|
|
|
|
|
|
|
|
// Maximum delay time
|
|
|
|
|
float MaxDelayTimeSeconds = Delay::DefaultMaxDelaySeconds;
|
2021-02-23 20:16:28 -04:00
|
|
|
};
|
|
|
|
|
|
2023-03-02 14:40:35 -05:00
|
|
|
FDelayOperator::FDelayOperator(const FBuildOperatorParams& InParams,
|
2021-12-13 13:14:24 -05:00
|
|
|
const FAudioBufferReadRef& InAudioInput,
|
2021-02-23 20:16:28 -04:00
|
|
|
const FTimeReadRef& InDelayTime,
|
2021-12-13 13:14:24 -05:00
|
|
|
const FFloatReadRef& InDryLevel,
|
|
|
|
|
const FFloatReadRef& InWetLevel,
|
2022-08-17 13:54:50 -04:00
|
|
|
const FFloatReadRef& InFeedback,
|
|
|
|
|
float InMaxDelayTimeSeconds)
|
2021-02-23 20:16:28 -04:00
|
|
|
|
|
|
|
|
: AudioInput(InAudioInput)
|
|
|
|
|
, DelayTime(InDelayTime)
|
|
|
|
|
, DryLevel(InDryLevel)
|
|
|
|
|
, WetLevel(InWetLevel)
|
|
|
|
|
, Feedback(InFeedback)
|
2023-03-02 14:40:35 -05:00
|
|
|
, AudioOutput(FAudioBufferWriteRef::CreateNew(InParams.OperatorSettings))
|
2021-02-23 20:16:28 -04:00
|
|
|
{
|
2022-08-17 13:54:50 -04:00
|
|
|
MaxDelayTimeSeconds = FMath::Clamp(InMaxDelayTimeSeconds, Delay::MinMaxDelaySeconds, Delay::MaxMaxDelaySeconds);
|
|
|
|
|
|
2023-03-02 14:40:35 -05:00
|
|
|
Reset(InParams);
|
2021-02-23 20:16:28 -04:00
|
|
|
}
|
|
|
|
|
|
2023-05-22 13:28:27 -04:00
|
|
|
|
|
|
|
|
void FDelayOperator::BindInputs(FInputVertexInterfaceData& InOutVertexData)
|
2022-08-17 13:54:50 -04:00
|
|
|
{
|
|
|
|
|
using namespace Delay;
|
|
|
|
|
|
2023-05-22 13:28:27 -04:00
|
|
|
InOutVertexData.BindReadVertex(METASOUND_GET_PARAM_NAME(InParamAudioInput), AudioInput);
|
|
|
|
|
InOutVertexData.BindReadVertex(METASOUND_GET_PARAM_NAME(InParamDelayTime), DelayTime);
|
|
|
|
|
InOutVertexData.BindReadVertex(METASOUND_GET_PARAM_NAME(InParamDryLevel), DryLevel);
|
|
|
|
|
InOutVertexData.BindReadVertex(METASOUND_GET_PARAM_NAME(InParamWetLevel), WetLevel);
|
|
|
|
|
InOutVertexData.BindReadVertex(METASOUND_GET_PARAM_NAME(InParamFeedbackAmount), Feedback);
|
|
|
|
|
InOutVertexData.SetValue(METASOUND_GET_PARAM_NAME(InParamMaxDelayTime), FTime::FromSeconds(MaxDelayTimeSeconds));
|
|
|
|
|
}
|
2022-08-17 13:54:50 -04:00
|
|
|
|
2023-05-22 13:28:27 -04:00
|
|
|
void FDelayOperator::BindOutputs(FOutputVertexInterfaceData& InOutVertexData)
|
|
|
|
|
{
|
|
|
|
|
using namespace Delay;
|
|
|
|
|
InOutVertexData.BindReadVertex(METASOUND_GET_PARAM_NAME(OutParamAudio), AudioOutput);
|
2022-08-17 13:54:50 -04:00
|
|
|
}
|
|
|
|
|
|
2021-02-23 20:16:28 -04:00
|
|
|
FDataReferenceCollection FDelayOperator::GetInputs() const
|
|
|
|
|
{
|
2022-08-17 13:54:50 -04:00
|
|
|
// This should never be called. Bind(...) is called instead. This method
|
2023-05-22 13:28:27 -04:00
|
|
|
// exists as a stop-gap until the API can be deprecated and removed.
|
2022-08-17 13:54:50 -04:00
|
|
|
checkNoEntry();
|
2023-05-22 13:28:27 -04:00
|
|
|
return {};
|
2021-02-23 20:16:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FDataReferenceCollection FDelayOperator::GetOutputs() const
|
|
|
|
|
{
|
2022-08-17 13:54:50 -04:00
|
|
|
// This should never be called. Bind(...) is called instead. This method
|
2023-05-22 13:28:27 -04:00
|
|
|
// exists as a stop-gap until the API can be deprecated and removed.
|
2022-08-17 13:54:50 -04:00
|
|
|
checkNoEntry();
|
2023-05-22 13:28:27 -04:00
|
|
|
return {};
|
2021-02-23 20:16:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float FDelayOperator::GetInputDelayTimeMsec() const
|
|
|
|
|
{
|
|
|
|
|
// Clamp the delay time to the max delay allowed
|
2022-08-17 13:54:50 -04:00
|
|
|
return 1000.0f * FMath::Clamp((float)DelayTime->GetSeconds(), 0.0f, MaxDelayTimeSeconds);
|
2021-02-23 20:16:28 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-02 14:40:35 -05:00
|
|
|
void FDelayOperator::Reset(const IOperator::FResetParams& InParams)
|
|
|
|
|
{
|
|
|
|
|
FeedbackSample = 0.f;
|
|
|
|
|
PrevDelayTimeMsec = GetInputDelayTimeMsec();
|
|
|
|
|
|
|
|
|
|
DelayBuffer.Reset();
|
|
|
|
|
DelayBuffer.Init(InParams.OperatorSettings.GetSampleRate(), MaxDelayTimeSeconds);
|
|
|
|
|
DelayBuffer.SetDelayMsec(PrevDelayTimeMsec);
|
|
|
|
|
|
|
|
|
|
AudioOutput->Zero();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 20:16:28 -04:00
|
|
|
void FDelayOperator::Execute()
|
|
|
|
|
{
|
|
|
|
|
// Get clamped delay time
|
|
|
|
|
float CurrentInputDelayTime = GetInputDelayTimeMsec();
|
|
|
|
|
|
|
|
|
|
// Check to see if our delay amount has changed
|
|
|
|
|
if (!FMath::IsNearlyEqual(PrevDelayTimeMsec, CurrentInputDelayTime))
|
|
|
|
|
{
|
|
|
|
|
PrevDelayTimeMsec = CurrentInputDelayTime;
|
|
|
|
|
DelayBuffer.SetEasedDelayMsec(PrevDelayTimeMsec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const float* InputAudio = AudioInput->GetData();
|
|
|
|
|
|
|
|
|
|
float* OutputAudio = AudioOutput->GetData();
|
|
|
|
|
int32 NumFrames = AudioInput->Num();
|
|
|
|
|
|
|
|
|
|
// Clamp the feedback amount to make sure it's bounded. Clamp to a number slightly less than 1.0
|
|
|
|
|
float FeedbackAmount = FMath::Clamp(*Feedback, 0.0f, 1.0f - SMALL_NUMBER);
|
|
|
|
|
float CurrentDryLevel = FMath::Clamp(*DryLevel, 0.0f, 1.0f);
|
|
|
|
|
float CurrentWetLevel = FMath::Clamp(*WetLevel, 0.0f, 1.0f);
|
|
|
|
|
|
|
|
|
|
if (FMath::IsNearlyZero(FeedbackAmount))
|
|
|
|
|
{
|
|
|
|
|
FeedbackSample = 0.0f;
|
|
|
|
|
|
|
|
|
|
for (int32 FrameIndex = 0; FrameIndex < NumFrames; ++FrameIndex)
|
|
|
|
|
{
|
2021-02-25 21:46:14 -04:00
|
|
|
OutputAudio[FrameIndex] = CurrentWetLevel * DelayBuffer.ProcessAudioSample(InputAudio[FrameIndex]) + CurrentDryLevel * InputAudio[FrameIndex];
|
2021-02-23 20:16:28 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// There is some amount of feedback so we do the feedback mixing
|
|
|
|
|
for (int32 FrameIndex = 0; FrameIndex < NumFrames; ++FrameIndex)
|
|
|
|
|
{
|
2021-02-25 21:46:14 -04:00
|
|
|
OutputAudio[FrameIndex] = CurrentWetLevel * DelayBuffer.ProcessAudioSample(InputAudio[FrameIndex] + FeedbackSample * FeedbackAmount) + CurrentDryLevel * InputAudio[FrameIndex];
|
2021-02-23 20:16:28 -04:00
|
|
|
FeedbackSample = OutputAudio[FrameIndex];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const FVertexInterface& FDelayOperator::GetVertexInterface()
|
|
|
|
|
{
|
2022-03-17 13:14:50 -04:00
|
|
|
using namespace Delay;
|
|
|
|
|
|
2022-08-17 13:54:50 -04:00
|
|
|
FDataVertexMetadata MaxDelayTimeMetadata = METASOUND_GET_PARAM_METADATA(InParamMaxDelayTime);
|
|
|
|
|
MaxDelayTimeMetadata.bIsAdvancedDisplay = true;
|
|
|
|
|
|
2021-02-23 20:16:28 -04:00
|
|
|
static const FVertexInterface Interface(
|
|
|
|
|
FInputVertexInterface(
|
2022-03-31 16:49:59 -04:00
|
|
|
TInputDataVertex<FAudioBuffer>(METASOUND_GET_PARAM_NAME_AND_METADATA(InParamAudioInput)),
|
|
|
|
|
TInputDataVertex<FTime>(METASOUND_GET_PARAM_NAME_AND_METADATA(InParamDelayTime), 1.0f),
|
|
|
|
|
TInputDataVertex<float>(METASOUND_GET_PARAM_NAME_AND_METADATA(InParamDryLevel), 0.0f),
|
|
|
|
|
TInputDataVertex<float>(METASOUND_GET_PARAM_NAME_AND_METADATA(InParamWetLevel), 1.0f),
|
2022-08-17 13:54:50 -04:00
|
|
|
TInputDataVertex<float>(METASOUND_GET_PARAM_NAME_AND_METADATA(InParamFeedbackAmount), 0.0f),
|
|
|
|
|
TInputConstructorVertex<FTime>(METASOUND_GET_PARAM_NAME(InParamMaxDelayTime), MaxDelayTimeMetadata, DefaultMaxDelaySeconds)
|
2021-02-23 20:16:28 -04:00
|
|
|
),
|
|
|
|
|
FOutputVertexInterface(
|
2022-03-31 16:49:59 -04:00
|
|
|
TOutputDataVertex<FAudioBuffer>(METASOUND_GET_PARAM_NAME_AND_METADATA(OutParamAudio))
|
2021-02-23 20:16:28 -04:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return Interface;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const FNodeClassMetadata& FDelayOperator::GetNodeInfo()
|
|
|
|
|
{
|
|
|
|
|
auto InitNodeInfo = []() -> FNodeClassMetadata
|
|
|
|
|
{
|
|
|
|
|
FNodeClassMetadata Info;
|
2022-01-18 17:44:56 -05:00
|
|
|
Info.ClassName = { StandardNodes::Namespace, "Delay", StandardNodes::AudioVariant };
|
2021-02-23 20:16:28 -04:00
|
|
|
Info.MajorVersion = 1;
|
2022-01-18 17:44:56 -05:00
|
|
|
Info.MinorVersion = 1;
|
2022-02-10 18:36:47 -05:00
|
|
|
Info.DisplayName = METASOUND_LOCTEXT("DelayNode_DisplayName", "Delay");
|
|
|
|
|
Info.Description = METASOUND_LOCTEXT("DelayNode_Description", "Delays an audio buffer by the specified amount.");
|
2021-02-23 20:16:28 -04:00
|
|
|
Info.Author = PluginAuthor;
|
|
|
|
|
Info.PromptIfMissing = PluginNodeMissingPrompt;
|
|
|
|
|
Info.DefaultInterface = GetVertexInterface();
|
2021-08-09 15:13:40 -04:00
|
|
|
Info.CategoryHierarchy.Emplace(NodeCategories::Delays);
|
2021-02-23 20:16:28 -04:00
|
|
|
return Info;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const FNodeClassMetadata Info = InitNodeInfo();
|
|
|
|
|
|
|
|
|
|
return Info;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 13:54:50 -04:00
|
|
|
TUniquePtr<IOperator> FDelayOperator::CreateOperator(const FBuildOperatorParams& InParams, FBuildResults& OutResults)
|
2021-02-23 20:16:28 -04:00
|
|
|
{
|
2022-03-17 13:14:50 -04:00
|
|
|
using namespace Delay;
|
|
|
|
|
|
2022-08-17 13:54:50 -04:00
|
|
|
const FInputVertexInterfaceData& InputData = InParams.InputData;
|
2021-02-23 20:16:28 -04:00
|
|
|
|
2022-08-17 13:54:50 -04:00
|
|
|
FAudioBufferReadRef AudioIn = InputData.GetOrConstructDataReadReference<FAudioBuffer>(METASOUND_GET_PARAM_NAME(InParamAudioInput), InParams.OperatorSettings);
|
|
|
|
|
FTimeReadRef DelayTime = InputData.GetOrCreateDefaultDataReadReference<FTime>(METASOUND_GET_PARAM_NAME(InParamDelayTime), InParams.OperatorSettings);
|
|
|
|
|
FFloatReadRef DryLevel = InputData.GetOrCreateDefaultDataReadReference<float>(METASOUND_GET_PARAM_NAME(InParamDryLevel), InParams.OperatorSettings);
|
|
|
|
|
FFloatReadRef WetLevel = InputData.GetOrCreateDefaultDataReadReference<float>(METASOUND_GET_PARAM_NAME(InParamWetLevel), InParams.OperatorSettings);
|
|
|
|
|
FFloatReadRef Feedback = InputData.GetOrCreateDefaultDataReadReference<float>(METASOUND_GET_PARAM_NAME(InParamFeedbackAmount), InParams.OperatorSettings);
|
|
|
|
|
FTime MaxDelayTime = InputData.GetOrCreateDefaultValue<FTime>(METASOUND_GET_PARAM_NAME(InParamMaxDelayTime), InParams.OperatorSettings);
|
2021-02-23 20:16:28 -04:00
|
|
|
|
2023-03-02 14:40:35 -05:00
|
|
|
return MakeUnique<FDelayOperator>(InParams, AudioIn, DelayTime, DryLevel, WetLevel, Feedback, MaxDelayTime.GetSeconds());
|
2021-02-23 20:16:28 -04:00
|
|
|
}
|
|
|
|
|
|
2021-04-03 18:40:14 -04:00
|
|
|
class FDelayNode : public FNodeFacade
|
2021-02-23 20:16:28 -04:00
|
|
|
{
|
2021-04-03 18:40:14 -04:00
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructor used by the Metasound Frontend.
|
|
|
|
|
*/
|
|
|
|
|
FDelayNode(const FNodeInitData& InitData)
|
|
|
|
|
: FNodeFacade(InitData.InstanceName, InitData.InstanceID, TFacadeOperatorClass<FDelayOperator>())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-23 20:16:28 -04:00
|
|
|
|
|
|
|
|
METASOUND_REGISTER_NODE(FDelayNode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#undef LOCTEXT_NAMESPACE
|