# MidiKeyboardComponent to produce sound

**URL:** <https://forum.juce.com/t/midikeyboardcomponent-to-produce-sound/51533>\
**Category:** Development\
**Created:** [May 19, 2022, 1:37pm UTC](https://forum.juce.com/t/midikeyboardcomponent-to-produce-sound/51533 "2022-05-19T13:37:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaulPulsar](https://avatars.discourse-cdn.com/v4/letter/r/b9e5f3/32.png) [@RaulPulsar](https://forum.juce.com/u/RaulPulsar)\
**Post date:** [May 19, 2022, 1:37pm UTC](https://forum.juce.com/t/midikeyboardcomponent-to-produce-sound/51533/1 "2022-05-19T13:37:50Z")

</div>

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:

> Blockquote  
> void Audio::handleIncomingMidiMessage(MidiInput\* source, const MidiMessage& message)  
> {  
> DBG(“Midi event…”);

```
if (message.isNoteOn())
{
      mySynth.setNote (message.getNoteNumber());                  
      stopGain = 1;
}
else 
{
    stopGain = 0;
}

```

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

> Blockquote  
> 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:

> Blockquote  
> 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

> Blockquote  
> 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();

  mySynth.setNote(noteNumberKeyboard);

```

}

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:

> Blockquote  
> 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?

Thanks
