-
Notifications
You must be signed in to change notification settings - Fork 11
/
debug.html
295 lines (227 loc) · 6.96 KB
/
debug.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<html>
<head>
<style>
html, body {
height: 100%;
width: 100%;
padding: 0px;
margin: 0px;
color:#000;
font-size: 10;
overflow:auto;
font-family: monospace;
}
.post {
white-space: -moz-pre-wrap; /* Firefox 1.0-2.0 */
white-space: pre-wrap; /* current browsers */
}
</style>
</head>
<body>
<b id="play"></b>
<b id="bpm"></b> bpm
<b id="signature"></b>
<b id="bar"></b>
<b id="beat"></b>
<b id="connection"></b>
<div id="send"></div>
<div id="post"></div>
<script>
///// utils /////
// get url args:
var querystring = (function() {
var qstr = window.location.search;
var query = {};
var a = qstr.substr(1).split('&');
for (var i = 0; i < a.length; i++) {
var b = a[i].split('=');
query[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
}
return query;
})();
// debug messaging:
function post(msg) {
document.getElementById("post").innerHTML += msg + "<br>";
//console.log(msg);
}
// randoms
function random(n) {
return Math.floor(Math.random()*n);
}
function pick(arr) {
return arr[random(arr.length)];
}
///// websocket /////
var wsocket;
var connected = false;
var connectTask;
var track = querystring.track || 0;
function ws_connect() {
if (connected) {
if (connectTask != undefined) {
clearInterval(connectTask);
connectTask = undefined;
}
}
if ('WebSocket' in window) {
var host = querystring.host || "localhost";
var port = querystring.port || "8081";
var address = "ws://" + host + ":" + port;
document.getElementById("connection").innerHTML = 'connecting to ' + address;
wsocket = new WebSocket(address);
wsocket.onopen = function(ev) {
document.getElementById("connection").innerHTML = 'connected to ' + address;
connected = true;
// cancel the auto-reconnect task:
if (connectTask != undefined) clearInterval(connectTask);
connectTask = undefined;
// handshake
send("get_scene");
};
wsocket.onclose = function(ev) {
document.getElementById("connection").innerHTML = 'disconnected from ' + address;
connected = false;
// set up an auto-reconnect task:
if (connectTask != undefined) connectTask = setInterval(ws_connect, 4000);
};
wsocket.onmessage = function(ev) {
handlemessage(ev.data);
};
wsocket.onerror = function(ev) {
post("WebSocket error");
};
} else {
post("WebSockets are not available in this browser!!!");
}
}
function send(msg) {
if(wsocket != undefined) wsocket.send(msg);
document.getElementById("send").innerHTML = msg;
}
var lom = {};
var gens = [];
function handlemessage(msg) {
if (msg.charAt(0) == "{") {
lom = JSON.parse(msg);
post("received object");
console.log(lom);
// clear memory:
gens = [];
// set up the globals:
document.getElementById("bar").innerHTML = lom.bar;
document.getElementById("beat").innerHTML = lom.bit;
document.getElementById("signature").innerHTML = lom.sig;
document.getElementById("bpm").innerHTML = lom.bpm;
document.getElementById("play").innerHTML = lom.ply;
} else {
var parts = msg.split(" ");
var track = parts[0];
// or search for first device with midi_input == 1
// subsequent devices are effects
var device = pick(lom.tracks[track].devices);
var cmd = parts[1];
var arg = parts[2];
if (cmd == "seq") {
var beat = +arg;
// demo: generate notes etc.
var notes = [ 60, 62, 64, 65, 67, 71, 72 ];
var msgarr = [];
for (var j=0; j<2; j++) {
var divs = Math.pow(2, random((j+1)*2)+1);
var div = 1/divs;
var pitch = pick(notes);
var dur = 500*div;
for (var i=0; i<divs; i++) {
var phase = i*div;
var vel = 60 + random(30);
// this way works too:
//msgarr.push(track + " add " + beat + " " + phase + " note " + pitch + " " + vel + " " + dur);
msgarr.push(track + " add " + beat + " " + phase + " duration " + dur);
msgarr.push(track + " add " + beat + " " + phase + " velocity " + vel);
msgarr.push(track + " add " + beat + " " + phase + " pitch " + pitch);
}
}
var phase = 0;
// "set" a parameter:
var param = pick(device.parameters);
if (param.name != "Device On") {
var val;
if (param.quantized) {
val = param.min + random(param.max - param.min);
} else {
val = param.min + Math.random() * (param.max - param.min);
}
msgarr.push(track + " add " + beat + " " + phase + " zet " + param.id + " " + val );
//console.log(param.name, " ", val);
}
var param = pick(device.parameters);
if (param.name != "Device On") {
var val = Math.random();
msgarr.push(track + " add " + beat + " " + phase + " set " + param.id + " " + val );
//console.log(param.name, " ", val);
}
// mute / solo testing:
if (random(4) == 0) {
if (random(2)) {
msgarr.push(track + " add " + beat + " " + phase + " mute " + random(2));
} else {
msgarr.push(track + " add " + beat + " " + phase + " solo " + random(2));
}
}
// gen testing:
// unset?
if (random(16) < gens.length) {
var which = random(gens.length);
var id = gens[which];
gens.splice(which, 1);
msgarr.push(track + " add " + beat + " " + phase + " ungen " + id);
}
if (random(16) < gens.length && random(2) == 0) {
// set a parameter:
var which = random(gens.length);
var id = gens[which];
msgarr.push(track + " add " + beat + " " + phase + " genp " + id + " p0 " + random(4));
} else {
// add a new mapping:
var param = pick(device.parameters);
if (param.name != "Device On") {
var code = "Param p0(" + random(4) + ");";
code += "out1 = 1 - rate(in1, (p0+1)*" + (1/Math.pow(2, random(4))) + ");";
// wrap the code in quotes:
code = '"' + code + '"';
var msg = track + " add " + beat + " " + phase + " gen " + param.id + " " + code;
msgarr.push(msg);
//console.log (msg);
gens.push(param.id);
}
}
send(msgarr.join("~"));
} else if (cmd == "bar") {
var bar = +arg;
document.getElementById("bar").innerHTML = bar;
} else if (cmd == "bit") {
var beat = +arg;
document.getElementById("beat").innerHTML = beat;
} else if (cmd == "ply") {
var play = +arg;
document.getElementById("play").innerHTML = play;
} else if (cmd == "sig") {
var signature = arg;
document.getElementById("signature").innerHTML = signature;
} else if (cmd == "bpm") {
var bpm = +arg;
document.getElementById("bpm").innerHTML = bpm;
} else if (cmd == "err") {
post("received " + msg);
} else if (cmd == "clr") {
document.getElementById("post").innerHTML = "";
} else {
post("received " + msg);
}
}
}
///// start up /////
ws_connect();
</script>
</body>
</html>