Simple example of a VST plugin sending a MIDI CC


Hi all,

i need to build a bank of MIDI CC in a plugin that can send MIDI CC from its MIDI output.

Any example / help around?

I don't understand if the Juce MIDIMessage classes can send to the plugin output or send just to hardware midi output of a Juce application.


thanks and all the best!


I actually came up with this in the processBlock

void StiXctrlAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
    MidiBuffer processedMidi;
    int time;
    MidiMessage m;

    for (MidiBuffer::Iterator i(midiMessages); i.getNextEvent(m, time);)
        int8 myVal = (int8)CC00Val;
        m = MidiMessage::controllerEvent(2, 1, myVal);
        processedMidi.addEvent(m, time);

    midiMessages.swapWith (processedMidi);

But it has some relevant problems that i can't figure out how to solve


1. It only works if some MIDI event is coming to the buffer from the host.

2. It only works if the host is running.


How can i keep it "alive" even when the host is not running and not sending any event to MidiBuffer?


many thanks and all the best




I guess that the thing is that in

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

the for loop doen not run because its exit condition is given by 

i.getNextEvent(m, time)

so basically if there is no event arriving to the buffer the for loop just quit.

Am i right?

If yes, is there anyway to let the for loop run while adding events to buffer?




Hi guys,

someone has any suggestion?

many thanks



Why don’t you just clear the incoming Midibuffer and add whatever new Midimessages you want to that.

I don’t see any point in iterating through the incoming buffer, when you eventually throw all it’s content away anyhow with the swapWith call?


Thanks oxxyyd! I just got the same idea but i’m a Juce newbie so still learning.
BTW now i have my CC being sent continously even if the value isn’t changed.
So at every audio process block i have a new MIDI CC sent even if the value isn’t changed.
Any idea on how to solve this?
thanx and all the best.


Why not save the value and in the next processBlock call you check if the value has changed?


Thanks oxxyyd,
yes i tried this but same beheaviour.
I guess i’m doing something really stupid until i’ll have the “ha ha!!!” moment :slight_smile:

void StiXctrlAudioProcessor::processBlock(AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
    MidiBuffer generatedMidi;
    int time;
    MidiMessage m;
    int8 ccTempVal;
    int8 myVal = (int8)CC00Val;

    if (myVal != ccTempVal)
        m = MidiMessage::controllerEvent(2, 1, myVal);
        generatedMidi.addEvent(m, midiMessages.getLastEventTime());
    else generatedMidi.clear();

    ccTempVal = myVal;
    midiMessages.swapWith (generatedMidi);


Well, if you define your ccTempVal inside the processBlock and furthermore don’t initialize it, it is pure luck, if the comparism returns true or false :wink:
Make it a member variable of your AudioProcessor, and initialize it in the constructor


Thanks! here’s the “haha!” moment dressed with “what a dummy mofo am i?” sauce :confused:
Now it works :slight_smile:


Don’t worry, happens to all of us from time to time…
except maybe Donald Knuth :wink:


:grin: thanks again!