MidiKeyboard Component to produce sound

Hi, I am pretty new to JUCE. I´m doing an Audio Application Synth without using the synthesiser class. (I started in that way before to know about this class).
When I use whatever external Midi keyboard it is working, but now I want to add a MidiKeyboard Component to use the synth without an external keyboard.

At the moment, to produce the sound I use MidiInputCallback inside the Audio class, and there I set the note number into my mySynth class in this way:

void Audio::handleIncomingMidiMessage(MidiInput* source, const MidiMessage& message)
     DBG("Midi event...");

    if (message.isNoteOn())
          mySynth.setNote (message.getNoteNumber());                  
          stopGain = 1;
        stopGain = 0;

Inside my mySynth class that Note Number is turned into frequency:

void MySynth::setNote(int newNote)
    frequency = MidiMessage::getMidiNoteInHertz(newNote);

This frequency subsequently is transformed into phase increment and it produces the sound with the next code:

float Oscillator::nextSample()
	auto out = renderWaveShape(phase);

	phase += phaseIncrement;
	if (phase > MathConstants<float>::twoPi)
		phase -= MathConstants<float>::twoPi;

	return out;

To use the MidiKeyboard Component, I just need to get the note number from the keyboard component, in the same way I did before. I suppose that this is using “handleNoteOn/Off” instead of “handleIncomingMidiMessage”, so I have the next code

void Audio::handleNoteOn(juce::MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)

    DBG("Midi Keyboard event...");
    MidiMessage midiMessageFromKeyboard = juce::MidiMessage::noteOn(midiChannel, midiNoteNumber, velocity);
    int noteNumberKeyboard = midiMessageFromKeyboard.getNoteNumber();


But it is not making any sound.
Also Audio.h inherited from MidiKeyboardStateListener and also in Audio.h I declared public MidiKeyboardState keyboardState;
It is public because in MainComponent I have the constructor of midiKeyboard Component in this way:

MainComponent::MainComponent(Audio& a) : audio(a), keyboardComponent(audio.keyboardState, juce::MidiKeyboardComponent::horizontalKeyboard)

Could someone say to me why I cannot get the midi Note Number from the keyboard component and use it to set mySynth.setNote() in the same way that I did in handleIncomingMidiMessage?


I would not create an audio application for a synth. It’s maybe easier to create a plugin using standalone as build option.

This way you will get MIDI events in our plugins processBlock call:

void TalCore::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
    // this adds all new MIDI events from the UI keyboard to the midiMessages buffer
    keyboardState.processNextMidiBuffer (midiMessages, 0, buffer.getNumSamples(), true);

    // add your code here

You don’t need to use the synthesizer class in a plugin. Another advantage is that you can use the available AudioProcessorValueTree and attachment classes for the parameters.

I will try it in that way, thanks so much.