Browse Source

cleanup trigger (fix obfuscating pointer stuff) and fix confusing

naming in midi CC
tags/v0.5.0
ben 7 years ago
parent
commit
801744f8d1
2 changed files with 55 additions and 61 deletions
  1. +17
    -18
      src/core/MidiCCToCV.cpp
  2. +38
    -43
      src/core/MidiTriggerToCV.cpp

+ 17
- 18
src/core/MidiCCToCV.cpp View File

@@ -175,7 +175,7 @@ struct CCTextField : TextField {


void onMouseLeave(); void onMouseLeave();


int num;
int outNum;


MIDICCToCVInterface *module; MIDICCToCVInterface *module;
}; };
@@ -183,14 +183,14 @@ struct CCTextField : TextField {
void CCTextField::draw(NVGcontext *vg) { void CCTextField::draw(NVGcontext *vg) {
/* This is necessary, since the save /* This is necessary, since the save
* file is loaded after constructing the widget*/ * file is loaded after constructing the widget*/
if (module->cc[num].numInited) {
module->cc[num].numInited = false;
text = std::to_string(module->cc[num].num);
if (module->cc[outNum].numInited) {
module->cc[outNum].numInited = false;
text = std::to_string(module->cc[outNum].num);
} }


/* If number is selected for midi learn*/ /* If number is selected for midi learn*/
if (module->cc[num].numSelected) {
text = std::to_string(module->cc[num].num);
if (module->cc[outNum].numSelected) {
text = std::to_string(module->cc[outNum].num);
} }


TextField::draw(vg); TextField::draw(vg);
@@ -198,41 +198,40 @@ void CCTextField::draw(NVGcontext *vg) {


void CCTextField::onMouseUpOpaque(int button) { void CCTextField::onMouseUpOpaque(int button) {
if (button == 1) { if (button == 1) {
module->cc[num].numSelected = false;
module->cc[outNum].numSelected = false;
} }


} }


void CCTextField::onMouseDownOpaque(int button) { void CCTextField::onMouseDownOpaque(int button) {
if (button == 1) { if (button == 1) {
module->cc[num].numSelected = true;
module->cc[outNum].numSelected = true;
} }
} }


void CCTextField::onMouseLeave() { void CCTextField::onMouseLeave() {
module->cc[num].numSelected = false;
module->cc[outNum].numSelected = false;
} }




void CCTextField::onTextChange() { void CCTextField::onTextChange() {
int *ccNum = &module->cc[num].num;
if (text.size() > 0) { if (text.size() > 0) {
try { try {
*ccNum = std::stoi(text);
int num = std::stoi(text);
// Only allow valid cc numbers // Only allow valid cc numbers
if (*ccNum < 0 || *ccNum > 127 || text.size() > 3) {
if (num < 0 || num > 127 || text.size() > 3) {
text = ""; text = "";
begin = end = 0; begin = end = 0;
*ccNum = -1;
return;
module->cc[outNum].num = -1;
} else {
module->cc[outNum].num = num;
module->cc[outNum].resetSync();
} }


module->cc[num].resetSync();

} catch (...) { } catch (...) {
text = ""; text = "";
begin = end = 0; begin = end = 0;
*ccNum = -1;
module->cc[outNum].num = -1;
} }
}; };
} }
@@ -295,7 +294,7 @@ MIDICCToCVWidget::MIDICCToCVWidget() {
for (int i = 0; i < MIDICCToCVInterface::NUM_OUTPUTS; i++) { for (int i = 0; i < MIDICCToCVInterface::NUM_OUTPUTS; i++) {
CCTextField *ccNumChoice = new CCTextField(); CCTextField *ccNumChoice = new CCTextField();
ccNumChoice->module = module; ccNumChoice->module = module;
ccNumChoice->num = i;
ccNumChoice->outNum = i;
ccNumChoice->text = std::to_string(module->cc[i].num); ccNumChoice->text = std::to_string(module->cc[i].num);
ccNumChoice->box.pos = Vec(11 + (i % 4) * (63), yPos); ccNumChoice->box.pos = Vec(11 + (i % 4) * (63), yPos);
ccNumChoice->box.size.x = 29; ccNumChoice->box.size.x = 29;


+ 38
- 43
src/core/MidiTriggerToCV.cpp View File

@@ -7,10 +7,13 @@


using namespace rack; using namespace rack;


/*
* MIDIToCVInterface converts midi note on/off events, velocity , channel aftertouch, pitch wheel and mod weel to
* CV
*/
struct TriggerValue {
int val = 0;
int num;
bool numInited = false;
bool onFocus = false;
};

struct MIDITriggerToCVInterface : MidiIO, Module { struct MIDITriggerToCVInterface : MidiIO, Module {
enum ParamIds { enum ParamIds {
NUM_PARAMS NUM_PARAMS
@@ -22,16 +25,11 @@ struct MIDITriggerToCVInterface : MidiIO, Module {
NUM_OUTPUTS = 16 NUM_OUTPUTS = 16
}; };


int trigger[NUM_OUTPUTS];
int triggerNum[NUM_OUTPUTS];
bool triggerNumInited[NUM_OUTPUTS];
bool onFocus[NUM_OUTPUTS];
TriggerValue trigger[NUM_OUTPUTS];


MIDITriggerToCVInterface() : MidiIO(), Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) { MIDITriggerToCVInterface() : MidiIO(), Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) {
for (int i = 0; i < NUM_OUTPUTS; i++) { for (int i = 0; i < NUM_OUTPUTS; i++) {
trigger[i] = 0;
triggerNum[i] = i;
onFocus[i] = false;
trigger[i].num = i;
} }
} }


@@ -48,7 +46,7 @@ struct MIDITriggerToCVInterface : MidiIO, Module {
json_t *rootJ = json_object(); json_t *rootJ = json_object();
addBaseJson(rootJ); addBaseJson(rootJ);
for (int i = 0; i < NUM_OUTPUTS; i++) { for (int i = 0; i < NUM_OUTPUTS; i++) {
json_object_set_new(rootJ, std::to_string(i).c_str(), json_integer(triggerNum[i]));
json_object_set_new(rootJ, std::to_string(i).c_str(), json_integer(trigger[i].num));
} }
return rootJ; return rootJ;
} }
@@ -58,8 +56,8 @@ struct MIDITriggerToCVInterface : MidiIO, Module {
for (int i = 0; i < NUM_OUTPUTS; i++) { for (int i = 0; i < NUM_OUTPUTS; i++) {
json_t *ccNumJ = json_object_get(rootJ, std::to_string(i).c_str()); json_t *ccNumJ = json_object_get(rootJ, std::to_string(i).c_str());
if (ccNumJ) { if (ccNumJ) {
triggerNum[i] = json_integer_value(ccNumJ);
triggerNumInited[i] = true;
trigger[i].num = json_integer_value(ccNumJ);
trigger[i].numInited = true;
} }


} }
@@ -68,7 +66,6 @@ struct MIDITriggerToCVInterface : MidiIO, Module {
void reset() final { void reset() final {
resetMidi(); resetMidi();
} }

}; };




@@ -88,13 +85,13 @@ void MIDITriggerToCVInterface::step() {
// Note: Could have an option to select between gate and velocity // Note: Could have an option to select between gate and velocity
// but trigger seams more useful // but trigger seams more useful
// outputs[i].value = trigger[i] / 127.0 * 10; // outputs[i].value = trigger[i] / 127.0 * 10;
outputs[i].value = trigger[i] > 0 ? 10.0 : 0.0;
outputs[i].value = trigger[i].val > 0 ? 10.0 : 0.0;
} }
} }


void MIDITriggerToCVInterface::resetMidi() { void MIDITriggerToCVInterface::resetMidi() {
for (int i = 0; i < NUM_OUTPUTS; i++) { for (int i = 0; i < NUM_OUTPUTS; i++) {
trigger[i] = 0;
trigger[i].val = 0;
} }
}; };


@@ -112,8 +109,8 @@ void MIDITriggerToCVInterface::processMidi(std::vector<unsigned char> msg) {


if (status == 0x8) { // note off if (status == 0x8) { // note off
for (int i = 0; i < NUM_OUTPUTS; i++) { for (int i = 0; i < NUM_OUTPUTS; i++) {
if (data1 == triggerNum[i]) {
trigger[i] = data2;
if (data1 == trigger[i].num) {
trigger[i].num = data2;
} }
} }
return; return;
@@ -121,14 +118,14 @@ void MIDITriggerToCVInterface::processMidi(std::vector<unsigned char> msg) {


if (status == 0x9) { // note on if (status == 0x9) { // note on
for (int i = 0; i < NUM_OUTPUTS; i++) { for (int i = 0; i < NUM_OUTPUTS; i++) {
if (onFocus[i]) {
this->triggerNum[i] = data1;
if (trigger[i].onFocus) {
trigger[i].num = data1;
} }
} }


for (int i = 0; i < NUM_OUTPUTS; i++) { for (int i = 0; i < NUM_OUTPUTS; i++) {
if (data1 == triggerNum[i]) {
trigger[i] = data2;
if (data1 == trigger[i].num) {
trigger[i].val = data2;
} }
} }
} }
@@ -146,22 +143,20 @@ struct TriggerTextField : TextField {


void onMouseLeave(); void onMouseLeave();



int *triggerNum;
bool *inited;
bool *onFocus;
int outNum;
MIDITriggerToCVInterface *module;
}; };


void TriggerTextField::draw(NVGcontext *vg) { void TriggerTextField::draw(NVGcontext *vg) {
/* This is necessary, since the save /* This is necessary, since the save
* file is loaded after constructing the widget*/ * file is loaded after constructing the widget*/
if (*inited) {
*inited = false;
text = std::to_string(*triggerNum);
if (module->trigger[outNum].numInited) {
module->trigger[outNum].numInited = false;
text = std::to_string(module->trigger[outNum].num);
} }


if (*onFocus) {
text = std::to_string(*triggerNum);
if (module->trigger[outNum].onFocus) {
text = std::to_string(module->trigger[outNum].num);
} }


TextField::draw(vg); TextField::draw(vg);
@@ -170,36 +165,38 @@ void TriggerTextField::draw(NVGcontext *vg) {
void TriggerTextField::onTextChange() { void TriggerTextField::onTextChange() {
if (text.size() > 0) { if (text.size() > 0) {
try { try {
*triggerNum = std::stoi(text);
int num = std::stoi(text);
// Only allow valid cc numbers // Only allow valid cc numbers
if (*triggerNum < 0 || *triggerNum > 127 || text.size() > 3) {
if (num < 0 || num > 127 || text.size() > 3) {
text = ""; text = "";
begin = end = 0; begin = end = 0;
*triggerNum = -1;
module->trigger[outNum].num = -1;
}else {
module->trigger[outNum].num = num;
} }
} catch (...) { } catch (...) {
text = ""; text = "";
begin = end = 0; begin = end = 0;
*triggerNum = -1;
module->trigger[outNum].num = -1;
} }
}; };
} }


void TriggerTextField::onMouseUpOpaque(int button) { void TriggerTextField::onMouseUpOpaque(int button) {
if (button == 1) { if (button == 1) {
*onFocus = false;
module->trigger[outNum].onFocus = false;
} }


} }


void TriggerTextField::onMouseDownOpaque(int button) { void TriggerTextField::onMouseDownOpaque(int button) {
if (button == 1) { if (button == 1) {
*onFocus = true;
module->trigger[outNum].onFocus = true;
} }
} }


void TriggerTextField::onMouseLeave() { void TriggerTextField::onMouseLeave() {
*onFocus = false;
module->trigger[outNum].onFocus = false;
} }


MIDITriggerToCVWidget::MIDITriggerToCVWidget() { MIDITriggerToCVWidget::MIDITriggerToCVWidget() {
@@ -259,10 +256,8 @@ MIDITriggerToCVWidget::MIDITriggerToCVWidget() {


for (int i = 0; i < MIDITriggerToCVInterface::NUM_OUTPUTS; i++) { for (int i = 0; i < MIDITriggerToCVInterface::NUM_OUTPUTS; i++) {
TriggerTextField *triggerNumChoice = new TriggerTextField(); TriggerTextField *triggerNumChoice = new TriggerTextField();
triggerNumChoice->triggerNum = &module->triggerNum[i];
triggerNumChoice->inited = &module->triggerNumInited[i];
triggerNumChoice->onFocus = &module->onFocus[i];
triggerNumChoice->text = std::to_string(module->triggerNum[i]);
triggerNumChoice->module = module;
triggerNumChoice->text = std::to_string(module->trigger[i].num);
triggerNumChoice->box.pos = Vec(11 + (i % 4) * (63), yPos); triggerNumChoice->box.pos = Vec(11 + (i % 4) * (63), yPos);
triggerNumChoice->box.size.x = 29; triggerNumChoice->box.size.x = 29;




Loading…
Cancel
Save