Skip to content

Commit 35a8213

Browse files
committed
Added range mapping from output to input
1 parent d6dd586 commit 35a8213

File tree

9 files changed

+101
-49
lines changed

9 files changed

+101
-49
lines changed

modules/foleys_dsp_magic/DSP/foleys_DspNode.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ void DspNode::addAudioInput (const juce::String& name)
2828
audioInputs.emplace_back (*this, ConnectionType::Audio, name, static_cast<int> (audioInputs.size()));
2929
}
3030

31-
void DspNode::addParameterInput (const juce::String& name)
31+
void DspNode::addParameterInput (const juce::String& name, float defaultValue, float minValue, float maxValue)
3232
{
33-
parameterInputs.emplace_back (*this, ConnectionType::Parameter, name, static_cast<int> (parameterInputs.size()));
33+
auto& newInput = parameterInputs.emplace_back (*this, ConnectionType::Parameter, name, static_cast<int> (parameterInputs.size()));
34+
newInput.defaultValue = defaultValue;
35+
newInput.minValue = minValue;
36+
newInput.maxValue = maxValue;
3437
}
3538

3639
void DspNode::addAudioOutput (const juce::String& name)
@@ -117,12 +120,12 @@ Input* DspNode::getInput (ConnectionType type, int index)
117120

