-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow_Backup.xaml.cs
357 lines (298 loc) · 11.1 KB
/
MainWindow_Backup.xaml.cs
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using NAudio.Wave;
using NeeqDMIs.Music;
using NeeqDMIs.Utils;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;
using Tong_Sharp.DMIBox;
using Tong_Sharp.Modules.FFT;
using Tong_Sharp.Modules.SpectrumAnalyzer;
using Tong_Sharp.Setups;
namespace Tong_Sharp
{
/// <summary>
/// Logica di interazione per MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, IPeakIntensityReceiver, IPeakBucketRealReceiver, IPeakBucketSmoothReceiver, IFftDataReceiver
{
private const string strPeakX = "Peak X: ";
private const string strPeakY = "Peak Y: ";
private const string strPeakSmoothX = "PeakSmooth X: ";
private const string strNote = "Note: ";
private const string strYaw = "Yaw: ";
private const string strPitch = "Pitch: ";
private const string strRoll = "Roll: ";
private const string strSpeed = "Speed: ";
private ValueMapperDouble cnvMapX;
private ValueMapperDouble cnvMapY;
private double maxDb = 6000f;
private Polyline spectrumLine;
private readonly SolidColorBrush spectrumLineBrush = new SolidColorBrush(Colors.White);
private Line peakRealLine;
private readonly SolidColorBrush peakRealBrush = new SolidColorBrush(Colors.Blue);
private Line peakSmoothLine;
private readonly SolidColorBrush peakSmoothBrush = new SolidColorBrush(Colors.Red);
private readonly SolidColorBrush failBrush = new SolidColorBrush(Colors.Red);
private readonly SolidColorBrush okBrush = new SolidColorBrush(Colors.DarkGreen);
private DispatcherTimer dispatcherTimer;
private ISetup setup;
private bool isSetup = false;
#region Interface
private double[] FftData;
private int PeakRealX = 0;
private int PeakSmoothX = 0;
private double PeakRealY = 0;
#endregion
public MainWindow()
{
InitializeComponent();
ScanWaveInSoundCards();
ScanWaveOutSoundCards();
AddComListElements();
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = new TimeSpan(100000);
dispatcherTimer.Tick += DispatcherTimer_Tick;
RemapCanvas();
}
private void RemapCanvas()
{
//cnvMapX = new ValueMapperDouble(1028, cnvsPlot.ActualWidth);
cnvMapX = new ValueMapperDouble(300, cnvsPlot.ActualWidth);
cnvMapY = new ValueMapperDouble(maxDb, cnvsPlot.ActualHeight);
}
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
if (FftData != null)
DrawFrame();
}
private void DrawFrame()
{
DrawSpectrumLine();
DrawPeakReal();
DrawPeakSmooth();
UpdateLabels();
UpdateKeysGrid();
}
private void UpdateKeysGrid()
{
R.TB.NoteKeysModule.SetOnlyALabel(R.TB.PitchRecognizerModule.BucketToMidiNoteOnPlayableList(R.TB.SpectrumAnalyzerModule.PeakBucketSmooth));
}
private void UpdateLabels()
{
lblPeaks.Content = strPeakX + PeakRealX.ToString() + "\n" +
strPeakY + PeakRealY.ToString() + "\n" +
strPeakSmoothX + PeakSmoothX.ToString() + "\n" +
strNote + R.TB.PitchRecognizerModule.BucketToAnyMidiNote(PeakRealX);
lblHeadTracker.Content = strYaw + R.TB.HeadTrackerModule.Data.TranspYaw.ToString() + "\n" +
strPitch + R.TB.HeadTrackerModule.Data.TranspPitch.ToString() + "\n" +
strRoll + R.TB.HeadTrackerModule.Data.TranspRoll.ToString() + "\n" +
strSpeed + R.TB.Speed.ToString() + "\n";
}
private void DrawPeakSmooth()
{
if (peakSmoothLine != null)
{
cnvsPlot.Children.Remove(peakSmoothLine);
}
peakSmoothLine = new Line();
peakSmoothLine.Stroke = peakSmoothBrush;
peakSmoothLine.StrokeThickness = 1;
double X = cnvMapX.Map(PeakSmoothX);
peakSmoothLine.X1 = X;
peakSmoothLine.Y1 = cnvsPlot.ActualHeight;
peakSmoothLine.X2 = X;
peakSmoothLine.Y2 = 0;
cnvsPlot.Children.Add(peakSmoothLine);
}
private void DrawPeakReal()
{
if (peakRealLine != null)
{
cnvsPlot.Children.Remove(peakRealLine);
}
peakRealLine = new Line();
peakRealLine.Stroke = peakRealBrush;
peakRealLine.StrokeThickness = 1;
double X = cnvMapX.Map(PeakRealX);
peakRealLine.X1 = X;
peakRealLine.Y1 = cnvsPlot.ActualHeight;
peakRealLine.X2 = X;
peakRealLine.Y2 = 0;
cnvsPlot.Children.Add(peakRealLine);
}
private void DrawSpectrumLine()
{
if (spectrumLine != null)
{
cnvsPlot.Children.Remove(spectrumLine);
}
spectrumLine = new Polyline();
spectrumLine.Stroke = spectrumLineBrush;
spectrumLine.StrokeThickness = 1;
cnvMapX.BaseMax = 300;
for (int i = 0; i < 300; i++)
{
spectrumLine.Points.Add(new Point(cnvMapX.Map(i), cnvsPlot.ActualHeight - cnvMapY.Map(FftData[i])));
if (FftData[i] > cnvMapY.BaseMax)
{
//cnvMapY.BaseMax = dataFft[i];
}
}
cnvsPlot.Children.Add(spectrumLine);
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
/// <summary>
/// Scans for soundcards to be put into the listbox for selection
/// </summary>
private void ScanWaveInSoundCards()
{
lstWaveInSoundCards.Items.Clear();
for (int i = 0; i < WaveIn.DeviceCount; i++)
lstWaveInSoundCards.Items.Add(WaveIn.GetCapabilities(i).ProductName);
if (lstWaveInSoundCards.Items.Count > 0)
lstWaveInSoundCards.SelectedIndex = 0;
else
MessageBox.Show("ERROR: no recording devices available");
}
private void ScanWaveOutSoundCards()
{
lstWaveOutSoundCards.Items.Clear();
for (int i = 0; i < WaveOut.DeviceCount; i++)
lstWaveOutSoundCards.Items.Add(WaveOut.GetCapabilities(i).ProductName);
if (lstWaveOutSoundCards.Items.Count > 0)
lstWaveOutSoundCards.SelectedIndex = 0;
else
MessageBox.Show("ERROR: no output devices available");
}
private void AddComListElements()
{
for(int i = 0; i < 20; i++)
{
lstComSensorPort.Items.Add(i);
}
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
if (!isSetup)
{
setup = new SetupDefault(this);
setup.Setup();
isSetup = true;
}
R.TB.AudioModule.WaveInActive = true;
R.TB.AudioModule.WaveOutActive = true;
R.TB.FftModule.Start();
dispatcherTimer.Start();
UpdateMidiPortLabel();
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
R.TB.FftModule.Stop();
R.TB.AudioModule.WaveInActive = false;
R.TB.AudioModule.WaveOutActive = false;
dispatcherTimer.Stop();
}
private void TestFunction()
{
spectrumLine = new Polyline();
spectrumLine.Stroke = spectrumLineBrush;
spectrumLine.StrokeThickness = 4;
spectrumLine.Points.Add(new Point(20, 20));
spectrumLine.Points.Add(new Point(30, 30));
cnvsPlot.Children.Add(spectrumLine);
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
RemapCanvas();
}
void IPeakIntensityReceiver.ReceivePeakIntensity(double peakIntensity)
{
PeakRealY = peakIntensity;
}
void IPeakBucketRealReceiver.ReceivePeakBucketReal(int peakBucketReal)
{
PeakRealX = peakBucketReal;
}
void IPeakBucketSmoothReceiver.ReceivePeakBucketSmooth(int peakBucketSmooth)
{
PeakSmoothX = peakBucketSmooth;
}
void IFftDataReceiver.ReceiveFFTData(double[] fftData)
{
FftData = fftData;
}
private void btnMidiPortPlus_Click(object sender, RoutedEventArgs e)
{
if (isSetup)
{
R.TB.MidiModule.OutDevice++;
UpdateMidiPortLabel();
}
}
private void btnMidiPortMinus_Click(object sender, RoutedEventArgs e)
{
if (isSetup)
{
R.TB.MidiModule.OutDevice--;
UpdateMidiPortLabel();
}
}
private void UpdateMidiPortLabel()
{
lblMidiPort.Content = R.TB.MidiModule.OutDevice.ToString();
switch (R.TB.MidiModule.IsMidiOk())
{
case true:
lblMidiPort.Foreground = okBrush;
break;
case false:
lblMidiPort.Foreground = failBrush;
break;
default:
break;
}
}
private void btnTest_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(R.TB.PlayableNotes.Count.ToString());
}
private void cbxPlaySines_Click(object sender, RoutedEventArgs e)
{
}
private void lstWaveInSoundCards_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (isSetup)
{
R.TB.AudioModule.WaveInDeviceIndex = lstWaveInSoundCards.SelectedIndex;
}
}
private void lstWaveOutSoundCards_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (isSetup)
{
R.TB.AudioModule.WaveOutDeviceIndex = lstWaveOutSoundCards.SelectedIndex;
}
}
private void comSensorPort_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
R.TB.HeadTrackerModule.Connect(lstComSensorPort.SelectedIndex);
if (R.TB.HeadTrackerModule.IsConnectionOk)
{
lstComSensorPort.Foreground = okBrush;
}
else
{
lstComSensorPort.Foreground = failBrush;
}
}
private void btnCenter_Click(object sender, RoutedEventArgs e)
{
R.TB.HeadTrackerModule.Data.SetDeltaForAll();
}
}
}