forked from NimmLor/IoT-Audio-Visualization-Center
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
330 lines (291 loc) · 11.9 KB
/
MainWindow.xaml.cs
File metadata and controls
330 lines (291 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using Un4seen.Bass;
using Un4seen.BassWasapi;
namespace Analyzer
{
public partial class MainWindow : MetroWindow
{
private string loadedAudioDevice = "";
private bool audioDeviceLoaded = false;
private bool deviceListInitialized = false;
public MainWindow()
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler(MyUtils.OnProcessExit);
MyUtils.SetDefaultTheme();
InitializeComponent();
InitDevices();
cboDevices.SelectedIndex = 0;
Load();
RefreshDeviceList();
//ThreadPool.QueueUserWorkItem(new WaitCallback(delegate (object state) { LoadAudioDevice(); }), null);
//ThreadPool.QueueUserWorkItem(new WaitCallback(delegate (object state) { LoadAudioDevice(); }), null);
}
public void LoadAudioDevice()
{
while (!deviceListInitialized || String.IsNullOrEmpty(loadedAudioDevice)) if(loadedAudioDevice == null)return;
cboDevices.Dispatcher.Invoke(() =>
{
if (cboDevices.Items.Contains(loadedAudioDevice as object))
{
cboDevices.SelectedIndex = cboDevices.Items.IndexOf(loadedAudioDevice as object);
MyUtils.SwitchDeviceFromString(loadedAudioDevice);
restartSourceSpectrum();
audioDeviceLoaded = true;
}
});
}
public void RefreshDeviceList()
{
icDevices.ItemsSource = null;
foreach (UdpDevice u in MyUtils.UdpDevices) u.getWebserverInfo();
icDevices.ItemsSource = MyUtils.UdpDevices;
RefreshDevicesBox();
}
private void InitDevices()
{
List<string> toAdd = new List<string>();
for (int i = 0; i < BassWasapi.BASS_WASAPI_GetDeviceCount(); i++)
{
var device = BassWasapi.BASS_WASAPI_GetDeviceInfo(i);
if (device.IsEnabled && (device.IsLoopback || device.IsInput)) /*Add Audio Inputs to Device List*/
{
toAdd.Add(string.Format("{0} - {1}", i, device.name));
}
}
cboDevices.Dispatcher.Invoke(() =>
{
cboDevices.Items.Clear();
foreach(string s in toAdd)cboDevices.Items.Add(s);
});
//cboDevices.SelectedIndex = 0;
Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATETHREADS, false);
if (!Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero)) MessageBox.Show("Error while initializing the sound device");
deviceListInitialized = true;
}
private void CboDevices_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
spcSource.wucd.Stop();
}
catch
{
}
string s = (sender as ComboBox).SelectedItem as string;
MyUtils.SwitchDeviceFromString(s);
StartWpfUserControls();
//StartWpfUserControls();
}
public void restartSourceSpectrum()
{
try
{
spcSource.wucd.Stop();
}
catch { }
try
{
spcSource.wucd.Start();
}
catch { }
}
public void StartWpfUserControls()
{
spcSource.wucd.Stop();
spcSource.wucd.Start();
foreach (Spectrum y in MyUtils.FindVisualChildren<Spectrum>(Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive)))
{
y.wucd.Stop();
y.wucd.Start();
}
}
private void BtnRefresh_Click(object sender, RoutedEventArgs e)
{
RefreshDevicesBox();
}
public void RefreshDevicesBox()
{
foreach (DeviceControl y in MyUtils.FindVisualChildren<DeviceControl>(grpDevices)) y.RefAll();
}
public async void AddDeviceDialogAsync()
{
string devicename = "";
string ip = "";
int port = 0;
int lines = 32;
int smoothing = 0;
var x = new MetroDialogSettings();
x.AffirmativeButtonText = "Next";
x.NegativeButtonText = "Cancel";
devicename = await this.ShowInputAsync("New Device", "How should the device be called?", x);
if (String.IsNullOrEmpty(devicename)) return;
var exist = MyUtils.UdpDevices.Find(o => o.DeviceName == devicename);
if (exist != null)
{
await this.ShowMessageAsync("Error!", "A device with that name already exists!");
return;
}
x.DefaultText = "192.168.0.";
ip = await this.ShowInputAsync("New Device", "Whats the IP-Address of your device?", x);
while (!MyUtils.ValidateIp(ip))
{
await this.ShowMessageAsync("Error!", "The IP-Address that was entered is invalid!");
ip = await this.ShowInputAsync("New Device", "Whats the IP-Address of your device?", x);
if (String.IsNullOrEmpty(ip)) return;
}
var controller = await this.ShowProgressAsync("Please wait...", "Trying to reach the device");
controller.SetIndeterminate();
Thread.Sleep(500);
bool success = MyUtils.IpReachable(ip);
await controller.CloseAsync();
if (!success)
{
x.AffirmativeButtonText = "Yes";
var cont = await this.ShowMessageAsync("The device could not be reached!", "Continue anyway?", MessageDialogStyle.AffirmativeAndNegative, x);
if (cont == MessageDialogResult.Negative) return;
}
string porttext = "";
x.DefaultText = "4210";
porttext = await this.ShowInputAsync("New Device", "On what UDP-Port should the data be sent?", x);
if (!int.TryParse(porttext, out port)) port = -1;
while (port <= 0)
{
porttext = await this.ShowInputAsync("Invalid Number!", "On what UDP-Port should the data be sent?", x);
if (String.IsNullOrEmpty(porttext)) return;
if (!int.TryParse(porttext, out port)) port = -1;
}
x.DefaultText = "32";
string linestext = "";
if (!success)
{
linestext = await this.ShowInputAsync("New Device", "How many lines of data should be sent to the device?", x);
if (!int.TryParse(linestext, out lines)) lines = -1;
while ((lines <= 0 || lines > 1023))
{
linestext = await this.ShowInputAsync("Invalid Number!", "How many lines of data should be sent to the device?", x);
if (String.IsNullOrEmpty(linestext)) return;
if (!int.TryParse(linestext, out lines)) lines = -1;
}
}
x.DefaultText = "2";
string smtext = "";
smtext = await this.ShowInputAsync("New Device", "How much should the spectrum be smoothed?", x);
if (!int.TryParse(smtext, out smoothing)) smoothing = -1;
while ((smoothing <= 0 || smoothing > 100))
{
smtext = await this.ShowInputAsync("Invalid Number!", "How much should the spectrum be smoothed?", x);
if (String.IsNullOrEmpty(smtext)) return;
if (!int.TryParse(smtext, out smoothing)) smoothing = -1;
}
x.AffirmativeButtonText = "Add Device";
x.NegativeButtonText = "Cancel Device Creation";
var res = await this.ShowMessageAsync("Confirm", "Name: " + devicename + "\nIP-Address: " + ip + "\nLines: " + lines, MessageDialogStyle.AffirmativeAndNegative, x);
if (res == MessageDialogResult.Negative) return;
try
{
UdpDevice newDev = new UdpDevice(devicename, ip, port, lines, smoothing);
newDev.Smooth = true;
MyUtils.UdpDevices.Add(newDev);
Save();
RefreshDeviceList();
await this.ShowMessageAsync("Success!", "Device created successfully!");
}
catch
{
await this.ShowInputAsync("Error", "Something went wrong while creating the new device!");
}
}
public void Save(string dev = "")
{
SaveObject s = new SaveObject(MyUtils.UdpDevices);
if (String.IsNullOrEmpty(dev)) dev = MyUtils.audioDevice;
s.audioDevice = dev;
MyUtils.SaveToProperties(s);
}
private void Load()
{
try
{
SaveObject s = MyUtils.RetrieveSettings();
if (s != null)
{
if (s.udps.Count != 0) MyUtils.UdpDevices.Clear();
foreach (UdpDevice u in s.udps)
{
if (MyUtils.ValidateIp(u.Ip))
{
var ad = new UdpDevice(u.DeviceName, u.Ip, u.Port, u.Lines, u.Smoothing);
//ad.Smoothing = 0;
MyUtils.UdpDevices.Add(ad);
}
}
loadedAudioDevice = s.audioDevice;
Save(loadedAudioDevice);
}
}
catch (Exception ex)
{
this.ShowMessageAsync("Error", "Loading devices failed!\n\nError message:\n" + ex.Message, MessageDialogStyle.Affirmative);
MyUtils.UdpDevices.Clear();
}
}
private void btnOTA_Click(object sender, RoutedEventArgs e)
{
foreach (UdpDevice u in MyUtils.UdpDevices) u.modeOTA();
}
private void btnAlexa_Click(object sender, RoutedEventArgs e)
{
foreach (UdpDevice u in MyUtils.UdpDevices) u.modeAlexa();
}
private void btnReboot_Click(object sender, RoutedEventArgs e)
{
foreach (UdpDevice u in MyUtils.UdpDevices) u.modeReboot();
}
private void sldSpeed_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
foreach (UdpDevice u in MyUtils.UdpDevices) u.setSpeedAsync((int)sldSpeed.Value);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
AddDeviceDialogAsync();
}
private void MetroWindow_Closed(object sender, EventArgs e)
{
//Environment.Exit(0);
//((App)Application.Current).ExitApplication();
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
spcSource.TotalWidth = (int)MainWindow_Window.ActualWidth - 30;
}
private void BtnRefresh_Click_1(object sender, RoutedEventArgs e)
{
RefreshDeviceList();
}
private void BtnSettings_Click(object sender, RoutedEventArgs e)
{
#if DEBUG
Window s = new Settings();
s.Show();
#else
MessageBox.Show("Work in progress...");
#endif
}
private void BtnNetworkscanner_Click(object sender, RoutedEventArgs e)
{
Window scan = new Networkscanner();
scan.Show();
}
private void sldSourceScale_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
MyUtils.sourceFactor = sldSourceScale.Value / 100.0;
}
}
}