-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect-key.js
More file actions
128 lines (127 loc) · 4.08 KB
/
Copy pathdetect-key.js
File metadata and controls
128 lines (127 loc) · 4.08 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
function detectKey(measures) {
// Information for sharps and flats in each key
var majorKeys = [
// ["A", "B", "C", "D", "E", "F", "G"]
{
name: "Ab",
notes: ["b", "b", "n", "b", "b", "n", "n"],
points: 0
},
{
name: "A",
notes: ["n", "n", "#", "n", "n", "#", "#"],
points: 0
},
{
name: "Bb",
notes: ["n", "b", "n", "n", "b", "n", "n"],
points: 0
},
{
name: "B",
notes: ["#", "n", "#", "#", "n", "#", "#"],
points: 0
},
{
name: "C",
notes: ["n", "n", "n", "n", "n", "n", "n"],
points: 0
},
{
name: "Db",
notes: ["b", "b", "n", "b", "b", "n", "b"],
points: 0
},
{
name: "D",
notes: ["n", "n", "#", "n", "n", "#", "n"],
points: 0
},
{
name: "Eb",
notes: ["b", "b", "n", "n", "b", "n", "n"],
points: 0
},
{
name: "E",
notes: ["n", "n", "#", "#", "n", "#", "#"],
points: 0
},
{
name: "F",
notes: ["n", "b", "n", "n", "n", "n", "n"],
points: 0
},
{
name: "F#",
notes: ["#", "n", "#", "#", "#", "#", "#"],
points: 0
},
{
name: "Gb",
notes: ["b", "b", "b", "b", "b", "n", "b"],
points: 0
},
{
name: "G",
notes: ["n", "n", "n", "n", "n", "#", "n"],
points: 0
}
];
const notePos ="ABCDEFG";
const noteNames = [
[{note: "G", accidental: "#"}, {note: "A", accidental: "b"}],
[{note: "A"}],
[{note: "A", accidental: "#"}, {note: "B", accidental: "b"}],
[{note: "B"}, {note: "C", accidental: "b"}],
[{note: "B", accidental: "#"}, {note: "C"}],
[{note: "C", accidental: "#"}, {note: "D", accidental: "b"}],
[{note: "D"}],
[{note: "D", accidental: "#"}, {note: "E", accidental: "b"}],
[{note: "E"}, {note: "F", accidental: "b"}],
[{note: "E", accidental: "#"}, {note: "F"}],
[{note: "F", accidental: "#"}, {note: "G", accidental: "b"}],
[{note: "G"}],
];
// Find best key signature
for (let measure of measures) {
for (let note of measure) {
if (note.note != "rest") {
// If note fits in the key, add points to that key
// Points are based on note length
for (let noteName of noteNames) {
for (let noteN of noteName) {
if ((note.note == noteN.note) && ((note.accidental == noteN.accidental) || (!note.accidental && !noteN.accidental))) {
for (let noteN2 of noteName) {
var pos = notePos.indexOf(noteN2.note);
for (let key of majorKeys) {
if (noteN2.accidental) {
if (noteN2.accidental == key.notes[pos]) {
key.points += note.note_length;
}
} else {
if (key.notes[pos] == "n") {
key.points += note.note_length;
}
}
}
}
}
}
}
}
}
}
console.log(majorKeys);
var maxPoints = 0;
var bestKey = undefined;
// Key with the most points is the best key
for (let key of majorKeys) {
if (key.points > maxPoints) {
bestKey = key.name;
maxPoints = key.points;
}
}
console.log("Best key:", bestKey);
return bestKey;
}