Skip to content

Instantly share code, notes, and snippets.

@Simon-L
Created December 9, 2023 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Simon-L/b3f5d2711b9e0f4f8ad99ba0790cffb4 to your computer and use it in GitHub Desktop.
Save Simon-L/b3f5d2711b9e0f4f8ad99ba0790cffb4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <sstream>
#include <thread>
#include <stdlib.h>
#include <unistd.h>
#include "faust/dsp/llvm-dsp.h"
#include "faust/dsp/libfaust.h"
#include "faust/audio/dummy-audio.h"
#include "faust/gui/DecoratorUI.h"
#include "faust/gui/PrintUI.h"
#include "faust/gui/UI.h"
#include "faust/misc.h"
#include "json.hpp"
using json = nlohmann::json;
struct Memory_Zone {
std::string name;
int offset;
int read;
int size;
int size_bytes;
std::string type;
int write;
};
void from_json(const json& j, Memory_Zone& p) {
j.at("name").get_to(p.name);
if (j.contains("offset")) j.at("offset").get_to(p.offset);
j.at("read").get_to(p.read);
j.at("size").get_to(p.size);
j.at("size_bytes").get_to(p.size_bytes);
j.at("type").get_to(p.type);
j.at("write").get_to(p.write);
}
typedef std::vector<Memory_Zone> MemoryLayout;
void to_json(json& j, const Memory_Zone& p) {
j = json{{"name", p.name}, {"offset", p.offset}, {"read", p.read}, {"size", p.size}, {"size_bytes", p.size_bytes}, {"type", p.type}, {"write", p.write}};
}
static void printList(const std::vector<std::string>& list)
{
for (int i = 0; i < list.size(); i++) {
std::cout << "item: " << list[i] << "\n";
}
}
struct myMeta : public Meta {
~myMeta() {}
void declare(const char* key, const char* value) {
std::cout << key << " : " << value << std::endl;
}
};
struct mm : public dsp_memory_manager {
void* dsp_memory = nullptr;
size_t dsp_memory_size = -1;
struct Table_Zone {
std::string name;
int offset;
int size;
int size_bytes;
FAUSTFLOAT* ptr = nullptr;
};
std::map<std::string, Table_Zone> tables;
bool is_valid() { return dsp_memory == nullptr; }
void begin(size_t count) { std::cout << "Memory Manager || begin count: " << count << '\n'; }
void end() { std::cout << "Memory Manager || end" << '\n'; }
void info(size_t size, size_t reads, size_t writes)
{
std::cout << "Memory Manager || info size: " << size << '\n';
if (dsp_memory_size == -1) {
dsp_memory_size = size;
std::cout << "Memory Manager || Expecting " << dsp_memory_size << '\n';
}
}
void* allocate(size_t size)
{
void* mem_ptr = calloc(1, size);
if (mem_ptr != NULL) {
if (size == dsp_memory_size) {
dsp_memory = mem_ptr;
std::cout << "Memory Manager || Got the expected " << size << '\n';
fill_memory_layout();
};
std::cout << "Memory Manager || allocated " << size << " at " << dsp_memory << '\n';
return mem_ptr;
}
return NULL;
}
void destroy(void* ptr)
{
std::cout << "Memory Manager || destroying " << ptr << '\n';
free(ptr);
dsp_memory = nullptr;
}
void fill_memory_layout() {
for ( auto &z : tables ) {
tables[z.first].ptr = &reinterpret_cast<FAUSTFLOAT*>(dsp_memory)[z.second.offset / sizeof(FAUSTFLOAT)];
}
std::cout << "DUMPING TABLES ---" << '\n';
for ( const auto &z : tables ) {
std::cout << z.second.name << " " << z.first << " " << z.second.offset << " " << z.second.size_bytes << " " << z.second.ptr << '\n';
}
}
void printFloat(int offset) {
float f = reinterpret_cast<FAUSTFLOAT*>(dsp_memory)[offset / sizeof(FAUSTFLOAT)];
std::cout << "Float at offset " << offset << " -> " << f << '\n';
}
void parse_memory_layout(std::string json_path) {
std::ifstream reader(json_path);
json data = json::parse(reader);
std::cout << "DUMP MEMORY LAYOUT:" << '\n';
std::cout << data["memory_layout"].dump() << std::endl;;
MemoryLayout ml = data["memory_layout"];
for ( const auto &p : ml )
{
if (p.type == "kFloat_ptr") {
struct Table_Zone tz;
tz.name = p.name;
tz.offset = p.offset;
tz.size = p.size;
tz.size_bytes = p.size_bytes;
tables[p.name] = tz;
}
}
// std::cout << data["ui"].dump() << std::endl;
std::cout << "Memory Manage || DSP allocation size: " << data["size"] << '\n';;
}
};
void go(const char* dspFile) {
int argc = 0;
const char* argv[64];
argv[argc++] = "-lj";
argv[argc++] = "-I";
argv[argc++] = "/home/xox/Sync/SyncMore/faust5/libraries";
argv[argc] = nullptr; // NULL terminated argv
std::string error_msg;
llvm_dsp_factory* factory = createDSPFactoryFromFile(dspFile, argc, argv, "", error_msg, -1);
if (!factory) {
std::cerr << "Cannot create factory : " << error_msg;
exit(EXIT_FAILURE);
}
std::cout << "getName " << factory->getName() << std::endl;
std::cout << "getSHAKey " << factory->getSHAKey() << std::endl;
mm _mm;
factory->setMemoryManager(&_mm);
char curDir[256];
getcwd(curDir, 256);
_mm.parse_memory_layout(std::string(curDir) + "/llvm-" + factory->getName() + ".json");
std::cout << "getCompileOptions " << factory->getCompileOptions() << std::endl;
printList(factory->getLibraryList());
printList(factory->getIncludePathnames());
dsp* DSP = factory->createDSPInstance();
if (!DSP) {
std::cerr << "Cannot create instance "<< std::endl;
exit(EXIT_FAILURE);
}
int num_ins = DSP->getNumInputs();
int num_outs = DSP->getNumOutputs();
std::cout << "Channels in : " << num_ins << " | Channels out : " << num_outs << std::endl;
std::cout << "Print UI parameters" << std::endl;
PrintUI print_ui;
DSP->buildUserInterface(&print_ui);
myMeta mm;
DSP->metadata(&mm);
FAUSTFLOAT** in = new FAUSTFLOAT*[num_ins];
FAUSTFLOAT** out = new FAUSTFLOAT*[num_outs];
for (int i = 0; i < num_ins; i++) {
in[i] = new FAUSTFLOAT[256];
memset(in[i], 0, sizeof(FAUSTFLOAT) * 256);
}
for (int i = 0; i < num_outs; i++) {
out[i] = new FAUSTFLOAT[256];
memset(out[i], 0, sizeof(FAUSTFLOAT) * 256);
}
DSP->init(44100);
std::cout << "Float in ftbl0 " << _mm.tables["ftbl0"].ptr[0] << '\n';
std::cout << "Float in ftbl1 " << _mm.tables["ftbl1"].ptr[0] << '\n';
_mm.printFloat(4096);
DSP->compute(256, in, out);
for (size_t i = 0; i < 16; i++) {
std::cout << "i: " << i << " -> " << out[0][i] << '\n';
}
delete DSP;
deleteDSPFactory(factory);
}
int main(int argc, char* argv[]) {
go(argv[1]);
}
make && ./main myrwtable1.dsp
g++ -std=c++11 -O3 main.cpp -I/.../faust/build/faustdir/pfx/include -L/.../faust/build/faustdir/pfx/lib /.../faust/build/faustdir/pfx/lib/libfaustwithllvm.a -lz -lzstd -ltinfo -lpthread -Wl,--export-dynamic -o main
getName myrwtable1
getSHAKey FB6CB12B4ABD91BE4CF17012A2BFEE79565E9573
DUMP MEMORY LAYOUT:
[{"name":"mydsp","read":0,"size":0,"size_bytes":44,"type":"kObj_ptr","write":0},{"name":"ftbl0","offset":0,"read":0,"size":512,"size_bytes":2048,"type":"kFloat_ptr","write":0},{"name":"ftbl1","offset":2048,"read":0,"size":512,"size_bytes":2048,"type":"kFloat_ptr","write":0},{"name":"fConst0","offset":4096,"read":0,"size":1,"size_bytes":4,"type":"kFloat","write":0},{"name":"mydspSIG0","read":0,"size":0,"size_bytes":0,"type":"kObj_ptr","write":0},{"name":"mydspSIG1","read":0,"size":0,"size_bytes":0,"type":"kObj_ptr","write":0}]
Memory Manage || DSP allocation size: 4108
getCompileOptions -lang llvm 15.0.7 -ct 1 -es 1 -mcd 16 -single -ftz 0
item: /.../faust /libraries
item: /share/faust
item: /usr/local/share/faust
item: /usr/share/faust
item: .
Memory Manager || begin count: 2
Memory Manager || info size: 4108
Memory Manager || Expecting 4108
Memory Manager || info size: 32
Memory Manager || end
Memory Manager || Got the expected 4108
DUMPING TABLES ---
ftbl0 ftbl0 0 2048 0x5591f91fa170
ftbl1 ftbl1 2048 2048 0x5591f91fa970
Memory Manager || allocated 4108 at 0x5591f91fa170
Memory Manager || allocated 32 at 0x5591f91fa170
Channels in : 0 | Channels out : 1
Print UI parameters
openVerticalBox label : [myrwtable1]
addHorizontalSlider label : [/myrwtable1/foo init : 1 min : 0 max : 1 step : 0.1]
closeBox
compile_options : -lang llvm 15.0.7 -ct 1 -es 1 -mcd 16 -single -ftz 0
filename : myrwtable1
name : myrwtable1
Float in ftbl0 18.37
Float in ftbl1 23.63
Float at offset 4096 -> 42
i: 0 -> 42
i: 1 -> 42
i: 2 -> 42
i: 3 -> 42
i: 4 -> 42
i: 5 -> 42
i: 6 -> 42
i: 7 -> 42
i: 8 -> 42
i: 9 -> 42
i: 10 -> 42
i: 11 -> 42
i: 12 -> 42
i: 13 -> 42
i: 14 -> 42
i: 15 -> 42
Memory Manager || destroying 0x5591f91fa170
Memory Manager || destroying 0x5591f9176dc0
process = 1 <: rwtable(512, 18.37, _, 0.3333, 128), rwtable(512, 23.63, _, 0.1333, 128) :> _ * hslider("foo", 1, 0, 1, 0.1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment