-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
570 lines (444 loc) · 16 KB
/
MainForm.cs
File metadata and controls
570 lines (444 loc) · 16 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
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
using Newtonsoft.Json;
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Input;
using TabPlayer.Properties;
namespace TabPlayer
{
public partial class MainForm : Form
{
public static MainForm Instance;
public NoteManager NoteManager;
private Tab _tab = null;
private Point _mouseDownPoint;
private double _mouseDownTime;
private double _tabOffset = 0;
private double _tabWidth = 100;
private int _dashPerSecond = 10;
private bool _spaceDown;
private bool _mouseDown;
private bool _loaded = false;
public MainForm()
{
Instance = this;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Application.Idle += OnIdle;
new Thread(() =>
{
var me = (MethodInvoker)(() =>
{
if (IsHandleCreated && !Disposing && !IsDisposed && Visible)
{
OnIdle();
}
});
while (IsHandleCreated && !Disposing && !IsDisposed && Visible)
{
try
{
Invoke(me);
}
catch { }
Thread.Sleep(2);
}
})
{ IsBackground = true }.Start();
Settings.Default.Reload();
BassManager.Reload();
LoadInstruments();
rtbTab.AllowDrop = true;
rtbTab.DragDrop += rtbTab_DragDrop;
rtbTab.Text = Settings.Default.Tab;
Size = Settings.Default.Size;
SetTab();
if (_tab is Tab tab)
{
tab.Time = Math.Min(tab.Length, Settings.Default.Time);
tab.Update(true);
}
chbAutoMute.Checked = Settings.Default.AutoMute;
chbRepeat.Checked = Settings.Default.Repeat;
chbMerge.Checked = Settings.Default.Merge;
chbPauseOnEdit.Checked = Settings.Default.PauseOnEdit;
tbarSpeed.Value = Math.Max(tbarSpeed.Minimum, Math.Min(tbarSpeed.Maximum, Settings.Default.Speed));
tbarSpeed_ValueChanged(null, null);
_loaded = true;
}
public void OnIdle(object sender = null, EventArgs e = null) => UpdateTab();
private void LoadInstruments()
{
var config = "instruments.json";
var loaded = new JSONInstrument[] { };
try
{
var json = File.ReadAllText(config);
loaded = JsonConvert.DeserializeObject<JSONInstrument[]>(json);
foreach (var instrument in loaded)
{
cbInstrument.Items.Add(instrument);
}
}
catch
{
loaded = new JSONInstrument[]
{
new JSONInstrument
{
ID = "guitar",
Name = "Guitar",
Tuning = new[]{ "E4", "B3", "G3", "D3", "A2", "E2" }
},
new JSONInstrument
{
ID = "guitar_nylon",
Name = "Nylon Guitar",
Tuning = new[]{ "E4", "B3", "G3", "D3", "A2", "E2" }
},
new JSONInstrument
{
ID = "guitar_nylon",
Name = "Ukulele",
Tuning = new[]{ "A4", "E4", "C4", "G4" }
},
new JSONInstrument
{
ID = "bass",
Name = "Bass",
Tuning = new[]{ "G2", "D2", "A1", "E1" }
}
};
cbInstrument.Items.AddRange(loaded);
var json = JsonConvert.SerializeObject(loaded, Formatting.Indented);
try
{
File.WriteAllText(config, json);
}
catch
{
}
}
NoteManager = new NoteManager(loaded);
var ins = Settings.Default.Instrument;
if (ins < 0 || ins >= cbInstrument.Items.Count)
ins = 0;
if (loaded.Length > 0)
cbInstrument.SelectedIndex = ins;
}
private void UpdateTab(bool skipStep = false)
{
if (skipStep)
return;
var captureInput = _tab != null && !btnPlayPause.Focused && ActiveControl != rtbTab && ContainsFocus;
if (captureInput && (Keyboard.IsKeyDown(Key.R) || Keyboard.IsKeyDown(Key.S)))
{
_tab?.Stop();
}
if (captureInput && Keyboard.IsKeyDown(Key.Space) && _tab != null)
{
if (!_spaceDown && _tab != null)
{
if (_tab.Playing)
{
_tab.Pause();
}
else
{
if (_tab.Time == _tab.Length)
{
_tab.Play();
}
else
{
_tab.Resume();
}
}
}
_spaceDown = true;
}
else
{
_spaceDown = false;
}
if (_mouseDown && _tab != null)
{
var mouse = PointToClient(System.Windows.Forms.Cursor.Position);
var drag = _mouseDownPoint.X - mouse.X;
var offset = drag / _tabWidth * _tab.Length;
var time = _mouseDownTime + offset;
_tab.Time = Math.Max(Math.Min(_tab.Length, time), 0);
_tab.Update(true);
}
else
{
_tab?.Update();
}
var progress = 0.0;
if (_tab != null)
{
progress = Math.Max(0, _tab.Time - 0.5) / _tab.Length;
btnPlayPause.Text = !_tab.Playing || _tab.Paused ? "PLAY" : "PAUSE";
lblTab.Tick();
}
lblTab.Location = new Point((int)(pTab.Size.Width / 2.0 - progress * _tabWidth - _tabOffset + 1), 0);
lblTuning.Location = new Point((int)(lblTab.Location.X - lblTuning.ClientSize.Width + _tabOffset * 1.45), lblTab.Location.Y);
pProgress.Size = new Size((int)(pTab.Size.Width * (_tab?.Progress ?? 0)), pProgress.Size.Height);
pCenter.Size = new Size(1, pTab.Size.Height);
pCenter.Location = new Point((int)(pTab.Size.Width / 2f - pCenter.Size.Width / 2f), 0);
var rect = pTab.ClientRectangle;
rect.Offset(-lblTab.Location.X, 0);
lblTab.Invalidate(rect);
lblTab.Update();
lblTuning.Update();
}
private Tab SetTab(bool adjustForStops = false)
{
if (cbInstrument.Items.Count == 0)
return null;
if (!Tab.TryParse(rtbTab.Text.Split('\n'), chbMerge.Checked, (JSONInstrument)cbInstrument.Items[cbInstrument.SelectedIndex], out var tab))
return null;
tab.OnPluck += OnPluck;
tab.DashPerScond = _tab?.DashPerScond ?? _dashPerSecond * tbarSpeed.Value / 100.0;
tab.RingingStrings = _tab?.RingingStrings ?? tab.RingingStrings;
tab.Index = _tab?.Index ?? tab.Index;
tab.Time = _tab?.Time ?? tab.Time;
tab.Playing = _tab?.Playing ?? tab.Playing;
tab.Paused = _tab?.Paused ?? tab.Paused;
tab.Repeat = chbRepeat.Checked;
tab.AutoMute = chbAutoMute.Checked;
if (adjustForStops)
{
var skips = tab.CountSkips();
var skipsLast = _tab?.CountSkips() ?? skips;
var diff = skips - skipsLast;
tab.Index += diff;
tab.Time += diff;
}
var content = string.Join("\n", tab.Data).Trim();
if (content.Length == 0)
{
return null;
}
lblTuning.Text = string.Join("\n", tab.Tuning.Select(n => n.Letter));
var width = lblTab.ClientSize.Width;
var height = pTab.Size.Height;
var line = height / (float)tab.Data.Length / 1.2f;
var font = new Font(lblTab.Font.FontFamily, line, FontStyle.Regular, GraphicsUnit.Pixel);
var tuningWidth = 1;
using (var bmp = new Bitmap(1, 1))
{
using (var g = Graphics.FromImage(bmp))
{
var size = TextRenderer.MeasureText(g, content, font, new Size(int.MaxValue, int.MaxValue), TextFormatFlags.TextBoxControl);
width = size.Width;
size = TextRenderer.MeasureText(g, lblTuning.Text, font, new Size(int.MaxValue, int.MaxValue), TextFormatFlags.TextBoxControl);
tuningWidth = size.Width;
}
}
lblTab.Font = font;
lblTab.Text = content;
lblTab.Size = new Size(width, height);
lblTuning.Font = lblTab.Font;
lblTuning.Size = new Size(tuningWidth, height);
lblTuning.BackColor = lblTab.BackColor;
lblTuning.ForeColor = lblTab.ForeColor;
var scale = lblTab.Font.Size / 16.80556;
var offset = 5 * scale;
_tabOffset = offset;
_tabWidth = lblTab.Size.Width - offset * 2;
_tab = tab;
UpdateTab();
Settings.Default.Tab = rtbTab.Text;
SaveSettings();
return tab;
}
private void SaveSettings()
{
if (!_loaded)
return;
Settings.Default.Time = _tab?.Time ?? 0;
Settings.Default.Repeat = chbRepeat.Checked;
Settings.Default.PauseOnEdit = chbPauseOnEdit.Checked;
Settings.Default.AutoMute = chbAutoMute.Checked;
Settings.Default.Merge = chbMerge.Checked;
Settings.Default.Speed = tbarSpeed.Value;
Settings.Default.Size = Size;
Settings.Default.Instrument = cbInstrument.SelectedIndex;
Settings.Default.Save();
}
private void OnPluck(object _, StringEventArgs e)
{
lblTab.Pluck(e.String, e.Mute);
}
private void rtbTab_DragDrop(object o, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
var file = files[0];
if (File.Exists(file))
{
try
{
rtbTab.Text = File.ReadAllText(file);
var old = _tab;
var tab = SetTab();
if (tab != null)
{
if (_tab != null && _tab.Playing)
{
tab.Play();
}
else
{
tab.Stop();
}
}
}
catch
{
}
}
}
}
private void rtbTab_TextChanged(object sender, EventArgs e)
{
var tab = SetTab();
if (tab != null)
{
if (chbPauseOnEdit.Checked)
{
tab.Pause();
}
}
SaveSettings();
}
private void chbRepeat_CheckedChanged(object sender, EventArgs e)
{
if (_tab != null)
_tab.Repeat = chbRepeat.Checked;
ActiveControl = lblTab;
SaveSettings();
}
private void chbPauseOnEdit_CheckedChanged(object sender, EventArgs e)
{
ActiveControl = lblTab;
SaveSettings();
}
private void chbAutoMute_CheckedChanged(object sender, EventArgs e)
{
if (_tab != null)
_tab.AutoMute = chbAutoMute.Checked;
ActiveControl = lblTab;
SaveSettings();
}
private void chbMerge_CheckedChanged(object sender, EventArgs e)
{
SetTab(true);
ActiveControl = lblTab;
SaveSettings();
}
private void tbarSpeed_ValueChanged(object sender, EventArgs e)
{
var dps = _dashPerSecond * tbarSpeed.Value / 100.0;
if (_tab != null)
_tab.DashPerScond = dps;
lblSpeed.Text = $"Speed: {tbarSpeed.Value}% ({dps:F1} dashes/s)";
SaveSettings();
}
private void cbInstrument_SelectedIndexChanged(object sender, EventArgs e)
{
if (_loaded)
{
SetTab();
}
SaveSettings();
}
private void lblTab_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (_tab == null)
return;
_mouseDownPoint = PointToClient(System.Windows.Forms.Cursor.Position);
_mouseDownTime = _tab?.Time ?? 0;
_mouseDown = true;
}
private void lblTab_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
_mouseDown = false;
}
private void tbarSpeed_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
ActiveControl = lblTab;
}
private void LoseTextBoxFocus(object sender, EventArgs e)
{
if (ActiveControl == rtbTab)
{
ActiveControl = lblTab;
}
}
private void btnStop_Click(object sender, EventArgs e)
{
_tab?.Stop();
ActiveControl = lblTab;
}
private void btnSave_Click(object sender, EventArgs e)
{
_tab?.Pause();
var sfd = new SaveFileDialog();
sfd.Filter = "Wave file|*.wav";
sfd.Title = "Save tab as a sound file";
sfd.DefaultExt = ".wav";
sfd.FileName = "myTab.wav";
if (sfd.ShowDialog() == DialogResult.OK)
{
var data = _tab.GenerateAudioFile();
if (data != null)
File.WriteAllBytes(sfd.FileName, data);
}
ActiveControl = lblTab;
}
private void btnPlayPause_Click(object sender, EventArgs e)
{
if (_tab != null)
{
if (!_tab.Playing && _tab.Index == _tab.Length)
_tab.Play();
else
{
if (_tab.Paused || !_tab.Playing)
_tab.Resume();
else
_tab.Pause();
}
}
ActiveControl = lblTab;
}
private void Form1_Resize(object sender, EventArgs e)
{
UpdateTab();
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
SaveSettings();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
NoteManager.Dispose();
BassManager.Dispose();
lblTab.Dispose();
Application.Idle -= OnIdle;
SaveSettings();
}
private void LoseTextBoxFocus(object sender, System.Windows.Forms.MouseEventArgs e)
{
ActiveControl = lblTab;
}
}
}