Update active voices with interval based on system clock

This commit is contained in:
Jean Pierre Cimalando 2021-04-03 17:47:31 +02:00
parent 12896fe168
commit 0bcfbc5495
2 changed files with 24 additions and 36 deletions

View file

@ -39,11 +39,10 @@ static const char* kRingIdOsc = "Osc";
static const char* kMsgIdSetNumVoices = "SetNumVoices"; static const char* kMsgIdSetNumVoices = "SetNumVoices";
static const char* kMsgIdSetOversampling = "SetOversampling"; static const char* kMsgIdSetOversampling = "SetOversampling";
static const char* kMsgIdSetPreloadSize = "SetPreloadSize"; static const char* kMsgIdSetPreloadSize = "SetPreloadSize";
static const char* kMsgIdNotifyPlayState = "NotifyPlayState";
static const char* kMsgIdReceiveMessage = "ReceiveMessage"; static const char* kMsgIdReceiveMessage = "ReceiveMessage";
static const char* kMsgIdNoteEvents = "NoteEvents"; static const char* kMsgIdNoteEvents = "NoteEvents";
static constexpr std::chrono::milliseconds kBackgroundIdleInterval { 500 }; static constexpr std::chrono::milliseconds kBackgroundIdleInterval { 50 };
SfizzVstProcessor::SfizzVstProcessor() SfizzVstProcessor::SfizzVstProcessor()
: _fifoToWorker(64 * 1024), _fifoMessageFromUi(64 * 1024), : _fifoToWorker(64 * 1024), _fifoMessageFromUi(64 * 1024),
@ -219,9 +218,6 @@ tresult PLUGIN_API SfizzVstProcessor::setActive(TBool state)
if (state) { if (state) {
synth->setSampleRate(processSetup.sampleRate); synth->setSampleRate(processSetup.sampleRate);
synth->setSamplesPerBlock(processSetup.maxSamplesPerBlock); synth->setSamplesPerBlock(processSetup.maxSamplesPerBlock);
_playStateChangePeriod = static_cast<uint32>(50e-3 * processSetup.sampleRate);
startBackgroundWork(); startBackgroundWork();
} else { } else {
stopBackgroundWork(); stopBackgroundWork();
@ -291,15 +287,6 @@ tresult PLUGIN_API SfizzVstProcessor::process(Vst::ProcessData& data)
synth.sendMessage(client, 0, "/cc/changed~", "", nullptr); synth.sendMessage(client, 0, "/cc/changed~", "", nullptr);
synth.sendMessage(client, 0, "/sw/last/current", "", nullptr); synth.sendMessage(client, 0, "/sw/last/current", "", nullptr);
_playStateChangeCounter += numFrames;
if (_playStateChangeCounter > _playStateChangePeriod) {
_playStateChangeCounter %= _playStateChangePeriod;
SfizzPlayState playState {};
playState.activeVoices = synth.getNumActiveVoices();
if (writeWorkerMessage(kMsgIdNotifyPlayState, &playState, sizeof(playState)))
_semaToWorker.post();
}
// //
std::pair<uint32, float> noteEvents[128]; std::pair<uint32, float> noteEvents[128];
size_t numNoteEvents = 0; size_t numNoteEvents = 0;
@ -663,6 +650,7 @@ void SfizzVstProcessor::doBackgroundWork()
bool haveDoneIdleWork = false; bool haveDoneIdleWork = false;
Clock::time_point lastIdleWorkTime; Clock::time_point lastIdleWorkTime;
size_t idleCounter = 0;
for (;;) { for (;;) {
bool isNotified = _semaToWorker.timed_wait(kBackgroundIdleInterval.count()); bool isNotified = _semaToWorker.timed_wait(kBackgroundIdleInterval.count());
@ -697,13 +685,6 @@ void SfizzVstProcessor::doBackgroundWork()
std::lock_guard<SpinMutex> lock(_processMutex); std::lock_guard<SpinMutex> lock(_processMutex);
_synth->setPreloadSize(value); _synth->setPreloadSize(value);
} }
else if (id == kMsgIdNotifyPlayState) {
SfizzPlayState playState = *msg->payload<SfizzPlayState>();
Steinberg::OPtr<Vst::IMessage> notification { allocateMessage() };
notification->setMessageID("NotifiedPlayState");
notification->getAttributes()->setBinary("PlayState", &playState, sizeof(playState));
sendMessage(notification);
}
else if (id == kMsgIdReceiveMessage) { else if (id == kMsgIdReceiveMessage) {
Steinberg::OPtr<Vst::IMessage> notification { allocateMessage() }; Steinberg::OPtr<Vst::IMessage> notification { allocateMessage() };
notification->setMessageID("ReceivedMessage"); notification->setMessageID("ReceivedMessage");
@ -719,24 +700,35 @@ void SfizzVstProcessor::doBackgroundWork()
Clock::time_point currentTime = Clock::now(); Clock::time_point currentTime = Clock::now();
if (!haveDoneIdleWork || currentTime - lastIdleWorkTime > kBackgroundIdleInterval) { if (!haveDoneIdleWork || currentTime - lastIdleWorkTime > kBackgroundIdleInterval) {
doBackgroundIdle(); doBackgroundIdle(idleCounter++);
haveDoneIdleWork = true; haveDoneIdleWork = true;
lastIdleWorkTime = currentTime; lastIdleWorkTime = currentTime;
} }
} }
} }
void SfizzVstProcessor::doBackgroundIdle() void SfizzVstProcessor::doBackgroundIdle(size_t idleCounter)
{ {
if (_synth->shouldReloadFile()) { {
fprintf(stderr, "[Sfizz] sfz file has changed, reloading\n"); SfizzPlayState ps;
std::lock_guard<SpinMutex> lock(_processMutex); ps.activeVoices = _synth->getNumActiveVoices();
loadSfzFileOrDefault(_state.sfzFile); Steinberg::OPtr<Vst::IMessage> notification { allocateMessage() };
notification->setMessageID("NotifiedPlayState");
notification->getAttributes()->setBinary("PlayState", &ps, sizeof(ps));
sendMessage(notification);
} }
if (_synth->shouldReloadScala()) {
fprintf(stderr, "[Sfizz] scala file has changed, reloading\n"); if (idleCounter % 10 == 0) {
std::lock_guard<SpinMutex> lock(_processMutex); if (_synth->shouldReloadFile()) {
_synth->loadScalaFile(_state.scalaFile); fprintf(stderr, "[Sfizz] sfz file has changed, reloading\n");
std::lock_guard<SpinMutex> lock(_processMutex);
loadSfzFileOrDefault(_state.sfzFile);
}
if (_synth->shouldReloadScala()) {
fprintf(stderr, "[Sfizz] scala file has changed, reloading\n");
std::lock_guard<SpinMutex> lock(_processMutex);
_synth->loadScalaFile(_state.scalaFile);
}
} }
} }

View file

@ -75,10 +75,6 @@ private:
Ring_Buffer _fifoMessageFromUi; Ring_Buffer _fifoMessageFromUi;
SpinMutex _processMutex; SpinMutex _processMutex;
// state notification periodic timer
uint32 _playStateChangeCounter = 0;
uint32 _playStateChangePeriod = 0;
// time info // time info
int _timeSigNumerator = 0; int _timeSigNumerator = 0;
int _timeSigDenominator = 0; int _timeSigDenominator = 0;
@ -98,7 +94,7 @@ private:
// worker // worker
void doBackgroundWork(); void doBackgroundWork();
void doBackgroundIdle(); void doBackgroundIdle(size_t idleCounter);
void startBackgroundWork(); void startBackgroundWork();
void stopBackgroundWork(); void stopBackgroundWork();
// writer // writer