-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
235 lines (188 loc) · 5.08 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
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
/**
* webaudio-daw
*/
var fork = require('worker-fork');
var preload = require('load-image');
var bufferSize = 4096;
if (fork.isMaster) master();
else worker();
function master(){
/**
* disable backspace leave page.
* TODO: make module
*/
var rx = /INPUT|SELECT|TEXTAREA/i;
document.body.addEventListener('keydown', function(ev){
if (8 === ev.which) { // backspace
if (!rx.test(ev.target.tagName) || ev.target.disabled || ev.target.readOnly) {
ev.preventDefault();
}
}
});
var bufferMax = 6;
var bufferQueue = [];
var k = require('k')(window);
k.ignore = false;
k('super + enter', function(ev){
ev.preventDefault();
toolbar.togglePause();
return false;
});
k('super + shift + enter', function(ev){
ev.preventDefault();
ctx.stop();
toolbar.togglePause();
return false;
});
var debounce = require('debounce');
var Sublime = require('./sublime');
var toolbar = require('./toolbar');
var sidebar = require('./sidebar');
// var fund = require('./fund');
var dsp = require('webaudio-dsp');
var ctx = dsp(bufferSize, process);
var sampleRate = ctx.audio.sampleRate;
var child = fork('build.js', 'wavepot');
toolbar.create(ctx);
document.body.appendChild(toolbar.el);
sidebar.create(ctx);
var sublime = ctx.sublime = Sublime();
setTimeout(function(){ // prevents flash of unstyled ace
document.body.appendChild(sublime.el);
document.body.appendChild(sidebar.el);
}, 500);
//oscilloscope.polyline.setAttribute('stroke', '#A6E22E');
ctx.isNewProject = true;
var session = sublime.editor.getSession();
session.setMode('ace/mode/javascript');
session.setTabSize(2);
session.setUseWorker(true);
session.on('change', debounce(onchange, 500));
session.on('change', debounce(function(){
if (ctx.isNewProject) {
ctx.isNewProject = false;
ctx.hasEdited = false;
window.onbeforeunload = function(){};
return;
}
ctx.hasEdited = true;
window.onbeforeunload = function(){
return 'You\'ve made some edits!\n\nIf you leave you will lose everything!';
};
}, 50));
child.on('message', function(b){
var buffer = new Float32Array(b);
bufferQueue.push(buffer);
});
ctx.on('stop', onstop);
send('bufferSize', bufferSize);
send('sampleRate', ctx.audio.sampleRate);
function onchange(){
var src = session.getValue();
send('compile', src);
}
function onstop(){
bufferQueue = [];
send('resetFrame');
}
function send(cmd, param){
child.send({ cmd: cmd, param: param });
}
function process(L, R, len){
play(L, R, len);
}
function next(){
if (bufferQueue.length < bufferMax) send('bufferAhead');
return bufferQueue.shift();
}
function play(L, R){
var samples = next();
if (!samples) return;
L.set(samples, 0);
R.set(samples, 0);
display(samples);
}
var timeouts = [];
var timeoutDelay = 1024 / (sampleRate / 1000);
function display(buffer){
for (var i = 0; i < bufferSize / 1024; i++) {
clearTimeout(timeouts[i]);
timeouts[i] = setTimeout(function(i){
var pos = i * 1024;
var buf = buffer.subarray(pos, pos + 1024);
toolbar.oscilloscope.render(buf);
}, timeoutDelay * i, i);
}
}
//session.setValue(demo.toString().split('\n').slice(1, -1).join('\n'));
}
function worker(){
var err = null;
var ok = true;
var fn = function(){ return 0 };
var lastFn = fn;
var sample;
var time = 0, frame = 0;
var volume = 100;
var sampleRate;
var bufferSize;
fork.on('message', function(msg){
switch (msg.cmd) {
case 'compile': return compile(msg.param);
case 'resetFrame': return (frame = 0);
case 'bufferSize': return (bufferSize = msg.param);
case 'sampleRate': return (sampleRate = msg.param);
case 'bufferAhead': return bufferAhead();
}
});
function bufferAhead(){
var floats = new Float32Array(bufferSize);
for (var i = 0, len = bufferSize; i < len; i++, frame++) {
time = frame / sampleRate;
floats[i] = fn(time, frame) * Math.pow(volume / 100, 3);
}
fork.send(floats.buffer, [floats.buffer]);
}
function compile(src){
try {
// wrap
src =
'var sampleRate = ' + sampleRate + ';\n'
+ src
+ ';\nreturn dsp;\n';
// compile
fn = Function(src)();
// test a sample
sample = Number(fn(0,0));
if (isNaN(sample)) err = new Error('sample is NaN');
else if (Math.abs(sample) === Infinity) err = new Error('sample is Infinity');
if (!err) {
if (!ok) console.log('dsp function compiled');
ok = true;
lastFn = fn;
return;
}
if (ok) throw err;
} catch(e) {
if (ok) console.error(e.stack);
}
err = null;
ok = false;
fn = lastFn;
}
}
/*
fundlist.create();
document.body.appendChild(fundlist.el);
fundlist.makeModal();
*/
//fund.show('project saving & sharing');
/*fund.modal.on('hide', function(){
setTimeout(function(){
fund.modal.show();
}, 1000);
})*/
/*
setTimeout(function(){
fund.modal.show();
}, 500);*/