osci-render/Source/audio/Effect.cpp

220 wiersze
6.8 KiB
C++
Czysty Zwykły widok Historia

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-12-17 21:32:56 +00:00
Effect::Effect(std::shared_ptr<EffectApplication> effectApplication, EffectParameter* parameter) : Effect(effectApplication, std::vector<EffectParameter*>{parameter}) {}
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)) {}
Effect::Effect(EffectApplicationType application, EffectParameter* parameter) : Effect(application, std::vector<EffectParameter*>{parameter}) {}
2024-01-07 16:17:20 +00:00
Point Effect::apply(int index, Point input, double volume) {
animateValues(volume);
if (application) {
2023-07-20 20:41:53 +00:00
return application(index, input, actualValues, sampleRate);
} else if (effectApplication != nullptr) {
2023-07-20 20:41:53 +00:00
return effectApplication->apply(index, input, actualValues, sampleRate);
}
return input;
}
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;
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);
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;
double newValue;
2024-01-01 16:21:10 +00:00
if (parameter->sidechain != nullptr && parameter->sidechain->getBoolValue()) {
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;
}
void Effect::apply() {
2024-01-07 16:17:20 +00:00
apply(0, Point());
}
double Effect::getValue(int index) {
return parameters[index]->getValueUnnormalised();
}
double Effect::getValue() {
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];
}
void Effect::setValue(int index, double value) {
parameters[index]->setUnnormalisedValueNotifyingHost(value);
}
void Effect::setValue(double value) {
setValue(0, value);
}
int Effect::getPrecedence() {
return precedence;
}
void Effect::setPrecedence(int precedence) {
this->precedence = precedence;
}
void Effect::addListener(int index, juce::AudioProcessorParameter::Listener* listener) {
parameters[index]->addListener(listener);
if (parameters[index]->lfo != nullptr) {
parameters[index]->lfo->addListener(listener);
}
if (parameters[index]->lfoRate != nullptr) {
parameters[index]->lfoRate->addListener(listener);
}
if (enabled != nullptr) {
enabled->addListener(listener);
}
2024-01-01 16:21:10 +00:00
if (parameters[index]->sidechain != nullptr) {
parameters[index]->sidechain->addListener(listener);
}
}
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);
}
if (enabled != nullptr) {
enabled->removeListener(listener);
}
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);
}
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);
}
}
juce::String Effect::getId() {
return parameters[0]->paramID;
}
2023-03-25 20:24:10 +00:00
juce::String Effect::getName() {
return parameters[0]->name;
2023-03-25 20:24:10 +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"));
}
}
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;
}
void Effect::updateSampleRate(int sampleRate) {
this->sampleRate = sampleRate;
}