2023-03-25 20:24:10 +00:00
|
|
|
#include "Effect.h"
|
2023-07-20 20:41:53 +00:00
|
|
|
#include <numbers>
|
2023-03-25 20:24:10 +00:00
|
|
|
|
2023-12-17 21:32:56 +00:00
|
|
|
Effect::Effect(std::shared_ptr<EffectApplication> effectApplication, const std::vector<EffectParameter*>& parameters) :
|
2023-11-25 18:45:04 +00:00
|
|
|
effectApplication(effectApplication),
|
|
|
|
parameters(parameters),
|
|
|
|
enabled(nullptr),
|
2023-12-17 21:32:56 +00:00
|
|
|
actualValues(std::vector<double>(parameters.size(), 0.0)) {}
|
2023-07-11 12:32:52 +00:00
|
|
|
|
2023-12-17 21:32:56 +00:00
|
|
|
Effect::Effect(std::shared_ptr<EffectApplication> effectApplication, EffectParameter* parameter) : Effect(effectApplication, std::vector<EffectParameter*>{parameter}) {}
|
2023-03-28 12:44:46 +00:00
|
|
|
|
2024-01-01 15:09:46 +00:00
|
|
|
Effect::Effect(EffectApplicationType application, const std::vector<EffectParameter*>& parameters) :
|
2023-11-25 18:45:04 +00:00
|
|
|
application(application),
|
|
|
|
parameters(parameters),
|
|
|
|
enabled(nullptr),
|
2023-12-17 21:32:56 +00:00
|
|
|
actualValues(std::vector<double>(parameters.size(), 0.0)) {}
|
2023-07-04 19:47:54 +00:00
|
|
|
|
2024-01-01 15:09:46 +00:00
|
|
|
Effect::Effect(EffectApplicationType application, EffectParameter* parameter) : Effect(application, std::vector<EffectParameter*>{parameter}) {}
|
2023-07-05 11:02:28 +00:00
|
|
|
|
2024-01-07 16:17:20 +00:00
|
|
|
Point Effect::apply(int index, Point input, double volume) {
|
2024-01-01 15:09:46 +00:00
|
|
|
animateValues(volume);
|
2023-07-05 11:02:28 +00:00
|
|
|
if (application) {
|
2023-07-20 20:41:53 +00:00
|
|
|
return application(index, input, actualValues, sampleRate);
|
2023-07-05 11:02:28 +00:00
|
|
|
} else if (effectApplication != nullptr) {
|
2023-07-20 20:41:53 +00:00
|
|
|
return effectApplication->apply(index, input, actualValues, sampleRate);
|
2023-07-04 19:47:54 +00:00
|
|
|
}
|
2023-07-05 11:02:28 +00:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2024-01-01 15:09:46 +00:00
|
|
|
void Effect::animateValues(double volume) {
|
2023-07-20 20:41:53 +00:00
|
|
|
for (int i = 0; i < parameters.size(); i++) {
|
|
|
|
auto parameter = parameters[i];
|
|
|
|
float minValue = parameter->min;
|
|
|
|
float maxValue = parameter->max;
|
2023-07-22 17:42:30 +00:00
|
|
|
bool lfoEnabled = parameter->lfo != nullptr && parameter->lfo->getValueUnnormalised() != (int)LfoType::Static;
|
|
|
|
float phase = lfoEnabled ? nextPhase(parameter) : 0.0;
|
2023-07-20 20:41:53 +00:00
|
|
|
float percentage = phase / (2 * std::numbers::pi);
|
2023-07-22 17:42:30 +00:00
|
|
|
LfoType type = lfoEnabled ? (LfoType)(int)parameter->lfo->getValueUnnormalised() : LfoType::Static;
|
2023-07-20 20:41:53 +00:00
|
|
|
|
2023-09-01 18:52:36 +00:00
|
|
|
switch (type) {
|
|
|
|
case LfoType::Sine:
|
|
|
|
actualValues[i] = std::sin(phase) * 0.5 + 0.5;
|
|
|
|
actualValues[i] = actualValues[i] * (maxValue - minValue) + minValue;
|
|
|
|
break;
|
|
|
|
case LfoType::Square:
|
|
|
|
actualValues[i] = (percentage < 0.5) ? maxValue : minValue;
|
|
|
|
break;
|
|
|
|
case LfoType::Seesaw:
|
|
|
|
// modified sigmoid function
|
|
|
|
actualValues[i] = (percentage < 0.5) ? percentage * 2 : (1 - percentage) * 2;
|
|
|
|
actualValues[i] = 1 / (1 + std::exp(-16 * (actualValues[i] - 0.5)));
|
|
|
|
actualValues[i] = actualValues[i] * (maxValue - minValue) + minValue;
|
|
|
|
break;
|
|
|
|
case LfoType::Triangle:
|
|
|
|
actualValues[i] = (percentage < 0.5) ? percentage * 2 : (1 - percentage) * 2;
|
|
|
|
actualValues[i] = actualValues[i] * (maxValue - minValue) + minValue;
|
|
|
|
break;
|
|
|
|
case LfoType::Sawtooth:
|
|
|
|
actualValues[i] = percentage * (maxValue - minValue) + minValue;
|
|
|
|
break;
|
|
|
|
case LfoType::ReverseSawtooth:
|
|
|
|
actualValues[i] = (1 - percentage) * (maxValue - minValue) + minValue;
|
|
|
|
break;
|
|
|
|
case LfoType::Noise:
|
|
|
|
actualValues[i] = ((float)rand() / RAND_MAX) * (maxValue - minValue) + minValue;
|
|
|
|
break;
|
2023-07-20 20:41:53 +00:00
|
|
|
default:
|
|
|
|
double weight = parameter->smoothValueChange ? 0.0005 : 1.0;
|
2024-01-01 15:09:46 +00:00
|
|
|
double newValue;
|
2024-01-01 16:21:10 +00:00
|
|
|
if (parameter->sidechain != nullptr && parameter->sidechain->getBoolValue()) {
|
2024-01-01 15:09:46 +00:00
|
|
|
newValue = volume * (maxValue - minValue) + minValue;
|
|
|
|
} else {
|
|
|
|
newValue = parameter->getValueUnnormalised();
|
|
|
|
}
|
|
|
|
actualValues[i] = (1.0 - weight) * actualValues[i] + weight * newValue;
|
2023-09-01 18:52:36 +00:00
|
|
|
break;
|
2023-07-20 20:41:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// should only be the audio thread calling this, but either way it's not a big deal
|
|
|
|
float Effect::nextPhase(EffectParameter* parameter) {
|
2023-09-01 18:52:36 +00:00
|
|
|
parameter->phase = parameter->phase + parameter->lfoRate->getValueUnnormalised() / sampleRate;
|
2023-07-20 20:41:53 +00:00
|
|
|
|
|
|
|
if (parameter->phase > 1) {
|
2023-09-01 18:52:36 +00:00
|
|
|
parameter->phase = parameter->phase - 1;
|
2023-07-20 20:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return parameter->phase * 2 * std::numbers::pi;
|
|
|
|
}
|
|
|
|
|
2023-07-05 11:02:28 +00:00
|
|
|
void Effect::apply() {
|
2024-01-07 16:17:20 +00:00
|
|
|
apply(0, Point());
|
2023-03-28 12:44:46 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 12:32:52 +00:00
|
|
|
double Effect::getValue(int index) {
|
2023-07-18 18:20:54 +00:00
|
|
|
return parameters[index]->getValueUnnormalised();
|
2023-07-11 12:32:52 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 12:44:46 +00:00
|
|
|
double Effect::getValue() {
|
2023-07-11 12:32:52 +00:00
|
|
|
return getValue(0);
|
|
|
|
}
|
|
|
|
|
2023-08-28 21:06:21 +00:00
|
|
|
// Not thread safe! Should only be called from the audio thread
|
|
|
|
double Effect::getActualValue(int index) {
|
|
|
|
return actualValues[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not thread safe! Should only be called from the audio thread
|
|
|
|
double Effect::getActualValue() {
|
|
|
|
return actualValues[0];
|
|
|
|
}
|
|
|
|
|
2023-07-11 12:32:52 +00:00
|
|
|
void Effect::setValue(int index, double value) {
|
2023-07-18 18:20:54 +00:00
|
|
|
parameters[index]->setUnnormalisedValueNotifyingHost(value);
|
2023-03-28 12:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Effect::setValue(double value) {
|
2023-07-11 12:32:52 +00:00
|
|
|
setValue(0, value);
|
2023-03-28 12:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Effect::getPrecedence() {
|
|
|
|
return precedence;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Effect::setPrecedence(int precedence) {
|
|
|
|
this->precedence = precedence;
|
|
|
|
}
|
|
|
|
|
2023-07-18 17:28:09 +00:00
|
|
|
void Effect::addListener(int index, juce::AudioProcessorParameter::Listener* listener) {
|
2023-07-18 18:20:54 +00:00
|
|
|
parameters[index]->addListener(listener);
|
2023-07-21 10:08:55 +00:00
|
|
|
if (parameters[index]->lfo != nullptr) {
|
|
|
|
parameters[index]->lfo->addListener(listener);
|
|
|
|
}
|
|
|
|
if (parameters[index]->lfoRate != nullptr) {
|
|
|
|
parameters[index]->lfoRate->addListener(listener);
|
|
|
|
}
|
2023-07-18 18:20:54 +00:00
|
|
|
if (enabled != nullptr) {
|
|
|
|
enabled->addListener(listener);
|
|
|
|
}
|
2024-01-01 16:21:10 +00:00
|
|
|
if (parameters[index]->sidechain != nullptr) {
|
|
|
|
parameters[index]->sidechain->addListener(listener);
|
|
|
|
}
|
2023-07-18 17:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Effect::removeListener(int index, juce::AudioProcessorParameter::Listener* listener) {
|
2024-01-01 16:21:10 +00:00
|
|
|
if (parameters[index]->sidechain != nullptr) {
|
|
|
|
parameters[index]->sidechain->removeListener(listener);
|
|
|
|
}
|
2023-07-18 18:20:54 +00:00
|
|
|
if (enabled != nullptr) {
|
|
|
|
enabled->removeListener(listener);
|
|
|
|
}
|
2023-07-21 10:08:55 +00:00
|
|
|
if (parameters[index]->lfoRate != nullptr) {
|
|
|
|
parameters[index]->lfoRate->removeListener(listener);
|
|
|
|
}
|
|
|
|
if (parameters[index]->lfo != nullptr) {
|
|
|
|
parameters[index]->lfo->removeListener(listener);
|
|
|
|
}
|
2023-07-20 19:01:09 +00:00
|
|
|
parameters[index]->removeListener(listener);
|
2023-07-18 18:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Effect::markEnableable(bool enable) {
|
|
|
|
if (enabled != nullptr) {
|
|
|
|
enabled->setValue(enable);
|
|
|
|
} else {
|
2023-09-01 18:52:36 +00:00
|
|
|
enabled = new BooleanParameter(getName() + " Enabled", getId() + "Enabled", parameters[0]->getVersionHint(), enable);
|
2023-07-18 18:20:54 +00:00
|
|
|
}
|
2023-07-18 17:28:09 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 12:32:52 +00:00
|
|
|
juce::String Effect::getId() {
|
2023-07-21 10:08:55 +00:00
|
|
|
return parameters[0]->paramID;
|
2023-03-28 12:44:46 +00:00
|
|
|
}
|
2023-03-25 20:24:10 +00:00
|
|
|
|
2023-07-11 12:32:52 +00:00
|
|
|
juce::String Effect::getName() {
|
2023-07-18 18:20:54 +00:00
|
|
|
return parameters[0]->name;
|
2023-03-25 20:24:10 +00:00
|
|
|
}
|
2023-07-25 11:23:27 +00:00
|
|
|
|
|
|
|
void Effect::save(juce::XmlElement* xml) {
|
2023-09-01 18:52:36 +00:00
|
|
|
if (enabled != nullptr) {
|
|
|
|
auto enabledXml = xml->createNewChildElement("enabled");
|
|
|
|
enabled->save(enabledXml);
|
|
|
|
}
|
|
|
|
xml->setAttribute("id", getId());
|
|
|
|
xml->setAttribute("precedence", precedence);
|
|
|
|
for (auto parameter : parameters) {
|
|
|
|
parameter->save(xml->createNewChildElement("parameter"));
|
2023-07-25 11:23:27 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-25 13:09:21 +00:00
|
|
|
|
|
|
|
void Effect::load(juce::XmlElement* xml) {
|
|
|
|
if (enabled != nullptr) {
|
|
|
|
auto enabledXml = xml->getChildByName("enabled");
|
|
|
|
if (enabledXml != nullptr) {
|
|
|
|
enabled->load(enabledXml);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (xml->hasAttribute("precedence")) {
|
|
|
|
setPrecedence(xml->getIntAttribute("precedence"));
|
|
|
|
}
|
|
|
|
for (auto parameterXml : xml->getChildIterator()) {
|
|
|
|
auto parameter = getParameter(parameterXml->getStringAttribute("id"));
|
|
|
|
if (parameter != nullptr) {
|
|
|
|
parameter->load(parameterXml);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectParameter* Effect::getParameter(juce::String id) {
|
|
|
|
for (auto parameter : parameters) {
|
|
|
|
if (parameter->paramID == id) {
|
|
|
|
return parameter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-12-21 21:33:05 +00:00
|
|
|
|
|
|
|
void Effect::updateSampleRate(int sampleRate) {
|
|
|
|
this->sampleRate = sampleRate;
|
|
|
|
}
|