|
0 |
/*
|
|
1 |
|
|
2 |
XXY Oscilloscope
|
|
3 |
|
|
4 |
version 1.0, April 2017
|
|
5 |
by Neil Thapen
|
|
6 |
venuspatrol.nfshost.com
|
|
7 |
|
|
8 |
Copyright 2017 Neil Thapen
|
|
9 |
|
|
10 |
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11 |
of this software and associated documentation files (the "Software"), to deal
|
|
12 |
in the Software without restriction, including without limitation the rights
|
|
13 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14 |
copies of the Software, and to permit persons to whom the Software is
|
|
15 |
furnished to do so, subject to the following conditions:
|
|
16 |
|
|
17 |
The above copyright notice and this permission notice shall be included in
|
|
18 |
all copies or substantial portions of the Software.
|
|
19 |
|
|
20 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26 |
THE SOFTWARE.
|
|
27 |
|
|
28 |
*/
|
|
29 |
|
|
30 |
|
|
31 |
var AudioSystem =
|
|
32 |
{
|
|
33 |
microphoneActive : false,
|
|
34 |
|
|
35 |
init : function (bufferSize)
|
|
36 |
{
|
|
37 |
window.AudioContext = window.AudioContext||window.webkitAudioContext;
|
|
38 |
this.audioContext = new window.AudioContext();
|
|
39 |
this.sampleRate = this.audioContext.sampleRate;
|
|
40 |
this.bufferSize = bufferSize;
|
|
41 |
this.timePerSample = 1/this.sampleRate;
|
|
42 |
this.oldXSamples = new Float32Array(this.bufferSize);
|
|
43 |
this.oldYSamples = new Float32Array(this.bufferSize);
|
|
44 |
this.smoothedXSamples = new Float32Array(Filter.nSmoothedSamples);
|
|
45 |
this.smoothedYSamples = new Float32Array(Filter.nSmoothedSamples);
|
|
46 |
|
|
47 |
if (!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia))
|
|
48 |
{
|
|
49 |
microphoneOutput.value = "unavailable in this browser";
|
|
50 |
}
|
|
51 |
},
|
|
52 |
|
|
53 |
startSound : function()
|
|
54 |
{
|
|
55 |
var audioElement = document.getElementById("audioElement");
|
|
56 |
this.source = this.audioContext.createMediaElementSource(audioElement);
|
|
57 |
this.audioVolumeNode = this.audioContext.createGain();
|
|
58 |
|
|
59 |
this.generator = this.audioContext.createScriptProcessor(this.bufferSize, 2, 2);
|
|
60 |
this.generator.onaudioprocess = SignalGenerator.generate;
|
|
61 |
|
|
62 |
this.scopeNode = this.audioContext.createScriptProcessor(this.bufferSize, 2, 2);
|
|
63 |
this.scopeNode.onaudioprocess = doScriptProcessor;
|
|
64 |
this.source.connect(this.scopeNode);
|
|
65 |
this.generator.connect(this.scopeNode);
|
|
66 |
|
|
67 |
this.scopeNode.connect(this.audioVolumeNode);
|
|
68 |
this.audioVolumeNode.connect(this.audioContext.destination);
|
|
69 |
},
|
|
70 |
|
|
71 |
tryToGetMicrophone : function()
|
|
72 |
{
|
|
73 |
if (this.microphoneActive)
|
|
74 |
{
|
|
75 |
this.connectMicrophone();
|
|
76 |
audioVolume.value = 0.0;
|
|
77 |
audioVolume.oninput();
|
|
78 |
return;
|
|
79 |
}
|
|
80 |
|
|
81 |
var constraints = {audio: { mandatory: { echoCancellation: false }}};
|
|
82 |
//var constraints = {audio: {echoCancellation: false} };
|
|
83 |
navigator.getUserMedia = navigator.getUserMedia ||
|
|
84 |
navigator.webkitGetUserMedia ||
|
|
85 |
navigator.mozGetUserMedia;
|
|
86 |
if (navigator.getUserMedia)
|
|
87 |
{
|
|
88 |
navigator.getUserMedia(constraints, onStream, function(){micCheckbox.checked = false;});
|
|
89 |
}
|
|
90 |
else
|
|
91 |
{
|
|
92 |
micCheckbox.checked = false;
|
|
93 |
}
|
|
94 |
},
|
|
95 |
|
|
96 |
disconnectMicrophone : function()
|
|
97 |
{
|
|
98 |
if (this.microphone) this.microphone.disconnect();
|
|
99 |
},
|
|
100 |
|
|
101 |
connectMicrophone : function()
|
|
102 |
{
|
|
103 |
if (!this.microphone) return;
|
|
104 |
if (!micCheckbox.checked) return;
|
|
105 |
this.disconnectMicrophone();
|
|
106 |
if (controls.signalGeneratorOn) AudioSystem.microphone.connect(AudioSystem.generator);
|
|
107 |
else AudioSystem.microphone.connect(AudioSystem.scopeNode);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
onStream = function(stream)
|
|
114 |
{
|
|
115 |
AudioSystem.microphoneActive = true;
|
|
116 |
AudioSystem.microphone = AudioSystem.audioContext.createMediaStreamSource(stream);
|
|
117 |
AudioSystem.connectMicrophone();
|
|
118 |
|
|
119 |
audioVolume.value = 0.0;
|
|
120 |
audioVolume.oninput();
|
|
121 |
};
|
|
122 |
|
|
123 |
var SignalGenerator =
|
|
124 |
{
|
|
125 |
oldA : 1.0,
|
|
126 |
oldB : 1.0,
|
|
127 |
timeInSamples : 0,
|
|
128 |
|
|
129 |
generate : function(event)
|
|
130 |
{
|
|
131 |
var xMic = event.inputBuffer.getChannelData(0);
|
|
132 |
var yMic = event.inputBuffer.getChannelData(1);
|
|
133 |
var xOut = event.outputBuffer.getChannelData(0);
|
|
134 |
var yOut = event.outputBuffer.getChannelData(1);
|
|
135 |
var newA = controls.aValue * Math.pow(10.0, controls.aExponent);
|
|
136 |
var newB = controls.bValue * Math.pow(10.0, controls.bExponent);
|
|
137 |
var oldA = SignalGenerator.oldA;
|
|
138 |
var oldB = SignalGenerator.oldB;
|
|
139 |
//var PI = Math.PI;
|
|
140 |
//var cos = Math.cos;
|
|
141 |
//var sin = Math.sin;
|
|
142 |
with (Math)
|
|
143 |
{
|
|
144 |
var xFunc = eval("(function xFunc(){return "+controls.xExpression+";})");
|
|
145 |
var yFunc = eval("(function yFunc(){return "+controls.yExpression+";})");
|
|
146 |
}
|
|
147 |
var bufferSize = AudioSystem.bufferSize;
|
|
148 |
var timeInSamples = SignalGenerator.timeInSamples;
|
|
149 |
var sampleRate = AudioSystem.sampleRate;
|
|
150 |
var x = 0.0;
|
|
151 |
var y = 0.0;
|
|
152 |
if (!controls.signalGeneratorOn)
|
|
153 |
{
|
|
154 |
for (var i=0; i<bufferSize; i++)
|
|
155 |
{
|
|
156 |
xOut[i] = 0;
|
|
157 |
yOut[i] = 0;
|
|
158 |
}
|
|
159 |
}
|
|
160 |
else if ((newA == oldA) && (newB == oldB))
|
|
161 |
{
|
|
162 |
var n = timeInSamples;
|
|
163 |
for (var i=0; i<bufferSize; i++)
|
|
164 |
{
|
|
165 |
var t = n/sampleRate;
|
|
166 |
var a = newA;
|
|
167 |
var b = newB;
|
|
168 |
var mx = xMic[i];
|
|
169 |
var my = yMic[i];
|
|
170 |
x = xFunc();
|
|
171 |
y = yFunc();
|
|
172 |
xOut[i] = x;
|
|
173 |
yOut[i] = y;
|
|
174 |
n += 1;
|
|
175 |
}
|
|
176 |
}
|
|
177 |
else
|
|
178 |
{
|
|
179 |
var n = timeInSamples;
|
|
180 |
for (var i=0; i<bufferSize; i++)
|
|
181 |
{
|
|
182 |
var t = n/sampleRate;
|
|
183 |
|
|
184 |
var a = oldA;
|
|
185 |
var b = oldB;
|
|
186 |
var mx = xMic[i];
|
|
187 |
var my = yMic[i];
|
|
188 |
var oldX = xFunc();
|
|
189 |
var oldY = yFunc();
|
|
190 |
a = newA;
|
|
191 |
b = newB;
|
|
192 |
var newX = xFunc();
|
|
193 |
var newY = yFunc();
|
|
194 |
var alpha_z = i/bufferSize;
|
|
195 |
x = oldX*(1.0-alpha_z)+newX*alpha_z;
|
|
196 |
y = oldY*(1.0-alpha_z)+newY*alpha_z;
|
|
197 |
|
|
198 |
xOut[i] = x;
|
|
199 |
yOut[i] = y;
|
|
200 |
n += 1;
|
|
201 |
}
|
|
202 |
}
|
|
203 |
|
|
204 |
SignalGenerator.timeInSamples += AudioSystem.bufferSize;
|
|
205 |
SignalGenerator.oldA = newA;
|
|
206 |
SignalGenerator.oldB = newB;
|
|
207 |
}
|
|
208 |
|
|
209 |
}
|
|
210 |
|
|
211 |
var Filter =
|
|
212 |
{
|
|
213 |
lanczosTweak : 1.5,
|
|
214 |
|
|
215 |
init : function(bufferSize, a, steps)
|
|
216 |
{
|
|
217 |
this.bufferSize = bufferSize;
|
|
218 |
this.a = a;
|
|
219 |
this.steps = steps;
|
|
220 |
this.radius = a * steps;
|
|
221 |
this.nSmoothedSamples = this.bufferSize*this.steps + 1;
|
|
222 |
this.allSamples = new Float32Array(2*this.bufferSize);
|
|
223 |
|
|
224 |
this.createLanczosKernel();
|
|
225 |
},
|
|
226 |
|
|
227 |
|
|
228 |
generateSmoothedSamples : function (oldSamples, samples, smoothedSamples)
|
|
229 |
{
|
|
230 |
//this.createLanczosKernel();
|
|
231 |
var bufferSize = this.bufferSize;
|
|
232 |
var allSamples = this.allSamples;
|
|
233 |
var nSmoothedSamples = this.nSmoothedSamples;
|
|
234 |
var a = this.a;
|
|
235 |
var steps = this.steps;
|
|
236 |
var K = this.K;
|
|
237 |
|
|
238 |
for (var i=0; i<bufferSize; i++)
|
|
239 |
{
|
|
240 |
allSamples[i] = oldSamples[i];
|
|
241 |
allSamples[bufferSize+i] = samples[i];
|
|
242 |
}
|
|
243 |
|
|
244 |
/*for (var s= -a+1; s<a; s++)
|
|
245 |
{
|
|
246 |
for (var r=0; r<steps; r++)
|
|
247 |
{
|
|
248 |
if (r==0 && !(s==0)) continue;
|
|
249 |
var kernelPosition = -r+s*steps;
|
|
250 |
if (kernelPosition<0) k = K[-kernelPosition];
|
|
251 |
else k = K[kernelPosition];
|
|
252 |
|
|
253 |
var i = r;
|
|
254 |
var pStart = bufferSize - 2*a + s;
|
|
255 |
var pEnd = pStart + bufferSize;
|
|
256 |
for (var p=pStart; p<pEnd; p++)
|
|
257 |
{
|
|
258 |
smoothedSamples[i] += k * allSamples[p];
|
|
259 |
i += steps;
|
|
260 |
}
|
|
261 |
}
|
|
262 |
}*/
|
|
263 |
|
|
264 |
var pStart = bufferSize - 2*a;
|
|
265 |
var pEnd = pStart + bufferSize;
|
|
266 |
var i = 0;
|
|
267 |
for (var position=pStart; position<pEnd; position++)
|
|
268 |
{
|
|
269 |
smoothedSamples[i] = allSamples[position];
|
|
270 |
i += 1;
|
|
271 |
for (var r=1; r<steps; r++)
|
|
272 |
{
|
|
273 |
var smoothedSample = 0;
|
|
274 |
for (var s= -a+1; s<a; s++)
|
|
275 |
{
|
|
276 |
var sample = allSamples[position+s];
|
|
277 |
var kernelPosition = -r+s*steps;
|
|
278 |
if (kernelPosition<0) smoothedSample += sample * K[-kernelPosition];
|
|
279 |
else smoothedSample += sample * K[kernelPosition];
|
|
280 |
}
|
|
281 |
smoothedSamples[i] = smoothedSample;
|
|
282 |
i += 1;
|
|
283 |
}
|
|
284 |
}
|
|
285 |
|
|
286 |
smoothedSamples[nSmoothedSamples-1] = allSamples[2*bufferSize-2*a];
|
|
287 |
},
|
|
288 |
|
|
289 |
createLanczosKernel : function ()
|
|
290 |
{
|
|
291 |
this.K = new Float32Array(this.radius);
|
|
292 |
this.K[0] = 1;
|
|
293 |
for (var i =1; i<this.radius; i++)
|
|
294 |
{
|
|
295 |
var piX = (Math.PI * i) / this.steps;
|
|
296 |
var sinc = Math.sin(piX)/piX;
|
|
297 |
var window = this.a * Math.sin(piX/this.a) / piX;
|
|
298 |
this.K[i] = sinc*Math.pow(window, this.lanczosTweak);
|
|
299 |
}
|
|
300 |
}
|
|
301 |
}
|
|
302 |
|
|
303 |
var UI =
|
|
304 |
{
|
|
305 |
sidebarWidth : 360,
|
|
306 |
|
|
307 |
init : function()
|
|
308 |
{
|
|
309 |
var kHzText = (AudioSystem.sampleRate/1000).toFixed(1)+"kHz";
|
|
310 |
document.getElementById("samplerate").innerHTML=kHzText;
|
|
311 |
mainGain.oninput();
|
|
312 |
trigger.oninput();
|
|
313 |
this.xInput = document.getElementById("xInput");
|
|
314 |
this.yInput = document.getElementById("yInput");
|
|
315 |
this.xInput.value = controls.xExpression;
|
|
316 |
this.yInput.value = controls.yExpression;
|
|
317 |
},
|
|
318 |
|
|
319 |
compile : function() //doesn't compile anything anymore
|
|
320 |
{
|
|
321 |
controls.xExpression = this.xInput.value;
|
|
322 |
controls.yExpression = this.yInput.value;
|
|
323 |
}
|
|
324 |
}
|
|
325 |
|
|
326 |
var Render =
|
|
327 |
{
|
|
328 |
debug : 0,
|
|
329 |
failed : false,
|
|
330 |
|
|
331 |
init : function()
|
|
332 |
{
|
|
333 |
this.canvas = document.getElementById("crtCanvas");
|
|
334 |
this.onResize();
|
|
335 |
window.onresize = this.onResize;
|
|
336 |
if (!this.supportsWebGl)
|
|
337 |
{
|
|
338 |
Render.failed = true;
|
|
339 |
return;
|
|
340 |
}
|
|
341 |
window.gl = this.canvas.getContext("webgl", {preserveDrawingBuffer: true}, { alpha: false } );
|
|
342 |
gl.viewport(0, 0, this.canvas.width, this.canvas.height);
|
|
343 |
gl.enable(gl.BLEND);
|
|
344 |
gl.blendEquation( gl.FUNC_ADD );
|
|
345 |
gl.clearColor(0.0, 0.0, 0.0, 1.0);
|
|
346 |
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
347 |
gl.colorMask(true, true, true, true);
|
|
348 |
|
|
349 |
var ext1 = gl.getExtension('OES_texture_float');
|
|
350 |
var ext2 = gl.getExtension('OES_texture_float_linear');
|
|
351 |
//this.ext = gl.getExtension('OES_texture_half_float');
|
|
352 |
//this.ext2 = gl.getExtension('OES_texture_half_float_linear');
|
|
353 |
if (!ext1 || !ext2)
|
|
354 |
{
|
|
355 |
Render.failed = true;
|
|
356 |
return;
|
|
357 |
}
|
|
358 |
|
|
359 |
this.fadeAmount = 0.2*AudioSystem.bufferSize/512;
|
|
360 |
this.fullScreenQuad = new Float32Array([
|
|
361 |
-1, 1, 1, 1, 1,-1, // Triangle 1
|
|
362 |
-1, 1, 1,-1, -1,-1 // Triangle 2
|
|
363 |
]);
|
|
364 |
|
|
365 |
this.simpleShader = this.createShader("vertex","fragment");
|
|
366 |
this.simpleShader.vertexPosition = gl.getAttribLocation(this.simpleShader, "vertexPosition");
|
|
367 |
this.simpleShader.colour = gl.getUniformLocation(this.simpleShader, "colour");
|
|
368 |
|
|
369 |
this.lineShader = this.createShader("gaussianVertex","gaussianFragment");
|
|
370 |
this.lineShader.aStart = gl.getAttribLocation(this.lineShader, "aStart");
|
|
371 |
this.lineShader.aEnd = gl.getAttribLocation(this.lineShader, "aEnd");
|
|
372 |
this.lineShader.aIdx = gl.getAttribLocation(this.lineShader, "aIdx");
|
|
373 |
this.lineShader.uGain = gl.getUniformLocation(this.lineShader, "uGain");
|
|
374 |
this.lineShader.uSize = gl.getUniformLocation(this.lineShader, "uSize");
|
|
375 |
this.lineShader.uInvert = gl.getUniformLocation(this.lineShader, "uInvert");
|
|
376 |
this.lineShader.uIntensity = gl.getUniformLocation(this.lineShader, "uIntensity");
|
|
377 |
this.lineShader.uNEdges = gl.getUniformLocation(this.lineShader, "uNEdges");
|
|
378 |
this.lineShader.uFadeAmount = gl.getUniformLocation(this.lineShader, "uFadeAmount");
|
|
379 |
this.lineShader.uScreen = gl.getUniformLocation(this.lineShader, "uScreen");
|
|
380 |
|
|
381 |
this.outputShader = this.createShader("outputVertex","outputFragment");
|
|
382 |
this.outputShader.aPos = gl.getAttribLocation(this.outputShader, "aPos");
|
|
383 |
this.outputShader.uTexture0 = gl.getUniformLocation(this.outputShader, "uTexture0");
|
|
384 |
this.outputShader.uTexture1 = gl.getUniformLocation(this.outputShader, "uTexture1");
|
|
385 |
this.outputShader.uTexture2 = gl.getUniformLocation(this.outputShader, "uTexture2");
|
|
386 |
this.outputShader.uTexture3 = gl.getUniformLocation(this.outputShader, "uTexture3");
|
|
387 |
this.outputShader.uExposure = gl.getUniformLocation(this.outputShader, "uExposure");
|
|
388 |
this.outputShader.uColour = gl.getUniformLocation(this.outputShader, "uColour");
|
|
389 |
this.outputShader.uResizeForCanvas = gl.getUniformLocation(this.outputShader, "uResizeForCanvas");
|
|
390 |
this.outputShader.graticuleLight = gl.getUniformLocation(this.outputShader, "graticuleLight");
|
|
391 |
|
|
392 |
this.texturedShader = this.createShader("texturedVertexWithResize","texturedFragment");
|
|
393 |
this.texturedShader.aPos = gl.getAttribLocation(this.texturedShader, "aPos");
|
|
394 |
this.texturedShader.uTexture0 = gl.getUniformLocation(this.texturedShader, "uTexture0");
|
|
395 |
this.texturedShader.uResizeForCanvas = gl.getUniformLocation(this.texturedShader, "uResizeForCanvas");
|
|
396 |
|
|
397 |
this.blurShader = this.createShader("texturedVertex","blurFragment");
|
|
398 |
this.blurShader.aPos = gl.getAttribLocation(this.blurShader, "aPos");
|
|
399 |
this.blurShader.uTexture0 = gl.getUniformLocation(this.blurShader, "uTexture0");
|
|
400 |
this.blurShader.uOffset = gl.getUniformLocation(this.blurShader, "uOffset");
|
|
401 |
|
|
402 |
this.vertexBuffer = gl.createBuffer();
|
|
403 |
this.setupTextures();
|
|
404 |
},
|
|
405 |
|
|
406 |
admitFailure : function()
|
|
407 |
{
|
|
408 |
canvasFailure.innerHTML=" sorry, it's not working"
|
|
409 |
},
|
|
410 |
|
|
411 |
setupArrays : function(nPoints)
|
|
412 |
{
|
|
413 |
this.nPoints = nPoints;
|
|
414 |
this.nEdges = this.nPoints-1;
|
|
415 |
|
|
416 |
this.quadIndexBuffer = gl.createBuffer();
|
|
417 |
var indices = new Float32Array(4*this.nEdges);
|
|
418 |
for (var i=0; i<indices.length; i++)
|
|
419 |
{
|
|
420 |
indices[i] = i;
|
|
421 |
}
|
|
422 |
gl.bindBuffer(gl.ARRAY_BUFFER, this.quadIndexBuffer);
|
|
423 |
gl.bufferData(gl.ARRAY_BUFFER, indices, gl.STATIC_DRAW);
|
|
424 |
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
425 |
|
|
426 |
this.vertexIndexBuffer = gl.createBuffer();
|
|
427 |
var len = this.nEdges * 2 * 3,
|
|
428 |
indices = new Uint16Array(len);
|
|
429 |
for (var i = 0, pos = 0; i < len;)
|
|
430 |
{
|
|
431 |
indices[i++] = pos;
|
|
432 |
indices[i++] = pos + 2;
|
|
433 |
indices[i++] = pos + 1;
|
|
434 |
indices[i++] = pos + 1;
|
|
435 |
indices[i++] = pos + 2;
|
|
436 |
indices[i++] = pos + 3;
|
|
437 |
pos += 4;
|
|
438 |
}
|
|
439 |
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.vertexIndexBuffer);
|
|
440 |
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
|
|
441 |
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
|
|
442 |
|
|
443 |
|
|
444 |
this.scratchVertices = new Float32Array(8*nPoints);
|
|
445 |
},
|
|
446 |
|
|
447 |
setupTextures : function()
|
|
448 |
{
|
|
449 |
this.frameBuffer = gl.createFramebuffer();
|
|
450 |
this.lineTexture = this.makeTexture(1024, 1024);
|
|
451 |
this.onResize();
|
|
452 |
this.blur1Texture = this.makeTexture(256,256);
|
|
453 |
this.blur2Texture = this.makeTexture(256, 256);
|
|
454 |
this.blur3Texture = this.makeTexture(32, 32);
|
|
455 |
this.blur4Texture = this.makeTexture(32, 32);
|
|
456 |
this.screenTexture = this.loadTexture('noise.jpg');
|
|
457 |
|
|
458 |
// test floating point textures working
|
|
459 |
this.activateTargetTexture(this.lineTexture);
|
|
460 |
if (gl.FRAMEBUFFER_COMPLETE != gl.checkFramebufferStatus(gl.FRAMEBUFFER)) Render.failed=true;
|
|
461 |
console.log(gl.checkFramebufferStatus(gl.FRAMEBUFFER), gl.FRAMEBUFFER_COMPLETE, this.frameBuffer);
|
|
462 |
},
|
|
463 |
|
|
464 |
onResize : function()
|
|
465 |
{
|
|
466 |
var windowWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)
|
|
467 |
var windowHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
|
|
468 |
var canvasSize = Math.min(windowHeight-21, windowWidth-UI.sidebarWidth-70);
|
|
469 |
Render.canvas.width = canvasSize;
|
|
470 |
Render.canvas.height = canvasSize;
|
|
471 |
if (Render.lineTexture)
|
|
472 |
{
|
|
473 |
var renderSize = Math.min(canvasSize, 1024);
|
|
474 |
Render.lineTexture.width = renderSize;
|
|
475 |
Render.lineTexture.height = renderSize;
|
|
476 |
//testOutputElement.value = windowHeight;
|
|
477 |
}
|
|
478 |
|
|
479 |
},
|
|
480 |
|
|
481 |
drawLineTexture : function(xPoints, yPoints)
|
|
482 |
{
|
|
483 |
this.fadeAmount = Math.pow(0.5, controls.persistence)*0.2*AudioSystem.bufferSize/512 ;
|
|
484 |
this.activateTargetTexture(this.lineTexture);
|
|
485 |
this.fade();
|
|
486 |
//gl.clear(gl.COLOR_BUFFER_BIT);
|
|
487 |
this.drawLine(xPoints, yPoints);
|
|
488 |
gl.bindTexture(gl.TEXTURE_2D, this.targetTexture);
|
|
489 |
gl.generateMipmap(gl.TEXTURE_2D);
|
|
490 |
},
|
|
491 |
|
|
492 |
drawCRT : function()
|
|
493 |
{
|
|
494 |
this.setNormalBlending();
|
|
495 |
|
|
496 |
this.activateTargetTexture(this.blur1Texture);
|
|
497 |
this.setShader(this.texturedShader);
|
|
498 |
gl.uniform1f(this.texturedShader.uResizeForCanvas, this.lineTexture.width/1024);
|
|
499 |
this.drawTexture(this.lineTexture);
|
|
500 |
|
|
501 |
//horizontal blur 256x256
|
|
502 |
this.activateTargetTexture(this.blur2Texture);
|
|
503 |
this.setShader(this.blurShader);
|
|
504 |
gl.uniform2fv(this.blurShader.uOffset, [1.0/256.0, 0.0]);
|
|
505 |
this.drawTexture(this.blur1Texture);
|
|
506 |
|
|
507 |
//vertical blur 256x256
|
|
508 |
this.activateTargetTexture(this.blur1Texture);
|
|
509 |
//this.setShader(this.blurShader);
|
|
510 |
gl.uniform2fv(this.blurShader.uOffset, [0.0, 1.0/256.0]);
|
|
511 |
this.drawTexture(this.blur2Texture);
|
|
512 |
|
|
513 |
//preserve blur1 for later
|
|
514 |
this.activateTargetTexture(this.blur3Texture);
|
|
515 |
this.setShader(this.texturedShader);
|
|
516 |
gl.uniform1f(this.texturedShader.uResizeForCanvas, 1.0);
|
|
517 |
this.drawTexture(this.blur1Texture);
|
|
518 |
|
|
519 |
//horizontal blur 64x64
|
|
520 |
this.activateTargetTexture(this.blur4Texture);
|
|
521 |
this.setShader(this.blurShader);
|
|
522 |
gl.uniform2fv(this.blurShader.uOffset, [1.0/32.0, 1.0/60.0]);
|
|
523 |
this.drawTexture(this.blur3Texture);
|
|
524 |
|
|
525 |
//vertical blur 64x64
|
|
526 |
this.activateTargetTexture(this.blur3Texture);
|
|
527 |
//this.setShader(this.blurShader);
|
|
528 |
gl.uniform2fv(this.blurShader.uOffset, [-1.0/60.0, 1.0/32.0]);
|
|
529 |
this.drawTexture(this.blur4Texture);
|
|
530 |
|
|
531 |
this.activateTargetTexture(null);
|
|
532 |
this.setShader(this.outputShader);
|
|
533 |
var brightness = Math.pow(2, controls.exposureStops-2.0);
|
|
534 |
//if (controls.disableFilter) brightness *= Filter.steps;
|
|
535 |
gl.uniform1f(this.outputShader.uExposure, brightness);
|
|
536 |
gl.uniform1f(this.outputShader.uResizeForCanvas, this.lineTexture.width/1024);
|
|
537 |
var colour = this.getColourFromHue(controls.hue);
|
|
538 |
gl.uniform3fv(this.outputShader.uColour, colour);
|
|
539 |
if (controls.light) gl.uniform1f(this.outputShader.graticuleLight, 0.15);
|
|
540 |
else gl.uniform1f(this.outputShader.graticuleLight, 0.0);
|
|
541 |
this.drawTexture(this.lineTexture, this.blur1Texture, this.blur3Texture, this.screenTexture);
|
|
542 |
},
|
|
543 |
|
|
544 |
getColourFromHue : function(hue)
|
|
545 |
{
|
|
546 |
var alpha = (hue/120.0) % 1.0;
|
|
547 |
var start = Math.sqrt(1.0-alpha);
|
|
548 |
var end = Math.sqrt(alpha);
|
|
549 |
var colour;
|
|
550 |
if (hue<120) colour = [start, end, 0.0];
|
|
551 |
else if (hue<240) colour = [0.0, start, end];
|
|
552 |
else colour = [end, 0.0, start];
|
|
553 |
return colour;
|
|
554 |
},
|
|
555 |
|
|
556 |
activateTargetTexture : function(texture)
|
|
557 |
{
|
|
558 |
if (texture)
|
|
559 |
{
|
|
560 |
gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);
|
|
561 |
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
|
|
562 |
gl.viewport(0, 0, texture.width, texture.height);
|
|
563 |
}
|
|
564 |
else
|
|
565 |
{
|
|
566 |
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
567 |
gl.viewport(0, 0, this.canvas.width, this.canvas.height);
|
|
568 |
}
|
|
569 |
this.targetTexture = texture;
|
|
570 |
},
|
|
571 |
|
|
572 |
setShader : function(program)
|
|
573 |
{
|
|
574 |
this.program = program;
|
|
575 |
gl.useProgram(program);
|
|
576 |
},
|
|
577 |
|
|
578 |
drawTexture : function(texture0, texture1, texture2, texture3)
|
|
579 |
{
|
|
580 |
//gl.useProgram(this.program);
|
|
581 |
gl.enableVertexAttribArray(this.program.aPos);
|
|
582 |
|
|
583 |
gl.activeTexture(gl.TEXTURE0);
|
|
584 |
gl.bindTexture(gl.TEXTURE_2D, texture0);
|
|
585 |
gl.uniform1i(this.program.uTexture0, 0);
|
|
586 |
|
|
587 |
if (texture1)
|
|
588 |
{
|
|
589 |
gl.activeTexture(gl.TEXTURE1);
|
|
590 |
gl.bindTexture(gl.TEXTURE_2D, texture1);
|
|
591 |
gl.uniform1i(this.program.uTexture1, 1);
|
|
592 |
}
|
|
593 |
|
|
594 |
if (texture2)
|
|
595 |
{
|
|
596 |
gl.activeTexture(gl.TEXTURE2);
|
|
597 |
gl.bindTexture(gl.TEXTURE_2D, texture2);
|
|
598 |
gl.uniform1i(this.program.uTexture2, 2);
|
|
599 |
}
|
|
600 |
|
|
601 |
if (texture3)
|
|
602 |
{
|
|
603 |
gl.activeTexture(gl.TEXTURE3);
|
|
604 |
gl.bindTexture(gl.TEXTURE_2D, texture3);
|
|
605 |
gl.uniform1i(this.program.uTexture3, 3);
|
|
606 |
}
|
|
607 |
|
|
608 |
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
|
609 |
gl.bufferData(gl.ARRAY_BUFFER, this.fullScreenQuad, gl.STATIC_DRAW);
|
|
610 |
gl.vertexAttribPointer(this.program.aPos, 2, gl.FLOAT, false, 0, 0);
|
|
611 |
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
612 |
|
|
613 |
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
|
614 |
gl.disableVertexAttribArray(this.program.aPos);
|
|
615 |
|
|
616 |
if (this.targetTexture)
|
|
617 |
{
|
|
618 |
gl.bindTexture(gl.TEXTURE_2D, this.targetTexture);
|
|
619 |
gl.generateMipmap(gl.TEXTURE_2D);
|
|
620 |
}
|
|
621 |
},
|
|
622 |
|
|
623 |
drawLine : function(xPoints, yPoints)
|
|
624 |
{
|
|
625 |
this.setAdditiveBlending();
|
|
626 |
|
|
627 |
var scratchVertices = this.scratchVertices;
|
|
628 |
//this.totalLength = 0;
|
|
629 |
var nPoints = xPoints.length;
|
|
630 |
for (var i=0; i<nPoints; i++)
|
|
631 |
{
|
|
632 |
var p = i*8;
|
|
633 |
scratchVertices[p]=scratchVertices[p+2]=scratchVertices[p+4]=scratchVertices[p+6]=xPoints[i];
|
|
634 |
scratchVertices[p+1]=scratchVertices[p+3]=scratchVertices[p+5]=scratchVertices[p+7]=yPoints[i];
|
|
635 |
/*if (i>0)
|
|
636 |
{
|
|
637 |
var xDelta = xPoints[i]-xPoints[i-1];
|
|
638 |
if (xDelta<0) xDelta = -xDelta;
|
|
639 |
var yDelta = yPoints[i]-yPoints[i-1];
|
|
640 |
if (yDelta<0) yDelta = -yDelta;
|
|
641 |
this.totalLength += xDelta + yDelta;
|
|
642 |
}*/
|
|
643 |
}
|
|
644 |
//testOutputElement.value = this.totalLength;
|
|
645 |
|
|
646 |
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
|
647 |
gl.bufferData(gl.ARRAY_BUFFER, scratchVertices, gl.STATIC_DRAW);
|
|
648 |
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
649 |
|
|
650 |
var program = this.lineShader;
|
|
651 |
gl.useProgram(program);
|
|
652 |
gl.enableVertexAttribArray(program.aStart);
|
|
653 |
gl.enableVertexAttribArray(program.aEnd);
|
|
654 |
gl.enableVertexAttribArray(program.aIdx);
|
|
655 |
|
|
656 |
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
|
657 |
gl.vertexAttribPointer(program.aStart, 2, gl.FLOAT, false, 0, 0);
|
|
658 |
gl.vertexAttribPointer(program.aEnd, 2, gl.FLOAT, false, 0, 8*4);
|
|
659 |
gl.bindBuffer(gl.ARRAY_BUFFER, this.quadIndexBuffer);
|
|
660 |
gl.vertexAttribPointer(program.aIdx, 1, gl.FLOAT, false, 0, 0);
|
|
661 |
|
|
662 |
gl.activeTexture(gl.TEXTURE0);
|
|
663 |
gl.bindTexture(gl.TEXTURE_2D, this.screenTexture);
|
|
664 |
gl.uniform1i(program.uScreen, 0);
|
|
665 |
|
|
666 |
gl.uniform1f(program.uSize, 0.015);
|
|
667 |
gl.uniform1f(program.uGain, Math.pow(2.0,controls.mainGain)*480/512);
|
|
668 |
if (controls.invertXY) gl.uniform1f(program.uInvert, -1.0);
|
|
669 |
else gl.uniform1f(program.uInvert, 1.0);
|
|
670 |
if (controls.disableFilter) gl.uniform1f(program.uIntensity, 0.005*(Filter.steps+1.5));
|
|
671 |
// +1.5 needed above for some reason for the brightness to match
|
|
672 |
else gl.uniform1f(program.uIntensity, 0.005);
|
|
673 |
gl.uniform1f(program.uFadeAmount, this.fadeAmount);
|
|
674 |
gl.uniform1f(program.uNEdges, this.nEdges);
|
|
675 |
|
|
676 |
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.vertexIndexBuffer);
|
|
677 |
var nEdgesThisTime = (xPoints.length-1);
|
|
678 |
|
|
679 |
/*if (this.totalLength > 300)
|
|
680 |
{
|
|
681 |
nEdgesThisTime *= 300/this.totalLength;
|
|
682 |
nEdgesThisTime = Math.floor(nEdgesThisTime);
|
|
683 |
}*/
|
|
684 |
|
|
685 |
gl.drawElements(gl.TRIANGLES, nEdgesThisTime * 6, gl.UNSIGNED_SHORT, 0);
|
|
686 |
|
|
687 |
gl.disableVertexAttribArray(program.aStart);
|
|
688 |
gl.disableVertexAttribArray(program.aEnd);
|
|
689 |
gl.disableVertexAttribArray(program.aIdx);
|
|
690 |
},
|
|
691 |
|
|
692 |
fade : function(alpha)
|
|
693 |
{
|
|
694 |
this.setNormalBlending();
|
|
695 |
|
|
696 |
var program = this.simpleShader;
|
|
697 |
gl.useProgram(program);
|
|
698 |
gl.enableVertexAttribArray(program.vertexPosition);
|
|
699 |
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
|
700 |
gl.bufferData(gl.ARRAY_BUFFER, this.fullScreenQuad, gl.STATIC_DRAW);
|
|
701 |
gl.vertexAttribPointer(program.vertexPosition, 2, gl.FLOAT, false, 0, 0);
|
|
702 |
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
703 |
gl.uniform4fv(program.colour, [0.0, 0.0, 0.0, this.fadeAmount]);
|
|
704 |
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
|
705 |
gl.disableVertexAttribArray(program.vertexPosition);
|
|
706 |
},
|
|
707 |
|
|
708 |
loadTexture : function(fileName)
|
|
709 |
{
|
|
710 |
var texture = gl.createTexture();
|
|
711 |
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
712 |
// Fill with grey pixel, as placeholder until loaded
|
|
713 |
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE,
|
|
714 |
new Uint8Array([128, 128, 128, 255]));
|
|
715 |
// Asynchronously load an image
|
|
716 |
var image = new Image();
|
|
717 |
image.src = fileName;
|
|
718 |
image.addEventListener('load', function()
|
|
719 |
{
|
|
720 |
// Now that the image has loaded copy it to the texture.
|
|
721 |
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
722 |
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
|
|
723 |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
724 |
//gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
725 |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
|
|
726 |
gl.generateMipmap(gl.TEXTURE_2D);
|
|
727 |
//hardcoded:
|
|
728 |
texture.width = texture.height = 512;
|
|
729 |
Render.fillBlueChannel(texture);
|
|
730 |
if (controls.grid) Render.drawGrid(texture);
|
|
731 |
});
|
|
732 |
return texture;
|
|
733 |
},
|
|
734 |
|
|
735 |
fillBlueChannel : function(texture)
|
|
736 |
{
|
|
737 |
this.activateTargetTexture(texture);
|
|
738 |
gl.colorMask(false, false, true, true);
|
|
739 |
this.setNormalBlending();
|
|
740 |
|
|
741 |
var program = this.simpleShader;
|
|
742 |
gl.useProgram(program);
|
|
743 |
gl.enableVertexAttribArray(program.vertexPosition);
|
|
744 |
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
|
745 |
gl.bufferData(gl.ARRAY_BUFFER, this.fullScreenQuad, gl.STATIC_DRAW);
|
|
746 |
gl.vertexAttribPointer(program.vertexPosition, 2, gl.FLOAT, false, 0, 0);
|
|
747 |
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
748 |
gl.uniform4fv(program.colour, [0.0, 0.0, 1.0, 1.0]);
|
|
749 |
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
|
750 |
gl.disableVertexAttribArray(program.vertexPosition);
|
|
751 |
gl.colorMask(true, true, true, true);
|
|
752 |
},
|
|
753 |
|
|
754 |
drawGrid : function(texture)
|
|
755 |
{
|
|
756 |
this.activateTargetTexture(texture);
|
|
757 |
this.setNormalBlending();
|
|
758 |
this.setShader(this.simpleShader);
|
|
759 |
|
|
760 |
gl.colorMask(true, false, true, true);
|
|
761 |
|
|
762 |
var data = [];
|
|
763 |
|
|
764 |
for (var i=0; i<11; i++)
|
|
765 |
{
|
|
766 |
var step = 48;
|
|
767 |
var s = i*step;
|
|
768 |
data.splice(0,0, 0, s, 10*step, s);
|
|
769 |
data.splice(0,0, s, 0, s, 10*step);
|
|
770 |
if (i!=0 && i!=10)
|
|
771 |
{
|
|
772 |
for (var j=0; j<51; j++)
|
|
773 |
{
|
|
774 |
t = j*step/5;
|
|
775 |
if (i!=5)
|
|
776 |
{
|
|
777 |
data.splice(0,0, t, s-2, t, s+1);
|
|
778 |
data.splice(0,0, s-2, t, s+1, t);
|
|
779 |
}
|
|
780 |
else
|
|
781 |
{
|
|
782 |
data.splice(0,0, t, s-5, t, s+4);
|
|
783 |
data.splice(0,0, s-5, t, s+4, t);
|
|
784 |
}
|
|
785 |
}
|
|
786 |
}
|
|
787 |
}
|
|
788 |
|
|
789 |
for (var j=0; j<51; j++)
|
|
790 |
{
|
|
791 |
var t = j*step/5;
|
|
792 |
if (t%5 == 0) continue;
|
|
793 |
data.splice(0,0, t-2, 2.5*step, t+2, 2.5*step);
|
|
794 |
data.splice(0,0, t-2, 7.5*step, t+2, 7.5*step);
|
|
795 |
}
|
|
796 |
|
|
797 |
|
|
798 |
var vertices = new Float32Array(data);
|
|
799 |
for (var i=0; i<data.length; i++)
|
|
800 |
{
|
|
801 |
vertices[i]=(vertices[i]+256-step*5)/256-1;
|
|
802 |
}
|
|
803 |
|
|
804 |
|
|
805 |
gl.enableVertexAttribArray(this.program.vertexPosition);
|
|
806 |
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
|
807 |
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
|
|
808 |
gl.vertexAttribPointer(this.program.vertexPosition, 2, gl.FLOAT, false, 0, 0);
|
|
809 |
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
810 |
gl.uniform4fv(this.program.colour, [0.01, 0.1, 0.01, 1.0]);
|
|
811 |
|
|
812 |
gl.lineWidth(1.0);
|
|
813 |
gl.drawArrays(gl.LINES, 0, vertices.length/2);
|
|
814 |
|
|
815 |
gl.bindTexture(gl.TEXTURE_2D, this.targetTexture);
|
|
816 |
gl.generateMipmap(gl.TEXTURE_2D);
|
|
817 |
gl.colorMask(true, true, true, true);
|
|
818 |
},
|
|
819 |
|
|
820 |
makeTexture : function(width, height)
|
|
821 |
{
|
|
822 |
var texture = gl.createTexture();
|
|
823 |
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
824 |
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.FLOAT, null);
|
|
825 |
//gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, Render.ext.HALF_FLOAT_OES, null);
|
|
826 |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
827 |
//gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
828 |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
|
|
829 |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
830 |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
831 |
gl.generateMipmap(gl.TEXTURE_2D);
|
|
832 |
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
833 |
texture.width = width;
|
|
834 |
texture.height = height;
|
|
835 |
return texture;
|
|
836 |
},
|
|
837 |
|
|
838 |
/*xactivateTargetTexture : function(ctx, texture)
|
|
839 |
{
|
|
840 |
gl.bindRenderbuffer(gl.RENDERBUFFER, ctx.renderBuffer);
|
|
841 |
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, ctx.frameBuffer.width, ctx.frameBuffer.height);
|
|
842 |
|
|
843 |
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
|
|
844 |
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, ctx.renderBuffer);
|
|
845 |
assert(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER))
|
|
846 |
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
847 |
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
|
|
848 |
},*/
|
|
849 |
|
|
850 |
drawSimpleLine : function(xSamples, ySamples, colour)
|
|
851 |
{
|
|
852 |
var nVertices = xSamples.length;
|
|
853 |
var vertices = new Float32Array(2*nVertices);
|
|
854 |
for (var i=0; i<nVertices; i++)
|
|
855 |
{
|
|
856 |
vertices[2*i] = xSamples[i];
|
|
857 |
vertices[2*i+1] = ySamples[i];
|
|
858 |
}
|
|
859 |
|
|
860 |
this.setAdditiveBlending();
|
|
861 |
|
|
862 |
var program = this.simpleShader;
|
|
863 |
gl.useProgram(program);
|
|
864 |
gl.enableVertexAttribArray(program.vertexPosition);
|
|
865 |
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
|
866 |
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
|
|
867 |
gl.vertexAttribPointer(program.vertexPosition, 2, gl.FLOAT, false, 0, 0);
|
|
868 |
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
869 |
if (colour=="green") gl.uniform4fv(program.colour, [0.01, 0.1, 0.01, 1.0]);
|
|
870 |
else if (colour == "red") gl.uniform4fv(program.colour, [0.1, 0.01, 0.01, 1.0]);
|
|
871 |
|
|
872 |
gl.lineWidth(3.0);
|
|
873 |
gl.drawArrays(gl.LINE_STRIP, 0, nVertices);
|
|
874 |
},
|
|
875 |
|
|
876 |
setAdditiveBlending : function()
|
|
877 |
{
|
|
878 |
//gl.blendEquation( gl.FUNC_ADD );
|
|
879 |
gl.blendFunc(gl.ONE, gl.ONE);
|
|
880 |
},
|
|
881 |
|
|
882 |
setNormalBlending : function()
|
|
883 |
{
|
|
884 |
//gl.blendEquation( gl.FUNC_ADD );
|
|
885 |
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
886 |
},
|
|
887 |
|
|
888 |
createShader : function(vsTag, fsTag)
|
|
889 |
{
|
|
890 |
var vsSource = document.getElementById(vsTag).firstChild.nodeValue;
|
|
891 |
var fsSource = document.getElementById(fsTag).firstChild.nodeValue;
|
|
892 |
|
|
893 |
var vs = gl.createShader(gl.VERTEX_SHADER);
|
|
894 |
gl.shaderSource(vs, vsSource);
|
|
895 |
gl.compileShader(vs);
|
|
896 |
if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS))
|
|
897 |
{
|
|
898 |
var infoLog = gl.getShaderInfoLog(vs);
|
|
899 |
gl.deleteShader(vs);
|
|
900 |
throw new Error('createShader, vertex shader compilation:\n' + infoLog);
|
|
901 |
}
|
|
902 |
|
|
903 |
var fs = gl.createShader(gl.FRAGMENT_SHADER);
|
|
904 |
gl.shaderSource(fs, fsSource);
|
|
905 |
gl.compileShader(fs);
|
|
906 |
if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS))
|
|
907 |
{
|
|
908 |
var infoLog = gl.getShaderInfoLog(fs);
|
|
909 |
gl.deleteShader(vs);
|
|
910 |
gl.deleteShader(fs);
|
|
911 |
throw new Error('createShader, fragment shader compilation:\n' + infoLog);
|
|
912 |
}
|
|
913 |
|
|
914 |
var program = gl.createProgram();
|
|
915 |
|
|
916 |
gl.attachShader(program, vs);
|
|
917 |
gl.deleteShader(vs);
|
|
918 |
|
|
919 |
gl.attachShader(program, fs);
|
|
920 |
gl.deleteShader(fs);
|
|
921 |
|
|
922 |
gl.linkProgram(program);
|
|
923 |
|
|
924 |
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
|
|
925 |
{
|
|
926 |
var infoLog = gl.getProgramInfoLog(program);
|
|
927 |
gl.deleteProgram(program);
|
|
928 |
throw new Error('createShader, linking:\n' + infoLog);
|
|
929 |
}
|
|
930 |
|
|
931 |
return program;
|
|
932 |
},
|
|
933 |
|
|
934 |
supportsWebGl : function()
|
|
935 |
{
|
|
936 |
// from https://github.com/Modernizr/Modernizr/blob/master/feature-detects/webgl.js
|
|
937 |
var canvas = document.createElement('canvas'),
|
|
938 |
supports = 'probablySupportsContext' in canvas ? 'probablySupportsContext' : 'supportsContext';
|
|
939 |
if (supports in canvas)
|
|
940 |
{
|
|
941 |
return canvas[supports]('webgl') || canvas[supports]('experimental-webgl');
|
|
942 |
}
|
|
943 |
return 'WebGLRenderingContext' in window;
|
|
944 |
}
|
|
945 |
}
|
|
946 |
|
|
947 |
var sweepPosition = -1;
|
|
948 |
var belowTrigger = false;
|
|
949 |
|
|
950 |
function doScriptProcessor(event)
|
|
951 |
{
|
|
952 |
var xSamplesRaw = event.inputBuffer.getChannelData(0);
|
|
953 |
var ySamplesRaw = event.inputBuffer.getChannelData(1);
|
|
954 |
var xOut = event.outputBuffer.getChannelData(0);
|
|
955 |
var yOut = event.outputBuffer.getChannelData(1);
|
|
956 |
|
|
957 |
var length = xSamplesRaw.length;
|
|
958 |
for (var i=0; i<length; i++)
|
|
959 |
{
|
|
960 |
xSamples[i] = xSamplesRaw[i];// + (Math.random()-0.5)*controls.noise/2000;
|
|
961 |
ySamples[i] = ySamplesRaw[i];// + (Math.random()-0.5)*controls.noise/2000;
|
|
962 |
}
|
|
963 |
|
|
964 |
if (controls.sweepOn && controls.disableFilter)
|
|
965 |
{
|
|
966 |
var gain = Math.pow(2.0,controls.mainGain);
|
|
967 |
var sweepMinTime = controls.sweepMsDiv*10/1000;
|
|
968 |
var triggerValue = controls.sweepTriggerValue/gain;
|
|
969 |
for (var i=0; i<length; i++)
|
|
970 |
{
|
|
971 |
sweepPosition += 2*AudioSystem.timePerSample/sweepMinTime;
|
|
972 |
if (sweepPosition > 1.1 && belowTrigger && ySamples[i]>=triggerValue)
|
|
973 |
{
|
|
974 |
if (i==0) sweepPosition = -1; //don't bother to calculate
|
|
975 |
else
|
|
976 |
{
|
|
977 |
var delta = (ySamples[i]-triggerValue)/(ySamples[i]-ySamples[i-1]);
|
|
978 |
sweepPosition =-1 + delta*2*AudioSystem.timePerSample/sweepMinTime;
|
|
979 |
}
|
|
980 |
}
|
|
981 |
xSamples[i] = sweepPosition / gain;
|
|
982 |
belowTrigger = ySamples[i]<triggerValue;
|
|
983 |
}
|
|
984 |
}
|
|
985 |
|
|
986 |
if (!controls.freezeImage)
|
|
987 |
{
|
|
988 |
if (!controls.disableFilter)
|
|
989 |
{
|
|
990 |
Filter.generateSmoothedSamples(AudioSystem.oldYSamples, ySamples, AudioSystem.smoothedYSamples);
|
|
991 |
if (!controls.sweepOn) Filter.generateSmoothedSamples(AudioSystem.oldXSamples, xSamples, AudioSystem.smoothedXSamples);
|
|
992 |
else
|
|
993 |
{
|
|
994 |
var xS = AudioSystem.smoothedXSamples;
|
|
995 |
var yS = AudioSystem.smoothedYSamples;
|
|
996 |
var gain = Math.pow(2.0,controls.mainGain);
|
|
997 |
var sweepMinTime = controls.sweepMsDiv*10/1000;
|
|
998 |
var triggerValue = controls.sweepTriggerValue/gain;
|
|
999 |
var smoothedLength = AudioSystem.smoothedYSamples.length;
|
|
1000 |
var timeIncrement = 2*AudioSystem.timePerSample/(sweepMinTime*Filter.steps);
|
|
1001 |
for (var i=0; i<smoothedLength; i++)
|
|
1002 |
{
|
|
1003 |
sweepPosition += timeIncrement;
|
|
1004 |
if (sweepPosition > 1.1 && belowTrigger && yS[i]>=triggerValue)
|
|
1005 |
sweepPosition =-1;
|
|
1006 |
xS[i] = sweepPosition / gain;
|
|
1007 |
belowTrigger = yS[i]<triggerValue;
|
|
1008 |
}
|
|
1009 |
}
|
|
1010 |
if (!controls.swapXY) Render.drawLineTexture(AudioSystem.smoothedXSamples, AudioSystem.smoothedYSamples);
|
|
1011 |
else Render.drawLineTexture(AudioSystem.smoothedYSamples, AudioSystem.smoothedXSamples);
|
|
1012 |
}
|
|
1013 |
else
|
|
1014 |
{
|
|
1015 |
if (!controls.swapXY) Render.drawLineTexture(xSamples, ySamples);
|
|
1016 |
else Render.drawLineTexture(ySamples, xSamples);
|
|
1017 |
}
|
|
1018 |
}
|
|
1019 |
|
|
1020 |
for (var i = 0; i<length; i++)
|
|
1021 |
{
|
|
1022 |
AudioSystem.oldXSamples[i] = xSamples[i];
|
|
1023 |
AudioSystem.oldYSamples[i] = ySamples[i];
|
|
1024 |
xOut[i] = xSamplesRaw[i];
|
|
1025 |
yOut[i] = ySamplesRaw[i];
|
|
1026 |
}
|
|
1027 |
|
|
1028 |
AudioSystem.audioVolumeNode.gain.value = controls.audioVolume;
|
|
1029 |
}
|
|
1030 |
|
|
1031 |
function drawCRTFrame(timeStamp)
|
|
1032 |
{
|
|
1033 |
Render.drawCRT();
|
|
1034 |
requestAnimationFrame(drawCRTFrame);
|
|
1035 |
}
|
|
1036 |
|
|
1037 |
Filter.init(512, 8, 6);
|
|
1038 |
AudioSystem.init(512);
|
|
1039 |
var xSamples = new Float32Array(512);
|
|
1040 |
var ySamples = new Float32Array(512);
|
|
1041 |
//Filter.init(1024, 8, 6);
|
|
1042 |
//AudioSystem.init(1024);
|
|
1043 |
//var xSamples = new Float32Array(1024);
|
|
1044 |
//var ySamples = new Float32Array(1024);
|
|
1045 |
UI.init();
|
|
1046 |
Render.init();
|
|
1047 |
if (Render.failed)
|
|
1048 |
{
|
|
1049 |
Render.admitFailure();
|
|
1050 |
}
|
|
1051 |
else
|
|
1052 |
{
|
|
1053 |
Render.setupArrays(Filter.nSmoothedSamples);
|
|
1054 |
AudioSystem.startSound();
|
|
1055 |
requestAnimationFrame(drawCRTFrame);
|
|
1056 |
Controls.setupControls();
|
|
1057 |
}
|