Skip to content

Update midi buffer #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions Source/RenderEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ void RenderEngine::renderPatch (const uint8 midiNote,
MidiMessage onMessage = MidiMessage::noteOn (1,
midiNote,
midiVelocity);
onMessage.setTimeStamp(0);
MidiBuffer midiNoteBuffer;
midiNoteBuffer.addEvent (onMessage, onMessage.getTimeStamp());
midiNoteBuffer.addEvent (onMessage, 0);

// Setup fft here so it is destroyed when rendering is finished and
// the stack unwinds so it doesn't share frames with a new patch.
Expand Down Expand Up @@ -144,6 +143,10 @@ void RenderEngine::renderPatch (const uint8 midiNote,

for (int i = 0; i < numberOfBuffers; ++i)
{
// Only trigger one note on during first audio buffer
if (i > 0)
midiNoteBuffer.clear();

// Trigger note off if in the correct audio buffer.
ifTimeSetNoteOff (noteLength,
sampleRate,
Expand Down Expand Up @@ -286,18 +289,20 @@ void RenderEngine::ifTimeSetNoteOff (const double noteLength,
MidiBuffer& bufferToNoteOff)
{
double eventFrame = noteLength * sampleRate;
bool bufferBeginIsBeforeEvent = currentBufferIndex * bufferSize < eventFrame;
bool bufferEndIsAfterEvent = (currentBufferIndex + 1) * bufferSize >= eventFrame;
double bufferStartSample = currentBufferIndex * bufferSize;

bool bufferBeginIsBeforeEvent = bufferStartSample < eventFrame;
bool bufferEndIsAfterEvent = bufferStartSample + bufferSize >= eventFrame;
bool noteOffEvent = bufferBeginIsBeforeEvent && bufferEndIsAfterEvent;

if (noteOffEvent)
{
MidiBuffer midiOffBuffer;
MidiMessage offMessage = MidiMessage::noteOff (midiChannel,
midiPitch,
midiVelocity);
offMessage.setTimeStamp(eventFrame);
midiOffBuffer.addEvent(offMessage, offMessage.getTimeStamp());
bufferToNoteOff = midiOffBuffer;

double samplesUntilNoteOff = eventFrame - bufferStartSample;
bufferToNoteOff.addEvent(offMessage, samplesUntilNoteOff);
}
}

Expand Down