-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.js
126 lines (112 loc) · 3.91 KB
/
midi.js
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
//Support for WebMIDI
//- will not work on Safari! Best supported on Chrome
//- laptop keyboard code at bottom
//- edit 'noteOn' and 'noteOff' functions to work with your script
//enable to let your laptop keyboard trigger notes
let midiOn = true;
if (!navigator.requestMIDIAccess) {
console.log("WebMIDI is not supported in this browser.");
} else {
console.log("WebMIDI supported. Setting up...");
navigator.requestMIDIAccess().then(onMIDISuccess, onMIDIFailure);
}
function onMIDISuccess(midiAccess) {
console.log("MIDI access obtained.");
midiAccess.inputs.forEach(function(input) {
console.log("Input device ID:", input.id, "Name:", input.name);
input.onmidimessage = handleMIDIMessage;
});
}
function onMIDIFailure() {
console.log("Failed to obtain MIDI access.");
}
function handleMIDIMessage(message) {
var data = message.data; // this gives us our [command/channel, note, velocity] data.
var command = data[0];
var note = data[1];
var velocity = (data.length > 2) ? data[2] : 0; // a velocity value might not be included with a note off command
switch (command) {
case 144: // noteOn message
if (velocity > 0) {
noteOn(note, velocity);
} else {
noteOff(note);
}
break;
case 128: // noteOff message
noteOff(note);
break;
}
}
function noteOn(note, velocity) {
console.log(`Note ${note} on with velocity ${velocity}`);
// Handle note on
}
function noteOff(note) {
console.log(`Note ${note} off`);
}
/************************************
* LAPTOP KEYBOARD
* ************************************/
let notesOn = new Set();
let octave = 4;
let activeKeys = {};
let keyToNote = {
90: { "midi": 60, "pitch": "C" }, // Z
83: { "midi": 61, "pitch": "C#/Db" }, // S
88: { "midi": 62, "pitch": "D" }, // X
68: { "midi": 63, "pitch": "D#/Eb" }, // D
67: { "midi": 64, "pitch": "E" }, // C
86: { "midi": 65, "pitch": "F" }, // V
71: { "midi": 66, "pitch": "F#/Gb" }, // G
66: { "midi": 67, "pitch": "G" }, // B
72: { "midi": 68, "pitch": "G#/Ab" }, // H
78: { "midi": 69, "pitch": "A" }, // N
74: { "midi": 70, "pitch": "A#/Bb" }, // J
77: { "midi": 71, "pitch": "B" }, // M
188: { "midi": 72, "pitch": "C" }, // ,
76: { "midi": 73, "pitch": "C#/Db" }, // L
190: { "midi": 74, "pitch": "D" }, // .
186: { "midi": 75, "pitch": "D#/Eb" },// ;
191: { "midi": 76, "pitch": "E" } // /
};
// Function to handle octave changes
function changeOctave(modifier) {
octave = Math.max(1, Math.min(7, octave + modifier));
}
document.addEventListener('keydown', handleKeyDown);
document.addEventListener('keyup', handleKeyUp);
function handleKeyDown(event) {
if (midiOn) {
const keyCode = event.keyCode;
if (!activeKeys[keyCode]) {
activeKeys[keyCode] = true;
if (keyToNote[keyCode]) {
let note = keyToNote[keyCode];
let midiNote = note["midi"] + (octave - 4) * 12;
if (midiNote <= 127) {
noteOn(midiNote,127)
console.log('Keyboard Note on:', midiNote); // Replace with MIDI handler
}
} else if (keyCode === 37) { // Arrow left
changeOctave(-1);
} else if (keyCode === 39) { // Arrow right
changeOctave(1);
}
}
}
}
function handleKeyUp(event) {
if (midiOn) {
const keyCode = event.keyCode;
activeKeys[keyCode] = false;
if (keyToNote[keyCode]) {
let note = keyToNote[keyCode];
let midiNote = note["midi"] + (octave - 4) * 12;
if (midiNote <= 127) {
noteOff(midiNote)
console.log('Keyboard Note off:', midiNote); // Replace with MIDI handler
}
}
}
}