C++ function question



Hi all this is more of a beginner C++ question than a JUCE question but anyhow …
How would I put this code into a function inside the audio ?
What’s a good way to do this?
I am trying to put some midi processing into a function …
Not sure if this is kosher !
Thanks for your generous replies

//can/should I declare these 3 variables globally
//at the top of PluginProcessor.cpp
MidiMessage m;
MidiBuffer processedMidi;
int time;

void WaylochorderAudioProcessor::processBlock (AudioBuffer<float>& buffer, MidiBuffer& midiMessages)


ScopedNoDenormals noDenormals;
auto totalNumInputChannels  = getTotalNumInputChannels();
auto totalNumOutputChannels = getTotalNumOutputChannels();

for (MidiBuffer::Iterator i (midiMessages); i.getNextEvent (m, time);)

if (m.isNoteOff()){
sendAllNotesOff(m.getChannel(), time);

midiMessages.swapWith (processedMidi);


void WaylochorderAudioProcessor::sendAllNotesOff (int channel, int Time)
for (int i = 0; i < 128; ++i){
m = MidiMessage::noteOff(channel, i, 0.0f);
processedMidi.addEvent(m, Time);


try to minimize the scope that they exist in.
the MidiMessage class is simple and lightweight, so you can declare that right where it gets used.
the MidiBuffer class is a bit heavier, so you might declare that at the class level as a class member.

But if you look at the JUCE examples for Midi processing (like the Arpeggiator example), you’ll see where those variables typically get declared.

If you want to know more about this stuff (where to declare variables and why), you can read some tutorials I put together here which are targeted towards new JUCE and C++ users: https://matkatmusic.github.io


Thanks matkat that’s a lot of useful stuff … can you post a link to the Arpeggiator example?
I can’t see it on the JUCE website.
I wish those tutorials had the same classes as what the projucer generates

Can I declare a function like that and use it in the PluginProcessor.cpp ?


It’s in the Juce repo.