Skip to content

AUv2: deliver queued parameter sets before a state save or restore - #656

Open
drbafflegab wants to merge 2 commits into
free-audio:nextfrom
drbafflegab:auv2-state-drain
Open

drbafflegab wants to merge 2 commits into
free-audio:nextfrom
drbafflegab:auv2-state-drain

Conversation

@drbafflegab

@drbafflegab drbafflegab commented Nov 27, 2026 •

Copy link
Copy Markdown

When a host sets a parameter, the AUv2 wrapper queues the value. The plugin only gets it on the next render or idle flush. SaveState and RestoreState don't flush the queue first, so:

  • a state saved right after a set doesn't contain the new value, and
  • values still queued when a state is restored reach the plugin afterwards and override it.

To reproduce, set a parameter and save the state (kAudioUnitProperty_ClassInfo) without rendering in between. The saved state has the old value.

This PR adds drainParameterEvents(), which does what onIdle() does for a pending flush, and only when onIdle() would: when the plugin is deactivated, or when the host hasn't rendered for a few blocks. SaveState and RestoreState call it before they call the plugin's state extension. It holds _processLock only for the drain, not across the save or load, so neither Render() nor ScheduleParameter() on the audio thread waits for the plugin's state code. _idleTicksSinceRender now starts saturated, so a new instance that hasn't rendered yet counts as stopped.

While the host is rendering, the drain is skipped and the next render delivers the queue instead. So during playback, a save can still miss a set made in the last buffer, and a set made just before a restore can still land after it.

Tested with auval -strict, pluginval (strictness 10, including AU state restoration) and a small AU host that fails without this change and also checks that saving state while rendering costs no block.

SetParameter queues values that only Render() and onIdle() deliver, so a save could miss a recent set and a queued set could override a restored state. SaveState and RestoreState now drain the queue first, when onIdle() would, holding _processLock only for the drain.
Comment thread src/wrapasauv2.cpp
Comment on lines +2372 to +2374
const bool rendering =
_initialized && (_renderedSinceIdle || _idleTicksSinceRender < _idleTicksBeforeFlush);
if (!_requestedFlush || rendering) return;

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants