Skip to content

Commit

Permalink
Improving mod matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
FigBug committed Feb 6, 2024
1 parent df5170f commit 80aa533
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 9 deletions.
92 changes: 92 additions & 0 deletions modules/gin_plugin/plugin/gin_modmatrix.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
//==============================================================================
ModMatrix::Function strToFunc (const juce::String& str)
{
if (str == "linear") return ModMatrix::linear;
if (str == "quadraticIn") return ModMatrix::quadraticIn;
if (str == "quadraticInOut") return ModMatrix::quadraticInOut;
if (str == "quadraticOut") return ModMatrix::quadraticOut;
if (str == "sineIn") return ModMatrix::sineIn;
if (str == "sineInOut") return ModMatrix::sineInOut;
if (str == "sineOut") return ModMatrix::sineOut;
if (str == "exponentialIn") return ModMatrix::exponentialIn;
if (str == "exponentialInOut") return ModMatrix::exponentialInOut;
if (str == "exponentialOut") return ModMatrix::exponentialOut;
if (str == "invLinear") return ModMatrix::invLinear;
if (str == "invQuadraticIn") return ModMatrix::invQuadraticIn;
if (str == "invQuadraticInOut") return ModMatrix::invQuadraticInOut;
if (str == "invQuadraticOut") return ModMatrix::invQuadraticOut;
if (str == "invSineIn") return ModMatrix::invSineIn;
if (str == "invSineInOut") return ModMatrix::invSineInOut;
if (str == "invSineOut") return ModMatrix::invSineOut;
if (str == "invExponentialIn") return ModMatrix::invExponentialIn;
if (str == "invExponentialInOut") return ModMatrix::invExponentialInOut;
if (str == "invExponentialOut") return ModMatrix::invExponentialOut;

return ModMatrix::linear;
}

juce::String funcToStr (ModMatrix::Function f)
{
if (f == ModMatrix::linear) return "linear";
if (f == ModMatrix::quadraticIn) return "quadraticIn";
if (f == ModMatrix::quadraticInOut) return "quadraticInOut";
if (f == ModMatrix::quadraticOut) return "quadraticOut";
if (f == ModMatrix::sineIn) return "sineIn";
if (f == ModMatrix::sineInOut) return "sineInOut";
if (f == ModMatrix::sineOut) return "sineOut";
if (f == ModMatrix::exponentialIn) return "exponentialIn";
if (f == ModMatrix::exponentialInOut) return "exponentialInOut";
if (f == ModMatrix::exponentialOut) return "exponentialOut";
if (f == ModMatrix::invLinear) return "invLinear";
if (f == ModMatrix::invQuadraticIn) return "invQuadraticIn";
if (f == ModMatrix::invQuadraticInOut) return "invQuadraticInOut";
if (f == ModMatrix::invQuadraticOut) return "invQuadraticOut";
if (f == ModMatrix::invSineIn) return "invSineIn";
if (f == ModMatrix::invSineInOut) return "invSineInOut";
if (f == ModMatrix::invSineOut) return "invSineOut";
if (f == ModMatrix::invExponentialIn) return "invExponentialIn";
if (f == ModMatrix::invExponentialInOut) return "invExponentialInOut";
if (f == ModMatrix::invExponentialOut) return "invExponentialOut";

return "linear";
}

//==============================================================================
void ModVoice::startVoice ()
{
Expand Down Expand Up @@ -40,6 +93,7 @@ void ModMatrix::stateUpdated (const juce::ValueTree& vt)

auto f = float (c.getProperty ("depth", 0.0f));
auto e = bool (c.getProperty ("enabled", true));
auto z = strToFunc (c.getProperty ("function", "linear"));

if (src.isNotEmpty() && dst.isNotEmpty())
{
Expand All @@ -48,6 +102,7 @@ void ModMatrix::stateUpdated (const juce::ValueTree& vt)
s.poly = getModSrcPoly (s.id);
s.depth = f;
s.enabled = e;
s.function = z;

auto foundParam = false;
for (auto& pi : parameters)
Expand Down Expand Up @@ -86,6 +141,7 @@ void ModMatrix::updateState (juce::ValueTree& vt)
c.setProperty ("depth", src.depth, nullptr);
c.setProperty ("enabled", src.enabled, nullptr);
c.setProperty ("dstId", pi.parameter->getUid(), nullptr);
c.setProperty ("function", funcToStr (src.function), nullptr);

mm.addChild (c, -1, nullptr);
}
Expand Down Expand Up @@ -223,6 +279,16 @@ float ModMatrix::getModDepth (ModSrcId src, ModDstId param)
return 0;
}

ModMatrix::Function ModMatrix::getModFunction (ModSrcId src, ModDstId param)
{
auto& pi = parameters.getReference (param.id);
for (auto& si : pi.sources)
if (si.id == src)
return si.function;

return Function::linear;
}

std::vector<std::pair<ModSrcId, float>> ModMatrix::getModDepths (ModDstId param)
{
std::vector<std::pair<ModSrcId, float>> res;
Expand Down Expand Up @@ -259,6 +325,32 @@ void ModMatrix::setModDepth (ModSrcId src, ModDstId param, float f)
listeners.call ([&] (Listener& l) { l.modMatrixChanged(); });
}

void ModMatrix::setModFunction (ModSrcId src, ModDstId param, Function f)
{
auto& pi = parameters.getReference (param.id);
for (auto& si : pi.sources)
{
if (si.id == src)
{
si.function = f;

listeners.call ([&] (Listener& l) { l.modMatrixChanged(); });

return;
}
}

Source s;
s.id = src;
s.poly = getModSrcPoly (src);
s.depth = 1.0f;
s.function = f;

pi.sources.add (s);

listeners.call ([&] (Listener& l) { l.modMatrixChanged(); });
}

