Pass more variables from audioprocessor to Lua

pull/170/head
James Ball 2023-12-20 21:41:28 +00:00
rodzic a0d2c93327
commit c17d5024cf
8 zmienionych plików z 41 dodań i 13 usunięć

Wyświetl plik

@ -350,7 +350,7 @@ void OscirenderAudioProcessor::addFile(juce::File file) {
fileBlocks.push_back(std::make_shared<juce::MemoryBlock>());
fileNames.push_back(file.getFileName());
fileIds.push_back(currentFileId++);
parsers.push_back(std::make_shared<FileParser>(errorCallback));
parsers.push_back(std::make_shared<FileParser>(errorCallback, variableCallback));
sounds.push_back(new ShapeSound(parsers.back()));
file.createInputStream()->readIntoMemoryBlock(*fileBlocks.back());
@ -362,7 +362,7 @@ void OscirenderAudioProcessor::addFile(juce::String fileName, const char* data,
fileBlocks.push_back(std::make_shared<juce::MemoryBlock>());
fileNames.push_back(fileName);
fileIds.push_back(currentFileId++);
parsers.push_back(std::make_shared<FileParser>(errorCallback));
parsers.push_back(std::make_shared<FileParser>(errorCallback, variableCallback));
sounds.push_back(new ShapeSound(parsers.back()));
fileBlocks.back()->append(data, size);
@ -374,7 +374,7 @@ void OscirenderAudioProcessor::addFile(juce::String fileName, std::shared_ptr<ju
fileBlocks.push_back(data);
fileNames.push_back(fileName);
fileIds.push_back(currentFileId++);
parsers.push_back(std::make_shared<FileParser>(errorCallback));
parsers.push_back(std::make_shared<FileParser>(errorCallback, variableCallback));
sounds.push_back(new ShapeSound(parsers.back()));
openFile(fileBlocks.size() - 1);

Wyświetl plik

@ -231,7 +231,10 @@ public:
std::shared_ptr<DelayEffect> delayEffect = std::make_shared<DelayEffect>();
std::function<void(int, juce::String, juce::String)> errorCallback = [this](int lineNum, juce::String fileName, juce::String error) { notifyErrorListeners(lineNum, fileName, error); };
std::shared_ptr<PerspectiveEffect> perspectiveEffect = std::make_shared<PerspectiveEffect>(VERSION_HINT, errorCallback);
std::function<LuaVariables()> variableCallback = [this]() {
return LuaVariables{ (int) currentSampleRate.load(), frequency.load()};
};
std::shared_ptr<PerspectiveEffect> perspectiveEffect = std::make_shared<PerspectiveEffect>(VERSION_HINT, errorCallback, variableCallback);
BooleanParameter* midiEnabled = new BooleanParameter("MIDI Enabled", "midiEnabled", VERSION_HINT, false);
BooleanParameter* inputEnabled = new BooleanParameter("Audio Input Enabled", "inputEnabled", VERSION_HINT, false);

Wyświetl plik

@ -4,7 +4,7 @@
const juce::String PerspectiveEffect::FILE_NAME = "6a3580b0-c5fc-4b28-a33e-e26a487f052f";
PerspectiveEffect::PerspectiveEffect(int versionHint, std::function<void(int, juce::String, juce::String)> errorCallback) : versionHint(versionHint), errorCallback(errorCallback) {
PerspectiveEffect::PerspectiveEffect(int versionHint, std::function<void(int, juce::String, juce::String)> errorCallback, std::function<LuaVariables()> variableCallback) : versionHint(versionHint), errorCallback(errorCallback), variableCallback(variableCallback) {
fixedRotateX = new BooleanParameter("Perspective Fixed Rotate X", "perspectiveFixedRotateX", versionHint, false);
fixedRotateY = new BooleanParameter("Perspective Fixed Rotate Y", "perspectiveFixedRotateY", versionHint, false);
fixedRotateZ = new BooleanParameter("Perspective Fixed Rotate Z", "perspectiveFixedRotateZ", versionHint, false);
@ -94,7 +94,7 @@ void PerspectiveEffect::updateCode(const juce::String& newCode) {
juce::SpinLock::ScopedLockType lock(codeLock);
defaultScript = newCode == DEFAULT_SCRIPT;
code = newCode;
parser = std::make_unique<LuaParser>(FILE_NAME, code, errorCallback);
parser = std::make_unique<LuaParser>(FILE_NAME, code, errorCallback, variableCallback);
}
void PerspectiveEffect::setVariable(juce::String variableName, double value) {

Wyświetl plik

@ -6,7 +6,7 @@
class PerspectiveEffect : public EffectApplication {
public:
PerspectiveEffect(int versionHint, std::function<void(int, juce::String, juce::String)> errorCallback);
PerspectiveEffect(int versionHint, std::function<void(int, juce::String, juce::String)> errorCallback, std::function<LuaVariables()> variableCallback);
// arbitrary UUID
static const juce::String FILE_NAME;
@ -25,7 +25,8 @@ private:
const juce::String DEFAULT_SCRIPT = "return { x, y, z }";
juce::String code = DEFAULT_SCRIPT;
std::function<void(int, juce::String, juce::String)> errorCallback;
std::unique_ptr<LuaParser> parser = std::make_unique<LuaParser>(FILE_NAME, code, errorCallback);
std::function<LuaVariables()> variableCallback;
std::unique_ptr<LuaParser> parser = std::make_unique<LuaParser>(FILE_NAME, code, errorCallback, variableCallback);
juce::SpinLock codeLock;
bool defaultScript = true;

Wyświetl plik

@ -1,7 +1,7 @@
#include "LuaParser.h"
#include "luaimport.h"
LuaParser::LuaParser(juce::String fileName, juce::String script, std::function<void(int, juce::String, juce::String)> errorCallback, juce::String fallbackScript) : fallbackScript(fallbackScript), errorCallback(errorCallback), fileName(fileName) {
LuaParser::LuaParser(juce::String fileName, juce::String script, std::function<void(int, juce::String, juce::String)> errorCallback, std::function<LuaVariables()> variableCallback, juce::String fallbackScript) : fallbackScript(fallbackScript), errorCallback(errorCallback), variableCallback(variableCallback), fileName(fileName) {
reset(script);
}
@ -72,6 +72,17 @@ std::vector<float> LuaParser::run() {
lua_pushnumber(L, step);
lua_setglobal(L, "step");
auto vars = variableCallback();
lua_pushnumber(L, vars.sampleRate);
lua_setglobal(L, "sample_rate");
lua_pushnumber(L, vars.frequency);
lua_setglobal(L, "frequency");
lua_pushnumber(L, phase);
lua_setglobal(L, "phase");
// this CANNOT run at the same time as setVariable
if (updateVariables) {
juce::SpinLock::ScopedTryLockType lock(variableLock);
@ -125,6 +136,10 @@ std::vector<float> LuaParser::run() {
lua_settop(L, 0);
step++;
phase += 2 * std::numbers::pi * vars.frequency / vars.sampleRate;
if (phase > 2 * std::numbers::pi) {
phase -= 2 * std::numbers::pi;
}
return values;
}

Wyświetl plik

@ -1,6 +1,7 @@
#pragma once
#include <JuceHeader.h>
#include <regex>
#include <numbers>
#include "../shape/Shape.h"
class ErrorListener {
@ -9,10 +10,15 @@ public:
virtual juce::String getFileName() = 0;
};
struct LuaVariables {
int sampleRate;
double frequency;
};
struct lua_State;
class LuaParser {
public:
LuaParser(juce::String fileName, juce::String script, std::function<void(int, juce::String, juce::String)> errorCallback, juce::String fallbackScript = "return { 0.0, 0.0 }");
LuaParser(juce::String fileName, juce::String script, std::function<void(int, juce::String, juce::String)> errorCallback, std::function<LuaVariables()> variableCallback, juce::String fallbackScript = "return { 0.0, 0.0 }");
~LuaParser();
std::vector<float> run();
@ -31,6 +37,7 @@ private:
int functionRef = -1;
bool usingFallbackScript = false;
long step = 1;
double phase = 0.0;
lua_State* L = nullptr;
juce::String script;
juce::String fallbackScript;
@ -39,5 +46,6 @@ private:
std::vector<juce::String> variableNames;
std::vector<double> variables;
std::function<void(int, juce::String, juce::String)> errorCallback;
std::function<LuaVariables()> variableCallback;
juce::String fileName;
};

Wyświetl plik

@ -3,7 +3,7 @@
#include "../shape/CircleArc.h"
#include <numbers>
FileParser::FileParser(std::function<void(int, juce::String, juce::String)> errorCallback) : errorCallback(errorCallback) {}
FileParser::FileParser(std::function<void(int, juce::String, juce::String)> errorCallback, std::function<LuaVariables()> variableCallback) : errorCallback(errorCallback), variableCallback(variableCallback) {}
void FileParser::parse(juce::String fileName, juce::String extension, std::unique_ptr<juce::InputStream> stream, juce::Font font) {
juce::SpinLock::ScopedLockType scope(lock);
@ -27,7 +27,7 @@ void FileParser::parse(juce::String fileName, juce::String extension, std::uniqu
} else if (extension == ".txt") {
text = std::make_shared<TextParser>(stream->readEntireStreamAsString(), font);
} else if (extension == ".lua") {
lua = std::make_shared<LuaParser>(fileName, stream->readEntireStreamAsString(), errorCallback, fallbackLuaScript);
lua = std::make_shared<LuaParser>(fileName, stream->readEntireStreamAsString(), errorCallback, variableCallback, fallbackLuaScript);
}
sampleSource = lua != nullptr;

Wyświetl plik

@ -10,7 +10,7 @@
class FileParser {
public:
FileParser(std::function<void(int, juce::String, juce::String)> errorCallback = nullptr);
FileParser(std::function<void(int, juce::String, juce::String)> errorCallback = nullptr, std::function<LuaVariables()> variableCallback = nullptr);
void parse(juce::String fileName, juce::String extension, std::unique_ptr<juce::InputStream>, juce::Font);
std::vector<std::unique_ptr<Shape>> nextFrame();
@ -41,4 +41,5 @@ private:
juce::String fallbackLuaScript = "return { 0.0, 0.0 }";
std::function<void(int, juce::String, juce::String)> errorCallback;
std::function<LuaVariables()> variableCallback;
};