# Simple example of a VST plugin sending a MIDI CC

**URL:** <https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070>\
**Category:** Audio Plugins\
**Created:** [April 1, 2016, 3:20pm UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070 "2016-04-01T15:20:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfonso-santimone](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alfonso-santimone/32/6_2.png) [@alfonso-santimone](https://forum.juce.com/u/alfonso-santimone)\
**Post date:** [April 1, 2016, 3:20pm UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/1 "2016-04-01T15:20:08Z")

</div>

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.

&nbsp;

thanks and all the best!

---

<div class="post-metadata">

**Author:** ![alfonso-santimone](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alfonso-santimone/32/6_2.png) [@alfonso-santimone](https://forum.juce.com/u/alfonso-santimone)\
**Post date:** [April 2, 2016, 9:07am UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/2 "2016-04-02T09:07:00Z")

</div>

I actually came up with this in the processBlock

```
void StiXctrlAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
{
    buffer.clear();
    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

&nbsp;

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.

&nbsp;

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

&nbsp;

many thanks and all the best

&nbsp;

a.

---

<div class="post-metadata">

**Author:** ![alfonso-santimone](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alfonso-santimone/32/6_2.png) [@alfonso-santimone](https://forum.juce.com/u/alfonso-santimone)\
**Post date:** [April 2, 2016, 12:55pm UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/3 "2016-04-02T12:55:13Z")

</div>

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&nbsp;

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

&nbsp;

thanks

---

<div class="post-metadata">

**Author:** ![alfonso-santimone](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alfonso-santimone/32/6_2.png) [@alfonso-santimone](https://forum.juce.com/u/alfonso-santimone)\
**Post date:** [April 5, 2016, 11:44am UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/4 "2016-04-05T11:44:46Z")

</div>

Hi guys,

someone has any suggestion?

many thanks

a.

---

<div class="post-metadata">

**Author:** ![oxxyyd](https://avatars.discourse-cdn.com/v4/letter/o/e47c2d/32.png) [@oxxyyd](https://forum.juce.com/u/oxxyyd)\
**Post date:** [April 8, 2016, 12:09pm UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/5 "2016-04-08T12:09:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![alfonso-santimone](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alfonso-santimone/32/6_2.png) [@alfonso-santimone](https://forum.juce.com/u/alfonso-santimone)\
**Post date:** [April 11, 2016, 11:48am UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/6 "2016-04-11T11:48:18Z")

</div>

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.  
a.

---

<div class="post-metadata">

**Author:** ![oxxyyd](https://avatars.discourse-cdn.com/v4/letter/o/e47c2d/32.png) [@oxxyyd](https://forum.juce.com/u/oxxyyd)\
**Post date:** [April 11, 2016, 8:28pm UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/7 "2016-04-11T20:28:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alfonso-santimone](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alfonso-santimone/32/6_2.png) [@alfonso-santimone](https://forum.juce.com/u/alfonso-santimone)\
**Post date:** [April 12, 2016, 10:46am UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/8 "2016-04-12T10:46:29Z")

</div>

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 🙂

```
void StiXctrlAudioProcessor::processBlock(AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
{
    buffer.clear();
    midiMessages.clear();
    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);
    
    
}
```

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [April 12, 2016, 10:57am UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/9 "2016-04-12T10:57:07Z")

</div>

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 😉  
Make it a member variable of your AudioProcessor, and initialize it in the constructor

---

<div class="post-metadata">

**Author:** ![alfonso-santimone](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alfonso-santimone/32/6_2.png) [@alfonso-santimone](https://forum.juce.com/u/alfonso-santimone)\
**Post date:** [April 12, 2016, 11:12am UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/10 "2016-04-12T11:12:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [April 12, 2016, 11:26am UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/11 "2016-04-12T11:26:25Z")

</div>

Don’t worry, happens to all of us from time to time…  
except maybe [Donald Knuth](https://en.wikipedia.org/wiki/Knuth_reward_check) 😉

---

<div class="post-metadata">

**Author:** ![alfonso-santimone](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alfonso-santimone/32/6_2.png) [@alfonso-santimone](https://forum.juce.com/u/alfonso-santimone)\
**Post date:** [April 12, 2016, 12:18pm UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/12 "2016-04-12T12:18:42Z")

</div>

😁 thanks again!

---

<div class="post-metadata">

**Author:** ![mixer1985](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@mixer1985](https://forum.juce.com/u/mixer1985)\
**Post date:** [November 27, 2019, 8:39am UTC](https://forum.juce.com/t/simple-example-of-a-vst-plugin-sending-a-midi-cc/17070/13 "2019-11-27T08:39:14Z")

</div>

hi @alfonso,

can you paste the code or explain what you did in the end?

I am also a newbie wrestling my way through this stuff!

thanks
