ofxMidi and ofxMidi - linking midi data to the piano note sound

I’m trying to get the midi device to play the sound of a piano note when the midi key is pressed however am unsure why it isn’t working. I’ve got the same procedure working with keyboard press, so when you press space the note plays however am having a bit of trouble with linking this to a midi device.

    //-------------------------------------------------------------------------------------------------
    void testApp::setup() {
    
    ... // Here the midi device is being initialized
        // Load the piano Sounds
        
        ofxMaxiSettings::setup(44100, 2, 512);
        ofSoundStreamSetup(2, 0, 44100, 512, 4);
        
        shared_ptr<ofxMaxiSample> pianoE(new ofxMaxiSample);
        pianoE->load(ofToDataPath("sounds/C3.wav"),1);
        
        keys.push_back(Key(pianoE, '48')); // This is for the letter C on the piano
        keys.push_back(Key(pianoE,' ')); // This is for the space bar on a keyboard
    }
    
    void testApp::draw() {
    ...
        ps[0].run();
        if(one)   ps[0].addParticle(ofVec2f(velocity_map,0));
    ...
    }

//--------------------------------------------------------------
I'm trying to  capture the same ata through the midi device however this is not going through
//--------------------------------------------------------------

    void testApp::newMidiMessage(ofxMidiMessage& msg) //
    {
    	// make a copy of the latest message
    	midiMessage = msg;
        
        if(msg.velocity !=0) // Note on
        {   
            for (auto it = keys.begin(); it!=keys.end(); it++)
            {
                if (it->triggerLetter == msg.pitch && it->noteOn == false)
                {
                    it->noteOn = true;
                    it->sample->trigger();
                }
            }
            if ((msg.pitch % 12) == 0)  one = true;
            else one = false;
        }
        else 
    {
    one = false; // Note off
        for (auto it = keys.begin(); it!=keys.end(); it++) {
            if (it->triggerLetter == key) {
                it->noteOn = false;
            }
        }
     }   

    
    }
    //--------------------------------------------------------------
    void testApp::keyPressed(int key)
    {
        if(key == ' ') one = true;
        
        for (auto it = keys.begin(); it!=keys.end(); it++)
        {
            if (it->triggerLetter == key && it->noteOn == false)
            {
                it->noteOn = true;
                it->sample->trigger();
            }
        }
    }
    
    //--------------------------------------------------------------
    void testApp::keyReleased(int key)
    {
        if(key == ' ') one = false;
        
        for (auto it = keys.begin(); it!=keys.end(); it++) {
            if (it->triggerLetter == key) {
                it->noteOn = false;
            }
        }
    }
    
    //--------------------------------------------------------------
    
    void testApp::audioOut(float *output, int bufferSize, int nChannnels)
    {
        for (auto it = keys.begin(); it!=keys.end(); it++) {
            for (int i = 0; i < bufferSize; i++) {
                float value = it->env.ar(it->sample->playOnce(),attack,release,holdTime,it->noteOn);
                output[2*i]+=value;
                output[2*i+1]+=value;
            }
        }
    }

Could someone give me a heads up as to why this isn’t working? that would be great.

hello Malaa,

are you having issues receiving midi in or is the issue that the midi doesn’t trigger your sample?

Best,
V

I figured it out in the end. Was trying to trigger the sound for each sample. Just had to change the char _ trigger to and int.

struct Key {
    bool noteOn;
    shared_ptr<ofxMaxiSample> sample;
    char triggerLetter;
    maxiEnv env;
    //    Key(shared_ptr<ofxMaxiSample> _sample,char _triggerLetter){ // Keyboard
    Key(shared_ptr<ofxMaxiSample> _sample, int _triggerLetter){ // Piano
        noteOn = false;
        sample = _sample;
        triggerLetter = _triggerLetter;
    }
};