2021-01-11 22:57:56 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
# include "CoreMinimal.h"
# include "MetasoundBuilderInterface.h"
# include "MetasoundDataReferenceCollection.h"
# include "MetasoundExecutableOperator.h"
# include "MetasoundFacade.h"
# include "MetasoundNode.h"
# include "MetasoundNodeInterface.h"
# include "MetasoundNodeRegistrationMacro.h"
# include "MetasoundOperatorInterface.h"
# include "MetasoundPrimitives.h"
2021-02-16 01:26:50 -04:00
# include "MetasoundSampleCounter.h"
2021-01-28 19:02:51 -04:00
# include "MetasoundStandardNodesNames.h"
2021-03-05 23:03:16 -04:00
# include "MetasoundStandardNodesCategories.h"
2021-03-24 03:22:53 -04:00
# include "MetasoundParamHelper.h"
2021-01-11 22:57:56 -04:00
2021-04-01 20:10:37 -04:00
# define LOCTEXT_NAMESPACE "MetasoundStandardNodes_DelayNode"
2021-01-11 22:57:56 -04:00
namespace Metasound
{
2021-03-24 03:22:53 -04:00
namespace TriggerDelayVertexNames
{
2021-04-01 20:10:37 -04:00
METASOUND_PARAM ( InputInTriggerDelay , " In " , " Input trigger which results in a delayed trigger. " ) ;
METASOUND_PARAM ( InputResetDelay , " Reset " , " Resets the trigger delay, clearing the execution task if pending. " ) ;
2021-03-24 03:22:53 -04:00
METASOUND_PARAM ( InputDelayTime , " Delay Time " , " Time to delay and execute deferred trigger in seconds. " ) ;
METASOUND_PARAM ( OutputOnTrigger , " Out " , " The delayed output trigger. " ) ;
}
2021-01-11 22:57:56 -04:00
class FTriggerDelayOperator : public TExecutableOperator < FTriggerDelayOperator >
{
public :
2021-01-28 19:02:51 -04:00
static const FNodeClassMetadata & GetNodeInfo ( ) ;
2021-02-03 15:40:56 -04:00
static const FVertexInterface & GetVertexInterface ( ) ;
2021-01-11 22:57:56 -04:00
static TUniquePtr < IOperator > CreateOperator ( const FCreateOperatorParams & InParams , FBuildErrorArray & OutErrors ) ;
2021-02-16 01:26:50 -04:00
FTriggerDelayOperator ( const FOperatorSettings & InSettings , const FTriggerReadRef & InTriggerReset , const FTriggerReadRef & InTriggerIn , const FTimeReadRef & InDelay ) ;
2021-01-11 22:57:56 -04:00
virtual FDataReferenceCollection GetInputs ( ) const override ;
virtual FDataReferenceCollection GetOutputs ( ) const override ;
void Execute ( ) ;
private :
2021-02-16 01:26:50 -04:00
FSampleCounter NextTriggerCounter ;
FSampleCount LastFrameReset ;
2021-01-11 22:57:56 -04:00
FTriggerReadRef TriggerIn ;
FTriggerReadRef TriggerReset ;
FTriggerWriteRef TriggerOut ;
2021-03-24 03:22:53 -04:00
FTimeReadRef DelayTime ;
2021-01-11 22:57:56 -04:00
2021-02-16 01:26:50 -04:00
FSampleCount FramesPerBlock ;
2021-01-11 22:57:56 -04:00
} ;
2021-03-24 03:22:53 -04:00
FTriggerDelayOperator : : FTriggerDelayOperator ( const FOperatorSettings & InSettings , const FTriggerReadRef & InTriggerReset , const FTriggerReadRef & InTriggerIn , const FTimeReadRef & InTimeDelay )
: NextTriggerCounter ( - 1 , InSettings . GetSampleRate ( ) )
, LastFrameReset ( - 1 )
, TriggerIn ( InTriggerIn )
, TriggerReset ( InTriggerReset )
, TriggerOut ( FTriggerWriteRef : : CreateNew ( InSettings ) )
, DelayTime ( InTimeDelay )
, FramesPerBlock ( InSettings . GetNumFramesPerBlock ( ) )
2021-01-11 22:57:56 -04:00
{
}
FDataReferenceCollection FTriggerDelayOperator : : GetInputs ( ) const
{
2021-03-24 03:22:53 -04:00
using namespace TriggerDelayVertexNames ;
2021-01-11 22:57:56 -04:00
FDataReferenceCollection InputDataReferences ;
2021-04-01 20:10:37 -04:00
InputDataReferences . AddDataReadReference ( METASOUND_GET_PARAM_NAME ( InputInTriggerDelay ) , TriggerIn ) ;
InputDataReferences . AddDataReadReference ( METASOUND_GET_PARAM_NAME ( InputResetDelay ) , TriggerReset ) ;
2021-03-24 03:22:53 -04:00
InputDataReferences . AddDataReadReference ( METASOUND_GET_PARAM_NAME ( InputDelayTime ) , DelayTime ) ;
2021-01-11 22:57:56 -04:00
return InputDataReferences ;
}
FDataReferenceCollection FTriggerDelayOperator : : GetOutputs ( ) const
{
2021-03-24 03:22:53 -04:00
using namespace TriggerDelayVertexNames ;
2021-01-11 22:57:56 -04:00
FDataReferenceCollection OutputDataReferences ;
2021-03-24 03:22:53 -04:00
OutputDataReferences . AddDataReadReference ( METASOUND_GET_PARAM_NAME ( OutputOnTrigger ) , TriggerOut ) ;
2021-01-11 22:57:56 -04:00
return OutputDataReferences ;
}
void FTriggerDelayOperator : : Execute ( )
{
// Advance internal counter to get rid of old triggers.
TriggerOut - > AdvanceBlock ( ) ;
2021-02-03 14:36:36 -04:00
LastFrameReset = - 1 ;
2021-02-16 01:26:50 -04:00
2021-02-03 14:36:36 -04:00
TriggerReset - > ExecuteBlock (
[ & ] ( int32 StartFrame , int32 EndFrame )
{
} ,
[ this ] ( int32 StartFrame , int32 EndFrame )
{
LastFrameReset = StartFrame ;
}
) ;
TriggerIn - > ExecuteBlock (
[ & ] ( int32 StartFrame , int32 EndFrame )
{
} ,
[ this ] ( int32 StartFrame , int32 EndFrame )
{
if ( StartFrame > LastFrameReset )
{
2021-03-24 03:22:53 -04:00
NextTriggerCounter . SetNumSamples ( * DelayTime ) ;
2021-02-03 14:36:36 -04:00
LastFrameReset = - 1 ;
}
else
{
2021-02-16 01:26:50 -04:00
NextTriggerCounter . SetNumSamples ( - 1 ) ;
2021-02-03 14:36:36 -04:00
}
}
) ;
2021-03-03 16:57:52 -04:00
if ( NextTriggerCounter . GetNumSamples ( ) > = 0 )
2021-01-11 22:57:56 -04:00
{
2021-02-16 01:26:50 -04:00
FSampleCount SamplesRemaining = NextTriggerCounter . GetNumSamples ( ) - FramesPerBlock ;
2021-01-11 22:57:56 -04:00
if ( SamplesRemaining > 0.0f )
{
2021-02-16 01:26:50 -04:00
NextTriggerCounter - = FramesPerBlock ;
2021-01-11 22:57:56 -04:00
}
else
{
2021-01-27 15:54:01 -04:00
TriggerOut - > TriggerFrame ( SamplesRemaining + ( int32 ) FramesPerBlock ) ;
2021-02-16 01:26:50 -04:00
NextTriggerCounter . SetNumSamples ( - 1 ) ;
2021-01-11 22:57:56 -04:00
}
}
}
TUniquePtr < IOperator > FTriggerDelayOperator : : CreateOperator ( const FCreateOperatorParams & InParams , FBuildErrorArray & OutErrors )
{
2021-03-24 03:22:53 -04:00
using namespace TriggerDelayVertexNames ;
2021-02-03 15:40:56 -04:00
const FInputVertexInterface & InputInterface = GetVertexInterface ( ) . GetInputInterface ( ) ;
2021-03-24 21:23:57 -04:00
FTimeReadRef Delay = InParams . InputDataReferences . GetDataReadReferenceOrConstructWithVertexDefault < FTime > ( InputInterface , METASOUND_GET_PARAM_NAME ( InputDelayTime ) , InParams . OperatorSettings ) ;
2021-02-03 15:40:56 -04:00
2021-04-01 20:10:37 -04:00
FTriggerReadRef TriggerIn = InParams . InputDataReferences . GetDataReadReferenceOrConstruct < FTrigger > ( METASOUND_GET_PARAM_NAME ( InputInTriggerDelay ) , InParams . OperatorSettings ) ;
FTriggerReadRef TriggerReset = InParams . InputDataReferences . GetDataReadReferenceOrConstruct < FTrigger > ( METASOUND_GET_PARAM_NAME ( InputResetDelay ) , InParams . OperatorSettings ) ;
2021-01-11 22:57:56 -04:00
return MakeUnique < FTriggerDelayOperator > ( InParams . OperatorSettings , TriggerReset , TriggerIn , Delay ) ;
}
2021-02-03 15:40:56 -04:00
const FVertexInterface & FTriggerDelayOperator : : GetVertexInterface ( )
2021-01-11 22:57:56 -04:00
{
2021-03-24 03:22:53 -04:00
using namespace TriggerDelayVertexNames ;
2021-01-11 22:57:56 -04:00
static const FVertexInterface Interface (
FInputVertexInterface (
2022-03-31 16:49:59 -04:00
TInputDataVertex < FTrigger > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( InputInTriggerDelay ) ) ,
TInputDataVertex < FTrigger > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( InputResetDelay ) ) ,
TInputDataVertex < FTime > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( InputDelayTime ) , 1.0f )
2021-01-11 22:57:56 -04:00
) ,
FOutputVertexInterface (
2022-03-31 16:49:59 -04:00
TOutputDataVertex < FTrigger > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( OutputOnTrigger ) )
2021-03-24 03:22:53 -04:00
)
2021-01-11 22:57:56 -04:00
) ;
return Interface ;
}
2021-01-28 19:02:51 -04:00
const FNodeClassMetadata & FTriggerDelayOperator : : GetNodeInfo ( )
2021-01-11 22:57:56 -04:00
{
2021-01-28 19:02:51 -04:00
auto InitNodeInfo = [ ] ( ) - > FNodeClassMetadata
2021-01-11 22:57:56 -04:00
{
2021-01-28 19:02:51 -04:00
FNodeClassMetadata Info ;
2021-08-09 15:08:37 -04:00
Info . ClassName = { StandardNodes : : Namespace , TEXT ( " Trigger Delay " ) , TEXT ( " " ) } ;
2021-01-11 22:57:56 -04:00
Info . MajorVersion = 1 ;
Info . MinorVersion = 0 ;
2022-02-10 18:36:47 -05:00
Info . DisplayName = METASOUND_LOCTEXT ( " Metasound_TriggerDelayNodeDisplayName " , " Trigger Delay " ) ;
Info . Description = METASOUND_LOCTEXT ( " Metasound_TriggerDelayNodeDescription " , " Executes output trigger after the given delay time from the most recent execution of the input trigger . " ) ;
2021-01-11 22:57:56 -04:00
Info . Author = PluginAuthor ;
Info . PromptIfMissing = PluginNodeMissingPrompt ;
2021-02-03 15:40:56 -04:00
Info . DefaultInterface = GetVertexInterface ( ) ;
2021-08-09 15:08:37 -04:00
Info . CategoryHierarchy . Emplace ( NodeCategories : : Trigger ) ;
2021-01-11 22:57:56 -04:00
return Info ;
} ;
2021-01-28 19:02:51 -04:00
static const FNodeClassMetadata Info = InitNodeInfo ( ) ;
2021-01-11 22:57:56 -04:00
return Info ;
}
2021-04-01 20:10:37 -04:00
class METASOUNDSTANDARDNODES_API FTriggerDelayNode : public FNodeFacade
2021-01-11 22:57:56 -04:00
{
2021-04-01 20:10:37 -04:00
public :
FTriggerDelayNode ( const FNodeInitData & InInitData )
: FNodeFacade ( InInitData . InstanceName , InInitData . InstanceID , TFacadeOperatorClass < FTriggerDelayOperator > ( ) )
{
}
} ;
METASOUND_REGISTER_NODE ( FTriggerDelayNode )
2021-01-11 22:57:56 -04:00
}
# undef LOCTEXT_NAMESPACE // MetasoundTriggerDelayNode