-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
222 lines (192 loc) · 7.46 KB
/
MainForm.cs
File metadata and controls
222 lines (192 loc) · 7.46 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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace PianoEnhancer
{
public partial class MainForm : Form
{
public sealed class DrawPanel : Panel
{
public DrawPanel()
{
DoubleBuffered = true;
Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
BackColor = Color.White;
}
}
private enum PianoState
{
Idle, Recording, Playing
}
private readonly PianoController _piano;
private bool _connected;
private PianoState _state;
private readonly RecordingDrawer _drawer;
private PianoState State
{
get => _state;
set { _state = value; RefreshPianoState(); }
}
public MainForm()
{
InitializeComponent();
RecordingPanel = new DrawPanel
{
Location = new Point(10, 30), Name = "RecordingPanel", Size = new Size(860, 160), TabIndex = 0
};
RecordingPanel.MouseClick += RecordingPanelClick;
RecordingPanel.Paint += RecordingPanelPaint;
RecordingBox.Controls.Add(RecordingPanel);
_piano = new PianoController();
_piano.PlayBack.PlaybackStopped += () => State = PianoState.Idle;
_connected = false;
State = PianoState.Idle;
EnumerateDevices();
_drawer = new RecordingDrawer {Recorder = _piano.Recorder, PlayBack = _piano.PlayBack};
DrawTimer.Start();
}
private void RecordingPanelClick(object sender, MouseEventArgs e)
{
_drawer.HandleClick(e.X,e.Y,RecordingPanel.Size, e.Button);
}
private void RefreshConnectionState()
{
ConnectButton.Enabled = InputDeviceComboBox.SelectedIndex >= 0 && OutputDeviceComboBox.SelectedIndex >= 0;
DeviceControlGroup.Enabled = _connected;
RecordingBox.Enabled = _connected;
}
private void RefreshPianoState()
{
//invoke on the ui thread specifically because it might be called from other threads
if (RecordButton.InvokeRequired)
{
RecordButton.Invoke((Action) delegate
{
RecordButton.Enabled = _state == PianoState.Idle;
PlayButton.Enabled = _state == PianoState.Idle;
StopButton.Enabled = _state != PianoState.Idle;
SaveButton.Enabled = _state == PianoState.Idle;
LoadButton.Enabled = _state == PianoState.Idle;
NewButton.Enabled = _state == PianoState.Idle;
});
}
else
{
RecordButton.Enabled = _state == PianoState.Idle;
PlayButton.Enabled = _state == PianoState.Idle;
StopButton.Enabled = _state != PianoState.Idle;
SaveButton.Enabled = _state == PianoState.Idle;
LoadButton.Enabled = _state == PianoState.Idle;
NewButton.Enabled = _state == PianoState.Idle;
}
}
private void EnumerateDevices()
{
InputDeviceComboBox.Items.Clear();
OutputDeviceComboBox.Items.Clear();
using var tempLabel = new Label();
var longestInDevice = InputDeviceComboBox.Width;
var longestOutDevice = OutputDeviceComboBox.Width;
foreach (var device in PianoController.GetConnectedInputDevices())
{
var name = $"{device.ProductName} - {device.Manufacturer}";
InputDeviceComboBox.Items.Add(name);
tempLabel.Text = name;
longestInDevice = Math.Max(longestInDevice, tempLabel.PreferredWidth);
}
InputDeviceComboBox.DropDownWidth = longestInDevice;
if(InputDeviceComboBox.Items.Count > 0) InputDeviceComboBox.SelectedIndex = 0;
foreach (var device in PianoController.GetConnectedOutDevices())
{
var name = $"{device.ProductName} - {device.Manufacturer}";
OutputDeviceComboBox.Items.Add(name);
tempLabel.Text = name;
longestOutDevice = Math.Max(longestOutDevice, tempLabel.PreferredWidth);
}
OutputDeviceComboBox.DropDownWidth = longestOutDevice;
if(OutputDeviceComboBox.Items.Count > 0) OutputDeviceComboBox.SelectedIndex = 0;
RefreshConnectionState();
}
private void SetVoiceButtonClick(object sender, EventArgs e)
{
using var voiceSelector = new VoiceSelector();
if (voiceSelector.ShowDialog() == DialogResult.OK)
{
_piano.SetVoice(voiceSelector.Voice.ToVoiceComposition());
}
}
private void ConnectButtonClick(object sender, EventArgs e)
{
_piano.Init(InputDeviceComboBox.SelectedIndex,OutputDeviceComboBox.SelectedIndex);
_connected = true;
RefreshConnectionState();
}
private void RefreshButtonClick(object sender, EventArgs e)
{
EnumerateDevices();
}
private void RecordButtonClick(object sender, EventArgs e)
{
_piano.Recorder.StartRecording();
State = PianoState.Recording;
}
private void StopButtonClick(object sender, EventArgs e)
{
switch (State)
{
case PianoState.Recording:
_piano.Recorder.StopRecording();
break;
case PianoState.Playing:
_piano.Stop();
break;
case PianoState.Idle:
break;
default:
throw new ArgumentOutOfRangeException();
}
State = PianoState.Idle;
}
private void PlayButtonClick(object sender, EventArgs e)
{
_piano.Play();
State = PianoState.Playing;
}
private void SaveButtonClick(object sender, EventArgs e)
{
if (SaveRecordingDialog.ShowDialog() != DialogResult.OK) return;
_piano.Recorder.SaveRecording(SaveRecordingDialog.FileName);
}
private void LoadButtonClick(object sender, EventArgs e)
{
if (LoadRecordingDialog.ShowDialog() != DialogResult.OK) return;
_piano.Recorder.LoadRecording(LoadRecordingDialog.FileName);
}
private void NewButtonClick(object sender, EventArgs e)
{
_piano.Recorder.CreateNewRecording();
}
private void DrawTimerTick(object sender, EventArgs e)
{
if(_connected) RecordingPanel.Refresh();
}
private void RecordingPanelPaint(object sender, PaintEventArgs e)
{
_drawer.DrawPanel(e.Graphics, RecordingPanel.Size);
SetPrevNextButtonsEnabled();
}
private void SetPrevNextButtonsEnabled()
{
PrevButton.Enabled = _drawer.CurrentPage > 0;
NextButton.Enabled = _drawer.CurrentPage < _drawer.PageCount - 1;
}
private void PrevButtonClick(object sender, EventArgs e)
{
_drawer.CurrentPage--;
}
private void NextButtonClick(object sender, EventArgs e)
{
_drawer.CurrentPage++;
}
}
}