Handling incoming midi note and add one more note for that incoming midi note


#1

Hello
I am using the code in the demo midi i/o
I want to harmonise the sound of the incoming midi
I am success on the keyboard on the screen, like when I press C4 it will output the sound C4 and E4 at the same time.
But I can not add the harmonised sound of the incoming midi device
How can I fix it?
Thank you

void setMidiInput (int index)
{
    const StringArray list (MidiInput::getDevices());

    deviceManager.removeMidiInputCallback (list[lastInputIndex], this);

    const String newInput (list[index]);

    if (! deviceManager.isMidiInputEnabled (newInput))
        deviceManager.setMidiInputEnabled (newInput, true);

    deviceManager.addMidiInputCallback (newInput, this);
    midiInputList.setSelectedId (index + 1, dontSendNotification);

    lastInputIndex = index;
}

//==============================================================================

void setMidiOutput (int index)
{
    currentMidiOutput = nullptr;

    if (MidiOutput::getDevices() [index].isNotEmpty())
    {
        currentMidiOutput = MidiOutput::openDevice (index);
       // chorus(index);
        jassert (currentMidiOutput);
    }
}


void comboBoxChanged (ComboBox* box) override
{
    if (box == &midiInputList)    setMidiInput  (midiInputList.getSelectedItemIndex());
    if (box == &midiOutputList)   setMidiOutput (midiOutputList.getSelectedItemIndex());
}

// These methods handle callbacks from the midi device + on-screen keyboard..
void handleIncomingMidiMessage (MidiInput*, const MidiMessage& message) override
{
    const ScopedValueSetter<bool> scopedInputFlag (isAddingFromMidiInput, true);
    keyboardState.processNextMidiEvent (message);
    postMessageToList (message);
}

void handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity) override
{
    if (! isAddingFromMidiInput)
    {
        MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity));
        m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
        postMessageToList (m);
        choursnoteon(midiChannel,  midiNoteNumber, velocity);
    }
}
void choursnoteon (int midiChannel, int midiNoteNumber, float velocity)
{   int c1[1]={4};
    int c2[1]={3};
    int flag=0;
    String::String C={"C"};
    String::String G={"G"};
    if (! isAddingFromMidiInput)
    {   int note= midiNoteNumber;
        
        String::String notename = MidiMessage::getMidiNoteName( midiNoteNumber, 0, 0, 0);
  
        
        if (notename.compare("C")==0){
        flag=1;
        }
        
        if (notename.compare("G")==0){
            flag=1;
        }
        if (notename.compare("F")==0){
            flag=1;
        }
        
        if (flag==1){
            note=note+c1[0];
            
        }
        if(flag==0){
        note=note+c2[0];
        
        }
        midiNoteNumber=note;
        MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity));
        m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
        postMessageToList (m);
    }
    }
void choursnoteoff( int midiChannel, int midiNoteNumber, float velocity)
{
    int c1[1]={4};
    int c2[1]={3};
    int flag;
    String::String C={"C"};
    String::String G={"G"};
    if (! isAddingFromMidiInput)
    {   int note= midiNoteNumber;
        String::String notename = MidiMessage::getMidiNoteName( midiNoteNumber, 0, 0, 3);
        if (notename.compare("C")==0){
            flag=1;
        }
        if (notename.compare("G")==0){
            flag=1;
        }
        if (notename.compare("F")==0){
            flag=1;
        }
        
        if (flag==1){
            note=note+c1[0];
            
        }else{
            note=note+c2[0];
            
        }
        midiNoteNumber=note;
        MidiMessage m (MidiMessage::noteOff (midiChannel, midiNoteNumber, velocity));
        m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
        postMessageToList (m);
    }
    
}
void handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity) override
{
    if (! isAddingFromMidiInput)
    {
        MidiMessage m (MidiMessage::noteOff (midiChannel, midiNoteNumber, velocity));
        m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
        postMessageToList (m);
        choursnoteoff( midiChannel, midiNoteNumber,  velocity);
    }
}

#2

You should set the timestamp to be the same as that of the incoming message. If you’re trying to get the Juce keyboard to show harmony, you’ll need to pass your generated midi message to the keyboardState as that’s what controls drawing for the midi keyboard component. PostMessageToList() is just going to make that GUI that shows incoming message descriptions update. Add a breakpoint to the handleIncoming method and step thru it to see how the message is sent to the keyboard vs the text window


#3

Thank you