Skip to content

Commit 023520d

Browse files
author
Jonas Dellinger
committed
Small code cleanup
1 parent e52b9ef commit 023520d

2 files changed

Lines changed: 22 additions & 40 deletions

File tree

SpotifyAPI/Local/Models/SpotifyUri.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace SpotifyAPI.Local.Models
99
{
1010
public class SpotifyUri
1111
{
12-
internal Dictionary<UriType, string> _properties = new Dictionary<UriType, string>();
12+
private readonly Dictionary<UriType, string> _properties = new Dictionary<UriType, string>();
1313

14-
public string Base { get; internal set; }
14+
public string Base { get; }
1515
public UriType Type => _properties?.LastOrDefault().Key ?? UriType.none;
1616
public string Id => _properties?.LastOrDefault().Value;
1717

@@ -29,27 +29,23 @@ public SpotifyUri(string uriBase, UriType uriType, string uriId)
2929

3030
public string GetUriPropValue(UriType type)
3131
{
32-
if (!_properties.ContainsKey(type))
33-
return null;
34-
return _properties[type];
32+
return !_properties.ContainsKey(type) ? null : _properties[type];
3533
}
3634

3735
public static SpotifyUri Parse(string uri)
3836
{
3937
if (string.IsNullOrEmpty(uri))
4038
throw new ArgumentNullException("Uri");
4139

42-
UriType uriType = UriType.none;
4340
string[] props = uri.Split(':');
44-
if (props.Length < 3 || !Enum.TryParse(props[1], out uriType))
41+
if (props.Length < 3 || !Enum.TryParse(props[1], out UriType uriType))
4542
throw new ArgumentException("Unexpected Uri");
4643

4744
Dictionary<UriType, string> properties = new Dictionary<UriType, string> { { uriType, props[2] } };
4845

4946
for (int index = 3; index < props.Length; index += 2)
5047
{
51-
UriType type = UriType.none;
52-
if (Enum.TryParse(props[index], out type))
48+
if (Enum.TryParse(props[index], out UriType type))
5349
properties.Add(type, props[index + 1]);
5450
}
5551

@@ -58,7 +54,7 @@ public static SpotifyUri Parse(string uri)
5854

5955
public override string ToString()
6056
{
61-
return $"{Base}:{string.Join(":", _properties.SelectMany(x => new string[] { x.Key.ToString(), x.Value }))}";
57+
return $"{Base}:{string.Join(":", _properties.SelectMany(x => new[] { x.Key.ToString(), x.Value }))}";
6258
}
6359
}
6460
}

SpotifyAPI/Local/VolumeMixerControl.cs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ internal static float GetSpotifyVolume()
1818
throw new COMException("Volume object creation failed");
1919
}
2020

21-
float level;
22-
volume.GetMasterVolume(out level);
21+
volume.GetMasterVolume(out float level);
2322
Marshal.ReleaseComObject(volume);
2423
return level * 100;
2524
}
@@ -33,8 +32,7 @@ internal static bool IsSpotifyMuted()
3332
throw new COMException("Volume object creation failed");
3433
}
3534

36-
bool mute;
37-
volume.GetMute(out mute);
35+
volume.GetMute(out bool mute);
3836
Marshal.ReleaseComObject(volume);
3937
return mute;
4038
}
@@ -79,12 +77,10 @@ private static ISimpleAudioVolume GetSpotifyVolumeObject()
7977
private static ISimpleAudioVolume GetVolumeObject(int pid)
8078
{
8179
// get the speakers (1st render + multimedia) device
82-
IMmDeviceEnumerator deviceEnumerator = (IMmDeviceEnumerator)(new MMDeviceEnumerator());
83-
IMmDevice speakers;
84-
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out speakers);
80+
IMmDeviceEnumerator deviceEnumerator = (IMmDeviceEnumerator) new MMDeviceEnumerator();
81+
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out IMmDevice speakers);
8582

86-
string defaultDeviceId;
87-
speakers.GetId(out defaultDeviceId);
83+
speakers.GetId(out string defaultDeviceId);
8884

8985
ISimpleAudioVolume volumeControl = GetVolumeObject(pid, speakers);
9086
Marshal.ReleaseComObject(speakers);
@@ -97,18 +93,13 @@ private static ISimpleAudioVolume GetVolumeObject(int pid)
9793
// As far as Spotify is concerned, if using the "--enable-audio-graph" command line argument,
9894
// a new option becomes available in the Settings that makes it possible to change the playback device.
9995

100-
IMmDeviceCollection deviceCollection;
101-
deviceEnumerator.EnumAudioEndpoints(EDataFlow.ERender, EDeviceState.Active, out deviceCollection);
96+
deviceEnumerator.EnumAudioEndpoints(EDataFlow.ERender, EDeviceState.Active, out IMmDeviceCollection deviceCollection);
10297

103-
int count;
104-
deviceCollection.GetCount(out count);
98+
deviceCollection.GetCount(out int count);
10599
for (int i = 0; i < count; i++)
106100
{
107-
IMmDevice device;
108-
deviceCollection.Item(i, out device);
109-
110-
string deviceId;
111-
device.GetId(out deviceId);
101+
deviceCollection.Item(i, out IMmDevice device);
102+
device.GetId(out string deviceId);
112103

113104
try
114105
{
@@ -134,29 +125,24 @@ private static ISimpleAudioVolume GetVolumeObject(int pid, IMmDevice device)
134125
{
135126
// activate the session manager. we need the enumerator
136127
Guid iidIAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
137-
object o;
138-
device.Activate(ref iidIAudioSessionManager2, 0, IntPtr.Zero, out o);
139-
IAudioSessionManager2 mgr = (IAudioSessionManager2)o;
128+
device.Activate(ref iidIAudioSessionManager2, 0, IntPtr.Zero, out object o);
129+
IAudioSessionManager2 mgr = (IAudioSessionManager2) o;
140130

141131
// enumerate sessions for on this device
142-
IAudioSessionEnumerator sessionEnumerator;
143-
mgr.GetSessionEnumerator(out sessionEnumerator);
144-
int count;
145-
sessionEnumerator.GetCount(out count);
132+
mgr.GetSessionEnumerator(out IAudioSessionEnumerator sessionEnumerator);
133+
sessionEnumerator.GetCount(out int count);
146134

147135
// search for an audio session with the required name
148136
// NOTE: we could also use the process id instead of the app name (with IAudioSessionControl2)
149137
ISimpleAudioVolume volumeControl = null;
150138
for (int i = 0; i < count; i++)
151139
{
152-
IAudioSessionControl2 ctl;
153-
sessionEnumerator.GetSession(i, out ctl);
154-
int cpid;
155-
ctl.GetProcessId(out cpid);
140+
sessionEnumerator.GetSession(i, out IAudioSessionControl2 ctl);
141+
ctl.GetProcessId(out int cpid);
156142

157143
if (cpid == pid)
158144
{
159-
volumeControl = (ISimpleAudioVolume)ctl;
145+
volumeControl = (ISimpleAudioVolume) ctl;
160146
break;
161147
}
162148
Marshal.ReleaseComObject(ctl);

0 commit comments

Comments
 (0)