Point to value in Object assigned by reference

Hello everyone,
I’ll try to be DRY and sample to understand: my goal is to map a MIDI controller and send data over OSC.
At the moment I’m having troubles retrieving data from the classes I made; let’s start from the code!

This is my MIDI struct the structure that is supposed to hold every data of each pad or knob:

struct MidiSgnl {
  int order;
  string label;
  int channel;
  int control;
  MidiSgnl(int ord, string lbl, int ch, int ctrl){
    order = ord;
    label = lbl;
    channel = ch;
    control = ctrl;
  bool operator< (const MidiSgnl &other) const {
    return order < other.order;
  string status;
  int pitch;
  int velocity;
  int value;
  int getValue();
  void data(string st, int pit, int vel, int val);

Here the default data, nothing funny:

class KnobsData {
  vector<MidiDataModel> data;
      data.push_back(MidiDataModel("Knob 1 top",1,7,21));
      data.push_back(MidiDataModel("Knob 2 top",1,7,22));
      data.push_back(MidiDataModel("Knob 3 top",1,7,23));
      data.push_back(MidiDataModel("Knob 4 top",1,7,24));
      data.push_back(MidiDataModel("Knob 5 top",1,7,25));
      data.push_back(MidiDataModel("Knob 6 top",1,7,26));
      data.push_back(MidiDataModel("Knob 7 top",1,7,27));
      data.push_back(MidiDataModel("Knob 8 top",1,7,28));
      data.push_back(MidiDataModel("Knob 1 bottom",1,7,41));
      data.push_back(MidiDataModel("Knob 2 bottom",1,7,42));
      data.push_back(MidiDataModel("Knob 3 bottom",1,7,43));
      data.push_back(MidiDataModel("Knob 4 bottom",1,7,44));
      data.push_back(MidiDataModel("Knob 5 bottom",1,7,45));
      data.push_back(MidiDataModel("Knob 6 bottom",1,7,46));
      data.push_back(MidiDataModel("Knob 7 bottom",1,7,47));
      data.push_back(MidiDataModel("Knob 8 bottom",1,7,48));

Here is where I create the vector of midi signals and where i populate the vector with metadata:

// In App.h
  std::vector<MidiSgnl> knobs;

// then in App.cpp
void ofApp::setup(){
    for(MidiDataModel data : knobsData.data){
    MidiSgnl newMidi = MidiSgnl(data.order, data.label, data.channel, data.control);

Here I make the assign using the MidiSgnl::(string st, int pit, int vel, int val); function described before

void ofApp::newMidiMessage(ofxMidiMessage &msg) {
   if(knobs.size() > 0)
    for(MidiSgnl k : knobs)
      if(msg.channel == k.channel && msg.control == k.control)                 
        k.data(msg.getStatusString(msg.status), pitch, velocity, value);

My problem is that if I call a value in the ofApp::draw() I always get 0:

void ofApp::draw(){
    ofDrawRectangle(200, 20, ofMap(knobs[0].getValue(), 0, 127.0, 10, 400, true), 30);

You can find the current implementation of this here

Hi, in this section k.data(msg.getStatusString(msg.status), pitch, velocity, value); } does not make much sense as the pitch, velocity and value are not coming from the midi message. Besides this, why dont you simply store the ofxMidiMessage in the knob vector?

1 Like