forked from naudio/NAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMixDiffForm.cs
612 lines (547 loc) · 19.5 KB
/
MixDiffForm.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using NAudio.Wave;
using MarkHeath.AudioUtils.Properties;
namespace MarkHeath.AudioUtils
{
public partial class MixDiffForm : Form
{
private PlaybackStatus playbackStatus;
private IWavePlayer wavePlayer;
readonly Font BigFont = new Font("Verdana", 36, FontStyle.Bold);
readonly Font EmptyFont = new Font("Verdana", 16, FontStyle.Bold);
private WaveMixerStream32 mixer;
private int skipSeconds;
private Button selectedButton;
private CompareMode compareMode;
private List<Button> fileButtons;
private bool shuffled;
public MixDiffForm()
{
InitializeComponent();
mixer = new WaveMixerStream32();
mixer.AutoStop = false;
skipSeconds = 3;
fileButtons = new List<Button>();
fileButtons.Add(buttonA);
fileButtons.Add(buttonB);
fileButtons.Add(buttonC);
fileButtons.Add(buttonD);
}
private bool LoadFile(Button button)
{
// prompt for file load
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "WAV Files (*.wav)|*.wav";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
MixdownInfo info = new MixdownInfo(openFileDialog.FileName);
// TODO: sort this out - post shuffle
info.Letter = button.Name.Substring(button.Name.Length - 1);
SetButtonInfo(button, info);
return true;
}
return false;
}
private void SetButtonInfo(Button button, MixdownInfo info)
{
if (button.Tag != null)
{
ClearFile(button);
}
button.Tag = info;
SetButtonAppearance(button);
mixer.AddInputStream(info.Stream);
SetLengthLabel();
}
private void ClearFile(Button button)
{
if (button.Tag != null)
{
MixdownInfo buttonInfo = button.Tag as MixdownInfo;
mixer.RemoveInputStream(buttonInfo.Stream);
buttonInfo.Stream.Close();
button.Tag = null;
SetButtonAppearance(button);
}
}
private void SetButtonAppearance(Button button)
{
MixdownInfo info = button.Tag as MixdownInfo;
if (info == null)
{
button.Text = "<Empty>";
button.Font = EmptyFont;
toolTip1.SetToolTip(button, null);
}
else
{
if (shuffled)
{
button.Text = "?";
toolTip1.SetToolTip(button, null);
button.Font = BigFont;
}
else
{
button.Text = info.Letter;
toolTip1.SetToolTip(button, info.FileName);
button.Font = BigFont;
}
}
}
private void OnMixButtonClick(object sender, EventArgs e)
{
Button button = sender as Button;
if (button.Tag == null)
{
if (LoadFile(button))
{
SelectButton(button);
}
}
else
{
SelectButton(button);
}
}
private void SelectButton(Button button)
{
MixdownInfo info;
if (selectedButton != null)
{
selectedButton.BackColor = SystemColors.Control;
selectedButton.ForeColor = SystemColors.ControlText;
info = selectedButton.Tag as MixdownInfo;
if (info != null)
{
// can be null if active button is cleared
info.Stream.Mute = true;
}
}
info = button.Tag as MixdownInfo;
button.ForeColor = Color.DarkGreen;
button.BackColor = Color.LightGoldenrodYellow;
info.Stream.Mute = false;
selectedButton = button;
if (playbackStatus == PlaybackStatus.Playing)
{
if (compareMode == CompareMode.SkipBack)
{
SkipBack();
}
else if (compareMode == CompareMode.Restart)
{
Rewind();
}
}
}
private void Play()
{
if (playbackStatus != PlaybackStatus.Playing)
{
if (playbackStatus != PlaybackStatus.Paused)
{
Rewind();
}
if (wavePlayer == null)
{
wavePlayer = new WaveOut();
wavePlayer.Init(mixer);
}
wavePlayer.Play();
playbackStatus = PlaybackStatus.Playing;
timer1.Start();
}
}
private void Stop()
{
if (playbackStatus != PlaybackStatus.Stopped)
{
if (wavePlayer != null)
{
wavePlayer.Stop();
playbackStatus = PlaybackStatus.Stopped;
timer1.Stop();
}
Rewind();
}
}
private void Pause()
{
if (playbackStatus == PlaybackStatus.Playing)
{
wavePlayer.Pause();
playbackStatus = PlaybackStatus.Paused;
}
}
private void toolStripButtonPlay_Click(object sender, EventArgs e)
{
Play();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (playbackStatus == PlaybackStatus.Playing)
{
if (mixer.Position > mixer.Length)
{
if (toolStripButtonLoop.Checked)
{
Rewind();
}
else
{
Stop();
}
}
SetPositionLabel();
}
}
private void SetPositionLabel()
{
TimeSpan currentTime = mixer.CurrentTime;
toolStripLabelPosition.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
currentTime.Hours,
currentTime.Minutes,
currentTime.Seconds,
currentTime.Milliseconds);
}
private void SetLengthLabel()
{
TimeSpan length = mixer.TotalTime;
toolStripLabelLength.Text = String.Format("{0:00}:{1:00}:{2:00}",
length.Hours,
length.Minutes,
length.Seconds);
}
private void MixDiffForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (playbackStatus != PlaybackStatus.Stopped)
{
Stop();
}
if (wavePlayer != null)
{
wavePlayer.Dispose();
wavePlayer = null;
}
if (mixer != null)
{
mixer.Dispose();
mixer = null;
}
}
private void toolStripButtonStop_Click(object sender, EventArgs e)
{
Stop();
}
private void toolStripButtonPause_Click(object sender, EventArgs e)
{
Pause();
}
private void toolStripButtonBack_Click(object sender, EventArgs e)
{
SkipBack();
}
private void toolStripButtonForward_Click(object sender, EventArgs e)
{
if (mixer != null)
{
mixer.CurrentTime += TimeSpan.FromSeconds(skipSeconds);
SetPositionLabel();
}
}
private void toolStripButtonRewind_Click(object sender, EventArgs e)
{
Rewind();
}
private void SkipBack()
{
if (mixer != null)
{
mixer.CurrentTime += TimeSpan.FromSeconds(0 - skipSeconds);
SetPositionLabel();
}
}
private void Rewind()
{
if (mixer != null)
{
mixer.Position = 0;
SetPositionLabel();
}
}
public CompareMode CompareMode
{
get { return compareMode; }
set
{
compareMode = value;
currentPositionToolStripMenuItem1.Checked = (compareMode == CompareMode.CurrentPosition);
skipBackToolStripMenuItem1.Checked = (compareMode == CompareMode.SkipBack);
restartToolStripMenuItem1.Checked = (compareMode == CompareMode.Restart);
}
}
private void currentPositionToolStripMenuItem1_Click(object sender, EventArgs e)
{
CompareMode = CompareMode.CurrentPosition;
}
private void skipBackToolStripMenuItem1_Click(object sender, EventArgs e)
{
CompareMode = CompareMode.SkipBack;
}
private void restartToolStripMenuItem1_Click(object sender, EventArgs e)
{
CompareMode = CompareMode.Restart;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
Button button = (Button)contextMenuStrip1.SourceControl;
ClearFile(button);
}
private void selectFileToolStripMenuItem_Click(object sender, EventArgs e)
{
Button button = (Button)contextMenuStrip1.SourceControl;
LoadFile(button);
}
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
Button button = (Button)contextMenuStrip1.SourceControl;
if (button.Tag != null)
{
MixdownInfo mixdownInfo = button.Tag as MixdownInfo;
PropertiesForm propertiesForm = new PropertiesForm(mixdownInfo);
if (propertiesForm.ShowDialog() == DialogResult.OK)
{
}
}
}
private void saveComparisonToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.DefaultExt = ".MixDiff";
saveFileDialog.Filter = "*.MixDiff (MixDiff Comparison Files)|*.MixDiff|*.* (All Files)|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
SaveComparison(saveFileDialog.FileName);
}
}
private void SaveComparison(string fileName)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
using (XmlWriter writer = XmlWriter.Create(fileName,settings))
{
writer.WriteStartElement("MixDiff");
writer.WriteStartElement("Settings");
writer.WriteAttributeString("CompareMode", compareMode.ToString());
writer.WriteEndElement();
foreach (Button button in fileButtons)
{
WriteMixdownInfo(writer, button.Tag as MixdownInfo);
}
writer.WriteEndElement();
}
}
private void LoadComparison(string fileName)
{
XmlDocument document = new XmlDocument();
document.Load(fileName);
XmlNode compareModeNode = document.SelectSingleNode("MixDiff/Settings/@CompareMode");
CompareMode = (CompareMode)Enum.Parse(typeof(CompareMode), compareModeNode.Value);
XmlNodeList mixes = document.SelectNodes("MixDiff/Mix");
int buttonIndex = 0;
foreach(XmlNode mixNode in mixes)
{
Button button = fileButtons[buttonIndex++];
MixdownInfo info = new MixdownInfo(mixNode.SelectSingleNode("@FileName").Value);
info.DelayMilliseconds = Int32.Parse(mixNode.SelectSingleNode("@DelayMilliseconds").Value);
info.OffsetMilliseconds = Int32.Parse(mixNode.SelectSingleNode("@OffsetMilliseconds").Value);
info.VolumeDecibels = Int32.Parse(mixNode.SelectSingleNode("@VolumeDecibels").Value);
info.Letter = button.Name.Substring(button.Name.Length - 1);
info.Stream.Mute = true;
SetButtonInfo(button,info);
}
SelectButton(fileButtons[0]);
}
private void WriteMixdownInfo(XmlWriter writer, MixdownInfo mixdownInfo)
{
if (mixdownInfo != null)
{
writer.WriteStartElement("Mix");
writer.WriteAttributeString("FileName", mixdownInfo.FileName);
writer.WriteAttributeString("DelayMilliseconds", mixdownInfo.DelayMilliseconds.ToString());
writer.WriteAttributeString("OffsetMilliseconds", mixdownInfo.OffsetMilliseconds.ToString());
writer.WriteAttributeString("VolumeDecibels", mixdownInfo.VolumeDecibels.ToString());
writer.WriteEndElement();
}
}
private void openSavedComparisonToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.DefaultExt = ".MixDiff";
openFileDialog.Filter = "*.MixDiff (MixDiff Comparison Files)|*.MixDiff|*.* (All Files)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
Stop();
foreach(Button button in fileButtons)
{
ClearFile(button);
}
LoadComparison(openFileDialog.FileName);
}
}
private void contentsToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start("http://www.codeplex.com/naudio/Wiki/View.aspx?title=MixDiff");
}
catch (Exception)
{
MessageBox.Show("Failed to launch browser to show help file");
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
NAudio.Utils.AboutForm aboutForm = new NAudio.Utils.AboutForm();
aboutForm.ShowDialog();
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
SettingsForm settingsForm = new SettingsForm();
if (settingsForm.ShowDialog() == DialogResult.OK)
{
// TODO: reopen WaveOut device
}
}
private void toolStripButtonShuffle_Click(object sender, EventArgs e)
{
if (!shuffled)
{
Shuffle();
}
else
{
Reveal();
}
toolStripButtonShuffle.Checked = shuffled;
}
private void Shuffle()
{
List<MixdownInfo> mixdowns = new List<MixdownInfo>();
foreach (Button button in fileButtons)
{
if (button.Tag != null)
{
mixdowns.Add(button.Tag as MixdownInfo);
}
}
Random rand = new Random();
if (mixdowns.Count < 2)
{
MessageBox.Show("You need to have at least two files to compare to use the shuffle feature",
Application.ProductName);
return;
}
shuffled = true;
if (Settings.Default.UseAllSlots)
{
foreach (Button button in fileButtons)
{
if (button.Tag == null)
{
button.Tag = mixdowns[rand.Next() % mixdowns.Count];
}
}
}
for (int n = 0; n < 12; n++)
{
int swap1 = rand.Next() % fileButtons.Count;
int swap2 = rand.Next() % fileButtons.Count;
if (swap1 != swap2)
{
object tag1 = fileButtons[swap1].Tag;
fileButtons[swap1].Tag = fileButtons[swap2].Tag;
fileButtons[swap2].Tag = tag1;
}
}
Button firstMix = null;
foreach (Button button in fileButtons)
{
SetButtonAppearance(button);
if (button.Tag != null && firstMix == null)
firstMix = button;
}
SelectButton(firstMix);
}
private void Reveal()
{
shuffled = false;
foreach (Button button in fileButtons)
{
SetButtonAppearance(button);
}
}
private void MixDiffForm_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.D1:
OnMixButtonClick(buttonA, EventArgs.Empty);
e.Handled = true;
break;
case Keys.D2:
OnMixButtonClick(buttonB, EventArgs.Empty);
e.Handled = true;
break;
case Keys.D3:
OnMixButtonClick(buttonC, EventArgs.Empty);
e.Handled = true;
break;
case Keys.D4:
OnMixButtonClick(buttonD, EventArgs.Empty);
e.Handled = true;
break;
case Keys.Space:
if (playbackStatus != PlaybackStatus.Playing)
{
Play();
}
else
{
Pause();
}
e.Handled = true;
break;
case Keys.Home:
Rewind();
e.Handled = true;
break;
}
}
}
public enum CompareMode
{
CurrentPosition,
SkipBack,
Restart,
}
enum PlaybackStatus
{
Stopped,
Playing,
Paused
}
}