-
Notifications
You must be signed in to change notification settings - Fork 6
/
granular.html
397 lines (311 loc) · 11.9 KB
/
granular.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
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
<!--
Copyright 2010, Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
Granular Effects
</title>
<!-- Slider stuff -->
<script type="text/javascript" src="lib/events.js"></script>
<style type="text/css">
#slider { margin: 10px; }
</style>
<link rel="stylesheet" type="text/css" href = "style/simple.css" />
<!-- Our javascript code -->
<script type="text/javascript">
// init() once the page has finished loading.
window.onload = init;
var context;
var convolver;
var compressor;
var buffer = 0;
var bufferDuration = 58.0;
var kSpeed = 0.05;
var kPitch = 0.0;
var kDiffusionRandomization = 0.2;
var kPanningRandomization = 0.15;
var kPitchRandomization = 0.0;
var kTimeRandomization = 0.0;
var kGrainSize = 0.090;
var speed = 0.3333;
var pitch = 1.0;
var pitchRandomization = kPitchRandomization;
var panningRandomization = kPanningRandomization;
var diffusionRandomization = kDiffusionRandomization;
var realTime = 0.0;
var grainTime = 0.0;
var grainDuration = kGrainSize;
var grainSpacing = 0.5 * kGrainSize;
var isSourceLoaded = false;
var isImpulseResponseLoaded = false;
var applyGrainWindow = false;
var grainWindow;
function speedHandler(event, ui) {
var param = parseFloat(ui.value);
if (param < 0.0) {
speed = 4.0 * param - 0.1;
} else {
speed = 4.0 * param + 0.1;
}
var info = document.getElementById("speed-value");
info.innerHTML = "speed = " + speed;
}
function pitchHandler(event, ui) {
var cents = parseFloat(ui.value);
pitch = cents;
var info = document.getElementById("pitch-value");
info.innerHTML = "pitch = " + cents + " cents";
}
function pitchRandomizationHandler(event, ui) {
var cents = parseFloat(ui.value);
pitchRandomization = cents;
var info = document.getElementById("pitchRandomization-value");
info.innerHTML = "pitchRandomization = " + cents + " cents";
}
function timeRandomizationHandler(event, ui) {
timeRandomization = parseFloat(ui.value);
var info = document.getElementById("timeRandomization-value");
info.innerHTML = "timeRandomization = " + timeRandomization + " seconds";
}
function diffusionRandomizationHandler(event, ui) {
diffusionRandomization = parseFloat(ui.value);
var info = document.getElementById("diffusionRandomization-value");
info.innerHTML = "diffusionRandomization = " + diffusionRandomization*100.0 + "%";
}
function panningRandomizationHandler(event, ui) {
panningRandomization = parseFloat(ui.value);
var info = document.getElementById("panningRandomization-value");
info.innerHTML = "panningRandomization = " + panningRandomization*100.0 + "%";
}
function grainSizeHandler(event, ui) {
grainDuration = parseFloat(ui.value);
grainSpacing = 0.25 * grainDuration;
var info = document.getElementById("grainSize-value");
info.innerHTML = "grainSize = " + grainDuration + " seconds";
}
function scheduleGrain() {
if (!buffer)
return;
var source = context.createBufferSource();
source.buffer = buffer;
var r = Math.random();
var r2 = Math.random();
var r3 = Math.random();
var r4 = Math.random();
var r5 = Math.random();
r1 = (r - 0.5) * 2.0;
r2 = (r2 - 0.5) * 2.0;
r3 = (r3 - 0.5) * 2.0;
r4 = (r4 - 0.5) * 2.0;
// Spatialization
var panner = context.createPanner();
var grainWindowNode;
if (applyGrainWindow) {
// Create a gain node with a special "grain window" shaping curve.
grainWindowNode = context.createGainNode();
source.connect(grainWindowNode);
grainWindowNode.connect(panner);
} else {
source.connect(panner);
}
var distance = 2.0;
var azimuth = Math.PI * panningRandomization * r3;
var elevation = Math.PI * (0.25 + 0.75 * panningRandomization * r4);
var x = Math.sin(azimuth);
var z = Math.cos(azimuth);
var y = Math.sin(elevation);
var scaleXZ = Math.cos(elevation);
x *= distance * scaleXZ;
y *= distance;
z *= distance * scaleXZ;
panner.panningModel = AudioPannerNode.HRTF;
panner.setPosition(x, y, z);
var dryGainNode = context.createGainNode();
var wetGainNode = context.createGainNode();
wetGainNode.gain.value = 0.5 * diffusionRandomization * r5;
dryGainNode.gain.value = 1.0 - wetGainNode.gain.value;
// Pitch
var totalPitch = pitch + r1 * pitchRandomization;
var pitchRate = Math.pow(2.0, totalPitch / 1200.0);
source.playbackRate.value = pitchRate;
// Connect dry mix
panner.connect(dryGainNode);
dryGainNode.connect(compressor);
// Connect wet mix
panner.connect(wetGainNode);
wetGainNode.connect(convolver);
// Time randomization
var randomGrainOffset = r2 * timeRandomization;
// Schedule sound grain
source.noteGrainOn(realTime, grainTime + randomGrainOffset, grainDuration);
// Schedule the grain window.
// This applies a time-varying gain change for smooth fade-in / fade-out.
if (applyGrainWindow) {
var windowDuration = grainDuration / pitchRate;
grainWindowNode.gain.value = 0.0; // make default value 0
grainWindowNode.gain.setValueCurveAtTime(grainWindow, realTime, windowDuration);
}
var lastGrainTime = grainTime;
// Update time params
realTime += grainSpacing;
grainTime += speed * grainSpacing;
if (grainTime > bufferDuration) grainTime = 0.0;
if (grainTime < 0.0) grainTime += bufferDuration; // backwards wrap-around
}
function schedule() {
var currentTime = context.currentTime;
while (realTime < currentTime + 0.100) {
scheduleGrain();
}
setTimeout("schedule()", 20);
}
function initAudio() {
context = new AudioContext();
// This check is a hack and will only be needed temporarily.
// The reason is that the noteGrainOn() method used to (in older builds) apply a hard-coded amplitude window.
// The newer and more flexible approach is that noteGrainOn() simply plays a portion of an AudioBuffer,
// without any gain scaling. Then we can apply a gain scaling (which is desired in this example)
// by using an AudioGainNode.
// We check the existence of the decodeAudioData() only because this is the time when the change in noteGrainOn()
// behavior happened -- yucky, but only temporary since it can be removed in a few weeks when all builds have the new behavior.
if (context.decodeAudioData) {
applyGrainWindow = true;
// Create a granular synthesis "grain window"
// Each small audio snippet will have a smooth fade-in / fade-out according to this shape.
var grainWindowLength = 16384;
grainWindow = new Float32Array(grainWindowLength);
for (var i = 0; i < grainWindowLength; ++i)
grainWindow[i] = Math.sin(Math.PI * i / grainWindowLength);
} else {
applyGrainWindow = false;
}
if (context.createDynamicsCompressor) {
// Create dynamics compressor to sweeten the overall mix.
compressor = context.createDynamicsCompressor();
compressor.connect(context.destination);
} else {
// Compressor is not available on this implementation - bypass and simply point to destination.
compressor = context.destination;
}
// Create a convolver for ambience
convolver = context.createConvolver();
convolver.connect(compressor);
load();
}
function init() {
initAudio();
addSlider("speed");
addSlider("pitch");
addSlider("pitchRandomization");
addSlider("timeRandomization");
addSlider("diffusionRandomization");
addSlider("panningRandomization");
addSlider("grainSize");
configureSlider("speed", kSpeed, -1.0, 1.0, speedHandler);
configureSlider("pitch", kPitch, -3600.0, 1800.0, pitchHandler);
configureSlider("pitchRandomization", 0.0, 0.0, 1200.0, pitchRandomizationHandler);
configureSlider("timeRandomization", 0.0, 0.0, 1.0, timeRandomizationHandler);
configureSlider("diffusionRandomization", kDiffusionRandomization, 0.0, 1.0, diffusionRandomizationHandler);
configureSlider("panningRandomization", kPanningRandomization, 0.0, 1.0, panningRandomizationHandler);
configureSlider("grainSize", kGrainSize, 0.010, 0.5, grainSizeHandler);
var ui = {value: 1.0};
ui.value = kSpeed;
speedHandler(0, ui);
ui.value = kPitch;
pitchHandler(0, ui);
ui.value = kPitchRandomization;
pitchRandomizationHandler(0, ui);
ui.value = kTimeRandomization;
timeRandomizationHandler(0, ui);
ui.value = kDiffusionRandomization;
diffusionRandomizationHandler(0, ui);
ui.value = kPanningRandomization;
panningRandomizationHandler(0, ui);
}
function load() {
// loadImpulseResponse('impulse-responses/spatialized4.wav');
loadImpulseResponse('impulse-responses/matrix-reverb5.wav');
loadHumanVoice("sounds/hyper-reality/human-voice.mp4");
}
function loadImpulseResponse(url) {
// Load impulse response asynchronously
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
request.onload = function() {
convolver.buffer = context.createBuffer(request.response, false);
isImpulseResponseLoaded = true;
finishLoading();
}
request.onerror = function() {
alert("error loading reverb");
}
request.send();
}
function loadHumanVoice(url) {
// Load asynchronously
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
request.onload = function() {
buffer = context.createBuffer(request.response, false /*true*/);
bufferDuration = buffer.duration - 0.050;
isSourceLoaded = true;
finishLoading(); // we have the voice, put up sliders and start playing...
};
request.onerror = function() {
alert("error loading");
};
request.send();
}
function finishLoading() {
if (!isSourceLoaded || !isImpulseResponseLoaded)
return;
// first, get rid of loading animation
var loading = document.getElementById("loading");
loading.innerHTML = "";
// start playing the granular effect
realTime = context.currentTime;
schedule();
}
</script>
</head>
<body>
<h1>Granular Effects</h1>
<!-- Sliders and other controls will be added here -->
<div id="controls"> </div>
<div id="info"> </div>
<!-- Initial loading animation -->
<div id="loading">
<img src="images/loading.gif">
</div>
</body>
</html>