From f75cf2753c737f1450b62d6b9b11447296c03f0d Mon Sep 17 00:00:00 2001 From: radj307 Date: Sun, 5 Nov 2023 19:17:46 -0500 Subject: [PATCH] bump appconfig version, remove bad session sync implementation --- VolumeControl.Core/VolumeControl.Core.csproj | 4 +- VolumeControl.CoreAudio/AudioSession.cs | 15 ++- .../Events/VolumeChangedEventArgs.cs | 12 +- .../Helpers/AudioControlExtensions.cs | 13 -- .../Interfaces/IReadOnlyAudioControl.cs | 14 +- VolumeControl.SDK/VolumeControl.SDK.csproj | 7 +- .../BoolExtensions.cs | 21 +++ VolumeControl/Helpers/LocalizationHelper.cs | 2 +- .../ViewModels/AudioDeviceManagerVM.cs | 20 +-- VolumeControl/ViewModels/SessionSyncVM.cs | 121 ------------------ VolumeControl/ViewModels/VolumeControlVM.cs | 1 - VolumeControl/VolumeControl.csproj | 7 +- 12 files changed, 63 insertions(+), 174 deletions(-) create mode 100644 VolumeControl.TypeExtensions/BoolExtensions.cs delete mode 100644 VolumeControl/ViewModels/SessionSyncVM.cs diff --git a/VolumeControl.Core/VolumeControl.Core.csproj b/VolumeControl.Core/VolumeControl.Core.csproj index 39a2402dc..15d5e2639 100644 --- a/VolumeControl.Core/VolumeControl.Core.csproj +++ b/VolumeControl.Core/VolumeControl.Core.csproj @@ -24,7 +24,9 @@ All - + + Resources + diff --git a/VolumeControl.CoreAudio/AudioSession.cs b/VolumeControl.CoreAudio/AudioSession.cs index 79cb7bf97..dd1288638 100644 --- a/VolumeControl.CoreAudio/AudioSession.cs +++ b/VolumeControl.CoreAudio/AudioSession.cs @@ -175,12 +175,13 @@ public float NativeVolume else if (value > 1.0f) value = 1.0f; + if (value == NativeVolume) return; + SimpleAudioVolume.MasterVolume = value; if (isNotifying) return; //< don't duplicate propertychanged notifications isNotifying = true; - NotifyPropertyChanged(); - NotifyPropertyChanged(nameof(Volume)); NotifyVolumeChanged(value, Mute); + NotifyPropertyChanged(); isNotifying = false; } } @@ -188,7 +189,11 @@ public float NativeVolume public int Volume { get => VolumeLevelConverter.FromNativeVolume(NativeVolume); - set => NativeVolume = VolumeLevelConverter.ToNativeVolume(value); + set + { + NativeVolume = VolumeLevelConverter.ToNativeVolume(value); + NotifyPropertyChanged(); + } } /// public bool Mute @@ -196,9 +201,11 @@ public bool Mute get => SimpleAudioVolume.Mute; set { + if (value == Mute) return; + SimpleAudioVolume.Mute = value; - NotifyPropertyChanged(); NotifyVolumeChanged(NativeVolume, value); + NotifyPropertyChanged(); } } #endregion Properties diff --git a/VolumeControl.CoreAudio/Events/VolumeChangedEventArgs.cs b/VolumeControl.CoreAudio/Events/VolumeChangedEventArgs.cs index 2ae3fde95..338b93209 100644 --- a/VolumeControl.CoreAudio/Events/VolumeChangedEventArgs.cs +++ b/VolumeControl.CoreAudio/Events/VolumeChangedEventArgs.cs @@ -7,7 +7,7 @@ namespace VolumeControl.CoreAudio.Events /// /// Contains event data for the event type. /// - public sealed class VolumeChangedEventArgs : EventArgs, IReadOnlyAudioControl + public sealed class VolumeChangedEventArgs : EventArgs { #region Constructor /// @@ -16,8 +16,8 @@ public sealed class VolumeChangedEventArgs : EventArgs, IReadOnlyAudioControl /// The object from the underlying event. internal VolumeChangedEventArgs(AudioVolumeNotificationData data) { - NativeVolume = data.MasterVolume; - Volume = VolumeLevelConverter.FromNativeVolume(NativeVolume); + NewNativeVolume = data.MasterVolume; + Volume = VolumeLevelConverter.FromNativeVolume(NewNativeVolume); Mute = data.Muted; } /// @@ -27,15 +27,15 @@ internal VolumeChangedEventArgs(AudioVolumeNotificationData data) /// The new mute state. internal VolumeChangedEventArgs(float newVolume, bool newMute) { - NativeVolume = newVolume; - Volume = VolumeLevelConverter.FromNativeVolume(NativeVolume); + NewNativeVolume = newVolume; + Volume = VolumeLevelConverter.FromNativeVolume(NewNativeVolume); Mute = newMute; } #endregion Constructor #region Properties /// - public float NativeVolume { get; } + public float NewNativeVolume { get; } /// public int Volume { get; } /// diff --git a/VolumeControl.CoreAudio/Helpers/AudioControlExtensions.cs b/VolumeControl.CoreAudio/Helpers/AudioControlExtensions.cs index 738df274a..c4327ecca 100644 --- a/VolumeControl.CoreAudio/Helpers/AudioControlExtensions.cs +++ b/VolumeControl.CoreAudio/Helpers/AudioControlExtensions.cs @@ -1,5 +1,4 @@ using VolumeControl.CoreAudio.Interfaces; -using VolumeControl.CoreAudio.Structs; namespace VolumeControl.CoreAudio.Helpers { @@ -50,18 +49,6 @@ public static void SetMute(this IAudioControl audioControl, bool muteState) public static void ToggleMute(this IAudioControl audioControl) => audioControl.Mute = !audioControl.Mute; /// - /// Sets the volume level & mute state to the values in the specified , if they're set. - /// - /// (implicit) instance. - /// instance containing the values to set. - public static void SetState(this IAudioControl audioControl, VolumeState volumeState) - { - if (volumeState.HasVolume) - audioControl.Volume = volumeState.Volume; - if (volumeState.HasMute) - audioControl.Mute = volumeState.Mute; - } - /// /// Sets the volume level & mute state to the values in the specified instance. /// /// (implicit) instance. diff --git a/VolumeControl.CoreAudio/Interfaces/IReadOnlyAudioControl.cs b/VolumeControl.CoreAudio/Interfaces/IReadOnlyAudioControl.cs index 5e2b043ed..85b7c45dd 100644 --- a/VolumeControl.CoreAudio/Interfaces/IReadOnlyAudioControl.cs +++ b/VolumeControl.CoreAudio/Interfaces/IReadOnlyAudioControl.cs @@ -6,21 +6,17 @@ public interface IReadOnlyAudioControl { /// - /// Gets the volume level of the audio instance, in the native float format. + /// Gets the volume level, in the native float format. /// - /// - /// Range: 0.0 - 1.0 - /// + /// A between 0.0 and 1.0. float NativeVolume { get; } /// - /// Gets the volume level of the audio instance. + /// Gets the volume level. /// - /// - /// Range: 0 - 100 - /// + /// An between 0 and 100. int Volume { get; } /// - /// Gets the mute state of the audio instance. + /// Gets the mute state. /// bool Mute { get; } } diff --git a/VolumeControl.SDK/VolumeControl.SDK.csproj b/VolumeControl.SDK/VolumeControl.SDK.csproj index 8cc48cfd4..774f054fd 100644 --- a/VolumeControl.SDK/VolumeControl.SDK.csproj +++ b/VolumeControl.SDK/VolumeControl.SDK.csproj @@ -24,6 +24,9 @@ 6.5.1 Copyright © 2023 by $(Authors) + + + True @@ -44,7 +47,9 @@ All - + + Resources + diff --git a/VolumeControl.TypeExtensions/BoolExtensions.cs b/VolumeControl.TypeExtensions/BoolExtensions.cs new file mode 100644 index 000000000..17515b687 --- /dev/null +++ b/VolumeControl.TypeExtensions/BoolExtensions.cs @@ -0,0 +1,21 @@ +namespace VolumeControl.TypeExtensions +{ + /// + /// Extension methods for the type. + /// + public static class BoolExtensions + { + /// + /// Converts the value to . + /// + /// (implicit) A boolean value. + /// 1 when was ; 0 when was . + public static int ToInt32(this bool value) => value ? 1 : 0; + /// + /// Converts the value to . + /// + /// (implicit) A boolean value. + /// 1 when was ; 0 when was . + public static uint ToUInt32(this bool value) => value ? 1u : 0u; + } +} diff --git a/VolumeControl/Helpers/LocalizationHelper.cs b/VolumeControl/Helpers/LocalizationHelper.cs index 5ff545bb0..99b460998 100644 --- a/VolumeControl/Helpers/LocalizationHelper.cs +++ b/VolumeControl/Helpers/LocalizationHelper.cs @@ -39,7 +39,7 @@ class StreamLoader /// Optional source file name. public void LoadFromStream(Stream stream, LocalizationLoader loader, string resourceName) { - using var reader = new StreamReader(stream, System.Text.Encoding.UTF8, true, leaveOpen: true); + using var reader = new StreamReader(stream, System.Text.Encoding.UTF8, true); string content = reader.ReadToEnd(); LoadFromString(content, loader, resourceName); } diff --git a/VolumeControl/ViewModels/AudioDeviceManagerVM.cs b/VolumeControl/ViewModels/AudioDeviceManagerVM.cs index d5e159bda..2a872c01f 100644 --- a/VolumeControl/ViewModels/AudioDeviceManagerVM.cs +++ b/VolumeControl/ViewModels/AudioDeviceManagerVM.cs @@ -80,8 +80,7 @@ public AudioDeviceManagerVM() }; // setup the session synchronizer - SessionSync = new(this); - SessionSync.PropertyChanged += this.SessionSync_PropertyChanged; + //SessionSync = new(this, initialState: false); // populate the sessions lists Devices.Select(d => d.AudioDevice.SessionManager).ForEach(AudioSessionManager.AddSessionManager); @@ -105,6 +104,9 @@ public AudioDeviceManagerVM() AudioSessionMultiSelector.SessionDeselected += this.AudioSessionMultiSelector_SessionDeselected; AudioSessionMultiSelector.CurrentSessionChanged += this.AudioSessionMultiSelector_CurrentSessionChanged; + // enable session sync + //SessionSync.IsEnabled = Settings.EnableSessionSync; + if (doDebugLogging) FLog.Debug($"Successfully initialized {AudioSessionManager.Sessions.Count + AudioSessionManager.HiddenSessions.Count} {(AudioSessionManager.HiddenSessions.Count == 0 ? "" : $"({AudioSessionManager.HiddenSessions.Count} hidden)")} audio sessions."); if (doDebugLogging) FLog.Debug("Finished initializing CoreAudio APIs."); @@ -130,7 +132,7 @@ public AudioDeviceManagerVM() public AudioDeviceSelector AudioDeviceSelector { get; } public AudioDeviceVM? SelectedDevice { get; set; } public AudioSessionMultiSelector AudioSessionMultiSelector { get; } - public SessionSyncVM SessionSync { get; } + // public SessionSyncVM SessionSync { get; } public bool? AllSessionsSelected { get @@ -284,18 +286,6 @@ private void AudioDeviceSelector_PropertyChanged(object? sender, PropertyChanged } #endregion AudioDeviceSelector - #region SessionSync - private void SessionSync_PropertyChanged(object? sender, PropertyChangedEventArgs e) - { - if (e.PropertyName == null) return; - - if (e.PropertyName.Equals(nameof(SessionSyncVM.IsEnabled), StringComparison.Ordinal)) - { - Settings.EnableSessionSync = SessionSync.IsEnabled; - } - } - #endregion SessionSync - #endregion EventHandlers } } diff --git a/VolumeControl/ViewModels/SessionSyncVM.cs b/VolumeControl/ViewModels/SessionSyncVM.cs deleted file mode 100644 index 002e4130a..000000000 --- a/VolumeControl/ViewModels/SessionSyncVM.cs +++ /dev/null @@ -1,121 +0,0 @@ -using System.ComponentModel; -using System.Runtime.CompilerServices; -using VolumeControl.Core; -using VolumeControl.CoreAudio; - -namespace VolumeControl.ViewModels -{ - public class SessionSyncVM : INotifyPropertyChanged - { - #region Constructor - public SessionSyncVM(AudioDeviceManagerVM audioAPI) - { - AudioAPI = audioAPI; - - AudioSessionMultiSelector.SessionSelected += this.AudioSessionMultiSelector_SessionSelected; - AudioSessionMultiSelector.SessionDeselected += this.AudioSessionMultiSelector_SessionDeselected; - Settings.PropertyChanged += this.Settings_PropertyChanged; - } - #endregion Constructor - - #region Properties - private static Config Settings => Config.Default; - private AudioSessionMultiSelector AudioSessionMultiSelector => AudioAPI.AudioSessionMultiSelector; - private AudioDeviceManagerVM AudioAPI { get; } - public bool IsEnabled - { - get => _isEnabled; - set - { - _isEnabled = value; - NotifyPropertyChanged(); - } - } - private bool _isEnabled; - public AudioSession? BaselineSession { get; private set; } - #endregion Properties - - #region Events - public event PropertyChangedEventHandler? PropertyChanged; - private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") => PropertyChanged?.Invoke(this, new(propertyName)); - #endregion Events - - #region Methods - /// - /// Syncs the volume levels & mute states of the specified with the session. - /// - /// The session to set the volume & mute state of the to. - /// Any number of audio sessions to sync the volume & mute state of. - public static void SyncSessions(AudioSession baseline, params AudioSession[] sessions) - { - foreach (var session in sessions) - { - session.Volume = baseline.Volume; - session.Mute = baseline.Mute; - } - } - #endregion Methods - - #region EventHandlers - - #region AudioSession - private void AudioSession_VolumeChanged(CoreAudio.Interfaces.IAudioControl? sender, CoreAudio.Events.VolumeChangedEventArgs e) - { - foreach (var session in AudioSessionMultiSelector.SelectedSessions) - { - if (session.Volume != sender!.Volume) - { - session.Volume = e.Volume; - } - else if (session.Mute != sender!.Mute) - { - session.Mute = e.Mute; - } - } - } - #endregion AudioSession - - #region AudioSessionMultiSelector - private void AudioSessionMultiSelector_SessionSelected(object? sender, AudioSession e) - { - e.VolumeChanged += this.AudioSession_VolumeChanged; - - if (!IsEnabled) return; - - if (BaselineSession != null) - { - SyncSessions(BaselineSession, e); - } - else BaselineSession = e; - } - private void AudioSessionMultiSelector_SessionDeselected(object? sender, AudioSession e) - { - e.VolumeChanged -= this.AudioSession_VolumeChanged; - - if (!AudioSessionMultiSelector.HasSelectedSessions) - { - BaselineSession = null; - } - else if (e.Equals(BaselineSession)) - { - BaselineSession = AudioSessionMultiSelector.SelectedSessions[0]; - } - } - #endregion AudioSessionMultiSelector - - #region Settings - private void Settings_PropertyChanged(object? sender, PropertyChangedEventArgs e) - { - if (e.PropertyName == null) return; - - if (e.PropertyName.Equals(nameof(Config.EnableSessionSync), System.StringComparison.Ordinal)) - { - _isEnabled = Settings.EnableSessionSync; - NotifyPropertyChanged(nameof(IsEnabled)); - } - } - #endregion Settings - - #endregion EventHandlers - } -} diff --git a/VolumeControl/ViewModels/VolumeControlVM.cs b/VolumeControl/ViewModels/VolumeControlVM.cs index 4fb878b83..d84d9d8c1 100644 --- a/VolumeControl/ViewModels/VolumeControlVM.cs +++ b/VolumeControl/ViewModels/VolumeControlVM.cs @@ -29,7 +29,6 @@ public VolumeControlVM() : base() if (Settings.CheckForUpdates) Updater.CheckForUpdateNow(); this.AudioAPI = new(); - AudioAPI.SessionSync.IsEnabled = true; TargetBoxVM = new(AudioAPI.AudioSessionManager, AudioAPI.AudioSessionMultiSelector); diff --git a/VolumeControl/VolumeControl.csproj b/VolumeControl/VolumeControl.csproj index 27104b572..7cdd66c5e 100644 --- a/VolumeControl/VolumeControl.csproj +++ b/VolumeControl/VolumeControl.csproj @@ -14,7 +14,7 @@ en-CA git https://github.com/radj307/volume-control - Copyright © 2023 by $(Authors) + Copyright © 2023 $(Authors) Resources\icons\iconSilvered.ico radj307 Volume Control @@ -36,6 +36,9 @@ + + + @@ -160,4 +163,4 @@ Resources.Designer.cs - \ No newline at end of file +