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;
}
Blockquote
Inside my mySynth class that Note Number is turned into frequency:
Blockquote
void MySynth::setNote(int newNote)
{
frequency = MidiMessage::getMidiNoteInHertz(newNote);
}
Blockquote
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;
}
Blockquote
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);
}
Blockquote
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)
Blockquote
Could someone say 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