From 6802455301aee38e74d6e305a4485365e3eb389f Mon Sep 17 00:00:00 2001 From: James Ball Date: Thu, 22 Feb 2024 13:10:24 +0000 Subject: [PATCH] Allow Lua effects to access frequency, step, phase, sampleRate variables --- Source/PluginProcessor.cpp | 7 +++++++ Source/audio/CustomEffect.cpp | 12 +++++++++--- Source/audio/CustomEffect.h | 2 ++ Source/audio/ShapeVoice.cpp | 10 +++++++--- Source/audio/ShapeVoice.h | 2 ++ 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Source/PluginProcessor.cpp b/Source/PluginProcessor.cpp index 31cc4e5..724b5c9 100644 --- a/Source/PluginProcessor.cpp +++ b/Source/PluginProcessor.cpp @@ -617,6 +617,13 @@ void OscirenderAudioProcessor::processBlock(juce::AudioBuffer& buffer, ju juce::SpinLock::ScopedLockType lock1(parsersLock); juce::SpinLock::ScopedLockType lock2(effectsLock); synth.renderNextBlock(outputBuffer3d, midiMessages, 0, buffer.getNumSamples()); + for (int i = 0; i < synth.getNumVoices(); i++) { + auto voice = dynamic_cast(synth.getVoice(i)); + if (voice->isVoiceActive()) { + customEffect->frequency = voice->getFrequency(); + break; + } + } } midiMessages.clear(); diff --git a/Source/audio/CustomEffect.cpp b/Source/audio/CustomEffect.cpp index cf8b79a..b2a49f5 100644 --- a/Source/audio/CustomEffect.cpp +++ b/Source/audio/CustomEffect.cpp @@ -24,17 +24,23 @@ Point CustomEffect::apply(int index, Point input, const std::vector& val parser->setVariable("y", y); parser->setVariable("z", z); - auto result = parser->run(L, LuaVariables{sampleRate, 0}, step, phase); - if (result.size() >= 3) { + auto result = parser->run(L, LuaVariables{sampleRate, frequency}, step, phase); + if (result.size() >= 2) { x = result[0]; y = result[1]; - z = result[2]; + if (result.size() >= 3) { + z = result[2]; + } } } else { parser->resetErrors(); } } + step++; + phase += 2 * std::numbers::pi * frequency / sampleRate; + phase = MathUtil::wrapAngle(phase); + return Point( (1 - effectScale) * input.x + effectScale * x, (1 - effectScale) * input.y + effectScale * y, diff --git a/Source/audio/CustomEffect.h b/Source/audio/CustomEffect.h index 58e7706..65ac12e 100644 --- a/Source/audio/CustomEffect.h +++ b/Source/audio/CustomEffect.h @@ -17,6 +17,8 @@ public: void setVariable(juce::String variableName, double value); juce::String getCode(); + + double frequency = 0; private: const juce::String DEFAULT_SCRIPT = "return { x, y, z }"; diff --git a/Source/audio/ShapeVoice.cpp b/Source/audio/ShapeVoice.cpp index b992387..a63555c 100644 --- a/Source/audio/ShapeVoice.cpp +++ b/Source/audio/ShapeVoice.cpp @@ -63,6 +63,10 @@ void ShapeVoice::incrementShapeDrawing() { } } +double ShapeVoice::getFrequency() { + return actualFrequency; +} + // should be called if the current file is changed so that we interrupt // any currently playing sounds / voices void ShapeVoice::updateSound(juce::SynthesiserSound* sound) { @@ -76,9 +80,9 @@ void ShapeVoice::renderNextBlock(juce::AudioSampleBuffer& outputBuffer, int star int numChannels = outputBuffer.getNumChannels(); - float actualFrequency = frequency * pitchWheelAdjustment; - - if (!audioProcessor.midiEnabled->getBoolValue()) { + if (audioProcessor.midiEnabled->getBoolValue()) { + actualFrequency = frequency * pitchWheelAdjustment; + } else { actualFrequency = audioProcessor.frequency; } diff --git a/Source/audio/ShapeVoice.h b/Source/audio/ShapeVoice.h index 6412673..9f4880b 100644 --- a/Source/audio/ShapeVoice.h +++ b/Source/audio/ShapeVoice.h @@ -18,6 +18,7 @@ public: void incrementShapeDrawing(); + double getFrequency(); private: const double MIN_TRACE = 0.005; @@ -37,6 +38,7 @@ private: bool currentlyPlaying = false; double frequency = 1.0; + std::atomic actualFrequency = 1.0; double velocity = 1.0; double pitchWheelAdjustment = 1.0;