-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicNotesControll.cs
More file actions
285 lines (272 loc) · 10 KB
/
MusicNotesControll.cs
File metadata and controls
285 lines (272 loc) · 10 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;
using System.Drawing.Drawing2D;
using NAudio.Midi;
namespace MusicNotes
{
[Designer(typeof(MusicNotes.Design.MusicNotesControlDesign), typeof(IRootDesigner))]
public partial class MusicNotesControll: UserControl
{
public delegate void DrawMusicNotes(Graphics g);
public List<Note> Notes = new List<Note>();
public MusicNotesControll()
{
InitializeComponent();
maxWidth = Width;
Notes.Add(new Note('A', 4, 0, false, "whole", 16));
Notes.Add(new Note('G', 3, 1, true, "quarter", 8));
Notes.Add(new Note('A', 3, -1, true, "eighth", 4));
Notes.Add(new Note('B', 3, 0, true, "16th", 2));
Notes.Add(new Note('C', 4, 0, false, "32th", 1));
Notes.Add(new Note('D', 4, 0, false, "whole", 16));
Notes.Add(new Note('E', 4, 0, false, "whole", 16));
Notes.Add(new Note('F', 4, 0, false, "whole", 16));
Notes.Add(new Note('G', 4, 0, false, "whole", 16));
Notes.Add(new Note('A', 4, 0, false, "whole", 16));
Notes.Add(new Note('B', 4, 0, false, "whole", 16));
Notes.Add(new Note('C', 5, 0, false, "whole", 16));
}
static int fontSize = 16;
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", fontSize, FontStyle.Bold);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
Brush back = new SolidBrush(Color.White);
Pen notePen = new Pen(Color.Black, 2);
Pen noteBarPen = new Pen(Color.Black, 3);
Pen barlinePen = new Pen(Color.Black, 1);
Brush noteBrush = new SolidBrush(Color.Black);
Brush noteRed = new SolidBrush(Color.Red);
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// draw background
g.FillRectangle(back, 0, 0, Width, Height);
g.TranslateTransform(10, 45);
// draw some lines
DrawLines(g, 5);
// handle scroll
g.TranslateTransform(-(scrollValue * maxWidth / 100), 0);
// draw content
drawDefaultNotes(g);
}
public void drawDefaultNotes(Graphics g)
{
// draw some notes
int cnt = 0;
int ticks = 0;
foreach (Note note in Notes)
{
//Console.WriteLine("{0}: {1} {2}", on, l, on.NoteLength);
DrawNote(g, noteBrush, note.Step, note.Octave, note.Alter, note.ShowAlter, note.Type, note.Duration, ticks, note.StartMeasure);
ticks += Math.Max(note.Duration, 4);
cnt += 1;
}
}
int LineHeight = 5;
int NoteSpacing = 8;
int MinNoteSpacing = 32;
protected void DrawNote(Graphics g, Brush brush, char step, int octave, int alter, bool showalter, string type, int duration, int ticks, bool startMeasure)
{
// base A midi note, correct to wrap around on C
var noteline = (step - 'A' + 5) % 7;
// base octave 4, 7 note lines per octave
noteline += (octave - 4) * 7;
// invert as lower = higher on the screen
noteline *= -1;
// offset 9 lines to put A4 on the correct line
noteline += 9;
//Console.WriteLine("linenr {0}", noteline);
// first three notes do not have a top bar
int numBars = 0;
// longer note tail for more bars
int tailLenght = numBars > 2 ? 30 : 25;
var s = g.Save();
// translate to note offset in X
g.TranslateTransform(ticks * NoteSpacing, 0);
// draw measures
if (startMeasure)
{
g.DrawLine(notePen, -dim.X / 2, -LineHeight, -dim.X / 2, 10 * LineHeight);
}
// draw extra lines
if (noteline < -2)
{
// even lines need to draw when not on the available 5 lines drawn
for (int i = -2; i > noteline; i-=2)
g.DrawLine(barlinePen, -5, i*LineHeight, dim.X+5, i*LineHeight);
}
if (noteline > 8)
{
// even lines need to draw when not on the available 5 lines drawn
for (int i = 8; i <= noteline+1; i += 2)
g.DrawLine(barlinePen, -5, i*LineHeight, dim.X + 5, i*LineHeight);
}
// translate to note position in Y
g.TranslateTransform(0, (noteline * LineHeight) + 2);
// draw alter (5 and up is special for not drawing)
if (showalter)
{
var m = g.MeasureString(Note.alterLookUp[1 + alter], drawFont);
//Console.WriteLine("measured {0} for {1}", m, alter);
g.DrawString(Note.alterLookUp[1 + alter], drawFont, drawBrush, -(Math.Min(22, m.Width) - 2), -fontSize / 2, drawFormat);
}
g.RotateTransform(-15f);
// draw note elipse (fill or open)
switch(type)
{
case "whole":
case "half":
g.DrawEllipse(notePen, 1, 1, dim.X-2, dim.Y-2);
break;
default:
g.FillEllipse(brush, 0, 0, dim.X, dim.Y);
break;
}
// draw line from elipse (for all but full length note)
switch (type)
{
case "whole":
break;
default:
g.RotateTransform(+30f);
g.DrawLine(notePen, dim.X, 0, dim.X, -tailLenght);
break;
}
switch (type)
{
case "eighth":
numBars = 1;
break;
case "16th":
numBars = 2;
break;
case "32th":
numBars = 3;
break;
default:
numBars = 0;
break;
}
var pts = new List<Point>();
for (int i = 0; i < numBars; i++)
pts.Add(new Point(dim.X, -tailLenght + (i * 6)));
var ptsa = pts.ToArray();
//g.TransformPoints(CoordinateSpace.Page, CoordinateSpace.World, pts);
if (pts.Any())
g.Transform.TransformPoints(ptsa);
g.Restore(s);
var t = g.Transform.Clone();
t.Invert();
if (pts.Any())
t.TransformPoints(ptsa);
// draw sideway line
foreach (Point p in ptsa )
{
g.DrawLine(noteBarPen, p, new Point(p.X + 10, p.Y));
}
// determine max widht
maxWidth = Math.Max(maxWidth, ticks * NoteSpacing);
}
protected void DrawLines(Graphics g, int count)
{
for (int i = 0; i < count; i++)
{
g.DrawLine(barlinePen, 0, i * LineHeight * 2, Width, i * LineHeight * 2);
}
}
private Point dim = new Point(15,10);
[Browsable(true)]
public Point NoteSize
{
get
{
return this.dim;
}
set
{
dim = value;
LineHeight = dim.Y / 2;
}
}
private string demoStringValue = null;
private int scrollValue = 0;
private int maxWidth = 0;
public int ScrollValue
{
get
{
return scrollValue;
}
set
{
scrollValue = value;
Invalidate();
}
}
[Browsable(true)]
public string DemoString
{
get
{
return this.demoStringValue;
}
set
{
demoStringValue = value;
}
}
private void MusicNotesControll_MouseEnter(object sender, EventArgs e)
{
Invalidate();
}
}
public class Note
{
public static string[] alterLookUp = new string[] { "\u266d", "\u266e", "\u266f" };
public static string[] alterLookUpASCII = new string[] { "b", "o", "#" };
// C D E F G A B
static int[] MidiLookup = new int[] { 0, 2, 4, 5, 7, 9, 11 }; // from C = 0
public Note(char step, int octave, int alter, bool show, string type, int duration, bool startmeasure = false)
{
Step = step;
Octave = octave;
Alter = alter;
ShowAlter = show;
var idx = (step - 'A' + 5) % 7;
Midi = (octave * 12) + MidiLookup[idx] + alter + 12;
Type = type;
Duration = duration;
StartMeasure = startmeasure;
}
public override string ToString() {
if (ShowAlter)
{
return string.Format("Note: {2}{0}{1} ({3})", Step, Octave, alterLookUpASCII[Alter + 1], Midi);
}
else
{
return string.Format("Note: {0}{1} ({3})", Step, Octave, alterLookUpASCII[Alter + 1], Midi);
}
}
public char Step;
public int Octave;
public int Alter;
public bool ShowAlter;
public string Type;
public int Midi;
public int Duration;
public bool StartMeasure;
}
}