void ModMatrix::clearModDepth (ModSrcId src, ModDstId param)
{
auto& pi = parameters.getReference (param.id);
Expand Down
126 changes: 117 additions & 9 deletions modules/gin_plugin/plugin/gin_modmatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,110 @@ class ModMatrix
//==============================================================================
void stateUpdated (const juce::ValueTree& vt);
void updateState (juce::ValueTree& vt);

//==============================================================================
enum Function
{
linear,

quadraticIn,
quadraticInOut,
quadraticOut,

sineIn,
sineInOut,
sineOut,

exponentialIn,
exponentialInOut,
exponentialOut,

invLinear,

invQuadraticIn,
invQuadraticInOut,
invQuadraticOut,

invSineIn,
invSineInOut,
invSineOut,

invExponentialIn,
invExponentialInOut,
invExponentialOut,
};

float shape (float v, Function f, bool bipolar)
{
if (bipolar)
v = juce::jmap (v, -1.0f, 1.0f, 0.0f, 1.0f);

switch (f)
{
case invLinear:
case invQuadraticIn:
case invQuadraticInOut:
case invQuadraticOut:
case invSineIn:
case invSineInOut:
case invSineOut:
case invExponentialIn:
case invExponentialInOut:
case invExponentialOut:
v = 1.0f - v;
break;
default:
break;
}

switch (f)
{
case linear:
case invLinear:
break;
case quadraticIn:
case invQuadraticIn:
v = easeQuadraticIn (v);
break;
case quadraticInOut:
case invQuadraticInOut:
v = easeQuadraticInOut (v);
break;
case quadraticOut:
case invQuadraticOut:
v = easeQuadraticOut (v);
break;
case sineIn:
case invSineIn:
v = easeSineIn (v);
break;
case sineInOut:
case invSineInOut:
v = easeSineInOut (v);
break;
case sineOut:
case invSineOut:
v = easeSineOut (v);
break;
case exponentialIn:
case invExponentialIn:
v = easeExponentialIn (v);
break;
case exponentialInOut:
case invExponentialInOut:
v = easeExponentialInOut (v);
break;
case exponentialOut:
case invExponentialOut:
v = easeExponentialOut (v);
break;
}

if (bipolar)
v = juce::jmap (v, 0.0f, 1.0f, -1.0f, 1.0f);

return v;
}

//==============================================================================
float getValue (gin::Parameter* p)
Expand All @@ -103,9 +207,9 @@ class ModMatrix
if (src.enabled)
{
if (src.poly && activeVoice != nullptr)
base += activeVoice->values[src.id.id] * src.depth;
base += shape (activeVoice->values[src.id.id], src.function, sources[src.id.id].bipolar) * src.depth;
else if (! src.poly)
base += sources[src.id.id].monoValue * src.depth;
base += shape (sources[src.id.id].monoValue, src.function, sources[src.id.id].bipolar) * src.depth;
}
}

Expand Down Expand Up @@ -136,9 +240,9 @@ class ModMatrix
if (src.enabled)
{
if (src.poly)
base += voice.values[src.id.id] * src.depth;
base += shape (voice.values[src.id.id], src.function, sources[src.id.id].bipolar) * src.depth;
else
base += sources[src.id.id].monoValue * src.depth;
base += shape (sources[src.id.id].monoValue, src.function, sources[src.id.id].bipolar) * src.depth;
}
}

Expand Down Expand Up @@ -179,9 +283,9 @@ class ModMatrix
if (src.enabled)
{
if (src.poly)
base += v->values[src.id.id] * src.depth;
base += shape (v->values[src.id.id], src.function, sources[src.id.id].bipolar) * src.depth;
else
base += sources[src.id.id].monoValue * src.depth;
base += shape (sources[src.id.id].monoValue, src.function, sources[src.id.id].bipolar) * src.depth;
ok = true;
}
}
Expand All @@ -203,7 +307,7 @@ class ModMatrix
{
if (src.enabled && ! src.poly)
{
base += sources[src.id.id].monoValue * src.depth;
base += shape (sources[src.id.id].monoValue, src.function, sources[src.id.id].bipolar) * src.depth;
ok = true;
}
}
Expand All @@ -230,12 +334,12 @@ class ModMatrix
if (src.poly && v != nullptr)
{
ok = true;
base += v->values[src.id.id] * src.depth;
base += shape (v->values[src.id.id], src.function, sources[src.id.id].bipolar) * src.depth;
}
else if (! src.poly)
{
ok = true;
base += sources[src.id.id].monoValue * src.depth;
base += shape (sources[src.id.id].monoValue, src.function, sources[src.id.id].bipolar) * src.depth;
}
}
}
Expand Down Expand Up @@ -298,6 +402,9 @@ class ModMatrix
std::vector<std::pair<ModSrcId, float>> getModDepths (ModDstId param);
void setModDepth (ModSrcId src, ModDstId param, float f);
void clearModDepth (ModSrcId src, ModDstId param);

Function getModFunction (ModSrcId src, ModDstId param);
void setModFunction (ModSrcId src, ModDstId param, Function f);

bool getModEnable (ModSrcId src, ModDstId param);
void setModEnable (ModSrcId src, ModDstId param, bool b);
Expand Down Expand Up @@ -358,6 +465,7 @@ class ModMatrix
bool poly = false;
bool enabled = true;
float depth = 0.0f;
Function function;
};

struct ParamInfo
Expand Down

0 comments on commit 80aa533

Please sign in to comment.