-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundtrackControl.cs
175 lines (154 loc) · 4.72 KB
/
SoundtrackControl.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
namespace ToneGenerator
{
public partial class SoundtrackControl : UserControl
{
public SoundtrackControl() : this(true) { }
public SoundtrackControl(bool play)
{
InitializeComponent();
frequencyNumeric.Maximum = SineWaveProvider.SAMPLE_RATE / 2M;
Amplitude = 1f;
Soundtrack.Frequency = (float)frequencyNumeric.Value;
Soundtrack.Left = leftCheckBox.Checked = play;
Soundtrack.Right = rightCheckBox.Checked = play;
}
public Soundtrack Soundtrack { get; private set; } = new Soundtrack();
public float Amplitude
{
get => Soundtrack.Amplitude;
set
{
if (value < 0.0f)
value = 0.0f;
if (value != Amplitude)
{
volumeLabel.Text = "Amplitude: " + value;
Soundtrack.Amplitude = value;
dBVolumeSlider.Volume = value;
}
}
}
public void SetMinimumVolume(float minVolume)
{
dBVolumeSlider.MinimumDB = minVolume;
}
internal void SetMaximumValue(float maxVolume)
{
dBVolumeSlider.MaximumDB = maxVolume;
}
public void SetLeft(bool value)
{
Soundtrack.Left = leftCheckBox.Checked = value;
}
public void SetRight(bool value)
{
Soundtrack.Right = rightCheckBox.Checked = value;
}
public void MultiplyFrequency(double factor)
{
var value = frequencyNumeric.Value * (decimal)factor;
if (value < frequencyNumeric.Minimum)
value = frequencyNumeric.Minimum;
if (value > frequencyNumeric.Maximum)
value = frequencyNumeric.Maximum;
frequencyNumeric.Value = value;
}
/// <summary>
/// Processes shortcut keys for frequency and volume controls.
/// </summary>
/// <param name="msg">A <see cref="Message"/>, passed by reference, that represents the Win32 message to process.</param>
/// <param name="keyData">One of the <see cref="Keys"/> values that represents the key to process.</param>
/// <returns>true if the keystroke was processed and consumed by the control; otherwise, false to allow further processing.</returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Add || keyData == Keys.Subtract)
{
// Change dbVolume by ±1 dB.
double logVolume = Math.Log10(dBVolumeSlider.Volume);
if (keyData == Keys.Subtract)
logVolume -= 0.05f;
else if (keyData == Keys.Add)
logVolume += 0.05f;
Amplitude = (float)Math.Pow(10, logVolume);
return true;
}
switch (keyData)
{
case Keys.OemMinus:
MultiplyFrequency(1.0 / GetHalfFrequencyIncrement());
return true;
case Keys.Oemplus:
MultiplyFrequency(GetHalfFrequencyIncrement());
return true;
case Keys.Oemcomma:
MultiplyFrequency(1.0 / GetFrequencyIncrement());
return true;
case Keys.OemPeriod:
MultiplyFrequency(GetFrequencyIncrement());
return true;
case Keys.OemSemicolon:
MultiplyFrequency(1.0 / GetDoubleFrequencyIncrement());
return true;
case Keys.OemQuotes:
MultiplyFrequency(GetDoubleFrequencyIncrement());
return true;
case Keys.OemOpenBrackets:
MultiplyFrequency(0.5);
return true;
case Keys.OemCloseBrackets:
MultiplyFrequency(2);
return true;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
}
private double GetFrequencyIncrement()
{
return ParentForm is not Form1 parent ? Math.Pow(2.0, 1.0 / 12) : parent.FrequencyIncrement;
}
private double GetDoubleFrequencyIncrement()
{
var x = GetFrequencyIncrement();
return x * x;
}
private double GetHalfFrequencyIncrement()
{
return Math.Sqrt(GetFrequencyIncrement());
}
private void DBVolumeSlider_VolumeChanged(object sender, EventArgs e)
{
Amplitude = dBVolumeSlider.Volume;
}
private void FrequencyNumeric_ValueChanged(object sender, EventArgs e)
{
Soundtrack.Frequency = (float)frequencyNumeric.Value;
}
private void ChangeFrequencyButtons_Click(object sender, EventArgs e)
{
if (sender == minusButton)
MultiplyFrequency(1.0 / GetHalfFrequencyIncrement());
else if (sender == plusButton)
MultiplyFrequency(GetHalfFrequencyIncrement());
else if (sender == prevNoteButton)
MultiplyFrequency(1.0 / GetFrequencyIncrement());
else if (sender == nextNoteButton)
MultiplyFrequency(GetFrequencyIncrement());
else if (sender == minus2Button)
MultiplyFrequency(1.0 / GetDoubleFrequencyIncrement());
else if (sender == plus2Button)
MultiplyFrequency(GetDoubleFrequencyIncrement());
else if (sender == prevOctaveButton)
MultiplyFrequency(0.5);
else if (sender == nextOctaveButton)
MultiplyFrequency(2.0);
}
private void LeftCheckBox_CheckedChanged(object sender, EventArgs e)
{
SetLeft(leftCheckBox.Checked);
}
private void RightCheckBox_CheckedChanged(object sender, EventArgs e)
{
SetRight(rightCheckBox.Checked);
}
}
}