-
Notifications
You must be signed in to change notification settings - Fork 825
Expand file tree
/
Copy pathWebAudioSoundInstance.js
More file actions
424 lines (364 loc) · 14.6 KB
/
WebAudioSoundInstance.js
File metadata and controls
424 lines (364 loc) · 14.6 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
/*
* WebAudioSoundInstance
* Visit http://createjs.com/ for documentation, updates and examples.
*
*
* Copyright (c) 2012 gskinner.com, inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @module SoundJS
*/
// namespace:
this.createjs = this.createjs || {};
/**
* WebAudioSoundInstance extends the base api of {{#crossLink "AbstractSoundInstance"}}{{/crossLink}} and is used by
* {{#crossLink "WebAudioPlugin"}}{{/crossLink}}.
*
* WebAudioSoundInstance exposes audioNodes for advanced users.
*
* @param {String} src The path to and file name of the sound.
* @param {Number} startTime Audio sprite property used to apply an offset, in milliseconds.
* @param {Number} duration Audio sprite property used to set the time the clip plays for, in milliseconds.
* @param {Object} playbackResource Any resource needed by plugin to support audio playback.
* @class WebAudioSoundInstance
* @extends AbstractSoundInstance
* @constructor
*/
(function () {
"use strict";
function WebAudioSoundInstance(src, startTime, duration, playbackResource) {
this.AbstractSoundInstance_constructor(src, startTime, duration, playbackResource);
// public properties
/**
* NOTE this is only intended for use by advanced users.
* <br />GainNode for controlling <code>WebAudioSoundInstance</code> volume. Connected to the {{#crossLink "WebAudioSoundInstance/destinationNode:property"}}{{/crossLink}}.
* @property gainNode
* @type {AudioGainNode}
* @since 0.4.0
*
*/
this.gainNode = s.context.createGain();
/**
* NOTE this is only intended for use by advanced users.
* <br />A panNode allowing left and right audio channel panning only. Connected to WebAudioSoundInstance {{#crossLink "WebAudioSoundInstance/gainNode:property"}}{{/crossLink}}.
* @property panNode
* @type {AudioPannerNode}
* @since 0.4.0
*/
this.panNode = s.context.createPanner();
this.panNode.panningModel = s._panningModel;
this.panNode.connect(this.gainNode);
this._updatePan();
/**
* NOTE this is only intended for use by advanced users.
* <br />A ConvolverNode allowing application of convolver buffers (e.g. reverb) Connected to WebAudioSoundInstance {{#crossLink "WebAudioSoundInstance/panNode:property"}}{{/crossLink}}.
* @property convolverNode
* @type { ConvolverNode}
* @since 0.6.?
*/
this.convolverNode = s.context.createConvolver();
this.convolverNode.buffer = this.testBuffer;
this.convolverNode.connect(this.panNode); //convolver node => pan node => gain node
/**
* NOTE this is only intended for use by advanced users.
* <br />A filterNode allowing frequency filtering. Connected to WebAudioSoundInstance {{#crossLink "WebAudioSoundInstance/convolverNode:property"}}{{/crossLink}}.
* @property filterNode
* @type {BiquadFilterNode}
* @since 0.6.?
*/
this.filterNode = s.context.createBiquadFilter();
this.filterNode.connect(this.convolverNode); //filter node => convolver node => pan node => gain node
/**
* NOTE this is only intended for use by advanced users.
* <br />A waveShaperNode allowing application of disortion curves. Connected to WebAudioSoundInstance {{#crossLink "WebAudioSoundInstance/filterNode:property"}}{{/crossLink}}.
* @property distortionNode
* @type {WaveShaperNode}
* @since 0.6.?
*/
this.distortionNode = s.context.createWaveShaper();
this.distortionNode.connect(this.filterNode); //distortion node => filter node => convolver node => pan node => gain node
/**
* NOTE this is only intended for use by advanced users.
* <br />sourceNode is the audio source. Connected to WebAudioSoundInstance {{#crossLink "WebAudioSoundInstance/panNode:property"}}{{/crossLink}}.
* @property sourceNode
* @type {AudioNode}
* @since 0.4.0
*
*/
this.sourceNode = null;
// private properties
/**
* Timeout that is created internally to handle sound playing to completion.
* Stored so we can remove it when stop, pause, or cleanup are called
* @property _soundCompleteTimeout
* @type {timeoutVariable}
* @default null
* @protected
* @since 0.4.0
*/
this._soundCompleteTimeout = null;
/**
* NOTE this is only intended for use by very advanced users.
* _sourceNodeNext is the audio source for the next loop, inserted in a look ahead approach to allow for smooth
* looping. Connected to {{#crossLink "WebAudioSoundInstance/gainNode:property"}}{{/crossLink}}.
* @property _sourceNodeNext
* @type {AudioNode}
* @default null
* @protected
* @since 0.4.1
*
*/
this._sourceNodeNext = null;
/**
* Time audio started playback, in seconds. Used to handle set position, get position, and resuming from paused.
* @property _playbackStartTime
* @type {Number}
* @default 0
* @protected
* @since 0.4.0
*/
this._playbackStartTime = 0;
// Proxies, make removing listeners easier.
this._endedHandler = createjs.proxy(this._handleSoundComplete, this);
};
var p = createjs.extend(WebAudioSoundInstance, createjs.AbstractSoundInstance);
var s = WebAudioSoundInstance;
// TODO: deprecated
// p.initialize = function() {}; // searchable for devs wondering where it is. REMOVED. See docs for details.
/**
* Note this is only intended for use by advanced users.
* <br />Audio context used to create nodes. This is and needs to be the same context used by {{#crossLink "WebAudioPlugin"}}{{/crossLink}}.
* @property context
* @type {AudioContext}
* @static
* @since 0.6.0
*/
s.context = null;
/**
* Note this is only intended for use by advanced users.
* <br />The scratch buffer that will be assigned to the buffer property of a source node on close.
* This is and should be the same scratch buffer referenced by {{#crossLink "WebAudioPlugin"}}{{/crossLink}}.
* @property _scratchBuffer
* @type {AudioBufferSourceNode}
* @static
*/
s._scratchBuffer = null;
/**
* Note this is only intended for use by advanced users.
* <br /> Audio node from WebAudioPlugin that sequences to <code>context.destination</code>
* @property destinationNode
* @type {AudioNode}
* @static
* @since 0.6.0
*/
s.destinationNode = null;
/**
* Value to set panning model to equal power for WebAudioSoundInstance. Can be "equalpower" or 0 depending on browser implementation.
* @property _panningModel
* @type {Number / String}
* @protected
* @static
* @since 0.6.0
*/
s._panningModel = "equalpower";
// Public methods
p.destroy = function() {
this.AbstractSoundInstance_destroy();
this.panNode.disconnect(0);
this.panNode = null;
this.gainNode.disconnect(0);
this.gainNode = null;
};
p.toString = function () {
return "[WebAudioSoundInstance]";
};
// Private Methods
p._updatePan = function() {
this.panNode.setPosition(this._pan, 0, -0.5);
// z need to be -0.5 otherwise the sound only plays in left, right, or center
};
p._updateFilter = function() {
this.filterNode.frequency.value = this._filterFrequency;
this.filterNode.Q.value = this._filterQ;
this.filterNode.type = this._filterType;
this.filterNode.detune.value = this._filterDetune;
};
// distortion curve for the waveshaper, thanks to Kevin Ennis
// http://stackoverflow.com/questions/22312841/waveshaper-node-in-webaudio-how-to-emulate-distortion
p._makeDistortionCurve = function(amount) {
var k = typeof amount === 'number' ? amount : 50;
var n_samples = 44100;
var curve = new Float32Array(n_samples);
var deg = Math.PI / 180;
var i = 0;
var x;
for ( ; i < n_samples; ++i ) {
x = i * 2 / n_samples - 1;
curve[i] = ( 3 + k ) * x * 20 * deg / ( Math.PI + k * Math.abs(x) );
}
return curve;
};
p._updateDistortion = function() {
this.distortionNode.oversample = '4x'; //pull this out into a config. Can be 'none', '2x' or '4x'
this.distortionNode.curve = this._makeDistortionCurve(this._distortionAmount);
};
p._updateConvolver = function() {
this.convolverNode.buffer = this._convolverBuffer;
};
p._getConvolverBufferFromFilepath = function(filepath) {
// grab audio track via XHR for convolver node
var req = new XMLHttpRequest();
req.open('GET', filepath, true);
req.responseType = 'arraybuffer';
var self = this; //save this reference
req.onload = function() {
var audioData = req.response;
s.context.decodeAudioData(audioData, function(buffer) {
self._convolverBuffer = buffer;
self._updateConvolver();
}, function(e){"Error with decoding audio data" + e.err});
}
req.send();
};
p._removeLooping = function(value) {
this._sourceNodeNext = this._cleanUpAudioNode(this._sourceNodeNext);
};
p._addLooping = function(value) {
if (this.playState != createjs.Sound.PLAY_SUCCEEDED) { return; }
this._sourceNodeNext = this._createAndPlayAudioNode(this._playbackStartTime, 0);
};
p._setDurationFromSource = function () {
this._duration = this.playbackResource.duration * 1000;
};
p._handleCleanUp = function () {
if (this.sourceNode && this.playState == createjs.Sound.PLAY_SUCCEEDED) {
this.sourceNode = this._cleanUpAudioNode(this.sourceNode);
this._sourceNodeNext = this._cleanUpAudioNode(this._sourceNodeNext);
}
if (this.gainNode.numberOfOutputs != 0) {this.gainNode.disconnect(0);}
// OJR there appears to be a bug that this doesn't always work in webkit (Chrome and Safari). According to the documentation, this should work.
clearTimeout(this._soundCompleteTimeout);
this._playbackStartTime = 0; // This is used by getPosition
};
/**
* Turn off and disconnect an audioNode, then set reference to null to release it for garbage collection
* @method _cleanUpAudioNode
* @param audioNode
* @return {audioNode}
* @protected
* @since 0.4.1
*/
p._cleanUpAudioNode = function(audioNode) {
if(audioNode) {
audioNode.stop(0);
audioNode.disconnect(0);
// necessary to prevent leak on iOS Safari 7-9. will throw in almost all other
// browser implementations.
try { audioNode.buffer = s._scratchBuffer; } catch(e) {}
audioNode = null;
}
return audioNode;
};
p._handleSoundReady = function (event) {
this.gainNode.connect(s.destinationNode); // this line can cause a memory leak. Nodes need to be disconnected from the audioDestination or any sequence that leads to it.
var dur = this._duration * 0.001;
var pos = this._position * 0.001;
if (pos > dur) {pos = dur;}
this.sourceNode = this._createAndPlayAudioNode((s.context.currentTime - dur), pos);
this._playbackStartTime = this.sourceNode.startTime - pos;
this._soundCompleteTimeout = setTimeout(this._endedHandler, (dur - pos) * 1000);
if(this._loop != 0) {
this._sourceNodeNext = this._createAndPlayAudioNode(this._playbackStartTime, 0);
}
};
/**
* Creates an audio node using the current src and context, connects it to the gain node, and starts playback.
* @method _createAndPlayAudioNode
* @param {Number} startTime The time to add this to the web audio context, in seconds.
* @param {Number} offset The amount of time into the src audio to start playback, in seconds.
* @return {audioNode}
* @protected
* @since 0.4.1
*/
p._createAndPlayAudioNode = function(startTime, offset) {
console.log('createandplayaudionode');
var audioNode = s.context.createBufferSource();
audioNode.buffer = this.playbackResource;
audioNode.connect(this.distortionNode);
//audioNode.connect(this.convolverNode);
console.log('connected to convolverNode');
var dur = this._duration * 0.001;
audioNode.startTime = startTime + dur;
audioNode.start(audioNode.startTime, offset+(this._startTime*0.001), dur - offset);
return audioNode;
};
p._pause = function () {
this._position = (s.context.currentTime - this._playbackStartTime) * 1000; // * 1000 to give milliseconds, lets us restart at same point
this.sourceNode = this._cleanUpAudioNode(this.sourceNode);
this._sourceNodeNext = this._cleanUpAudioNode(this._sourceNodeNext);
if (this.gainNode.numberOfOutputs != 0) {this.gainNode.disconnect(0);}
clearTimeout(this._soundCompleteTimeout);
};
p._resume = function () {
this._handleSoundReady();
};
/*
p._handleStop = function () {
// web audio does not need to do anything extra
};
*/
p._updateVolume = function () {
var newVolume = this._muted ? 0 : this._volume;
if (newVolume != this.gainNode.gain.value) {
this.gainNode.gain.value = newVolume;
}
};
p._calculateCurrentPosition = function () {
return ((s.context.currentTime - this._playbackStartTime) * 1000); // pos in seconds * 1000 to give milliseconds
};
p._updatePosition = function () {
this.sourceNode = this._cleanUpAudioNode(this.sourceNode);
this._sourceNodeNext = this._cleanUpAudioNode(this._sourceNodeNext);
clearTimeout(this._soundCompleteTimeout);
if (!this._paused) {this._handleSoundReady();}
};
// OJR we are using a look ahead approach to ensure smooth looping.
// We add _sourceNodeNext to the audio context so that it starts playing even if this callback is delayed.
// This technique is described here: http://www.html5rocks.com/en/tutorials/audio/scheduling/
// NOTE the cost of this is that our audio loop may not always match the loop event timing precisely.
p._handleLoop = function () {
this._cleanUpAudioNode(this.sourceNode);
this.sourceNode = this._sourceNodeNext;
this._playbackStartTime = this.sourceNode.startTime;
this._sourceNodeNext = this._createAndPlayAudioNode(this._playbackStartTime, 0);
this._soundCompleteTimeout = setTimeout(this._endedHandler, this._duration);
};
p._updateDuration = function () {
if(this.playState == createjs.Sound.PLAY_SUCCEEDED) {
this._pause();
this._resume();
}
};
createjs.WebAudioSoundInstance = createjs.promote(WebAudioSoundInstance, "AbstractSoundInstance");
}());