From dec1664c2fb7283fece640f61b4050ebf812112b Mon Sep 17 00:00:00 2001 From: paulfd Date: Mon, 16 Sep 2019 18:17:45 +0200 Subject: [PATCH] Added a double atomic guard when changing the sfz file; could be used to change the number of voices too --- sfizz/Synth.cpp | 16 ++++++++++++++++ sfizz/Synth.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/sfizz/Synth.cpp b/sfizz/Synth.cpp index 13b72566..ec5f7f52 100644 --- a/sfizz/Synth.cpp +++ b/sfizz/Synth.cpp @@ -29,8 +29,10 @@ #include "StringViewHelpers.h" #include "absl/algorithm/container.h" #include +#include #include #include +using namespace std::literals; sfz::Synth::Synth() { @@ -179,6 +181,11 @@ void addEndpointsToVelocityCurve(sfz::Region& region) bool sfz::Synth::loadSfzFile(const std::filesystem::path& filename) { + canEnterCallback = false; + while (inCallback) { + std::this_thread::sleep_for(1ms); + } + clear(); auto parserReturned = sfz::Parser::loadSfzFile(filename); if (!parserReturned) @@ -237,6 +244,8 @@ bool sfz::Synth::loadSfzFile(const std::filesystem::path& filename) DBG("Removed " << regions.size() - std::distance(regions.begin(), lastRegion) - 1 << " out of " << regions.size() << " regions."); regions.resize(std::distance(regions.begin(), lastRegion) + 1); + + canEnterCallback = true; return parserReturned; } @@ -302,11 +311,18 @@ void sfz::Synth::renderBlock(AudioSpan buffer) noexcept { ScopedFTZ ftz; buffer.fill(0.0f); + if (!canEnterCallback) + return; + + inCallback = true; + auto tempSpan = AudioSpan(tempBuffer).first(buffer.getNumFrames()); for (auto& voice : voices) { voice->renderBlock(tempSpan); buffer.add(tempSpan); } + + inCallback = false; } void sfz::Synth::noteOn(int delay, int channel, int noteNumber, uint8_t velocity) noexcept diff --git a/sfizz/Synth.h b/sfizz/Synth.h index ab1dcb7e..4954b37a 100644 --- a/sfizz/Synth.h +++ b/sfizz/Synth.h @@ -100,6 +100,9 @@ private: std::uniform_real_distribution randNoteDistribution { 0, 1 }; unsigned fileTicket { 1 }; + std::atomic canEnterCallback { true }; + std::atomic inCallback { false }; + LEAK_DETECTOR(Synth); };