Skip to content

Commit

Permalink
Use one common node data iterator. Must distruinguish between constan…
Browse files Browse the repository at this point in the history
…t and array buffers during init where set input is never used, for example in source op time. Structured definitions better.
  • Loading branch information
linuscu committed Sep 22, 2024
1 parent 335b8b3 commit a189da6
Show file tree
Hide file tree
Showing 20 changed files with 316 additions and 218 deletions.
4 changes: 2 additions & 2 deletions packages/audio/include/audio/AudioUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ namespace l::audio {
}

FilterRWA<T>& SetConvergenceInMs(T convergenceInMS, T limit = static_cast<T>(0.0001), T sampleRate = static_cast<T>(44100.0)) {
mSmooth = GetRWAFactorFromMS(convergenceInMS, limit, mRWAUpdateRate, sampleRate);
mSmoothSkewed = GetRWAFactorFromMSSkewed(convergenceInMS, limit, mRWAUpdateRate, sampleRate);
mSmooth = GetRWAFactorFromMS(convergenceInMS, limit, mRWAUpdateRate, sampleRate / mRWAUpdateRate);
mSmoothSkewed = GetRWAFactorFromMSSkewed(convergenceInMS, limit, mRWAUpdateRate, sampleRate / mRWAUpdateRate);
return *this;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/audio/source/common/AudioUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ namespace l::audio {
return numAudioSamples * 1000.0f / sampleRate;
}

float GetRWAFactorFromMS(float ms, float limit, float , float sampleRate) {
int32_t updateSteps = GetAudioTicksFromMS(ms, sampleRate);
float GetRWAFactorFromMS(float ms, float limit, float rwaUpdateRate, float sampleRate) {
int32_t updateSteps = GetAudioTicksFromMS(ms, sampleRate / rwaUpdateRate);
return l::math::tween::GetRWAFactor(updateSteps, limit);
}

float GetRWAFactorFromMSSkewed(float ms, float limit, float, float sampleRate) {
float GetRWAFactorFromMSSkewed(float ms, float limit, float rwaUpdateRate, float sampleRate) {
float msRoot = l::math::functions::sqrt(ms);
int32_t steps = GetAudioTicksFromMS(msRoot, sampleRate);
int32_t steps = GetAudioTicksFromMS(msRoot, sampleRate / rwaUpdateRate);
float factor = l::math::tween::GetRWAFactor(steps, limit);
factor *= factor;
return factor;
Expand Down
14 changes: 4 additions & 10 deletions packages/nodegraph/include/nodegraph/core/NodeGraphBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "math/MathConstants.h"

#include "nodegraph/core/NodeGraphData.h"
#include "nodegraph/core/NodeGraphInput.h"
#include "nodegraph/core/NodeGraphOutput.h"

Expand All @@ -19,21 +20,13 @@ namespace l::nodegraph {
int32_t CreateUniqueId();
bool IsValidInOutNum(int8_t inoutNum, size_t inoutSize);

enum class DataType {
FLOAT32,
INT32,
BITFIELD32
};

class NodeGraphGroup;


/**********************************************************************************/
class NodeGraphBase {
public:
NodeGraphBase(OutputType outputType) : mId(CreateUniqueId()), mOutputType(outputType) {
mInputs.resize(1);
mOutputs.resize(1);
}
virtual ~NodeGraphBase() {
LOG(LogInfo) << "Node graph base destroyed";
Expand All @@ -52,6 +45,8 @@ namespace l::nodegraph {

virtual float& GetInput(int8_t inputChannel, int32_t size = 1);
virtual float& GetOutput(int8_t outputChannel, int32_t size = 1);
virtual NodeGraphInput& GetInputOf(int8_t inputChannel);
virtual NodeGraphOutput& GetOutputOf(int8_t outputChannel);

virtual int32_t GetInputSize(int8_t inputChannel);
virtual int32_t GetOutputSize(int8_t outputChannel);
Expand All @@ -66,7 +61,7 @@ namespace l::nodegraph {

virtual bool SetInput(int8_t inputChannel, NodeGraphBase& source, int8_t sourceOutputChannel);
virtual bool SetInput(int8_t inputChannel, NodeGraphGroup& source, int8_t sourceOutputChannel);
virtual bool SetInput(int8_t inputChannel, float constant, int32_t size = 1);
virtual bool SetInput(int8_t inputChannel, float initialValue, int32_t size = 1);
virtual bool SetInput(int8_t inputChannel, float* floatPtr);
virtual void SetDefaultOutput(int8_t outputChannel, float constant, int32_t size = 1);

Expand Down Expand Up @@ -220,7 +215,6 @@ namespace l::nodegraph {
T mOperation;
};

/**********************************************************************************/

class NodeInputManager {
public:
Expand Down
108 changes: 108 additions & 0 deletions packages/nodegraph/include/nodegraph/core/NodeGraphData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#pragma once

#include "logging/LoggingAll.h"

#include <string>
#include <vector>
#include <map>
#include <typeinfo>
#include <type_traits>
#include <memory>

#include "math/MathAll.h"
#include "audio/AudioUtils.h"

namespace l::nodegraph {

enum class InputType {
INPUT_EMPTY,
INPUT_NODE,
INPUT_CONSTANT,
INPUT_VALUE,
INPUT_ARRAY
};

enum class InputBound {
INPUT_UNBOUNDED,
INPUT_0_TO_1,
INPUT_0_TO_2,
INPUT_NEG_1_POS_1,
INPUT_0_100,
INPUT_CUSTOM,
};

enum class OutputType {
Default, // node will be processed if it is connected to the groups output by some route
ExternalOutput, // node does not have meaningful output for other nodes but should still be processed (ex speaker output only has input)
ExternalVisualOutput,
};

std::pair<float, float> GetInputBounds(InputBound bound);

class NodeGraphBase;

union Input {
NodeGraphBase* mInputNode = nullptr;
float* mInputFloat;
float mInputFloatConstant;
};

/**********************************************************************************/
class NodeDataIterator {
public:
NodeDataIterator(float* data = nullptr, float stepPerIndex = 1.0f) {
mData = data;
mIncrement = stepPerIndex;
mIndex = mIncrement * 0.5f; // warmstart accumulator half a step to avoid rounding down (to previous index) close to whole integers because of floating point inprecision
}
~NodeDataIterator() = default;

float& operator*() {
return *(mData + static_cast<int32_t>(mIndex));
}
float* operator++(int) {
auto p = mData + static_cast<int32_t>(mIndex);
mIndex += mIncrement;
return p;
}
float& operator[](int offset) {
return *(mData + static_cast<int32_t>(offset * offset));
}
float* data() {
return mData;
}
void Reset(float* data, float stepPerIndex = 1.0f) {
mData = data;
mIncrement = stepPerIndex;
}
protected:
int32_t mSize = 0;
float mIndex = 0.0f;
float* mData = nullptr;
float mIncrement = 0.0f;
};

/*********************************************************************************/
enum class InputTypeBase {
SAMPLED = 0, // todo: make it interpolate in smaller custom buffers
//SAMPLED_RWA = 0, // todo: add a smoothed sampled variant. Will replace interp_rwa and interp_rwa_ms as we will add a separate config for passing ticks or millis
INTERP_RWA, // TBR
INTERP_RWA_MS, // TBR
CONSTANT_VALUE, // Will basically be replace by sampled as it should be able to handle 1-sized arrays
CONSTANT_ARRAY, // Same here, will be replaced
CUSTOM_INTERP_TWEEN, // custom input vars should not be used at all
CUSTOM_INTERP_TWEEN_MS,
CUSTOM_INTERP_RWA_MS
};

union InputUnion {
l::audio::FilterRWAFloat mFilterRWA;
l::math::tween::DynamicTween mTween;
NodeDataIterator mIterator;

InputUnion() : mFilterRWA() {}
~InputUnion() = default;
};

}

98 changes: 7 additions & 91 deletions packages/nodegraph/include/nodegraph/core/NodeGraphInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,9 @@
#include "math/MathAll.h"
#include "audio/AudioUtils.h"

namespace l::nodegraph {

enum class InputType {
INPUT_EMPTY,
INPUT_NODE,
INPUT_CONSTANT,
INPUT_VALUE,
INPUT_ARRAY
};

enum class InputBound {
INPUT_UNBOUNDED,
INPUT_0_TO_1,
INPUT_0_TO_2,
INPUT_NEG_1_POS_1,
INPUT_0_100,
INPUT_CUSTOM,
};

std::pair<float, float> GetInputBounds(InputBound bound);

class NodeGraphBase;

union Input {
NodeGraphBase* mInputNode = nullptr;
float* mInputFloat;
float mInputFloatConstant;
};

/*********************************************************************************/

class NodeInputDataIterator {
public:
NodeInputDataIterator(float* data = nullptr, int32_t size = 0) {
mData = data;
mSize = size;
mIncrement = size > 1 ? 1 : 0;
}
~NodeInputDataIterator() = default;

float& operator*() {
return *mData;
}
float* operator++(int) {
float* data = mData;
mData += mIncrement;
return data;
}
float operator[](int index) {
return mData[index];
}
float* data() {
return mData;
}
void Reset(float* data, int32_t size) {
mData = data;
mSize = size;
mIncrement = size > 1 ? 1 : 0;
}

protected:
float* mData = nullptr;
int32_t mSize = 0;
int32_t mIncrement = 0;
};
#include "nodegraph/core/NodeGraphData.h"

/*********************************************************************************/
namespace l::nodegraph {

class NodeGraphInput {
public:
Expand All @@ -93,37 +29,17 @@ namespace l::nodegraph {

// hack to get input buffers working
std::unique_ptr<std::vector<float>> mInputBuf = nullptr;
float mInputLod = 1.0f; // buffer size level of detail value[1.0f, buffer size] (if 1 it will write all generated values to the buffer, if 'buffer size' it will only have the latest written value),

void Reset();
bool HasInputNode();
float& Get(int32_t numSamples = 1);
NodeInputDataIterator GetBufferIterator(int32_t numSamples = 1);
NodeInputDataIterator GetArrayIterator();
float& Get(int32_t size = 1);
NodeDataIterator GetIterator(int32_t size = 1, float lod = 1.0f);
NodeDataIterator GetArrayIterator();
int32_t GetSize();
};


/****************************************************************************************/
enum class InputTypeBase {
SAMPLED = 0,
INTERP_RWA,
INTERP_RWA_MS,
CONSTANT_VALUE,
CONSTANT_ARRAY,
CUSTOM_INTERP_TWEEN,
CUSTOM_INTERP_TWEEN_MS,
CUSTOM_INTERP_RWA_MS
};

union InputUnion {
l::audio::FilterRWAFloat mFilterRWA;
l::math::tween::DynamicTween mTween;
NodeInputDataIterator mIterator;

InputUnion() : mFilterRWA() {}
~InputUnion() = default;
};

/*********************************************************************************/

class NodeGraphInputAccessor {
Expand All @@ -148,7 +64,7 @@ namespace l::nodegraph {
case InputTypeBase::SAMPLED:
case InputTypeBase::CONSTANT_VALUE:
case InputTypeBase::CONSTANT_ARRAY:
new (&mInput.mIterator) NodeInputDataIterator();
new (&mInput.mIterator) NodeDataIterator(nullptr);
//mInput.mIterator = NodeInputDataIterator();
break;
}
Expand Down
34 changes: 5 additions & 29 deletions packages/nodegraph/include/nodegraph/core/NodeGraphOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,23 @@

#include "math/MathConstants.h"

namespace l::nodegraph {

enum class OutputType {
Default, // node will be processed if it is connected to the groups output by some route
ExternalOutput, // node does not have meaningful output for other nodes but should still be processed (ex speaker output only has input)
ExternalVisualOutput,
};

#include "nodegraph/core/NodeGraphData.h"

class NodeOutputDataIterator {
public:
NodeOutputDataIterator(float* data, int32_t size) {
mData = data;
mSize = size;
mIncrement = size > 1 ? 1 : 0;
}
float& operator*() {
return *mData;
}
float* operator++(int) {
float* data = mData;
mData += mIncrement;
return data;
}
protected:
float* mData = nullptr;
int32_t mSize = 0;
int32_t mIncrement = 0;
};
namespace l::nodegraph {

class NodeGraphOutput {
public:
NodeGraphOutput() = default;

float mOutput = 0.0f;
float mOutputLod = 1.0f; // buffer size level of detail value[1.0f, buffer size] (if 1 it will write all generated values to the buffer, if 'buffer size' it will only have the latest written value),
std::unique_ptr<std::vector<float>> mOutputBuf = nullptr;
std::unique_ptr<std::string> mName = nullptr;
bool mOutputPolled = false;

float& Get(int32_t size = 1);
NodeOutputDataIterator GetIterator(int32_t numSamples = 1);
NodeDataIterator GetIterator(int32_t size, float lod = 1.0f);
NodeDataIterator GetIterator();
int32_t GetSize();
bool IsPolled();
void ResetPollState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ namespace l::nodegraph {
class GraphControlArpeggio: public NodeGraphOp {
public:

const float gArpeggioUpdateRate = 16.0f;

const static int32_t gPolyphony = 12;
GraphControlArpeggio(NodeGraphBase* node) :
NodeGraphOp(node, "Arpeggio"),
Expand Down
Loading

0 comments on commit a189da6

Please sign in to comment.