From 713613f2c66cc0508976c9d285a34ce5f4dc40a9 Mon Sep 17 00:00:00 2001 From: clwe Date: Sun, 28 Jun 2020 22:05:05 +0200 Subject: [PATCH] implements Makefile build flow --- Makefile | 27 +++++++++- src/LibPDEngine.cpp | 118 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 src/LibPDEngine.cpp diff --git a/Makefile b/Makefile index 0c0cc98..56e4eab 100644 --- a/Makefile +++ b/Makefile @@ -14,10 +14,11 @@ DISTRIBUTABLES += $(wildcard LICENSE*) include $(RACK_DIR)/arch.mk DUKTAPE ?= 0 -QUICKJS ?= 1 +QUICKJS ?= 0 LUAJIT ?= 1 PYTHON ?= 0 SUPERCOLLIDER ?= 0 +<<<<<<< HEAD VULT ?= 1 # Vult depends on both LuaJIT and QuickJS @@ -25,6 +26,9 @@ ifeq ($(VULT), 1) QUICKJS := 1 LUAJIT := 1 endif +======= +LIBPD ?= 1 +>>>>>>> implements Makefile build flow # Entropia File System Watcher efsw := dep/lib/libefsw-static-release.a @@ -37,6 +41,25 @@ $(efsw): cd efsw && cp lib/libefsw-static-release.a $(DEP_PATH)/lib/ cd efsw && cp -R include/efsw $(DEP_PATH)/include/ +# LibPD +ifeq ($(LIBPD), 1) +libpd := dep/lib/libpd.a +SOURCES += src/LibPDEngine.cpp +OBJECTS += $(libpd) +DEPS += $(libpd) +FLAGS += -Idep/include/libpd + +$(libpd): + $(WGET) "https://github.com/chairaudio/libpd/archive/master.tar.gz" + #$(SHA256) master.tar.gz f8dd2ea21f295badeae1b2ea8d00a91135b24710e67d437f7f18719d728e6e04 + cd dep && $(UNTAR) ../master.tar.gz + $(WGET) "https://github.com/pure-data/pure-data/archive/0.50-2.tar.gz" + #$(SHA256) 0.51-0.tar.gz 68b13342aaee70b8ef993eef11dc8d6837323dd6ea74a2f0705461d59f3ad2af + cd dep/libpd-master/pure-data && $(UNTAR) ../../../0.50-2.tar.gz --strip-components=1 + cd dep/libpd-master && make MULTI=true + cd dep/libpd-master && $(MAKE) install prefix="$(DEP_PATH)" +endif + # Duktape ifeq ($(DUKTAPE), 1) SOURCES += src/DuktapeEngine.cpp @@ -63,7 +86,7 @@ endif $(quickjs): cd dep && git clone "https://github.com/JerrySievert/QuickJS.git" cd dep/QuickJS && git checkout 807adc8ca9010502853d471bd8331cdc1d376b94 - cd dep/QuickJS && $(MAKE) $(QUICKJS_MAKE_FLAGS) + cd dep/QuickJS && $(MAKE) $(QUICKJS_MAKE_FLAGS) cd dep/QuickJS && $(MAKE) $(QUICKJS_MAKE_FLAGS) install endif diff --git a/src/LibPDEngine.cpp b/src/LibPDEngine.cpp new file mode 100644 index 0000000..7f4ee6e --- /dev/null +++ b/src/LibPDEngine.cpp @@ -0,0 +1,118 @@ +#include "ScriptEngine.hpp" +#include "z_libpd.h" +#include +#include +using namespace std; + +#define BUFFERSIZE 4096 + +void chair_write_patch_to_file(const string &patch, const string &path, const string &name ) +{ + + ofstream myfile; + cout << "file to write: " << path + "/" + name << endl; + myfile.open (path + "/" + name); + myfile << patch; + myfile.close(); +} + +void chair_load_patch(const string &patch, const string &name) +{ + + string path = "."; + chair_write_patch_to_file(patch, path, name); + + libpd_openfile(name.c_str(), path.c_str()); + + remove( (path + "/" + name).c_str() ); +} + + +struct LibPDEngine : ScriptEngine { + + ~LibPDEngine() { + } + // // variables for libpd + t_pdinstance *_lpd; + int _sampleRate = 0; + int _ticks = 0; + float _output[BUFFERSIZE]; + float _input[BUFFERSIZE];// = (float*)malloc(1024*2*sizeof(float)); + // end variables for libpd + + + std::string getEngineName() override { + return "Pure Data"; + } + + int run(const std::string& path, const std::string& script) override { + + ProcessBlock* block = getProcessBlock(); + + _sampleRate = block->sampleRate; + + libpd_init(); + _lpd = libpd_new_instance(); + + libpd_set_instance(_lpd); + libpd_init_audio(2, 2, _sampleRate); + //cout << "_lpd is initialized" << endl; + + //cout << "num od pd instances: " << libpd_num_instances() << endl; + + // compute audio [; pd dsp 1( + libpd_start_message(1); // one entry in list + libpd_add_float(1.0f); + libpd_finish_message("pd", "dsp"); + + std::string name = "test.pd"; + std::string patch = "#N canvas 333 425 105 153 10;\n" + "#X obj 32 79 dac~;\n" + "#X obj 32 27 adc~;\n" + "#X connect 1 0 0 0;\n" + "#X connect 1 1 0 1;"; + + //libpd_openfile(patch.c_str(), path.c_str()); + + chair_load_patch(patch, name); + + return 0; + } + + int process() override { + // block + ProcessBlock* block = getProcessBlock(); + int curr_bufSize = 256; + block->bufferSize = curr_bufSize; + display(std::to_string(curr_bufSize)); + + const int nChans = 2; + + // pass samples to/from libpd + _ticks = curr_bufSize / 64; + for (int s = 0; s < curr_bufSize; s++) { + for (int c = 0; c < nChans; c++) { + _input[s*nChans+c] = block->inputs[c][s]; + } + } + + libpd_set_instance(_lpd); + libpd_process_float(_ticks, _input, _output); + + for (int s = 0; s < curr_bufSize; s++) { + for (int c = 0; c < nChans; c++) { + block->outputs[c][s] = _output[s*nChans+c]; + } + } + + return 0; + } + + +}; + + +__attribute__((constructor(1000))) +static void constructor() { + addScriptEngine("pd"); +}