Merge pull request #122 from paulfd/vst-setenable

Avoid purging and recreating the synth in Reaper
This commit is contained in:
Paul Ferrand 2020-03-16 09:45:43 +01:00 committed by GitHub
commit 7a8b290c61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 66 additions and 9 deletions

View file

@ -416,7 +416,8 @@ activate(LV2_Handle instance)
static void
deactivate(LV2_Handle instance)
{
UNUSED(instance);
sfizz_plugin_t *self = (sfizz_plugin_t *)instance;
sfizz_all_sound_off(self->synth);
}
static void

View file

@ -369,6 +369,13 @@ SFIZZ_EXPORTED_API void sfizz_enable_logging(sfizz_synth_t* synth);
*/
SFIZZ_EXPORTED_API void sfizz_disable_logging(sfizz_synth_t* synth);
/**
* @brief Shuts down the current processing, clear buffers and reset the voices.
*
* @param synth
*/
SFIZZ_EXPORTED_API void sfizz_all_sound_off(sfizz_synth_t* synth);
#ifdef __cplusplus
}
#endif

View file

@ -256,6 +256,11 @@ public:
*
*/
void disableLogging() noexcept;
/**
* @brief Shuts down the current processing, clear buffers and reset the voices.
*
*/
void allSoundOff() noexcept;
private:
std::unique_ptr<sfz::Synth> synth;
};

View file

@ -962,3 +962,16 @@ void sfz::Synth::disableLogging() noexcept
{
resources.logger.disableLogging();
}
void sfz::Synth::allSoundOff() noexcept
{
AtomicDisabler callbackDisabler{ canEnterCallback };
while (inCallback) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
for (auto &voice: voices)
voice->reset();
for (auto& effectBus: effectBuses)
effectBus->clear();
}

View file

@ -361,6 +361,12 @@ public:
*
*/
void disableLogging() noexcept;
/**
* @brief Shuts down the current processing, clear buffers and reset the voices.
*
*/
void allSoundOff() noexcept;
protected:
/**
* @brief The parser callback; this is called by the parent object each time

View file

@ -195,3 +195,8 @@ void sfz::Sfizz::disableLogging() noexcept
{
synth->disableLogging();
}
void sfz::Sfizz::allSoundOff() noexcept
{
synth->allSoundOff();
}

View file

@ -237,12 +237,19 @@ void sfizz_enable_logging(sfizz_synth_t* synth)
auto self = reinterpret_cast<sfz::Synth*>(synth);
return self->enableLogging();
}
void sfizz_disable_logging(sfizz_synth_t* synth)
{
auto self = reinterpret_cast<sfz::Synth*>(synth);
return self->disableLogging();
}
void sfizz_all_sound_off(sfizz_synth_t* synth)
{
auto self = reinterpret_cast<sfz::Synth*>(synth);
return self->allSoundOff();
}
#ifdef __cplusplus
}
#endif

View file

@ -26,6 +26,17 @@ TEST_CASE("[Synth] Play and check active voices")
REQUIRE(synth.getNumActiveVoices() == 0);
}
TEST_CASE("[Synth] All sound off")
{
sfz::Synth synth;
synth.loadSfzFile(fs::current_path() / "tests/TestFiles/groups_avl.sfz");
synth.noteOn(0, 36, 24);
synth.noteOn(0, 36, 89);
REQUIRE(synth.getNumActiveVoices() == 2);
synth.allSoundOff();
REQUIRE(synth.getNumActiveVoices() == 0);
}
TEST_CASE("[Synth] Change the number of voice while playing")
{
sfz::Synth synth;

View file

@ -46,6 +46,9 @@ tresult PLUGIN_API SfizzVstProcessor::initialize(FUnknown* context)
_state = SfizzVstState();
fprintf(stderr, "[sfizz] new synth\n");
_synth.reset(new sfz::Sfizz);
return result;
}
@ -105,21 +108,20 @@ tresult PLUGIN_API SfizzVstProcessor::canProcessSampleSize(int32 symbolicSampleS
tresult PLUGIN_API SfizzVstProcessor::setActive(TBool state)
{
stopBackgroundWork();
_synth.reset();
sfz::Sfizz* synth = _synth.get();
if (!synth)
return kResultFalse;
if (state) {
fprintf(stderr, "[Sfizz] new synth\n");
sfz::Sfizz* synth = new sfz::Sfizz;
_synth.reset(synth);
synth->setSampleRate(processSetup.sampleRate);
synth->setSamplesPerBlock(processSetup.maxSamplesPerBlock);
syncStateToSynth();
_workRunning = true;
_worker = std::thread([this]() { doBackgroundWork(); });
} else {
synth->allSoundOff();
stopBackgroundWork();
}
return kResultTrue;