From 023520d3de18c202e00989c21d7046da9a2706bc Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Mon, 12 Mar 2018 00:13:38 +0100 Subject: [PATCH] Small code cleanup --- SpotifyAPI/Local/Models/SpotifyUri.cs | 16 ++++----- SpotifyAPI/Local/VolumeMixerControl.cs | 46 +++++++++----------------- 2 files changed, 22 insertions(+), 40 deletions(-) diff --git a/SpotifyAPI/Local/Models/SpotifyUri.cs b/SpotifyAPI/Local/Models/SpotifyUri.cs index 7b4f6e43..16bd9122 100644 --- a/SpotifyAPI/Local/Models/SpotifyUri.cs +++ b/SpotifyAPI/Local/Models/SpotifyUri.cs @@ -9,9 +9,9 @@ namespace SpotifyAPI.Local.Models { public class SpotifyUri { - internal Dictionary _properties = new Dictionary(); + private readonly Dictionary _properties = new Dictionary(); - public string Base { get; internal set; } + public string Base { get; } public UriType Type => _properties?.LastOrDefault().Key ?? UriType.none; public string Id => _properties?.LastOrDefault().Value; @@ -29,9 +29,7 @@ namespace SpotifyAPI.Local.Models public string GetUriPropValue(UriType type) { - if (!_properties.ContainsKey(type)) - return null; - return _properties[type]; + return !_properties.ContainsKey(type) ? null : _properties[type]; } public static SpotifyUri Parse(string uri) @@ -39,17 +37,15 @@ namespace SpotifyAPI.Local.Models if (string.IsNullOrEmpty(uri)) throw new ArgumentNullException("Uri"); - UriType uriType = UriType.none; string[] props = uri.Split(':'); - if (props.Length < 3 || !Enum.TryParse(props[1], out uriType)) + if (props.Length < 3 || !Enum.TryParse(props[1], out UriType uriType)) throw new ArgumentException("Unexpected Uri"); Dictionary properties = new Dictionary { { uriType, props[2] } }; for (int index = 3; index < props.Length; index += 2) { - UriType type = UriType.none; - if (Enum.TryParse(props[index], out type)) + if (Enum.TryParse(props[index], out UriType type)) properties.Add(type, props[index + 1]); } @@ -58,7 +54,7 @@ namespace SpotifyAPI.Local.Models public override string ToString() { - return $"{Base}:{string.Join(":", _properties.SelectMany(x => new string[] { x.Key.ToString(), x.Value }))}"; + return $"{Base}:{string.Join(":", _properties.SelectMany(x => new[] { x.Key.ToString(), x.Value }))}"; } } } diff --git a/SpotifyAPI/Local/VolumeMixerControl.cs b/SpotifyAPI/Local/VolumeMixerControl.cs index 2cc3f80b..2c0133a2 100644 --- a/SpotifyAPI/Local/VolumeMixerControl.cs +++ b/SpotifyAPI/Local/VolumeMixerControl.cs @@ -18,8 +18,7 @@ namespace SpotifyAPI.Local throw new COMException("Volume object creation failed"); } - float level; - volume.GetMasterVolume(out level); + volume.GetMasterVolume(out float level); Marshal.ReleaseComObject(volume); return level * 100; } @@ -33,8 +32,7 @@ namespace SpotifyAPI.Local throw new COMException("Volume object creation failed"); } - bool mute; - volume.GetMute(out mute); + volume.GetMute(out bool mute); Marshal.ReleaseComObject(volume); return mute; } @@ -79,12 +77,10 @@ namespace SpotifyAPI.Local private static ISimpleAudioVolume GetVolumeObject(int pid) { // get the speakers (1st render + multimedia) device - IMmDeviceEnumerator deviceEnumerator = (IMmDeviceEnumerator)(new MMDeviceEnumerator()); - IMmDevice speakers; - deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out speakers); + IMmDeviceEnumerator deviceEnumerator = (IMmDeviceEnumerator) new MMDeviceEnumerator(); + deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out IMmDevice speakers); - string defaultDeviceId; - speakers.GetId(out defaultDeviceId); + speakers.GetId(out string defaultDeviceId); ISimpleAudioVolume volumeControl = GetVolumeObject(pid, speakers); Marshal.ReleaseComObject(speakers); @@ -97,18 +93,13 @@ namespace SpotifyAPI.Local // As far as Spotify is concerned, if using the "--enable-audio-graph" command line argument, // a new option becomes available in the Settings that makes it possible to change the playback device. - IMmDeviceCollection deviceCollection; - deviceEnumerator.EnumAudioEndpoints(EDataFlow.ERender, EDeviceState.Active, out deviceCollection); + deviceEnumerator.EnumAudioEndpoints(EDataFlow.ERender, EDeviceState.Active, out IMmDeviceCollection deviceCollection); - int count; - deviceCollection.GetCount(out count); + deviceCollection.GetCount(out int count); for (int i = 0; i < count; i++) { - IMmDevice device; - deviceCollection.Item(i, out device); - - string deviceId; - device.GetId(out deviceId); + deviceCollection.Item(i, out IMmDevice device); + device.GetId(out string deviceId); try { @@ -134,29 +125,24 @@ namespace SpotifyAPI.Local { // activate the session manager. we need the enumerator Guid iidIAudioSessionManager2 = typeof(IAudioSessionManager2).GUID; - object o; - device.Activate(ref iidIAudioSessionManager2, 0, IntPtr.Zero, out o); - IAudioSessionManager2 mgr = (IAudioSessionManager2)o; + device.Activate(ref iidIAudioSessionManager2, 0, IntPtr.Zero, out object o); + IAudioSessionManager2 mgr = (IAudioSessionManager2) o; // enumerate sessions for on this device - IAudioSessionEnumerator sessionEnumerator; - mgr.GetSessionEnumerator(out sessionEnumerator); - int count; - sessionEnumerator.GetCount(out count); + mgr.GetSessionEnumerator(out IAudioSessionEnumerator sessionEnumerator); + sessionEnumerator.GetCount(out int count); // search for an audio session with the required name // NOTE: we could also use the process id instead of the app name (with IAudioSessionControl2) ISimpleAudioVolume volumeControl = null; for (int i = 0; i < count; i++) { - IAudioSessionControl2 ctl; - sessionEnumerator.GetSession(i, out ctl); - int cpid; - ctl.GetProcessId(out cpid); + sessionEnumerator.GetSession(i, out IAudioSessionControl2 ctl); + ctl.GetProcessId(out int cpid); if (cpid == pid) { - volumeControl = (ISimpleAudioVolume)ctl; + volumeControl = (ISimpleAudioVolume) ctl; break; } Marshal.ReleaseComObject(ctl);