-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·513 lines (441 loc) · 14.7 KB
/
index.html
File metadata and controls
executable file
·513 lines (441 loc) · 14.7 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sema engine - Default</title>
<style type="text/css">
html, body {
width: 100%;
height: 100%;
overflow: hidden;
margin: 0px;
}
.prettyprint ol.linenums>li {
list-style-type: decimal
}
.CodeMirror {
height: 100%; /* need to call editor.refresh() after resize: */
overflow: auto !important;
}
.main-container {
width: 100%;
height: 100%;
display: grid;
grid-gap: 3px;
grid-template-columns: 0.2rem 1fr 1fr 0.2rem;
grid-template-rows: 40px 20px 30px 20px 0.9fr 30px 20px 0.9fr;
}
h3 {
margin-block-start: 0.5em;
margin-block-end: 0.5em;
}
.header {
grid-row: 1;
grid-column: 2 / -1;
}
.instruction {
grid-row: 2;
grid-column: 2 / -1;
}
.commands-container-a {
grid-row:3;
grid-column: 2 / -1;
/* padding-top: 10px;
padding-bottom: 10px; */
}
.editor-header {
font-weight: bold;
width: auto;
}
#editor-header-lc {
grid-row:4;
grid-column:2/2;
font-weight: bold;
width: auto;
}
#editor-header-js {
grid-row:4;
grid-column:3/3;
font-weight: bold;
width: auto;
}
#console-header {
grid-row:7;
grid-column:3/3;
font-weight: bold;
width: auto;
}
#editor-header-ge {
grid-row:7;
grid-column:2/2;
font-weight: bold;
width: auto;
}
.container-lc {
width: 100%;
height: 100%;
grid-row: 5;
grid-column: 2/2;
/* display: grid; */
/* grid-template-rows: auto 1fr; */
position:relative;
/* width: auto; */
border: 1px solid #ddd;
/* max-height: calc(90vh - 40px); */
min-height: 22em;
max-height: 82vh;
min-width: 25em;
overflow:hidden;
resize: both;
}
button {
height: 25px;
}
#container-eval-lc{
grid-row: 6 / 6;
grid-column: 2/2;
}
#evalButton:hover + .hide {
width: 280px;
visibility: visible;
}
#container-js {
width: 100%;
height: 100%;
grid-row: 5;
grid-column: 3 / 3;
border: 1px solid #ddd;
position: relative;
/* position: absolute; */
min-height: 200px;
max-height: 82vh;
/* width: auto; */
/* overflow: hidden; */
overflow: auto;
resize: vertical;
}
#textArea-console {
height:100%;
width: 100%;
}
#container-eval-js{
grid-row: 6 / 6;
grid-column: 3/3;
}
#evalJsButton:hover + .hide {
width: 280px;
visibility: visible;
}
.hide {
visibility:hidden;
color:gray;
}
.container-ge {
grid-row:8;
grid-column: 2/ 2;
position:relative;
height:100%;
width: 100%;
border: 1px solid #ddd;
min-width: 20em;
/* max-width: 0.5fr; */
/* overflow:hidden; */
/* resize: both; */
/* margin-top:-5em; */
}
.container-console {
grid-row:8;
grid-column: 3/ 4;
position:relative;
height:100%;
width: 100%;
border: 1px solid #ddd;
min-width: 20em;
/* max-width: 0.5fr; */
/* overflow:hidden; */
/* resize: both; */
/* margin-top:-5em; */
}
.console-container-ce {
width: 100%;
height: 100%;
position:absolute;
}
.editor-container-ge {
width: 100%;
height: 100%;
position:absolute;
}
.commands-container-b {
grid-row:5;
width: auto;
grid-column: 1 / -1;
padding-top: 5px;
padding-bottom: 5px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.59.1/codemirror.min.js" integrity="sha512-9fASXXoC6x4BKdqe6IcEYZnt4KPMiQx5YJIsiWevf2QpSXfPcXMj4MTzIbwQuKJRysBlRh1LGHIaQm/415WyEA==" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.59.1/codemirror.min.css" integrity="sha512-MWdvo/Qqcf4pY1ecQUB1uBn0qLp19U/qJ1Rpp2BDZeuBA7YsFEwkvqR/+aG4BroPiAYDunKJ6X8R/Pmdt3p7oA==" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.59.1/keymap/vim.min.js" integrity="sha512-g2nzBS/fBHxdSRXaDcYGHVg2Rjk7+3gITKVMv1q/ylh2izUiw1AZ50urelrhy7I6EYLQi5RM8FFj3rdATH5wIg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.59.1/mode/javascript/javascript.min.js" integrity="sha512-ssJuYJm7WuzXiDvMgmhFfPcXAM3QLILVaH8VhLDkauwZRTSlw7uxxUh2TXDau3y2eK0PsJfzL/q0lYsjTEO8sQ==" crossorigin="anonymous"></script>
<script async="" src="//localhost:35729/livereload.js?snipver=1" id="livereloadscript" crossorigin="anonymous"></script>
</head>
<body>
<div class="main-container">
<div class='header'>
<h3>Sema engine – <i>default</i> language</h3>
<!-- <h3>Sema engine – <i>rubber-duckling</i> language</h3> -->
<!-- <h3>Sema engine – <i>nibble</i> language</h3> -->
</div>
<div class='instruction'>
<span>You might need to clean the cache when testing your worklet processor code (cmd-shift-backspace/ctrl-shift-backspace)</span>
</div>
<div class="commands-container-a">
<button id="playButton">Start Engine</button>
<button id="stopButton">Stop Engine</button>
<button id="loadSamplesButton">Load Samples</button>
<button id="learnerButton">Create Learner</button>
<button id="plusButton">Vol+</button>
<button id="minusButton">Vol–</button>
<button id="hushButton">Hush–</button>
<button id="createAnalyserButton">Create Analyser</button>
<button id="createInputButton">Create Input</button>
<button id="mouseButton">Mouse input</button>
<span id="outputText"></span>
</div>
<span id="editor-header-lc" class="editor-header">Livecode Editor</span>
<div class="container-lc">
<textarea id="textArea-livecode"></textarea>
</div>
<div id="container-eval-lc">
<button id="evalButton">Eval Livecode</button>
<span class="hide">( left [cmd/ctrl] + enter )</span>
</div>
<span id="editor-header-js" class="editor-header">JS Editor</span>
<div id="container-js">
<!-- <div class="editor-container-js"> -->
<textarea id="textArea-js"></textarea>
<!-- </div> -->
</div>
<div id="container-eval-js">
<button id="evalJsButton">Eval JS block</button>
<span class="hide">( right [cmd/ctrl] + enter )</span>
</div>
<span id="editor-header-ge" class="editor-header">Grammar Editor</span>
<div class="container-ge">
<div class="editor-container-ge">
<textarea id="textArea-grammar"></textarea>
</div>
</div>
<span id="console-header" class="editor-header">Console</span>
<div class="container-console">
<!-- <div class="console-container-c"> -->
<textarea id="textArea-console"></textarea>
<!-- </div> -->
</div>
</div>
<script type="module">
import {
Engine,
compile,
Learner,
getBlock,
Logger
// takeOverConsole
} from "../index.mjs";
let engine,
analyser = 0,
dspCode,
learner,
logger = new Logger(),
inputStreamConnected = false;
;
const $ = (elemId, event, callback) =>
document.getElementById(elemId).addEventListener(event, callback);
// var origin = 'https://frantic0.github.io/sema-engine';
let origin = document.location.origin;
function eventListener(log){
let myConsole = document.getElementById('textArea-console')
myConsole.value = logger.rawLog + '\r\n';
myConsole.scrollTop = myConsole.scrollHeight;
}
logger.addEventListener("onLog", eventListener);
$("playButton", "click", () => {
if(!engine){
try{
logger.takeOverConsole();
engine = new Engine();
engine.init(origin);
} catch (error) {
console.error("Error creating and initialising Engine: ", error);
}
}
engine.play();
});
$("stopButton", "click", () => {
engine.hush();
// engine.stop()
});
$("loadSamplesButton", "click", () => {
if(engine){
try{
engine.loadSample("909.wav", "/test/audio/909.wav");
engine.loadSample("909b.wav", "/test/audio/909b.wav");
engine.loadSample("909closed.wav", "/test/audio/909closed.wav");
engine.loadSample("909open.wav", "/test/audio/909closed.wav");
engine.loadSample("crebit2.ogg", "/test/audio/crebit2.ogg");
engine.loadSample("kick1.wav", "/test/audio/kick1.wav");
engine.loadSample("snare1.wav", "/test/audio/snare1.wav");
} catch (error) {
console.error("ERROR: Failed to compile and eval: ", error);
}
}
else throw new Error('ERROR: Engine not initialized. Press Start Engine first.')
});
$("learnerButton", "click", async () => {
if(engine){
try{
learner = new Learner();
await engine.addLearner('l1', learner);
}catch(error){
console.error("ERROR: Error creating or initialising learner: ", err);
}
}
});
const evalLiveCode = () => {
if(engine){
try{
const { errors, dspCode } = compile( editorGrammar.getValue(), editorLivecode.getValue() );
if(dspCode){
console.info(editorLivecode.getValue());
engine.eval(dspCode);
}
} catch (err) {
console.error("ERROR: Failed to compile and eval: ", err);
}
}
else throw new Error('ERROR: Engine not initialized. Please press Start Engine first.')
}
const evalJs = async () => {
if(learner && editorJS){
const code = getBlock(editorJS);
learner.eval(code);
}
else throw new Error('ERROR: Learner not initialized. Please press Create Learner first.')
}
const createInput = async () => {
if(engine){
try{
if(!inputStreamConnected){
inputStreamConnected = await engine.connectMediaStream();
}
else {
inputStreamConnected = await engine.disconnectMediaStream();
}
} catch (err) {
console.error("ERROR: Failed to compile and eval: ", err);
}
}
else throw new Error('ERROR: Engine not initialized. Please press Start Engine first.')
}
$("evalButton", "click", () => evalLiveCode() );
$("evalJsButton", "click", async () => evalJs() );
$("createInputButton", "click", async () => createInput() );
$("mouseButton", "click", () => {
if(engine){
try{
const id = "mxy",
ttype = "mouseXY",
blockSize = 2;
let sab = engine.createSharedBuffer(id, ttype, blockSize);
const onMouseMove = e => {
const x = e.pageX/window.innerWidth;
const y = e.pageY/window.innerHeight;
document.getElementById('outputText').innerText = `${parseFloat(x).toFixed(5)} ${parseFloat(y).toFixed(5)}`;
engine.pushDataToSharedBuffer(id, [ x, y ]);
}
// Subscribe Left `Alt`-key down event to subscribe mouse move
document.addEventListener("keydown", e => {
if(e.keyCode === 18){
document.addEventListener( 'mousemove', onMouseMove, true )
}
});
// Subscribe Left `Alt`-key UP event to unsubscribe mouse move
document.addEventListener("keyup", e => {
if(e.which === 18){
document.getElementById('outputText').innerText = ``;
document.removeEventListener( 'mousemove', onMouseMove, true );
}
});
} catch (err) {
console.error("ERROR: Failed to create new channel for mouse data: ", err);
}
}
else throw new Error('ERROR: Engine not initialized. Please press Start Engine first.')
});
(() => {
// Subscribe Left `Alt`-key down event to subscribe mouse move
document.addEventListener("keydown", e => {
if ( e.keyCode == 13 && ( e.ctrlKey || e.metaKey ) ){
evalLiveCode();
}
else if ( e.keyCode == 13 && e.altKey ){
evalJs();
}
});
})();
function sabPrinter() {
try {
for (let v in engine.sharedArrayBuffers) {
let avail = engine.sharedArrayBuffers[v].rb.available_read();
if ( avail > 0 && avail != engine.sharedArrayBuffers[v].rb.capacity ) {
for (let i = 0; i < avail; i += engine.sharedArrayBuffers[v].blocksize) {
if(engine.sharedArrayBuffers[v].ttype === 'scope'){
let val = new Float64Array(engine.sharedArrayBuffers[v].blocksize);
engine.sharedArrayBuffers[v].rb.pop(val);
console.log(v, val);
}
}
}
}
setTimeout(sabPrinter, 20);
} catch (error) {
// console.log(error);
setTimeout(sabPrinter, 100);
}
}
$("minusButton", "click", () => engine.less());
$("plusButton", "click", () => engine.more());
$("hushButton", "click", () => engine.hush());
$("createAnalyserButton", "click", () => sabPrinter() );
</script>
<script>
// need to replicate here as this is outside the module context
// origin already exists as global scope var
// var origin = 'https://frantic0.github.io/sema-engine';
var origin = document.location.origin;
const setupEditorWithContent = ( elementId, contentFileRelativeURL ) => {
let textArea = document.getElementById(elementId);
// Transform textarea into Codemirror instance
const editor = CodeMirror.fromTextArea(textArea, {
lineWrapping: true,
lineNumbers: true
});
// Refresh Codemirror on container resize
new ResizeObserver( e => editor.setSize("100%", "100%"))
.observe(textArea.parentElement);
// Populate Codemirror with content
fetch( origin + contentFileRelativeURL )
.then( response => response.text() )
.then( data => editor.setValue(data) )
return editor;
}
const editorLivecode = setupEditorWithContent("textArea-livecode", '/test/langs/default-5-lc.txt');
const editorGrammar = setupEditorWithContent("textArea-grammar", '/test/langs/default-grammar.txt');
const editorJS = setupEditorWithContent("textArea-js", '/test/js/default-3-js.txt');
// const editorLivecode = setupEditorWithContent("textArea-livecode", '/test/langs/rubber-duckling-lc-1.txt');
// const editorGrammar = setupEditorWithContent("textArea-grammar", '/test/langs/rubber-duckling-grammar.txt');
</script>
</body>
</html>