Skip to content

Commit 320f853

Browse files
committed
Clean up
- Got rid of unused refrences - Cleaned up logging - Got rid of some unnecessary allocations
1 parent 9fde01a commit 320f853

File tree

8 files changed

+30
-67
lines changed

8 files changed

+30
-67
lines changed

VolumeControl/App.xaml.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Configuration;
4-
using System.Data;
5-
using System.Linq;
6-
using System.Threading.Tasks;
7-
using System.Windows;
1+
using System.Windows;
82

93
namespace VolumeControl
104
{

VolumeControl/AudioData.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
62

73
namespace VolumeControl
84
{
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using AudioSwitcher.AudioApi.Session;
22
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
73

84
namespace VolumeControl
95
{
@@ -14,12 +10,12 @@ class AudioSessionKeeper : IDisposable
1410
private IDisposable m_volumeSubscription;
1511
private IDisposable m_muteSubscription;
1612

17-
public AudioSessionKeeper(IAudioSession session, MainWindow mainWindow)
13+
public AudioSessionKeeper(IAudioSession session, AudioSessionVolumeListener volumeListener, AudioSessionMuteListener muteListener)
1814
{
1915
m_id = session.Id;
2016

21-
m_volumeSubscription = session.VolumeChanged.Subscribe(new AudioSessionVolumeListener(mainWindow));
22-
m_muteSubscription = session.MuteChanged.Subscribe(new AudioSessionMuteListener(mainWindow));
17+
m_volumeSubscription = session.VolumeChanged.Subscribe(volumeListener);
18+
m_muteSubscription = session.MuteChanged.Subscribe(muteListener);
2319
}
2420

2521
public string id()
@@ -31,6 +27,9 @@ public void Dispose()
3127
{
3228
m_volumeSubscription.Dispose();
3329
m_muteSubscription.Dispose();
30+
31+
m_volumeSubscription = null;
32+
m_muteSubscription = null;
3433
}
3534
}
3635
}

VolumeControl/Listeners.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using AudioSwitcher.AudioApi;
22
using AudioSwitcher.AudioApi.Session;
33
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Text;
7-
using System.Threading.Tasks;
84

95
namespace VolumeControl
106
{

VolumeControl/MainWindow.xaml.cs

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
using CSCore.CoreAudioAPI;
2-
using System;
3-
using System.Collections.ObjectModel;
1+
using System;
42
using System.Diagnostics;
5-
using System.Runtime.InteropServices;
63
using System.Threading;
74
using System.Windows;
8-
using System.Web.Script.Serialization;
95
using Newtonsoft.Json;
10-
using CSCore.Win32;
116
using System.Collections.Generic;
127
using System.Net.Sockets;
138
using System.Net;
149
using AudioSwitcher.AudioApi.CoreAudio;
15-
using AudioSwitcher.AudioApi.Observables;
1610
using AudioSwitcher.AudioApi;
1711
using System.Reactive.Linq;
18-
using System.Text.RegularExpressions;
1912
using System.Reactive.Subjects;
20-
using System.Reactive.Concurrency;
2113
using System.Windows.Navigation;
2214
using AudioSwitcher.AudioApi.Session;
2315

@@ -35,7 +27,10 @@ public partial class MainWindow : Window, ClientListener
3527
private UpdateListener m_updateListener;
3628
private Dictionary<string, AudioSessionKeeper> m_sessions = new Dictionary<string, AudioSessionKeeper>();
3729

38-
Subject<bool> m_updateSubject = new Subject<bool>();
30+
private AudioSessionVolumeListener m_sessionVolumeListener;
31+
private AudioSessionMuteListener m_sessionMuteListener;
32+
33+
private Subject<bool> m_updateSubject = new Subject<bool>();
3934

4035
JsonSerializerSettings m_jsonsettings = new JsonSerializerSettings
4136
{
@@ -60,6 +55,9 @@ public MainWindow()
6055
server_ip.Content = ipAddress;
6156
Console.WriteLine("ipAddress: " + ipAddress);
6257

58+
m_sessionVolumeListener = new AudioSessionVolumeListener(this);
59+
m_sessionMuteListener = new AudioSessionMuteListener(this);
60+
6361
updateConnectionStatus();
6462

6563
m_coreAudioController = new CoreAudioController();
@@ -71,11 +69,11 @@ public MainWindow()
7169
MasterVolumeListener masterVolumeListener = new MasterVolumeListener(this);
7270

7371
m_coreAudioController.DefaultPlaybackDevice.VolumeChanged
74-
.Throttle(TimeSpan.FromMilliseconds(100))
72+
//.Throttle(TimeSpan.FromMilliseconds(10))
7573
.Subscribe(masterVolumeListener);
7674

7775
m_coreAudioController.DefaultPlaybackDevice.MuteChanged
78-
.Throttle(TimeSpan.FromMilliseconds(100))
76+
//.Throttle(TimeSpan.FromMilliseconds(10))
7977
.Subscribe(masterVolumeListener);
8078

8179
new Thread(() =>
@@ -99,7 +97,7 @@ public void requestUpdate()
9997

10098
private void updateConnectionStatus()
10199
{
102-
Application.Current.Dispatcher.Invoke(new Action(() =>
100+
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
103101
{
104102
if (m_server == null || !m_server.isRunning())
105103
{
@@ -193,13 +191,11 @@ private void updateState(PcAudio audioUpdate)
193191
PcAudio audioState = new PcAudio();
194192
audioState.version = VERSION;
195193

196-
Console.WriteLine("Scrapping device IDs");
197-
198194
cleanUpSessionKeepers();
199195

200196
var defaultDevice = m_coreAudioController.GetDefaultDevice(DeviceType.Playback, AudioSwitcher.AudioApi.Role.Multimedia);
201197
string defaultDeviceId = defaultDevice.Id.ToString();
202-
198+
203199
// Add all avalible audio devices to our list of device IDs
204200
IEnumerable<CoreAudioDevice> devices = m_coreAudioController.GetPlaybackDevices();
205201
foreach (var device in devices)
@@ -210,20 +206,13 @@ private void updateState(PcAudio audioUpdate)
210206
}
211207
}
212208

213-
Console.WriteLine("Done scrapping device IDs");
214-
215209
// Master device updates
216210
if (audioUpdate != null && audioUpdate.defaultDevice != null)
217211
{
218-
Console.WriteLine("Has default device");
219-
220212
if (!audioUpdate.defaultDevice.deviceId.Equals(defaultDeviceId))
221213
{
222-
Console.WriteLine("Audio update default device change request");
223-
224214
Guid deviceId = Guid.Parse(audioUpdate.defaultDevice.deviceId);
225215

226-
Console.WriteLine("About to get new default device");
227216
CoreAudioDevice newDefaultAudioDevice = m_coreAudioController.GetDevice(deviceId);
228217
if (newDefaultAudioDevice != null)
229218
{
@@ -244,7 +233,6 @@ private void updateState(PcAudio audioUpdate)
244233
{
245234
if (audioUpdate.defaultDevice.masterMuted != null)
246235
{
247-
Console.WriteLine("Getting current mute state");
248236
bool muted = audioUpdate.defaultDevice.masterMuted ?? m_coreAudioController.DefaultPlaybackDevice.IsMuted;
249237
Console.WriteLine("Updating master mute: " + muted);
250238

@@ -253,32 +241,25 @@ private void updateState(PcAudio audioUpdate)
253241

254242
if (audioUpdate.defaultDevice.masterVolume != null)
255243
{
256-
Console.WriteLine("Getting current volume");
257244
float volume = audioUpdate.defaultDevice.masterVolume ?? (float)m_coreAudioController.DefaultPlaybackDevice.Volume;
258245
Console.WriteLine("Updating master volume: " + volume);
259246

260-
m_coreAudioController.DefaultPlaybackDevice.Volume = volume * 100;
247+
m_coreAudioController.DefaultPlaybackDevice.Volume = volume;
261248
}
262249

263250
return;
264251
}
265252
}
266253
}
267254

268-
Console.WriteLine("Creating new device data");
269-
270255
// Create our default audio device and populate it's volume and mute status
271256
AudioDevice audioDevice = new AudioDevice(defaultDevice.FullName, defaultDeviceId);
272257
audioState.defaultDevice = audioDevice;
273258

274-
Console.WriteLine("Creating new device data");
275-
276259
CoreAudioDevice defaultPlaybackDevice = m_coreAudioController.DefaultPlaybackDevice;
277260
audioDevice.masterVolume = (float)defaultPlaybackDevice.Volume;
278261
audioDevice.masterMuted = defaultPlaybackDevice.IsMuted;
279262

280-
Console.WriteLine("Iterating sessions");
281-
282263
// Go through all audio sessions
283264
foreach (var session in defaultDevice.SessionController.All())
284265
{
@@ -288,9 +269,9 @@ private void updateState(PcAudio audioUpdate)
288269
string sessionId = session.Id.ToString();
289270
if (!m_sessions.ContainsKey(sessionId))
290271
{
291-
Console.WriteLine("Found new audio session");
272+
//Console.WriteLine("Found new audio session");
292273

293-
AudioSessionKeeper sessionKeeper = new AudioSessionKeeper(session, this);
274+
AudioSessionKeeper sessionKeeper = new AudioSessionKeeper(session, m_sessionVolumeListener, m_sessionMuteListener);
294275
m_sessions.Add(session.Id, sessionKeeper);
295276
}
296277

@@ -325,8 +306,10 @@ private void updateState(PcAudio audioUpdate)
325306
string sessionName = session.DisplayName;
326307
if (sessionName == null || sessionName.Trim() == "")
327308
{
328-
var process = Process.GetProcessById(session.ProcessId);
329-
sessionName = process.ProcessName;
309+
using (var process = Process.GetProcessById(session.ProcessId))
310+
{
311+
sessionName = process.ProcessName;
312+
}
330313
}
331314

332315
AudioSession audioSession = new AudioSession(sessionName, (float)session.Volume, session.IsMuted);
@@ -341,8 +324,6 @@ private void updateState(PcAudio audioUpdate)
341324
AudioSessionKeeper sessionKeeper = m_sessions[session.Id];
342325
m_sessions.Remove(session.Id);
343326
sessionKeeper.Dispose();
344-
345-
Console.WriteLine("Done cleaning up");
346327
}
347328
}
348329
}

VolumeControl/PcVolumeControl.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
77
<ProjectGuid>{C4184892-C6A9-41DD-89FD-9677A806C900}</ProjectGuid>
8-
<OutputType>Exe</OutputType>
8+
<OutputType>WinExe</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>VolumeControl</RootNamespace>
1111
<AssemblyName>PcVolumeControl</AssemblyName>

VolumeControl/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.Reflection;
2-
using System.Resources;
3-
using System.Runtime.CompilerServices;
42
using System.Runtime.InteropServices;
53
using System.Windows;
64

VolumeControl/Server.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Net.Sockets;
77
using System.Text;
88
using System.Threading;
9-
using System.Threading.Tasks;
109

1110
namespace VolumeControl
1211
{
@@ -17,6 +16,7 @@ class Server
1716
private Thread listenThread;
1817
private List<TcpClient> m_clients = new List<TcpClient>();
1918
private bool m_running = false;
19+
private ASCIIEncoding m_encoder = new ASCIIEncoding();
2020

2121
public Server(ClientListener clientListener)
2222
{
@@ -171,8 +171,7 @@ public void sendData(string data)
171171
clients = m_clients.ToList();
172172
}
173173

174-
ASCIIEncoding encoder = new ASCIIEncoding();
175-
byte[] buffer = encoder.GetBytes(finalData);
174+
byte[] buffer = m_encoder.GetBytes(finalData);
176175

177176
foreach (var client in clients)
178177
{

0 commit comments

Comments
 (0)