-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (88 loc) · 2.63 KB
/
index.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
"use strict";
var ac = null,
sr = null;
var codeEditor = new Editor('return RA();'),
lengthElement = document.querySelector('[name=length]'),
zoomPlusElement = document.querySelector('.zoom-widget-button-plus'),
zoomMinusElement = document.querySelector('.zoom-widget-button-minus'),
form = document.querySelector('form'),
canvas = document.querySelector('#canvas'),
timelineTracker = document.querySelector('#timelineTracker'),
soundCanvas = new SoundCanvas(canvas, timelineTracker);
codeEditor.appendTo(document.querySelector('#editor'));
codeEditor.show();
var busy = false;
function setBusy(bool) {
busy = bool;
if (busy) {
document.body.classList.add('busy');
} else {
document.body.classList.remove('busy');
}
}
var zoom = 1;
var ww = new Worker('./webworker.js?cache=' + Date.now());
ww.onmessage = function (e) {
if (e.data.type === 'error') {
console.error(e.data.message);
console.warn(e.data.stack);
} else if (e.data.type === 'result') {
var buffer = new Float32Array(e.data.buffer);
soundCanvas.setBuffer(buffer);
soundCanvas.setOffset(0);
soundCanvas.setZoom(zoom);
var ab = ac.createBuffer(1, buffer.length, sr);
ab.getChannelData(0).set(buffer);
var s = ac.createBufferSource();
s.buffer = ab;
s.connect(ac.destination);
s.start();
}
setBusy(false);
};
function generateSound () {
if (ac === null) {
ac = new AudioContext;
sr = ac.sampleRate;
}
if (!busy) {
setBusy(true);
ww.postMessage({
type: 'generate',
code: codeEditor.getContent().trim(),
duration: lengthElement.value,
sampleRate: sr
});
url.updateHash(lengthElement.value, codeEditor.getContent())
}
}
form.addEventListener('submit', function (e) {
e.preventDefault();
generateSound();
});
document.body.addEventListener('keyup', function (e) {
if ((e.metaKey || e.ctrlKey) && e.keyCode === 13) {
e.preventDefault();
generateSound();
}
});
function update () {
soundCanvas.refresh();
requestAnimationFrame(update);
}
zoomPlusElement.addEventListener('click', function () {
zoom = Math.min(4, Math.max(0, zoom + 1));
soundCanvas.setZoom(zoom);
});
zoomMinusElement.addEventListener('click', function () {
zoom = Math.min(4, Math.max(0, zoom - 1));
soundCanvas.setZoom(zoom);
});
update();
var data = url.parseHash();
if (data.code !== null) {
codeEditor.setContent(data.code);
}
if (data.duration !== null) {
lengthElement.value = +data.duration;
}