Skip to content

Commit

Permalink
Split out distinct phasor types; one with reset, one without
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-thompson committed Sep 24, 2023
1 parent 2a47a46 commit 57d9ce3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
20 changes: 15 additions & 5 deletions js/packages/core/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,24 @@ export function accum(a, b, c?) {
}

// Phasor node
export function phasor(rate: ElemNode, reset: ElemNode): NodeRepr_t;
export function phasor(props: OptionalKeyProps, rate: ElemNode, reset: ElemNode): NodeRepr_t;
export function phasor(a, b, c?) {
export function phasor(rate: ElemNode): NodeRepr_t;
export function phasor(props: OptionalKeyProps, rate: ElemNode): NodeRepr_t;
export function phasor(a, b?) {
if (typeof a === "number" || isNode(a)) {
return createNode("phasor", {}, [resolve(a), resolve(b)]);
return createNode("phasor", {}, [resolve(a)]);
}

return createNode("phasor", a, [resolve(b), resolve(c)]);
return createNode("phasor", a, [resolve(b)]);
}

export function syncphasor(rate: ElemNode, reset: ElemNode): NodeRepr_t;
export function syncphasor(props: OptionalKeyProps, rate: ElemNode, reset: ElemNode): NodeRepr_t;
export function syncphasor(a, b, c?) {
if (typeof a === "number" || isNode(a)) {
return createNode("sphasor", {}, [resolve(a), resolve(b)]);
}

return createNode("sphasor", a, [resolve(b), resolve(c)]);
}

// Latch node
Expand Down
3 changes: 2 additions & 1 deletion runtime/DefaultNodeTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ namespace elem
// Core nodes
callback("root", GenericNodeFactory<RootNode<FloatType>>());
callback("const", GenericNodeFactory<ConstNode<FloatType>>());
callback("phasor", GenericNodeFactory<PhasorNode<FloatType>>());
callback("phasor", GenericNodeFactory<PhasorNode<FloatType, false>>());
callback("sphasor", GenericNodeFactory<PhasorNode<FloatType, true>>());
callback("sr", GenericNodeFactory<SampleRateNode<FloatType>>());
callback("seq", GenericNodeFactory<SequenceNode<FloatType>>());
callback("seq2", GenericNodeFactory<Seq2Node<FloatType>>());
Expand Down
41 changes: 23 additions & 18 deletions runtime/builtins/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace elem
std::atomic<int> channelIndex = -1;
};

template <typename FloatType>
template <typename FloatType, bool WithReset = false>
struct PhasorNode : public GraphNode<FloatType> {
using GraphNode<FloatType>::GraphNode;

Expand All @@ -97,27 +97,32 @@ namespace elem
auto numChannels = ctx.numInputChannels;
auto numSamples = ctx.numSamples;

// If we don't have the inputs we need, we bail here and zero the buffer
// hoping to prevent unexpected signals.
if (numChannels < 1)
return (void) std::fill_n(outputData, numSamples, FloatType(0));
if constexpr (WithReset) {
// If we don't have the inputs we need, we bail here and zero the buffer
// hoping to prevent unexpected signals.
if (numChannels < 2)
return (void) std::fill_n(outputData, numSamples, FloatType(0));

// Now if we have a second input channel, we treat that as the reset
// signal, hard syncing our phasor back to 0 when the reset signal goes high
if (numChannels >= 2) {
for (size_t i = 0; i < numSamples; ++i) {
auto const xn = inputData[0][i];
// The seocnd input in this mode is for hard syncing our phasor back
// to 0 when the reset signal goes high
for (size_t i = 0; i < numSamples; ++i) {
auto const xn = inputData[0][i];

if (change(inputData[1][i]) > FloatType(0.5)) {
phase = FloatType(0);
}
if (change(inputData[1][i]) > FloatType(0.5)) {
phase = FloatType(0);
}

outputData[i] = tick(xn);
}
outputData[i] = tick(xn);
}
} else {
for (size_t i = 0; i < numSamples; ++i) {
outputData[i] = tick(inputData[0][i]);
}
// If we don't have the inputs we need, we bail here and zero the buffer
// hoping to prevent unexpected signals.
if (numChannels < 1)
return (void) std::fill_n(outputData, numSamples, FloatType(0));

for (size_t i = 0; i < numSamples; ++i) {
outputData[i] = tick(inputData[0][i]);
}
}
}

Expand Down

0 comments on commit 57d9ce3

Please sign in to comment.