Repository navigation
Performance: Prevent UI unresponsiveness when rapidly adjusting output device volume - #103
Conversation
|
Thanks, but the PR needs a bit of rework:
Also, |
Thanks for the detailed review. Fair warning: this is quite a lot for me, as I have very limited experience with C# and WPF. I therefore delegated the implementation to Claude and focused on reviewing the results, cleaning things up, and requesting changes wherever something looked like—or could potentially be considered—slop code. Hopefully, everything looks good at a glance. I also tried to preserve the existing comments and coding style as much as possible. Here is what changed for each point:
All The 2 second limit that |
G-Stas
left a comment
There was a problem hiding this comment.
A few things to fix and it's ready to merge
Hopefully all three points are fixed:
Tests updated: Thank you for your patience and reviews. |
| /// <param name="volumeWriter">Writes the volume to the device</param> | ||
| /// <param name="muteWriter">Writes the mute state to the device</param> | ||
| /// <param name="background">False writes synchronously on the caller's thread instead of on the worker (for tests)</param> | ||
| internal MasterAudioSessionVolume(Action<int, Guid> volumeWriter, Action<bool, Guid> muteWriter, bool background) |
There was a problem hiding this comment.
background: false is only used to test itself; it can go, together with audioVolume = null!.
| return; | ||
| VolumeChangedEventArgs deviceState = state.Value; | ||
| //the properties this changes belong to the UI thread | ||
| var dispatcher = System.Windows.Application.Current?.Dispatcher; |
There was a problem hiding this comment.
Please use the dispatcher helper the other models have (App.Current?.Dispatcher ?? Dispatcher.CurrentDispatcher
| private void ShowDeviceState(VolumeChangedEventArgs real) | ||
| { | ||
| //the device may have changed while we were writing (its notification was dropped) or never got what the UI shows: | ||
| //show what it really has, unless a newer write is already on its way | ||
| if(disposed || masterVolume.HasPendingWrites) | ||
| return; | ||
| ApplyDeviceState(real); | ||
| } |
There was a problem hiding this comment.
Small race: the worker reads the device (50), then the user presses a volume key (52). OnVolumeChanged applies 52, because busy is already false. Then WritesCompleted with 50 arrives here and sets 50 back. The mixer shows 50 while the device is at 52, until the next change. Please read the device again here instead of using the state from the worker, or skip the state if a notification came in after it was read.
There was a problem hiding this comment.
Small race: the worker...
Thanks, all three seem done:
background: false: removed, together withaudioVolume = null!. The internal constructor ofMasterAudioSessionVolumenow takes the two writers plus the two readers (volume and mute), so the tests use the same worker as production. The two synchronous-mode tests are gone.- Dispatcher:
MasterSessionModelnow uses the sameApp.Current?.Dispatcher ?? Dispatcher.CurrentDispatcherhelper as the other models. - Race in
ShowDeviceState: the worker no longer reads the device.WritesCompletednow only reports the error (or null) once the worker is idle, andMasterSessionModel.ShowDeviceStatereads the device itself on the UI thread, skipping it if a newer write is pending. Sincebusyis already false by then, a change made after the worker went idle can't be overwritten with an older state. AddedANotificationAfterTheWorkerWentIdleMustNotBeOverwrittenByAnOlderState, and the otherWritesCompletedtests read the state from the mock.
Description
This PR aims to fix a UI lag and stutter issue that happens when rapidly changing the master volume (e.g., holding down a volume hotkey or scrolling the mouse wheel quickly).
It seems that on certain audio hardware setups, the synchronous COM calls to
masterVolume.SetVolumecan take a moment to return. Since these calls were running on the main UI thread, it caused the application to briefly hang, making the volume trackbar and OSD lag behind the user's actual input.To give a concrete example: in my personal setup (a Hi-Fi mini system connected to a Sound Blaster Z card), the delay was massive. A fast, long scroll with the mouse wheel caused the UI to choke. The volume trackbar would keep sliding for several seconds after I had already stopped scrolling, or it would just freeze and jump directly to the target position without any smooth path animation.
Technical Details
To work around this, I made the following adjustments:
MasterSessionModel.SetVolumeto a background thread.trueto the caller. This frees up the WPF UI (trackbars, text, OSD) to update instantly, keeping the visual experience smooth regardless of the audio driver's response time.Visual Comparison