-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNote.cs
More file actions
134 lines (110 loc) · 2.36 KB
/
Note.cs
File metadata and controls
134 lines (110 loc) · 2.36 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
using System;
namespace TabPlayer
{
public struct Note
{
public static Note operator +(Note n, int offset)
{
var newVal = n.Value + offset;
var octaves = newVal / 12;
var value = newVal < 12 ? newVal : newVal % 12;
var octave = n.Octave + octaves;
var note = Notes[value];
n.Value = value;
n.Octave = octave;
n.Letter = $"{note}{octave}";
return n;
}
public static string[] Notes = new[]
{
"C", "C#", "D",
"D#", "E", "F",
"F#", "G", "G#",
"A", "A#", "B",
};
public int Value;
public int Octave;
public string Letter;
public static bool IsNote(string note)
{
note = note.ToUpper();
return note.Length > 0 && (note[0] == 'H' || Array.IndexOf(Notes, $"{note[0]}") > -1);
}
public static bool GetOctave(string note, out int octave)
{
var combined = "";
for (int i = note.Length - 1; i >= 0; i--)
{
var c = note[i];
if (!c.IsNumber())
break;
combined = c + combined;
}
return int.TryParse(combined, out octave);
}
public static Note Parse(string note)
{
var whole = note.ToUpper();
var n = whole.Substring(0, whole.Length - 1);
if (n.Length >= 2)
{
if (n[1] == 'B' || n[1] == '#')
{
n = n.Substring(0, 2);
}
else
{
n = $"{n[0]}";
}
}
GetOctave(whole, out var o);
return Parse(n, o);
}
public static Note Parse(string note, int octave)
{
note = note.ToUpper();
if (note.Length >= 2 && (note[1] == 'B' || note[1] == '#'))
{
note = note.Substring(0, 2);
}
else
{
note = $"{note[0]}";
}
var origNote = note;
if (origNote[0] == 'H')
{
var str = "B";
if (note.Length >= 2)
{
str += note[1];
}
note = str;
}
if (note.Length >= 2)
{
var off = note[1] == 'B' ? -1 : (note[1] == '#' ? 1 : 0);
var index = Array.IndexOf(Notes, $"{note[0]}") + off;
var value = index < 0 ? Notes.Length - Math.Abs(index) % Notes.Length : index % Notes.Length;
octave = index < 0 ? octave - 1 : octave;
note = Notes[value];
if (origNote == "H")
{
note = "H";
}
return new Note()
{
Value = value,
Octave = octave,
Letter = $"{note}{octave}"
};
}
return new Note
{
Value = Array.IndexOf(Notes, note),
Octave = octave,
Letter = $"{(origNote == "H" ? origNote : note)}{octave}"
};
}
}
}