-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
112 lines (90 loc) · 3.17 KB
/
script.js
File metadata and controls
112 lines (90 loc) · 3.17 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
const Tonal = require("@tonaljs/tonal");
const notes = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
console.log(Tonal.Chord.detect(["D", "Gb", "A", "C"]));
const keys = document.querySelectorAll(".key");
keys.forEach((key) => {
key.addEventListener("click", () => {
console.log(key.dataset.note);
//const noteAudio = document.getElementById(key.dataset.note);
//noteAudio.currentTime = 0;
//noteAudio.play();
//key.classList.add("active");
//noteAudio.addEventListener("ended", () => {
//key.classList.remove("active");
//});
});
});
navigator.requestMIDIAccess().then(onMIDISuccess, onMIDIFailure);
function onMIDIFailure() {
console.log("Could not access your MIDI devices.");
}
function onMIDISuccess(midiAccess) {
for (var input of midiAccess.inputs.values())
input.onmidimessage = getMIDIMessage;
}
var active_keys = [];
function getMIDIMessage(message) {
var command = message.data[0];
var note = message.data[1];
var velocity = message.data.length > 2 ? message.data[2] : 0; // a velocity value might not be included with a noteOff command
//determine note name and octave
note_name = notes[note % 12];
octave = Math.floor(note / 12) - 1;
note_name = note_name + octave;
switch (command) {
case 144: // noteOn
if (velocity > 0) {
console.log("note on ", note_name, velocity);
active_keys.push(note_name);
console.log("active keys ", active_keys.toString());
//illuminate key
//loop through keys array and use dataset.note to identify correct key in page
keys.forEach((key) => {
if (key.dataset.note == note_name) key.classList.add("active");
});
} else {
console.log("note off ", note_name, velocity);
const index = active_keys.indexOf(note_name);
if (index > -1) {
active_keys.splice(index, 1);
}
console.log("active keys ", active_keys.toString());
//loop through keys array and use dataset.note to identify correct key in page
keys.forEach((key) => {
if (key.dataset.note == note_name) key.classList.remove("active");
});
}
break;
case 128: // noteOff
console.log("note off ", note, velocity);
break;
}
active_keys.sort();
if (active_keys.length > 2) {
console.log(Tonal.Chord.detect(active_keys));
document.getElementById("chordText").innerHTML = Tonal.Chord.detect(
active_keys
)[0];
} else {
document.getElementById("chordText").innerHTML = "";
}
}
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("settings-button");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function () {
modal.style.display = "block";
};
// When the user clicks on <span> (x), close the modal
span.onclick = function () {
modal.style.display = "none";
};
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
}
};