Browse Source

PD: adds switch send, switch lights receive, adds examples/gain.pd, initializes patch with sending messages

tags/v1.3.0
clwe max 4 years ago
parent
commit
b5816de8bd
3 changed files with 196 additions and 93 deletions
  1. +0
    -52
      examples/audio-trough.pd
  2. +106
    -0
      examples/gain.pd
  3. +90
    -41
      src/LibPDEngine.cpp

+ 0
- 52
examples/audio-trough.pd View File

@@ -1,52 +0,0 @@
#N canvas 2220 331 802 519 12;
#X obj 112 43 adc~ 1 2 3 4 5 6, f 47;
#X obj 113 199 dac~ 1 2 3 4 5 6, f 47;
#X obj 112 127 *~ 1;
#X obj 137 94 r K1;
#X obj 178 126 *~ 1;
#X obj 242 128 *~ 1;
#X obj 308 127 *~ 1;
#X obj 372 127 *~ 1;
#X obj 438 126 *~ 1;
#X obj 203 93 r K2;
#X obj 267 93 r K3;
#X obj 333 94 r K4;
#X obj 397 93 r K5;
#X obj 463 93 r K6;
#X obj 290 416 print toVCV;
#X msg 267 278 L3 \$1 0 0;
#X msg 333 310 L4 \$1 0 0;
#X msg 463 311 L6 \$1 0 0;
#X msg 136 277 L1 \$1 0 0;
#X msg 203 309 L2 \$1 0 0;
#X msg 397 280 L5 \$1 0 0;
#X connect 0 0 2 0;
#X connect 0 1 4 0;
#X connect 0 2 5 0;
#X connect 0 3 6 0;
#X connect 0 4 7 0;
#X connect 0 5 8 0;
#X connect 2 0 1 0;
#X connect 3 0 2 1;
#X connect 3 0 18 0;
#X connect 4 0 1 1;
#X connect 5 0 1 2;
#X connect 6 0 1 3;
#X connect 7 0 1 4;
#X connect 8 0 1 5;
#X connect 9 0 4 1;
#X connect 9 0 19 0;
#X connect 10 0 5 1;
#X connect 10 0 15 0;
#X connect 11 0 6 1;
#X connect 11 0 16 0;
#X connect 12 0 7 1;
#X connect 12 0 20 0;
#X connect 13 0 8 1;
#X connect 13 0 17 0;
#X connect 15 0 14 0;
#X connect 16 0 14 0;
#X connect 17 0 14 0;
#X connect 18 0 14 0;
#X connect 19 0 14 0;
#X connect 20 0 14 0;

+ 106
- 0
examples/gain.pd View File

@@ -0,0 +1,106 @@
#N canvas 2220 331 805 580 12;
#X obj 112 43 adc~ 1 2 3 4 5 6, f 47;
#X obj 113 281 dac~ 1 2 3 4 5 6, f 47;
#X obj 112 127 *~ 1;
#X obj 137 94 r K1;
#X obj 178 126 *~ 1;
#X obj 242 128 *~ 1;
#X obj 308 127 *~ 1;
#X obj 372 127 *~ 1;
#X obj 438 126 *~ 1;
#X obj 203 93 r K2;
#X obj 267 93 r K3;
#X obj 333 94 r K4;
#X obj 397 93 r K5;
#X obj 463 93 r K6;
#X obj 290 502 print toVCV;
#X msg 267 315 L3 \$1 0 0;
#X msg 333 347 L4 \$1 0 0;
#X msg 463 348 L6 \$1 0 0;
#X msg 136 314 L1 \$1 0 0;
#X msg 203 346 L2 \$1 0 0;
#X msg 397 317 L5 \$1 0 0;
#X obj 112 230 *~ 1;
#X obj 178 229 *~ 1;
#X obj 242 231 *~ 1;
#X obj 308 230 *~ 1;
#X obj 372 230 *~ 1;
#X obj 438 229 *~ 1;
#X obj 147 158 r S1;
#X obj 213 157 r S2;
#X obj 277 157 r S3;
#X obj 343 158 r S4;
#X obj 407 157 r S5;
#X obj 473 158 r S6;
#X obj 473 197 - 1;
#X obj 407 196 - 1;
#X obj 343 194 - 1;
#X obj 277 195 - 1;
#X obj 213 196 - 1;
#X obj 147 195 - 1;
#X msg 146 404 S1 \$1 0 0;
#X msg 213 436 S2 \$1 0 0;
#X msg 277 405 S3 \$1 0 0;
#X msg 343 438 S4 \$1 0 0;
#X msg 407 407 S5 \$1 0 0;
#X msg 473 437 S6 \$1 0 0;
#X connect 0 0 2 0;
#X connect 0 1 4 0;
#X connect 0 2 5 0;
#X connect 0 3 6 0;
#X connect 0 4 7 0;
#X connect 0 5 8 0;
#X connect 2 0 21 0;
#X connect 3 0 2 1;
#X connect 3 0 18 0;
#X connect 4 0 22 0;
#X connect 5 0 23 0;
#X connect 6 0 24 0;
#X connect 7 0 25 0;
#X connect 8 0 26 0;
#X connect 9 0 4 1;
#X connect 9 0 19 0;
#X connect 10 0 5 1;
#X connect 10 0 15 0;
#X connect 11 0 6 1;
#X connect 11 0 16 0;
#X connect 12 0 7 1;
#X connect 12 0 20 0;
#X connect 13 0 8 1;
#X connect 13 0 17 0;
#X connect 15 0 14 0;
#X connect 16 0 14 0;
#X connect 17 0 14 0;
#X connect 18 0 14 0;
#X connect 19 0 14 0;
#X connect 20 0 14 0;
#X connect 21 0 1 0;
#X connect 22 0 1 1;
#X connect 23 0 1 2;
#X connect 24 0 1 3;
#X connect 25 0 1 4;
#X connect 26 0 1 5;
#X connect 27 0 38 0;
#X connect 27 0 39 0;
#X connect 28 0 37 0;
#X connect 28 0 40 0;
#X connect 29 0 36 0;
#X connect 29 0 41 0;
#X connect 30 0 35 0;
#X connect 30 0 42 0;
#X connect 31 0 34 0;
#X connect 31 0 43 0;
#X connect 32 0 33 0;
#X connect 32 0 44 0;
#X connect 33 0 26 1;
#X connect 34 0 25 1;
#X connect 35 0 24 1;
#X connect 36 0 23 1;
#X connect 37 0 22 1;
#X connect 38 0 21 1;
#X connect 39 0 14 0;
#X connect 40 0 14 0;
#X connect 41 0 14 0;
#X connect 42 0 14 0;
#X connect 43 0 14 0;
#X connect 44 0 14 0;

