#include "plugin.hpp" #include #define BUFFER_S 256 #define IR_S 2 int BUFFER_SIZE = BUFFER_S; int IR_SIZE = IR_S; void importIR(char *filename, std::vector *IR_ARRAY) { std::ifstream file(filename); std::string bufstr; while(std::getline(file, bufstr)) { std::istringstream stream(bufstr); float value; stream >> value; IR_ARRAY->push_back(value); } } struct SConv : Module { enum ParamIds { TIME_KNOB_PARAM, NUM_PARAMS }; enum InputIds { IN_INPUT, NUM_INPUTS }; enum OutputIds { OUT_OUTPUT, NUM_OUTPUTS }; enum LightIds { NUM_LIGHTS }; float IN_BUFF[BUFFER_S]; std::vector IR_ARRAY; float *OUT_BUFF; //[BUFFER_S+IR_S]; float *TEMP_BUFF; //[BUFFER_S+IR_S]; int in_buff_index = 0; int out_buff_index = 0; int delay_sample = 0; dsp::RealTimeConvolver *convolver = NULL; int read_index_out = 0; SConv() { config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS); configParam(TIME_KNOB_PARAM, 0.f, 1.f, 0.f, ""); importIR("PATH/TO/IR", &IR_ARRAY); IR_SIZE = IR_ARRAY.size(); std::cout << "[+] Start of malloc" << std::endl; OUT_BUFF = (float*)malloc(sizeof(float)*(BUFFER_S+IR_SIZE)); TEMP_BUFF = (float*)malloc(sizeof(float)*(BUFFER_S+IR_SIZE)); std::cout << "[+] End of malloc" << std::endl; // IN_BUFF initialization for(int i = 0; i < BUFFER_SIZE; i++){ IN_BUFF[i] = 0; } std::cout << "[+] Start of OUT_BUFF init" << std::endl; // OUT_BUFF initialization for(int i = 0; i < BUFFER_SIZE+IR_SIZE; i++){ OUT_BUFF[i] = 0; } std::cout << "[+] End of OUT_BUFF init" << std::endl; // Initialize the convolver convolver = new dsp::RealTimeConvolver(BUFFER_S); convolver->setKernel(IR_ARRAY.data(), IR_S); } ~SConv() { std::cout << "[+] Start of Free" << std::endl; free(OUT_BUFF); free(TEMP_BUFF); } void process(const ProcessArgs &args) override { int out_len = BUFFER_SIZE+IR_SIZE; // Whatever hapens we need to read the input and add it to the input buffer IN_BUFF[in_buff_index] = inputs[IN_INPUT].getVoltage(); // If the input buffer is full we process it and then erase the values it contains with 0 if(in_buff_index >= (BUFFER_SIZE-1)) { convolver->processBlock(IN_BUFF, TEMP_BUFF); // TEMP_BUFF is of length of L+P where L is the lenght of the input buffer and P is the length of the impulse response // Now we need to put the L+P first samples into the output buffer for(int i = 0; i < out_len; i++) { OUT_BUFF[(i+read_index_out) % out_len] += TEMP_BUFF[i]; } // Reset of the input buffer for(int i = 0; i < BUFFER_SIZE; i++) { IN_BUFF[i] = 0.0; } in_buff_index = 0; } else { in_buff_index++; } outputs[OUT_OUTPUT].setVoltage(OUT_BUFF[read_index_out]); OUT_BUFF[read_index_out] = 0.0; read_index_out = (read_index_out + 1) % out_len; } }; struct SConvWidget : ModuleWidget { SConvWidget(SConv *module) { setModule(module); setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/SConv.svg"))); addChild(createWidget(Vec(RACK_GRID_WIDTH, 0))); addChild(createWidget(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0))); addChild(createWidget(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH))); addChild(createWidget(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH))); addParam(createParamCentered(mm2px(Vec(8.733, 45.978)), module, SConv::TIME_KNOB_PARAM)); addInput(createInputCentered(mm2px(Vec(8.742, 72.53)), module, SConv::IN_INPUT)); addOutput(createOutputCentered(mm2px(Vec(23.069, 86.731)), module, SConv::OUT_OUTPUT)); } }; Model *modelSConv = createModel("SConv"); //IR_ARRAY.push_back(...); /*for (auto it = IR_ARRAY.begin();it != IR_ARRAY.end();it++) { float value = *it; }*/ // IR_ARRAY.data();