2021-04-01 18:38:40 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
# include "CoreMinimal.h"
# include "DSP/MidiNoteQuantizer.h"
# include "Internationalization/Text.h"
# include "MetasoundFacade.h"
# include "MetasoundPrimitives.h"
# include "MetasoundAudioBuffer.h"
# include "MetasoundParamHelper.h"
# include "MetasoundOperatorSettings.h"
# include "MetasoundStandardNodesNames.h"
# include "MetasoundExecutableOperator.h"
# include "MetasoundNodeRegistrationMacro.h"
# include "MetasoundStandardNodesCategories.h"
# include "MetasoundDataTypeRegistrationMacro.h"
2021-04-07 02:57:54 -04:00
# define LOCTEXT_NAMESPACE "MetasoundStandardNodes_MidiNoteQuantizerNode"
2021-04-01 18:38:40 -04:00
namespace Metasound
{
// forward declarations
// ...
# pragma region Parameter Names
namespace MidiNoteQuantizerParameterNames
{
// inputs
METASOUND_PARAM ( ParamNoteIn , " Note In " , " Midi Note to quantize " ) ;
METASOUND_PARAM ( ParamRootNote , " Root Note " , " Midi note to treat as the Root (0 = C, 1 = D, etc.). Octave does not matter. Values < 0 will clamp to 0 " ) ;
METASOUND_PARAM ( ParamScaleDegrees , " Scale Degrees "
, " Set of notes in ascending order, represeting half steps starting at 0.0f meaning the Root Note. The highest value MUST be the same as 0.0 one octave higher: 12.0 in most cases, but it doesn't have to be (i.e. could be 24.0 if you want to define a 2 octave range) " ) ;
// outputs
METASOUND_PARAM ( ParamNoteOutput , " Note Out " , " Quantized Note " ) ;
} // namespace MidiNoteQuantizerParameterNames
using namespace MidiNoteQuantizerParameterNames ;
# pragma endregion // Parameter Names
# pragma region Operator Declaration
class FMidiNoteQuantizerOperator : public TExecutableOperator < FMidiNoteQuantizerOperator >
{
public :
using FArrayScaleDegreeReadRef = TDataReadReference < TArray < float > > ;
using ScaleDegreeArrayType = TArray < float > ;
// ctor
FMidiNoteQuantizerOperator (
const FOperatorSettings & InSettings
, const FFloatReadRef & InMidiNoteIn
, const FFloatReadRef & InRootNote
, const FArrayScaleDegreeReadRef & InScale
) ;
// node interface
static const FNodeClassMetadata & GetNodeInfo ( ) ;
static FVertexInterface DeclareVertexInterface ( ) ;
static TUniquePtr < IOperator > CreateOperator ( const FCreateOperatorParams & InParams , FBuildErrorArray & OutErrors ) ;
virtual FDataReferenceCollection GetInputs ( ) const override ;
virtual FDataReferenceCollection GetOutputs ( ) const override ;
void Execute ( ) ;
private : // members
// input pins
FFloatReadRef MidiNoteIn ;
FFloatReadRef RootNote ;
FArrayScaleDegreeReadRef Scale ;
// output pins
FFloatWriteRef MidiNoteOut ;
// cached values
float PreviousNoteIn = TNumericLimits < float > : : Lowest ( ) ;
float PreviousRoot = - 1.0f ;
TArray < float > PreviousScale ;
float PreviousNoteOut = 0.0f ;
// other
FOperatorSettings Settings ;
} ; // class FMidiNoteQuantizerOperator
# pragma endregion // Operator Declaration
# pragma region Operator Implementation
// ctor
FMidiNoteQuantizerOperator : : FMidiNoteQuantizerOperator (
const FOperatorSettings & InSettings
, const FFloatReadRef & InMidiNoteIn
, const FFloatReadRef & InRootNote
, const FArrayScaleDegreeReadRef & InScale
)
: MidiNoteIn ( InMidiNoteIn )
, RootNote ( InRootNote )
, Scale ( InScale )
, MidiNoteOut ( FFloatWriteRef : : CreateNew ( ) )
, Settings ( InSettings )
{
}
const FNodeClassMetadata & FMidiNoteQuantizerOperator : : GetNodeInfo ( )
{
auto InitNodeInfo = [ ] ( ) - > FNodeClassMetadata
{
FNodeClassMetadata Info ;
2021-08-09 15:08:37 -04:00
Info . ClassName = { StandardNodes : : Namespace , TEXT ( " MIDI Note Quantizer " ) , StandardNodes : : AudioVariant } ;
2021-04-01 18:38:40 -04:00
Info . MajorVersion = 1 ;
Info . MinorVersion = 0 ;
2022-02-10 18:36:47 -05:00
Info . DisplayName = METASOUND_LOCTEXT ( " Metasound_MIDI_Note_Quantizer_NodeDisplayName " , " MIDI Note Quantizer " ) ;
Info . Description = METASOUND_LOCTEXT ( " MidiNoteQuantizer_NodeDescription " , " Quantizes a MIDI note to the nearset note that matches provided criteria " ) ;
2021-04-01 18:38:40 -04:00
Info . Author = PluginAuthor ;
Info . PromptIfMissing = PluginNodeMissingPrompt ;
Info . DefaultInterface = DeclareVertexInterface ( ) ;
2021-08-09 15:08:37 -04:00
Info . CategoryHierarchy . Emplace ( NodeCategories : : Music ) ;
2021-04-01 18:38:40 -04:00
return Info ;
} ;
static const FNodeClassMetadata Info = InitNodeInfo ( ) ;
return Info ;
}
FVertexInterface FMidiNoteQuantizerOperator : : DeclareVertexInterface ( )
{
static const FVertexInterface Interface (
FInputVertexInterface (
2022-03-31 16:49:59 -04:00
TInputDataVertex < float > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( ParamNoteIn ) , 60.0f ) ,
TInputDataVertex < float > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( ParamRootNote ) , 0.0f ) ,
TInputDataVertex < ScaleDegreeArrayType > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( ParamScaleDegrees ) )
2021-04-01 18:38:40 -04:00
) ,
FOutputVertexInterface (
2022-03-31 16:49:59 -04:00
TOutputDataVertex < float > ( METASOUND_GET_PARAM_NAME_AND_METADATA ( ParamNoteOutput ) )
2021-04-01 18:38:40 -04:00
)
) ;
return Interface ;
}
TUniquePtr < IOperator > FMidiNoteQuantizerOperator : : CreateOperator ( const FCreateOperatorParams & InParams , FBuildErrorArray & OutErrors )
{
const FDataReferenceCollection & InputDataRefs = InParams . InputDataReferences ;
// inputs
FFloatReadRef MidiNoteIn = InputDataRefs . GetDataReadReferenceOrConstruct < float > ( METASOUND_GET_PARAM_NAME ( ParamNoteIn ) ) ;
FFloatReadRef RootNoteIn = InputDataRefs . GetDataReadReferenceOrConstruct < float > ( METASOUND_GET_PARAM_NAME ( ParamRootNote ) ) ;
FArrayScaleDegreeReadRef InScaleArray = InParams . InputDataReferences . GetDataReadReferenceOrConstruct < ScaleDegreeArrayType > ( METASOUND_GET_PARAM_NAME ( ParamScaleDegrees ) ) ;
return MakeUnique < FMidiNoteQuantizerOperator > (
InParams . OperatorSettings
, MidiNoteIn
, RootNoteIn
, InScaleArray
) ;
}
FDataReferenceCollection FMidiNoteQuantizerOperator : : GetInputs ( ) const
{
FDataReferenceCollection InputDataReferences ;
InputDataReferences . AddDataReadReference ( METASOUND_GET_PARAM_NAME ( ParamNoteIn ) , FFloatReadRef ( MidiNoteIn ) ) ;
InputDataReferences . AddDataReadReference ( METASOUND_GET_PARAM_NAME ( ParamRootNote ) , FFloatReadRef ( RootNote ) ) ;
InputDataReferences . AddDataReadReference ( METASOUND_GET_PARAM_NAME ( ParamScaleDegrees ) , FArrayScaleDegreeReadRef ( Scale ) ) ;
return InputDataReferences ;
}
FDataReferenceCollection FMidiNoteQuantizerOperator : : GetOutputs ( ) const
{
// expose read access to our output buffer for other processors in the graph
FDataReferenceCollection OutputDataReferences ;
OutputDataReferences . AddDataReadReference ( METASOUND_GET_PARAM_NAME ( ParamNoteOutput ) , FFloatReadRef ( MidiNoteOut ) ) ;
return OutputDataReferences ;
}
void FMidiNoteQuantizerOperator : : Execute ( )
{
if ( ( * Scale ) . IsEmpty ( ) )
{
* MidiNoteOut = * MidiNoteIn ;
2021-04-06 23:20:55 -04:00
return ;
2021-04-01 18:38:40 -04:00
}
// calculate new output and cache values if needed
const float CurrentRoot = FMath : : Max ( * RootNote , 0.0f ) ;
if ( ! FMath : : IsNearlyEqual ( PreviousNoteIn , * MidiNoteIn )
| | ! FMath : : IsNearlyEqual ( PreviousRoot , CurrentRoot )
| | PreviousScale ! = * Scale
)
{
// cache values
PreviousNoteIn = * MidiNoteIn ;
PreviousRoot = * RootNote ;
PreviousScale = * Scale ;
PreviousScale . Sort ( ) ;
PreviousNoteOut = Audio : : FMidiNoteQuantizer : : QuantizeMidiNote ( PreviousNoteIn , PreviousRoot , PreviousScale ) ;
}
// set the output value
* MidiNoteOut = PreviousNoteOut ;
}
# pragma endregion // Operator Implementation
# pragma region Node Declaration
class METASOUNDSTANDARDNODES_API FMidiNoteQuantizerNode : public FNodeFacade
{
public :
// public node api needs to define two conversion constructors:
// (1: from FString)
2021-09-13 14:13:39 -04:00
FMidiNoteQuantizerNode ( const FVertexName & InInstanceName , const FGuid & InInstanceID )
2021-04-01 18:38:40 -04:00
: FNodeFacade ( InInstanceName , InInstanceID , TFacadeOperatorClass < FMidiNoteQuantizerOperator > ( ) )
{ }
// (2: From an NodeInitData struct)
FMidiNoteQuantizerNode ( const FNodeInitData & InInitData )
: FMidiNoteQuantizerNode ( InInitData . InstanceName , InInitData . InstanceID )
{ }
} ;
# pragma endregion // Node Declaration
# pragma region Node Registration
METASOUND_REGISTER_NODE ( FMidiNoteQuantizerNode ) ;
# pragma endregion // Node Registration
} // namespace Metasound
2022-03-31 16:49:59 -04:00
# undef LOCTEXT_NAMESPACE //MetasoundBasicFilterNodes