From 0a63256b651a8a27650ef1701948698897a7634c Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Thu, 18 Mar 2021 02:17:06 +0100 Subject: [PATCH] Background work when idle --- plugins/vst/SfizzVstProcessor.cpp | 75 +++++++++++++++++-------------- plugins/vst/SfizzVstProcessor.h | 5 +-- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/plugins/vst/SfizzVstProcessor.cpp b/plugins/vst/SfizzVstProcessor.cpp index 93519ee6..711aa049 100644 --- a/plugins/vst/SfizzVstProcessor.cpp +++ b/plugins/vst/SfizzVstProcessor.cpp @@ -37,7 +37,6 @@ static const char* kRingIdOsc = "Osc"; static const char* kMsgIdSetNumVoices = "SetNumVoices"; static const char* kMsgIdSetOversampling = "SetOversampling"; static const char* kMsgIdSetPreloadSize = "SetPreloadSize"; -static const char* kMsgIdCheckShouldReload = "CheckShouldReload"; static const char* kMsgIdNotifyPlayState = "NotifyPlayState"; static const char* kMsgIdReceiveMessage = "ReceiveMessage"; static const char* kMsgIdNoteEvents = "NoteEvents"; @@ -204,7 +203,6 @@ tresult PLUGIN_API SfizzVstProcessor::setActive(TBool state) synth->setSampleRate(processSetup.sampleRate); synth->setSamplesPerBlock(processSetup.maxSamplesPerBlock); - _fileChangePeriod = static_cast(1.0 * processSetup.sampleRate); _playStateChangePeriod = static_cast(50e-3 * processSetup.sampleRate); startBackgroundWork(); @@ -271,13 +269,6 @@ tresult PLUGIN_API SfizzVstProcessor::process(Vst::ProcessData& data) synth.renderBlock(outputs, numFrames, numChannels); - _fileChangeCounter += numFrames; - if (_fileChangeCounter > _fileChangePeriod) { - _fileChangeCounter %= _fileChangePeriod; - if (writeWorkerMessage(kMsgIdCheckShouldReload, nullptr, 0)) - _semaToWorker.post(); - } - _playStateChangeCounter += numFrames; if (_playStateChangeCounter > _playStateChangePeriod) { _playStateChangeCounter %= _playStateChangePeriod; @@ -640,19 +631,28 @@ void SfizzVstProcessor::loadSfzFileOrDefault(sfz::Sfizz& synth, const std::strin void SfizzVstProcessor::doBackgroundWork() { + using Clock = std::chrono::steady_clock; + + bool haveDoneIdleWork = false; + Clock::time_point lastIdleWorkTime; + for (;;) { - _semaToWorker.wait(); + bool isNotified = _semaToWorker.timed_wait(1000); if (!_workRunning) break; - RTMessagePtr msg = readWorkerMessage(); - if (!msg) { - fprintf(stderr, "[Sfizz] message synchronization error in worker\n"); - std::abort(); - } + const char* id = nullptr; + RTMessagePtr msg; - const char* id = msg->type; + if (isNotified) { + msg = readWorkerMessage(); + if (!msg) { + fprintf(stderr, "[Sfizz] message synchronization error in worker\n"); + std::abort(); + } + id = msg->type; + } if (id == kMsgIdSetNumVoices) { int32 value = *msg->payload(); @@ -669,23 +669,6 @@ void SfizzVstProcessor::doBackgroundWork() std::lock_guard lock(_processMutex); _synth->setPreloadSize(value); } - else if (id == kMsgIdCheckShouldReload) { - if (_synth->shouldReloadFile()) { - fprintf(stderr, "[Sfizz] sfz file has changed, reloading\n"); - std::lock_guard lock(_processMutex); - loadSfzFileOrDefault(*_synth, _state.sfzFile); - - Steinberg::OPtr reply { allocateMessage() }; - reply->setMessageID("LoadedSfz"); - reply->getAttributes()->setBinary("File", _state.sfzFile.data(), _state.sfzFile.size()); - sendMessage(reply); - } - else if (_synth->shouldReloadScala()) { - fprintf(stderr, "[Sfizz] scala file has changed, reloading\n"); - std::lock_guard lock(_processMutex); - _synth->loadScalaFile(_state.scalaFile); - } - } else if (id == kMsgIdNotifyPlayState) { SfizzPlayState playState = *msg->payload(); Steinberg::OPtr notification { allocateMessage() }; @@ -705,6 +688,32 @@ void SfizzVstProcessor::doBackgroundWork() notification->getAttributes()->setBinary("Events", msg->payload(), msg->size); sendMessage(notification); } + + Clock::time_point currentTime = Clock::now(); + if (!haveDoneIdleWork || currentTime - lastIdleWorkTime > std::chrono::seconds(1)) { + doBackgroundIdle(); + haveDoneIdleWork = true; + lastIdleWorkTime = currentTime; + } + } +} + +void SfizzVstProcessor::doBackgroundIdle() +{ + if (_synth->shouldReloadFile()) { + fprintf(stderr, "[Sfizz] sfz file has changed, reloading\n"); + std::lock_guard lock(_processMutex); + loadSfzFileOrDefault(*_synth, _state.sfzFile); + + Steinberg::OPtr reply { allocateMessage() }; + reply->setMessageID("LoadedSfz"); + reply->getAttributes()->setBinary("File", _state.sfzFile.data(), _state.sfzFile.size()); + sendMessage(reply); + } + if (_synth->shouldReloadScala()) { + fprintf(stderr, "[Sfizz] scala file has changed, reloading\n"); + std::lock_guard lock(_processMutex); + _synth->loadScalaFile(_state.scalaFile); } } diff --git a/plugins/vst/SfizzVstProcessor.h b/plugins/vst/SfizzVstProcessor.h index 2219ab6a..f34e1b4d 100644 --- a/plugins/vst/SfizzVstProcessor.h +++ b/plugins/vst/SfizzVstProcessor.h @@ -72,10 +72,6 @@ private: Ring_Buffer _fifoMessageFromUi; SpinMutex _processMutex; - // file modification periodic checker - uint32 _fileChangeCounter = 0; - uint32 _fileChangePeriod = 0; - // state notification periodic timer uint32 _playStateChangeCounter = 0; uint32 _playStateChangePeriod = 0; @@ -99,6 +95,7 @@ private: // worker void doBackgroundWork(); + void doBackgroundIdle(); void startBackgroundWork(); void stopBackgroundWork(); // writer