Skip to content

Commit

Permalink
Correct the Threading for JUCE Patch Load (surge-synthesizer#5547)
Browse files Browse the repository at this point in the history
I actually had this API from the old VST3 and had just mis-ported
JUCE. So this makes sure the raw data load happens properly on teh
audio thread and then bounces back to the UI, with just a slight
mod on the VST3 code.

Closes surge-synthesizer#5544
  • Loading branch information
baconpaul authored Dec 3, 2021
1 parent 8775f73 commit ccf5d84
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/common/SurgeSynthesizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3370,6 +3370,8 @@ void SurgeSynthesizer::processThreadunsafeOperations(bool dangerMode)
{
if (!audio_processing_active || dangerMode)
{
processEnqueuedPatchIfNeeded();

// if the audio processing is inactive, patchloading should occur anyway
if (patchid_queue >= 0)
{
Expand Down
6 changes: 3 additions & 3 deletions src/common/SurgeSynthesizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,10 @@ class alignas(16) SurgeSynthesizer
public:
std::atomic<bool> rawLoadEnqueued{false}, rawLoadNeedsUIDawExtraState{false};
std::mutex rawLoadQueueMutex;
void *enqueuedLoadData{nullptr}; // if this is set I need to free it
std::unique_ptr<char[]> enqueuedLoadData{nullptr}; // if this is set I need to free it
int enqueuedLoadSize{0};
void enqueuePatchForLoad(void *data, int size); // safe from any thread
void processEnqueuedPatchIfNeeded(); // only safe from audio thread
void enqueuePatchForLoad(const void *data, int size); // safe from any thread
void processEnqueuedPatchIfNeeded(); // only safe from audio thread

void loadRaw(const void *data, int size, bool preset = false);
void loadPatch(int id);
Expand Down
15 changes: 4 additions & 11 deletions src/common/SurgeSynthesizerIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,13 @@ bool SurgeSynthesizer::loadPatchByPath(const char *fxpPath, int categoryId, cons
return true;
}

void SurgeSynthesizer::enqueuePatchForLoad(void *data, int size)
void SurgeSynthesizer::enqueuePatchForLoad(const void *data, int size)
{
{
std::lock_guard<std::mutex> g(rawLoadQueueMutex);

if (enqueuedLoadData) // this means we missed one because we only free under the lock
free(enqueuedLoadData);

enqueuedLoadData = data;
enqueuedLoadData.reset(new char[size]);
memcpy(enqueuedLoadData.get(), data, size);
enqueuedLoadSize = size;
rawLoadEnqueued = true;
rawLoadNeedsUIDawExtraState = false;
Expand All @@ -377,20 +375,15 @@ void SurgeSynthesizer::enqueuePatchForLoad(void *data, int size)
void SurgeSynthesizer::processEnqueuedPatchIfNeeded()
{
bool expected = true;
void *freeThis = nullptr;
if (rawLoadEnqueued.compare_exchange_weak(expected, true) && expected)
{
std::lock_guard<std::mutex> g(rawLoadQueueMutex);
rawLoadEnqueued = false;
loadRaw(enqueuedLoadData, enqueuedLoadSize);
loadRaw(enqueuedLoadData.get(), enqueuedLoadSize);
loadFromDawExtraState();

freeThis = enqueuedLoadData;
enqueuedLoadData = nullptr;
rawLoadNeedsUIDawExtraState = true;
}
if (freeThis)
free(freeThis); // do this outside the lock
}

void SurgeSynthesizer::loadRaw(const void *data, int size, bool preset)
Expand Down
11 changes: 2 additions & 9 deletions src/surge-xt/SurgeSynthProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,8 @@ void SurgeSynthProcessor::getStateInformation(juce::MemoryBlock &destData)

void SurgeSynthProcessor::setStateInformation(const void *data, int sizeInBytes)
{
// FIXME - casting away constness is gross
surge->loadRaw(data, sizeInBytes, false);

surge->loadFromDawExtraState();
auto sse = dynamic_cast<SurgeSynthEditor *>(getActiveEditor());
if (sse)
{
sse->populateFromStreaming(surge.get());
}
surge->enqueuePatchForLoad(data, sizeInBytes);
surge->processThreadunsafeOperations();
}

void SurgeSynthProcessor::surgeParameterUpdated(const SurgeSynthesizer::ID &id, float f)
Expand Down

0 comments on commit ccf5d84

Please sign in to comment.