Skip to content

Commit

Permalink
working MIDI
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed Mar 12, 2024
1 parent 6c24c7f commit 4b5ce2b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 11 deletions.
40 changes: 32 additions & 8 deletions src/plugin/DesktopAudioDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,11 @@ class DesktopAudioDriver : public JackAudioDriver

for (uint16_t i = 0; i < fShmData->midiEventCount; ++i)
{
if (jack_midi_data_t* const data = pbuf->ReserveEvent(fShmData->midiFrames[i], 4))
if (jack_midi_data_t* const data = cbuf->ReserveEvent(fShmData->midiFrames[i], 4))
{
memcpy(data, fShmData->midiData + (i * 4), 4);
continue;
}

break;
}

Expand All @@ -402,9 +401,27 @@ class DesktopAudioDriver : public JackAudioDriver
memcpy(fShmData->audio + fEngineControl->fBufferSize, GetOutputBuffer(1), sizeof(float) * fEngineControl->fBufferSize);

JackMidiBuffer* cbuf = (JackMidiBuffer*)fGraphManager->GetBuffer(fCaptureMidiPort, fEngineControl->fBufferSize);
JackMidiBuffer* pbuf = (JackMidiBuffer*)fGraphManager->GetBuffer(fPlaybackMidiPort, fEngineControl->fBufferSize);
cbuf->Reset(fEngineControl->fBufferSize);

fShmData->midiEventCount = 0;
uint16_t mec = 0;
for (uint32_t i = 0; i < pbuf->event_count; ++i)
{
JackMidiEvent& ev(pbuf->events[i]);

if (ev.size > 4)
continue;

fShmData->midiFrames[mec] = ev.time;
memcpy(fShmData->midiData + (mec * 4), ev.GetData(pbuf), ev.size);

for (uint8_t j = ev.size; j < 4; ++j)
fShmData->midiData[mec * 4 + j] = 0;

if (++mec == 511)
break;
}
fShmData->midiEventCount = mec;

return 0;
}
Expand All @@ -430,15 +447,19 @@ SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()

jack_driver_desc_t* const desc = jack_driver_descriptor_construct("desktop", JackDriverMaster, "MOD Desktop plugin audio backend", &filler);

value.ui = 48000U;
jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
value.ui = 48000;
jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, nullptr, "Sample rate", nullptr);

value.ui = 128;
jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, nullptr, "Frames per period", nullptr);

return desc;
}

SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
{
jack_nframes_t srate = 48000;
jack_nframes_t rate = 48000;
jack_nframes_t period = 128;

for (const JSList* node = params; node; node = jack_slist_next(node))
{
Expand All @@ -447,14 +468,17 @@ SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLocke
switch (param->character)
{
case 'r':
srate = param->value.ui;
rate = param->value.ui;
break;
case 'p':
period = param->value.ui;
break;
}
}

Jack::JackDriverClientInterface* driver = new Jack::DesktopAudioDriver("system", "mod-desktop", engine, table);

if (driver->Open(128, srate, true, true, 2, 2, false, "", "", 0, 0) == 0)
if (driver->Open(period, rate, true, true, 2, 2, false, "", "", 0, 0) == 0)
{
printf("%03d:%s OK\n", __LINE__, __FUNCTION__);
return driver;
Expand Down
60 changes: 57 additions & 3 deletions src/plugin/DesktopPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ class DesktopPlugin : public Plugin
continue;
}

if (midiEvents->frame >= i)
if (midiEvents->frame >= framesDone + 128)
break;

shm.data->midiFrames[mec] = ti + (midiEvents->frame - i);
shm.data->midiFrames[mec] = midiEvents->frame - framesDone;
shm.data->midiData[mec * 4 + 0] = midiEvents->data[0];
shm.data->midiData[mec * 4 + 1] = midiEvents->data[1];
shm.data->midiData[mec * 4 + 2] = midiEvents->data[2];
Expand All @@ -284,8 +284,8 @@ class DesktopPlugin : public Plugin
for (uint16_t j = 0; j < shm.data->midiEventCount; ++j)
{
MidiEvent midiEvent = {
framesDone + shm.data->midiFrames[j] - to,
4,
i + shm.data->midiFrames[j],
{
shm.data->midiData[j * 4 + 0],
shm.data->midiData[j * 4 + 1],
Expand Down Expand Up @@ -325,6 +325,33 @@ class DesktopPlugin : public Plugin
{
std::memset(outputs[0], 0, sizeof(float) * frames);
std::memset(outputs[1], 0, sizeof(float) * frames);

if (midiEventCount != 0)
{
uint16_t mec = shm.data->midiEventCount;

while (midiEventCount != 0 && mec != 511)
{
if (midiEvents->size > 4)
{
--midiEventCount;
++midiEvents;
continue;
}

shm.data->midiFrames[mec] = midiEvents->frame;
shm.data->midiData[mec * 4 + 0] = midiEvents->data[0];
shm.data->midiData[mec * 4 + 1] = midiEvents->data[1];
shm.data->midiData[mec * 4 + 2] = midiEvents->data[2];
shm.data->midiData[mec * 4 + 3] = midiEvents->data[3];

--midiEventCount;
++midiEvents;
++mec;
}

shm.data->midiEventCount = mec;
}
}
else if (framesDone != frames)
{
Expand All @@ -335,6 +362,33 @@ class DesktopPlugin : public Plugin
to -= framesToCopy;
std::memmove(tmpBuffers[0], tmpBuffers[0] + framesToCopy, sizeof(float) * to);
std::memmove(tmpBuffers[1], tmpBuffers[1] + framesToCopy, sizeof(float) * to);

if (midiEventCount != 0)
{
uint16_t mec = shm.data->midiEventCount;

while (midiEventCount != 0 && mec != 511)
{
if (midiEvents->size > 4)
{
--midiEventCount;
++midiEvents;
continue;
}

shm.data->midiFrames[mec] = ti + framesDone - midiEvents->frame;
shm.data->midiData[mec * 4 + 0] = midiEvents->data[0];
shm.data->midiData[mec * 4 + 1] = midiEvents->data[1];
shm.data->midiData[mec * 4 + 2] = midiEvents->data[2];
shm.data->midiData[mec * 4 + 3] = midiEvents->data[3];

--midiEventCount;
++midiEvents;
++mec;
}

shm.data->midiEventCount = mec;
}
}

numFramesInShmBuffer = ti;
Expand Down

0 comments on commit 4b5ce2b

Please sign in to comment.