2023-01-15 17:01:27 +00:00
|
|
|
#include "FileParser.h"
|
|
|
|
#include "../shape/Line.h"
|
2023-04-04 15:17:37 +00:00
|
|
|
#include "../shape/CircleArc.h"
|
2023-02-05 17:39:02 +00:00
|
|
|
#include <numbers>
|
2025-01-05 15:55:53 +00:00
|
|
|
#include "../PluginProcessor.h"
|
2023-01-15 17:01:27 +00:00
|
|
|
|
2024-05-11 22:10:54 +00:00
|
|
|
FileParser::FileParser(OscirenderAudioProcessor &p, std::function<void(int, juce::String, juce::String)> errorCallback) : errorCallback(errorCallback), audioProcessor(p) {}
|
2023-01-15 17:01:27 +00:00
|
|
|
|
2024-05-11 22:10:54 +00:00
|
|
|
void FileParser::parse(juce::String fileId, juce::String extension, std::unique_ptr<juce::InputStream> stream, juce::Font font) {
|
2023-07-11 21:28:54 +00:00
|
|
|
juce::SpinLock::ScopedLockType scope(lock);
|
|
|
|
|
2023-09-14 19:21:08 +00:00
|
|
|
if (extension == ".lua" && lua != nullptr && lua->isFunctionValid()) {
|
|
|
|
fallbackLuaScript = lua->getScript();
|
|
|
|
}
|
|
|
|
|
2023-02-05 00:43:57 +00:00
|
|
|
object = nullptr;
|
|
|
|
svg = nullptr;
|
2023-04-04 15:17:37 +00:00
|
|
|
text = nullptr;
|
2024-04-15 19:54:25 +00:00
|
|
|
gpla = nullptr;
|
2023-04-04 15:17:37 +00:00
|
|
|
lua = nullptr;
|
2024-05-11 22:10:54 +00:00
|
|
|
img = nullptr;
|
2024-08-22 15:59:21 +00:00
|
|
|
wav = nullptr;
|
2023-02-05 00:43:57 +00:00
|
|
|
|
2023-01-15 22:34:02 +00:00
|
|
|
if (extension == ".obj") {
|
2023-02-05 19:36:50 +00:00
|
|
|
object = std::make_shared<WorldObject>(stream->readEntireStreamAsString().toStdString());
|
2023-02-05 00:43:57 +00:00
|
|
|
} else if (extension == ".svg") {
|
2023-02-05 19:36:50 +00:00
|
|
|
svg = std::make_shared<SvgParser>(stream->readEntireStreamAsString());
|
2023-02-05 20:36:51 +00:00
|
|
|
} else if (extension == ".txt") {
|
2023-08-27 18:33:42 +00:00
|
|
|
text = std::make_shared<TextParser>(stream->readEntireStreamAsString(), font);
|
2023-04-04 15:17:37 +00:00
|
|
|
} else if (extension == ".lua") {
|
2024-05-11 22:10:54 +00:00
|
|
|
lua = std::make_shared<LuaParser>(fileId, stream->readEntireStreamAsString(), errorCallback, fallbackLuaScript);
|
2024-04-15 19:54:25 +00:00
|
|
|
} else if (extension == ".gpla") {
|
2025-01-21 21:34:25 +00:00
|
|
|
juce::MemoryBlock buffer{};
|
|
|
|
int bytesRead = stream->readIntoMemoryBlock(buffer);
|
|
|
|
if (bytesRead < 8) return;
|
|
|
|
char* gplaData = (char*)buffer.getData();
|
|
|
|
const char tag[] = "GPLA ";
|
|
|
|
bool isBinary = true;
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
isBinary = isBinary && tag[i] == gplaData[i];
|
|
|
|
}
|
|
|
|
if (isBinary) {
|
|
|
|
gpla = std::make_shared<LineArtParser>(gplaData, bytesRead);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stream->setPosition(0);
|
|
|
|
gpla = std::make_shared<LineArtParser>(stream->readEntireStreamAsString());
|
|
|
|
}
|
2024-06-01 20:50:56 +00:00
|
|
|
} else if (extension == ".gif" || extension == ".png" || extension == ".jpg" || extension == ".jpeg") {
|
2024-05-11 22:10:54 +00:00
|
|
|
juce::MemoryBlock buffer{};
|
|
|
|
int bytesRead = stream->readIntoMemoryBlock(buffer);
|
|
|
|
img = std::make_shared<ImageParser>(audioProcessor, extension, buffer);
|
2024-08-22 15:59:21 +00:00
|
|
|
} else if (extension == ".wav" || extension == ".aiff") {
|
2025-01-07 17:51:08 +00:00
|
|
|
wav = std::make_shared<WavParser>(audioProcessor);
|
|
|
|
wav->parse(std::move(stream));
|
2023-01-15 22:34:02 +00:00
|
|
|
}
|
2023-07-01 14:29:53 +00:00
|
|
|
|
2024-06-01 20:50:56 +00:00
|
|
|
isAnimatable = gpla != nullptr || (img != nullptr && extension == ".gif");
|
2024-08-22 15:59:21 +00:00
|
|
|
sampleSource = lua != nullptr || img != nullptr || wav != nullptr;
|
2023-01-15 17:01:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 15:17:37 +00:00
|
|
|
std::vector<std::unique_ptr<Shape>> FileParser::nextFrame() {
|
2023-07-11 21:28:54 +00:00
|
|
|
juce::SpinLock::ScopedLockType scope(lock);
|
|
|
|
|
2024-01-07 19:48:02 +00:00
|
|
|
if (object != nullptr) {
|
|
|
|
return object->draw();
|
2023-07-11 21:28:54 +00:00
|
|
|
} else if (svg != nullptr) {
|
|
|
|
return svg->draw();
|
|
|
|
} else if (text != nullptr) {
|
|
|
|
return text->draw();
|
2024-04-15 19:54:25 +00:00
|
|
|
} else if (gpla != nullptr) {
|
|
|
|
return gpla->draw();
|
2023-01-15 22:34:02 +00:00
|
|
|
}
|
2023-02-05 00:43:57 +00:00
|
|
|
auto tempShapes = std::vector<std::unique_ptr<Shape>>();
|
2024-02-20 14:57:52 +00:00
|
|
|
// return a square
|
2024-10-23 11:44:31 +00:00
|
|
|
tempShapes.push_back(std::make_unique<Line>(OsciPoint(-0.5, -0.5, 0), OsciPoint(0.5, -0.5, 0)));
|
|
|
|
tempShapes.push_back(std::make_unique<Line>(OsciPoint(0.5, -0.5, 0), OsciPoint(0.5, 0.5, 0)));
|
|
|
|
tempShapes.push_back(std::make_unique<Line>(OsciPoint(0.5, 0.5, 0), OsciPoint(-0.5, 0.5, 0)));
|
|
|
|
tempShapes.push_back(std::make_unique<Line>(OsciPoint(-0.5, 0.5, 0), OsciPoint(-0.5, -0.5, 0)));
|
2023-02-05 00:43:57 +00:00
|
|
|
return tempShapes;
|
2023-01-15 17:01:27 +00:00
|
|
|
}
|
|
|
|
|
2024-10-23 11:44:31 +00:00
|
|
|
OsciPoint FileParser::nextSample(lua_State*& L, LuaVariables& vars) {
|
2023-07-11 21:28:54 +00:00
|
|
|
juce::SpinLock::ScopedLockType scope(lock);
|
|
|
|
|
|
|
|
if (lua != nullptr) {
|
2024-02-26 22:11:37 +00:00
|
|
|
auto values = lua->run(L, vars);
|
2024-02-22 14:28:09 +00:00
|
|
|
if (values.size() == 2) {
|
2024-10-23 11:44:31 +00:00
|
|
|
return OsciPoint(values[0], values[1], 0);
|
2024-02-22 14:28:09 +00:00
|
|
|
} else if (values.size() > 2) {
|
2024-10-23 11:44:31 +00:00
|
|
|
return OsciPoint(values[0], values[1], values[2]);
|
2024-02-22 14:28:09 +00:00
|
|
|
}
|
2024-05-11 22:10:54 +00:00
|
|
|
} else if (img != nullptr) {
|
|
|
|
return img->getSample();
|
2024-08-22 15:59:21 +00:00
|
|
|
} else if (wav != nullptr) {
|
|
|
|
return wav->getSample();
|
|
|
|
}
|
2024-02-22 14:28:09 +00:00
|
|
|
|
2024-10-23 11:44:31 +00:00
|
|
|
return OsciPoint();
|
2023-07-01 14:29:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 23:30:20 +00:00
|
|
|
void FileParser::closeLua(lua_State*& L) {
|
|
|
|
if (lua != nullptr) {
|
|
|
|
lua->close(L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-01 14:29:53 +00:00
|
|
|
bool FileParser::isSample() {
|
|
|
|
return sampleSource;
|
|
|
|
}
|
|
|
|
|
2023-01-15 17:01:27 +00:00
|
|
|
bool FileParser::isActive() {
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileParser::disable() {
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileParser::enable() {
|
|
|
|
active = true;
|
|
|
|
}
|
2023-07-04 13:58:36 +00:00
|
|
|
|
|
|
|
std::shared_ptr<WorldObject> FileParser::getObject() {
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<SvgParser> FileParser::getSvg() {
|
|
|
|
return svg;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<TextParser> FileParser::getText() {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2024-04-15 19:54:25 +00:00
|
|
|
std::shared_ptr<LineArtParser> FileParser::getLineArt() {
|
|
|
|
return gpla;
|
|
|
|
}
|
|
|
|
|
2023-07-04 13:58:36 +00:00
|
|
|
std::shared_ptr<LuaParser> FileParser::getLua() {
|
|
|
|
return lua;
|
|
|
|
}
|
2024-05-11 22:10:54 +00:00
|
|
|
|
|
|
|
std::shared_ptr<ImageParser> FileParser::getImg() {
|
|
|
|
return img;
|
2024-06-01 20:50:56 +00:00
|
|
|
}
|
2024-08-22 15:59:21 +00:00
|
|
|
|
|
|
|
std::shared_ptr<WavParser> FileParser::getWav() {
|
|
|
|
return wav;
|
|
|
|
}
|