118121
Input* DspNode::getInputChecked (ConnectionType type, int idx)
119122
{
120-
auto index = static_cast<size_t>(idx);
123+
auto index = static_cast<size_t> (idx);
121124
switch (type)
122125
{
123126
case ConnectionType::MIDI: return (hasMidiInput() ? &midiInput : nullptr);
124-
case ConnectionType::Audio: return (juce::isPositiveAndBelow(index, audioInputs.size()) ? &audioInputs[index] : nullptr);
125-
case ConnectionType::Parameter: return (juce::isPositiveAndBelow(index, parameterInputs.size()) ? &parameterInputs[index] : nullptr);
127+
case ConnectionType::Audio: return (juce::isPositiveAndBelow (index, audioInputs.size()) ? &audioInputs[index] : nullptr);
128+
case ConnectionType::Parameter: return (juce::isPositiveAndBelow (index, parameterInputs.size()) ? &parameterInputs[index] : nullptr);
126129
case ConnectionType::Invalid: return nullptr;
127130
default: jassertfalse; return nullptr;
128131
}

modules/foleys_dsp_magic/DSP/foleys_DspNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class DspNode
116116

117117
protected:
118118
void addAudioInput (const juce::String& name);
119-
void addParameterInput (const juce::String& name);
119+
void addParameterInput (const juce::String& name, float defaultValue, float minValue, float maxValue);
120120

121121
void addAudioOutput (const juce::String& name);
122122
void addParameterOutput (const juce::String& name);

modules/foleys_dsp_magic/DSP/foleys_Input.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ bool Input::isConnected() const
6060

6161
void Input::connect (int sourceUID, int sourceIdx)
6262
{
63-
sourceNode = targetNode.getProgram().getNodeWithUID(sourceUID);
63+
sourceNode = targetNode.getProgram().getNodeWithUID (sourceUID);
6464
sourceIndex = sourceIdx;
6565

6666
save();
@@ -82,6 +82,45 @@ Output* Input::getOutput() const
8282
return sourceNode->getOutput (type, sourceIndex);
8383
}
8484

85+
bool Input::isStatic() const
86+
{
87+
if (auto* output = getOutput())
88+
return output->isStatic();
89+
90+
return true;
91+
}
92+
93+
float Input::getStaticValue() const
94+
{
95+
if (auto* output = getOutput())
96+
{
97+
auto staticValue = output->getStaticValue();
98+
return (minValue + output->normalize (staticValue) * (maxValue - minValue));
99+
}
100+
101+
return defaultValue;
102+
}
103+
104+
void Input::mapOutput()
105+
{
106+
if (auto* output = getOutput())
107+
{
108+
auto srcMin = output->getRangeMin();
109+
auto srcMax = output->getRangeMax();
110+
111+
if (juce::approximatelyEqual (srcMin, minValue) && juce::approximatelyEqual (srcMax, maxValue))
112+
return;
113+
114+
auto block = output->getAudio();
115+
auto* samples = block.getChannelPointer (0);
116+
117+
for (size_t i = 0; i < block.getNumSamples(); ++i)
118+
{
119+
samples[i] = minValue + ((samples[i] - srcMin) / (srcMax - srcMin)) * (maxValue - minValue);
120+
}
121+
}
122+
}
123+
85124
juce::ValueTree Input::getConfigForInput()
86125
{
87126
for (const auto& child: targetNode.getConfig())

modules/foleys_dsp_magic/DSP/foleys_Input.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ enum class ConnectionType
2323

2424
struct Input
2525
{
26-
2726
Input (DspNode& owner, ConnectionType connectionType, const juce::String& name, int targetIndex = 0);
2827

29-
bool isConnected() const;
28+
[[nodiscard]] bool isConnected() const;
3029

3130
juce::ValueTree toValueTree();
3231

@@ -45,6 +44,13 @@ struct Input
4544
static juce::String getTypeName (ConnectionType type);
4645

4746
[[nodiscard]] Output* getOutput() const;
47+
[[nodiscard]] bool isStatic() const;
48+
[[nodiscard]] float getStaticValue() const;
49+
50+
/**
51+
* This maps the AudioBlock from the source range to the input range
52+
*/
53+
void mapOutput();
4854

4955
DspNode& targetNode;
5056
ConnectionType type = ConnectionType::Invalid;

modules/foleys_dsp_magic/Nodes/foleys_Biquad.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Biquad::Biquad (DspProgram& program, const juce::ValueTree& config) : DspNode (p
1212
addAudioInput (TRANS ("Audio In"));
1313
addAudioOutput (TRANS ("Audio Out"));
1414

15-
addParameterInput (TRANS ("Filter Type"));
16-
addParameterInput (TRANS ("Frequency"));
17-
addParameterInput (TRANS ("Gain"));
18-
addParameterInput (TRANS ("Quality"));
15+
addParameterInput (TRANS ("Type"), 0.0f, 0.0f, 5.0f);
16+
addParameterInput (TRANS ("Frequency"), 1000.0f, 20.0f, 20000.0f);
17+
addParameterInput (TRANS ("Gain"), 1.0f, 0.0f, 1.0f);
18+
addParameterInput (TRANS ("Q"), 1.0f, 0.01f, 10.0f);
1919
}
2020

2121
void Biquad::prepare ([[maybe_unused]] juce::dsp::ProcessSpec spec) { }

modules/foleys_dsp_magic/Nodes/foleys_Gain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace foleys::dsp
1111
Gain::Gain (DspProgram& program, const juce::ValueTree& config) : DspNode (program, config)
1212
{
1313
addAudioInput (TRANS ("Audio In"));
14-
addParameterInput (TRANS ("Gain"));
15-
addParameterInput (TRANS ("Phase"));
14+
addParameterInput (TRANS ("Gain"), 1.0f, 0.0f, 1.0f);
15+
addParameterInput (TRANS ("Phase"), 0.0f, 0.0f, 1.0f);
1616

1717
addAudioOutput (TRANS ("Audio Out"));
1818
}

modules/foleys_dsp_magic/Nodes/foleys_Multiplier.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ namespace foleys::dsp
1010

1111
Multiplier::Multiplier (DspProgram& program, const juce::ValueTree& config) : DspNode (program, config)
1212
{
13-
addParameterInput (TRANS ("Param In"));
14-
addParameterInput (TRANS ("Multiplier"));
15-
addParameterInput (TRANS ("DC Offset"));
16-
addParameterInput (TRANS ("Phase"));
13+
addParameterInput (TRANS ("Param In"), 0.0f, 0.0f, 1.0f);
14+
addParameterInput (TRANS ("Multiplier"), 1.0f, 0.0f, 1.0f);
15+
addParameterInput (TRANS ("DC Offset"), 0.0f, -1.0f, 1.0f);
16+
addParameterInput (TRANS ("Phase"), 0.0f, 0.0f, 1.0f);
1717

1818
addParameterOutput (TRANS ("Param Out"));
1919
}
@@ -27,10 +27,6 @@ void Multiplier::process ([[maybe_unused]] int numSamples)
2727

2828
output->setStaticValue (0.0);
2929

30-
bool phaseSwap = false;
31-
if (auto* phaseParameter = getConnectedOutput (ConnectionType::Parameter, 3))
32-
phaseSwap = phaseParameter->getStaticValue() < 0.5f;
33-
3430
if (auto* input = getConnectedOutput (ConnectionType::Parameter, 0))
3531
{
3632
output->setRange (input->getRangeMin(), input->getRangeMax());
@@ -42,8 +38,6 @@ void Multiplier::process ([[maybe_unused]] int numSamples)
4238
if (auto* multiplier = getConnectedOutput (ConnectionType::Parameter, 1))
4339
{
4440
multiplier->multiply (block);
45-
if (phaseSwap)
46-
block *= -1.0;
4741
}
4842
}
4943
else
@@ -55,37 +49,45 @@ void Multiplier::process ([[maybe_unused]] int numSamples)
5549
auto block = multiplier->getAudio();
5650
output->setAudioBlock (block);
5751
block *= input->getStaticValue();
58-
if (phaseSwap)
59-
block *= -1.0;
6052
}
6153
}
6254
}
6355
}
6456

