Allow VST to reload its files automatically

This commit is contained in:
Jean Pierre Cimalando 2020-03-28 15:08:54 +01:00
parent 99f0833a96
commit fa65fd65f7
2 changed files with 33 additions and 0 deletions

View file

@ -117,6 +117,16 @@ tresult PLUGIN_API SfizzVstProcessor::setActive(TBool state)
synth->setSampleRate(processSetup.sampleRate);
synth->setSamplesPerBlock(processSetup.maxSamplesPerBlock);
_fileChangePeriod = static_cast<uint32>(processSetup.sampleRate);
if (!_msgCheckShouldReload) {
auto msg = IPtr<Vst::IMessage>::adopt(allocateMessage());
if (!msg)
return kResultFalse;
msg->setMessageID("CheckShouldReload");
_msgCheckShouldReload = msg;
}
_workRunning = true;
_worker = std::thread([this]() { doBackgroundWork(); });
} else {
@ -169,6 +179,17 @@ tresult PLUGIN_API SfizzVstProcessor::process(Vst::ProcessData& data)
synth.setVolume(_state.volume);
synth.renderBlock(outputs, numFrames, numChannels);
_fileChangeCounter += numFrames;
if (_fileChangeCounter > _fileChangePeriod) {
_fileChangeCounter %= _fileChangePeriod;
Vst::IMessage* msg = _msgCheckShouldReload.get();
if (_fifoToWorker.push(msg)) {
msg->addRef();
_semaToWorker.post();
}
}
return kResultTrue;
}
@ -380,6 +401,13 @@ void SfizzVstProcessor::doBackgroundWork()
_synth->setPreloadSize(value);
}
}
else if (!std::strcmp(id, "CheckShouldReload")) {
if (_synth->shouldReloadFile()) {
fprintf(stderr, "[Sfizz] file has changed, reloading\n");
std::lock_guard<std::mutex> lock(_processMutex);
_synth->loadSfzFile(_state.sfzFile);
}
}
msg->release();
}

View file

@ -55,6 +55,11 @@ private:
RTSemaphore _semaToWorker;
std::mutex _processMutex;
// file modification periodic checker
uint32 _fileChangeCounter = 0;
uint32 _fileChangePeriod = 0;
IPtr<Vst::IMessage> _msgCheckShouldReload;
// worker
void doBackgroundWork();
void stopBackgroundWork();