Tempo sync gate; more accurate timing than process block?

I am working on my first vst plugin using juce.

My goal is fairly simple; to create a tempo synchronized gating effect that simply switches the sound on and off.

Using the demo plugin, and stripping it down to the gainer, I’ve determined how to use ppq and/or samplerate/bpm to determine when the sound should be switched on or off, but I seem to be taking the wrong approach to get the timing right.

Whenever processBlock is called, I check against the current ppqPosition and set applyGain to 1 or 0. This works, but the timing is only as accurate as AudioSampleBuffer which is determined by the soundcard settings.

I’m pretty sure I need to do something on a sample-by-sample basis within the block, using the samplerate/bpm to determine the switch of the gain. This I can determine the math for myself.

My question is how to work on a sample-by-sample basis within processBlock? Would that be the right approach? I am seeking to get the timing as accurate as 12 cycles per beat depending on the input variable. Once I determine this, I will then add smoothing and some other features.


unfortunately I can’t help you in this topic but would like to ask you if you would
share some code snipped for the above as I am still struggling with passing Midi events from the process block to a physical Midi output (not used by the host)
on time. I am not good in doing the math for it :frowning:

Thank you!

yes, you have to work on a per sample basis.
and the processBlock is sample accurate.

you have the call

there you get the AudioSampleBuffer.

first use the float * AudioSampleBuffer::getSampleData (const int channelNumber) method to get a pointer to the sampledata e.g:

use the

method to determin the number of samples in the AudioBuffer.

then you can do something like this:

for(int i=0;i<buffer.getNumSamples ();i++) { data[i]... }

so for each sample in the block you can manipulate the data or do smth. different.

now from sampleRate and bpm you can calculate how many samples the sound should stay on and off.

then do something like that:

for(int i=0;i<buffer.getNumSamples ();i++) { if(mCounter-- == 0) { mCounter = mDurationInSamples; mGain = 1-mGain; } data[i] *= mGain; }

mCounter is a member variable (int) initialized to mDurationInSamples .
mDurationInSamples is a member variable (int) holding the time in samples derived from bpm and SR;
mGain is a member variable (int) initialized to 1. it is flipped between 1 and 0 whenever mCounter reaches zero.

this is a simple example and doesn’t work when you fast forward in the host or change positions.
to work with transport changes you have to include the abs. PPQ position as well.