65-
if (auto* dc = getConnectedOutput (ConnectionType::Parameter, 2))
57+
if (auto* dc = getInput (ConnectionType::Parameter, 2))
6658
{
6759
if (output->isStatic())
6860
{
6961
auto outputValue = output->getStaticValue();
7062
if (dc->isStatic())
7163
{
72-
if (phaseSwap)
73-
output->setStaticValue (outputValue * dc->getStaticValue() * -1.0f);
74-
else
75-
output->setStaticValue (outputValue * dc->getStaticValue());
64+
output->setStaticValue (outputValue * dc->getStaticValue());
7665
}
7766
else
7867
{
79-
auto block = dc->getAudio();
80-
block += outputValue;
81-
output->setAudioBlock (block);
82-
if (phaseSwap)
83-
block *= -1.0f;
68+
auto dcBlock = dc->getOutput()->getAudio();
69+
dcBlock += outputValue;
70+
output->setAudioBlock (dcBlock);
8471
}
8572
}
8673
else
8774
{
88-
dc->add (output->getAudio());
75+
auto outputBlock = output->getAudio();
76+
if (dc->isStatic())
77+
outputBlock += dc->getStaticValue();
78+
else
79+
outputBlock += dc->getOutput()->getAudio();
80+
}
81+
}
82+
83+
if (getInput (ConnectionType::Parameter, 3)->getStaticValue() > 0.5f)
84+
{
85+
if (output->isStatic())
86+
output->setStaticValue (output->getStaticValue() * -1.0f);
87+
else
88+
{
89+
auto block = output->getAudio();
90+
block *= -1.0f;
8991
}
9092
}
9193
}

modules/foleys_dsp_magic/Nodes/foleys_Oscillator.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Oscillator::Oscillator (DspProgram& program, const juce::ValueTree& config) : Ds
1616
addAudioOutput (TRANS ("Audio Out"));
1717
addParameterOutput (TRANS ("Signal Out"));
1818

19-
addParameterInput (TRANS ("Signal Type"));
20-
addParameterInput (TRANS ("Frequency"));
19+
addParameterInput (TRANS ("Signal Type"), 1.0f, 0.0f, 5.0f);
20+
addParameterInput (TRANS ("Frequency"), 440.0f, 20.0f, 20000.0f);
2121

2222
// make sure to repeat the first sample at the end for interpolation
2323
wavetable.resize (wavetablesize + 1, 0);
@@ -62,13 +62,12 @@ void Oscillator::process (int numSamples)
6262
buffer.clear();
6363
auto block = juce::dsp::AudioBlock<float> (buffer).getSubBlock (0, numSamples);
6464

65-
auto* signalType = getConnectedOutput (ConnectionType::Parameter, 0);
66-
auto* frequency = getConnectedOutput (ConnectionType::Parameter, 1);
65+
auto* frequency = getInput (ConnectionType::Parameter, 1);
66+
jassert (frequency);
6767

68-
if (signalType)
69-
setWaveType (static_cast<WaveType> (signalType->getStaticValue()));
68+
setWaveType (static_cast<WaveType> (getInput (ConnectionType::Parameter, 0)->getStaticValue()));
7069

71-
if (!frequency || currentWaveType == WaveType::None)
70+
if (currentWaveType == WaveType::None)
7271
{
7372
getOutput (ConnectionType::Audio, 0)->setStaticValue (0.0f);
7473
getOutput (ConnectionType::Parameter, 0)->setStaticValue (0.0f);
@@ -79,7 +78,7 @@ void Oscillator::process (int numSamples)
7978
auto* ptr = block.getChannelPointer (0);
8079
if (frequency->isStatic())
8180
{
82-
auto freq = frequency->normalize (frequency->getStaticValue());
81+
auto freq = frequency->getStaticValue();
8382
auto inc = std::clamp (static_cast<float> (freq / sampleRate), 0.0f, 1.0f);
8483
for (size_t i = 0; i < block.getNumSamples(); ++i)
8584
{
@@ -94,14 +93,17 @@ void Oscillator::process (int numSamples)
9493
}
9594
else
9695
{
96+
auto* frequencyInput = frequency->getOutput();
97+
frequency->mapOutput();
98+
auto* freqs = frequencyInput->getAudio().getChannelPointer (0);
99+
97100
for (size_t i = 0; i < block.getNumSamples(); ++i)
98101
{
99102
auto pos = phase * wavetablesize;
100103
auto p0 = static_cast<size_t> (pos);
101104
ptr[i] = std::lerp (wavetable[p0], wavetable[p0 + 1], pos - static_cast<float> (p0));
102105

103-
auto freq = frequency->getValueAt (i);
104-
phase += std::clamp (static_cast<float> (freq / sampleRate), 0.0f, 1.0f);
106+
phase += std::clamp (static_cast<float> (freqs[i] / sampleRate), 0.0f, 1.0f);
105107
if (phase >= 1.0f)
106108
phase -= 1.0f;
107109
}

modules/foleys_dsp_magic/Nodes/foleys_Oscilloscope.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Oscilloscope::Oscilloscope (DspProgram& program, const juce::ValueTree& config)
1212
addAudioInput (TRANS ("Audio In"));
1313
addAudioOutput (TRANS ("Audio Out"));
1414

15-
addParameterInput (TRANS ("Param In"));
15+
addParameterInput (TRANS ("Param In"), 0.0f, -1.0f, 1.0f);
1616

1717
auto& state = program.getMagicProcessorState();
1818
scope = state.getObjectWithType<MagicOscilloscope> (getName());

0 commit comments

Comments
 (0)