Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/detail/auv2/auv2_base_classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <atomic>
#include <chrono>
#include <iostream>
#include <limits>
#include <memory>
#include <map>
#include <mutex>
Expand Down Expand Up @@ -884,6 +885,8 @@ class WrapAsAUV2 : public ausdk::AUBase,
// parameter-only round trip on the deactivated-case adapter, for when no render
// is running to carry the events. Only legal while the CLAP is deactivated.
void flushParameters();
// delivers the queued parameter sets to the plugin unless a render is about to; main thread
void drainParameterEvents();
// the AU-level half of the teardown, which an internal restart must not do
void releaseHostMIDIOutput();
bool IsBypassEffect()
Expand Down Expand Up @@ -1086,8 +1089,9 @@ class WrapAsAUV2 : public ausdk::AUBase,
// has carried the plugin's events since it last looked. See onIdle().
std::atomic_bool _renderedSinceIdle = false;
// consecutive idle ticks that saw no render, saturating at the point where the
// idle tick takes over the flushing. Only onIdle() touches it.
uint32_t _idleTicksSinceRender = 0;
// idle tick takes over the flushing; a new instance has seen none. Only onIdle()
// writes it, and its readers hold the AU mutex onIdle() runs under.
uint32_t _idleTicksSinceRender = std::numeric_limits<uint32_t>::max();
// how many of those ticks make a stopped host, sized from the block period in
// activateCLAP(). See onIdle().
static constexpr double kIdleTickMs = 10.0;
Expand Down
25 changes: 25 additions & 0 deletions src/wrapasauv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,28 @@ void WrapAsAUV2::flushParameters()
}
}

void WrapAsAUV2::drainParameterEvents()
{
// onIdle()'s stopped-host test: while a host renders, its next render delivers the queue
const bool rendering =
_initialized && (_renderedSinceIdle || _idleTicksSinceRender < _idleTicksBeforeFlush);
if (!_requestedFlush || rendering) return;
Comment on lines +2373 to +2375

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One gap worth noting: drainParameterEvents() skips the drain while the host is rendering, so both halves of the original bug still happen during playback, not just the restore half the description mentions.

  • Save: a ClassInfo get right after a SetParameter saves the old value, because the set is still queued until the next render.
  • Restore: values queued before the restore reach the plugin one buffer after state->load() and overwrite the restored state.

Could you add the save case to the description, and maybe to the comment on drainParameterEvents()?

For restore, a follow-up could throw the pending host parameter events away instead of delivering them. The load supersedes them anyway, and dropping them runs no plugin code, so it could also be done while rendering. The cost is that it still needs _processLock for a moment, and since Render() only try-locks, a render that collides with it outputs one silent block. So it's a trade-off, not a clear win, and I don't think it needs to block this PR.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'v updated the PR description and the comment on drainParameterEvents(). Dropping the queued events on restore sounds like a good follow-up.


ClapWrapper::detail::shared::SpinLockGuard processGuard(_processLock);

_requestedFlush = false;

if (_initialized && _processAdapter)
{
auto guarantee_audiothread = _plugin->AlwaysAudioThread();
_processAdapter->flush();
}
else
{
flushParameters();
}
}

OSStatus WrapAsAUV2::SaveState(CFPropertyListRef *ptPList)
{
if (!ptPList) return kAudioUnitErr_InvalidParameter;
Expand All @@ -2381,6 +2403,7 @@ OSStatus WrapAsAUV2::SaveState(CFPropertyListRef *ptPList)
else
{
Clap::StateMemento chunk;
drainParameterEvents();
_plugin->_ext._state->save(_plugin->_plugin, chunk);

#if DICTIONARY_STREAM_FORMAT_JUCE
Expand Down Expand Up @@ -2494,6 +2517,7 @@ OSStatus WrapAsAUV2::RestoreState(CFPropertyListRef plist)
UInt8 *streamData = (UInt8 *)(CFDataGetBytePtr(juceData));

chunk.setData(streamData, numBytes);
drainParameterEvents();
_plugin->_ext._state->load(_plugin->_plugin, chunk);
}
return noErr;
Expand All @@ -2511,6 +2535,7 @@ OSStatus WrapAsAUV2::RestoreState(CFPropertyListRef plist)
{
Clap::StateMemento chunk;
chunk.setData(pData, lLen);
drainParameterEvents();
_plugin->_ext._state->load(_plugin->_plugin, chunk);
}
}
Expand Down
Loading