+ 90
- 41
src/LibPDEngine.cpp View File

@@ -9,7 +9,8 @@ using namespace rack;
// there is no multi-instance support for receiving messages from libpd
// for now, received values for the prototype gui will be stored in global variables

float g_leds[NUM_ROWS][3];
float g_lights[NUM_ROWS][3] = {};
float g_switchLights[NUM_ROWS][3] = {};

std::vector<std::string> split (const std::string &s, char delim) {
std::vector<std::string> result;
@@ -25,7 +26,6 @@ std::vector<std::string> split (const std::string &s, char delim) {




struct LibPDEngine : ScriptEngine {

~LibPDEngine() {
@@ -33,23 +33,25 @@ struct LibPDEngine : ScriptEngine {

}

static void receiveLEDs(const char *s);
void sendInitialStates(const ProcessBlock* block);
static void receiveLights(const char *s);
bool knobChanged(const float* knobs, int idx);

bool switchChanged(const float* knobs, int idx);
bool switchChanged(const bool* knobs, int idx);
void sendKnob(const int idx, const float value);
void sendSwitch(const int idx, const bool value);

t_pdinstance *_lpd;
int _pd_block_size = 64;
int _sampleRate = 0;
int _ticks = 0;
bool _init = true;
float _old_knobs[NUM_ROWS];
bool _old_switches[NUM_ROWS];
float _output[BUFFERSIZE];
float _input[BUFFERSIZE];// = (float*)malloc(1024*2*sizeof(float));
const static std::map<std::string, int> _led_map;
float _old_knobs[NUM_ROWS] = {};
bool _old_switches[NUM_ROWS] = {};
float _output[BUFFERSIZE] = {};
float _input[BUFFERSIZE] = {};// = (float*)malloc(1024*2*sizeof(float));
const static std::map<std::string, int> _light_map;
const static std::map<std::string, int> _switchLight_map;

std::string getEngineName() override {
return "Pure Data";
@@ -65,12 +67,8 @@ struct LibPDEngine : ScriptEngine {
_lpd = libpd_new_instance();

libpd_set_printhook((t_libpd_printhook)libpd_print_concatenator);
libpd_set_concatenated_printhook( receiveLEDs );
//libpd_set_printhook(receiveLEDs);
//libpd_bind("L2");
//libpd_bind("foo");
//libpd_set_listhook(receiveList);
//libpd_set_messagehook(receiveMessage);
libpd_set_concatenated_printhook( receiveLights );


if(libpd_num_instances()>2)
{
@@ -89,6 +87,8 @@ struct LibPDEngine : ScriptEngine {
std::string name = string::filename(path);
std::string dir = string::directory(path);
libpd_openfile(name.c_str(), dir.c_str());

sendInitialStates(block);
return 0;
}
@@ -110,22 +110,25 @@ struct LibPDEngine : ScriptEngine {
// knobs
for (int i=0; i<NUM_ROWS; i++){
if( knobChanged(block->knobs, i) ){
std::string knob = "K"+std::to_string(i+1);
libpd_float(knob.c_str(), block->knobs[i]);
sendKnob(i, block->knobs[i]);
}
}
// leds
//display(std::to_string(g_leds[0]));
// lights
for(int i=0; i<NUM_ROWS; i++){
block->lights[i][0] = g_lights[i][0];
block->lights[i][1] = g_lights[i][1];
block->lights[i][2] = g_lights[i][2];
}
// switch lights
for(int i=0; i<NUM_ROWS; i++){
block->lights[i][0] = g_leds[i][0];
block->lights[i][1] = g_leds[i][1];
block->lights[i][2] = g_leds[i][2];
block->switchLights[i][0] = g_switchLights[i][0];
block->switchLights[i][1] = g_switchLights[i][1];
block->switchLights[i][2] = g_switchLights[i][2];
}
// switches
for(int i=0; i<NUM_ROWS; i++){
if( switchChanged(block->switches, i) ){
std::string sw = "S"+std::to_string(i+1);
libpd_float(sw.c_str(), block->switches[i]);
sendSwitch(i, block->switches[i]);
}
}
// process samples in libpd
@@ -150,29 +153,48 @@ static void constructor() {
}


void LibPDEngine::receiveLEDs(const char *s) {
void LibPDEngine::receiveLights(const char *s) {
std::string str = std::string(s);
std::vector<std::string> atoms = split (str, ' ');

if(atoms[0]=="toVCV:"){
// led list
bool led_is_valid = true;
int led_idx = -1;
// light list
bool light_is_valid = true;
int light_idx = -1;
try {
led_idx = _led_map.at(atoms[1]); // map::at throws an out-of-range
light_idx = _light_map.at(atoms[1]); // map::at throws an out-of-range
}
catch (const std::out_of_range& oor) {
led_is_valid = false;
light_is_valid = false;
//display("Warning:"+atoms[1]+" not found!");
}
//std::cout << v[1] << ", " << g_led_map[v[1]] << std::endl;
if(led_is_valid && atoms.size()==5){
g_leds[led_idx][0] = stof(atoms[2]); // red
g_leds[led_idx][1] = stof(atoms[3]); // green
g_leds[led_idx][2] = stof(atoms[4]); // blue
if(light_is_valid && atoms.size()==5){
g_lights[light_idx][0] = stof(atoms[2]); // red
g_lights[light_idx][1] = stof(atoms[3]); // green
g_lights[light_idx][2] = stof(atoms[4]); // blue
}
else {
// error
}

bool switchLight_is_valid = true;
int switchLight_idx = -1;
try {
switchLight_idx = _switchLight_map.at(atoms[1]); // map::at throws an out-of-range
}
catch (const std::out_of_range& oor) {
switchLight_is_valid = false;
//display("Warning:"+atoms[1]+" not found!");
}
//std::cout << v[1] << ", " << g_led_map[v[1]] << std::endl;
if(switchLight_is_valid && atoms.size()==5){
g_switchLights[switchLight_idx][0] = stof(atoms[2]); // red
g_switchLights[switchLight_idx][1] = stof(atoms[3]); // green
g_switchLights[switchLight_idx][2] = stof(atoms[4]); // blue
}
else {
// error
}
}
else {
@@ -189,7 +211,7 @@ bool LibPDEngine::knobChanged(const float* knobs, int i){
return knob_changed;
}

bool LibPDEngine::switchChanged(const float* switches, int i){
bool LibPDEngine::switchChanged(const bool* switches, int i){
bool switch_changed = false;
if (_old_switches[i] != switches[i]){
switch_changed = true;
@@ -198,11 +220,38 @@ bool LibPDEngine::switchChanged(const float* switches, int i){
return switch_changed;
}

const std::map<std::string, int> LibPDEngine::_led_map{
const std::map<std::string, int> LibPDEngine::_light_map{
{ "L1", 0 },
{ "L2", 1 },
{ "L3", 2 },
{ "L4", 3 },
{ "L5", 4 },
{ "L6", 5 }
};
};

const std::map<std::string, int> LibPDEngine::_switchLight_map{
{ "S1", 0 },
{ "S2", 1 },
{ "S3", 2 },
{ "S4", 3 },
{ "S5", 4 },
{ "S6", 5 }
};

void LibPDEngine::sendKnob(const int idx, const float value){
std::string knob = "K"+std::to_string(idx+1);
libpd_float(knob.c_str(), value);
}

void LibPDEngine::sendSwitch(const int idx, const bool value){
std::string sw = "S"+std::to_string(idx+1);
libpd_float(sw.c_str(), value);
}

void LibPDEngine::sendInitialStates(const ProcessBlock* block){
// knobs
for (int i=0; i<NUM_ROWS; i++){
sendKnob(i, block->knobs[i]);
sendSwitch(i, block->knobs[i]);
}
}

Loading…
Cancel
Save