-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathskeleton.html
190 lines (165 loc) · 5.21 KB
/
skeleton.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="Chris Webb, [email protected]">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MIDI Skeleton</title>
<script>
async function midi(rxchooser, txchooser, receive) {
let access, input, output;
try {
access = await navigator.requestMIDIAccess({sysex: true});
} catch {
return null;
}
rxchooser = rxchooser(function(id) {
if (input && input.id != id && receive)
input.removeEventListener("midimessage", receive);
input = access.inputs.get(id);
if (input && receive)
input.addEventListener("midimessage", receive);
});
txchooser = txchooser(function(id) {
output = access.outputs.get(id);
});
access.addEventListener("statechange", function(event) {
const id = event.port.id, state = event.port.state;
const name = state == "connected" ? event.port.name : null;
if (event.port.type == "input")
rxchooser(id, name);
else if (event.port.type == "output")
txchooser(id, name);
});
access.inputs.forEach(port => rxchooser(port.id, port.name));
access.outputs.forEach(port => txchooser(port.id, port.name));
return (data, time) => output && output.send(data, time);
}
function chooser(select) {
select = document.getElementById(select);
return function(choose) {
select.onchange = event => choose(select.value);
return function(id, name) {
for (let option = 0; option < select.length; option++)
if (select.options[option].value == id) {
if (name) {
if (select.options[option].text != name)
select.options[option].text = name;
return;
}
select.options[option].remove(option);
}
if (name) {
const option = document.createElement("option");
option.value = id;
option.text = name;
select.add(option);
}
choose(select.value);
};
};
}
function hex(data) {
return data.reduce(function(s, x) {
return s + " " + x.toString(16).padStart(2, "0");
}, "").slice(1);
}
function raw(line) {
const words = line.split(/\s+/).filter(Boolean);
const bytes = words.map(x => Number("0x" + x));
return bytes.map(x => isNaN(x) || x > 0xff ? null : x);
}
function match(pattern, data) {
if (pattern == null || data.length < pattern.length)
return false;
for (let index = 0; index < pattern.length; index++)
if (pattern[index] != null && pattern[index] != data[index])
return false;
return true;
}
function receive(listener, timeout) {
if (listener == null)
listener = (data, time) => data;
return new Promise(function(resolve) {
function handler(event) {
const result = listener(event.data, event.receivedTime);
if (result) {
clearTimeout(timer);
receivers.delete(handler);
resolve(result);
}
}
const timer = timeout && setTimeout(() => {
receivers.delete(handler);
resolve();
}, timeout);
receivers.add(handler);
});
}
async function dump(request, start, filter, finish, timeout) {
const messages = [];
const promise = receive(function(data) {
if (start && match(start, data))
start = null;
else if (start)
return false;
if (match(filter || [], data))
messages.push(data);
return match(finish, data);
}, timeout);
transmit(request);
return await promise, messages;
}
const error = console.error, log = console.log;
const receivers = new Set();
let main, transmit;
document.addEventListener("DOMContentLoaded", async function() {
main = document.querySelector("main");
transmit = await midi(chooser("rx"), chooser("tx"),
event => receivers.forEach(handler => handler(event)));
if (transmit == null)
return error("Web MIDI not supported or port access refused");
if (document.getElementById("rx").childNodes.length == 0)
error("Warning: no MIDI inputs found");
if (document.getElementById("tx").childNodes.length == 0)
error("Warning: no MIDI outputs found");
});
</script>
<style>
html, body {
margin: 0;
}
header {
display: flex;
border-bottom: 1px solid #ddd;
padding: 4px;
font-family: system-ui, sans-serif;
}
header > * {
margin: 4px;
}
header #title {
flex: 1;
color: inherit;
font-weight: bold;
text-decoration: inherit;
}
header select:empty {
visibility: hidden;
}
main {
padding: 8px;
}
</style>
</head>
<body>
<header>
<a href="https://arachsys.github.io/webmidi/" id="title">
MIDI Skeleton
</a>
<select id="rx" title="MIDI Receive Port"></select>
<select id="tx" title="MIDI Transmit Port"></select>
</header>
<main></main>
</body>
</html>