Question

Example: If I want to play a sound in my soon to be homemade VST instrument, and this note in the sequencer is 15000 samples long and the note is set to ON by the processEvents, the processReplace function starts to play the sound from 0 to sampleFrames (that is 10000 in my example here). Then there will be 5000 samples left to play of the note the next time it enters the loop in processReplace, but still the loop will play from 0 to 10000 which is 5000 more than samples to play from the note. There will be 5000 samples of silence that are going to be played and that means that this note will not be turned off until 5000 samples later!?

If a new sound in the sequencer is placed directly after the first one the processReplace function will not see the short period of OFF set by the processEvents and see the next sample as a continous sound of the first one because it will only see the new notes ON when the loop is entered again.

How can I prevent this from happening? How can I stop the processReplace loop if the note ends in the middle of the loop?

Was it helpful?

Solution

I am not sure I get your question, but here's a stab at it.

When you have determined what sounds to generate in processEvents from the incoming Midi note events the next call to processReplace has to make that happen. So you need internal state that remember the notes that were detected in processEvents and need to be output during processReplace.

When the duration of a sample (in your case) is longer than the buffer can fit (sampleFrames) you need to remember what you were doing and where you were. In your example 2/3s of the sample has been played and the next processReplace needs to play the last 1/3 of the sample.

If the (Midi) note has not turned off yet (you did not receive a Midi note-off event) you have to decide as a VSTi plugin how you will handle that. So you may want to repeat that same sample and play the first 1/3 of it in that 2nd processEvents call - and remember you have to play the rest of that sample later.

If you receive a Midi note-off message anywhere during this scenario - you will have to store internal state (data) that tells the processReplace logic of your plugin to stop at the correct moment with the playback of that sample. A lot of plugins take the shortcut and will process this information in blocks of samples equal to the sampleFrames of the processReplace buffers. But if you want to be precise you must examine the deltaFrames of the incoming Midi note event and take that offset into account in the processReplace processing.

The Host (DAW) determines when it calls your processEvents and processReplace methods. It will generally keep doing so during the entire lifetime of your plugin (instance). So it is key to maintain that internal state of what it is your plugin is doing. If it is doing nothing - you don't change the output buffers in anyway (zero out).

Hope it helps. Marc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top