Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed new features: filtering, distortion and convolution (reverb) #238

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/soundjs/AbstractSoundInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,20 @@ this.createjs = this.createjs || {};
set: this.setDistortionAmount
});

/**
* The convolver buffer to use on the convolver node. This can be an audioBuffer or a filepath, and will be handled appropriately depending on which.
* Note that convolver is not supported by HTML Audio.
*
* @property convolverBuffer
* @type {String} || {AudioBuffer}
* @default null
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<script src="https://gist.github.com/millsymel/fd110ff692f85b30acd2364bc56c4aab.js"></script>

*/
this._convolverBuffer = null;
Object.defineProperty(this, "convolverBuffer", {
get: this.getConvolverBuffer,
set: this.setConvolverBuffer
});

/**
* Audio sprite property used to determine the starting offset.
* @property startTime
Expand Down Expand Up @@ -735,6 +749,44 @@ this.createjs = this.createjs || {};
return this._distortionAmount;
};

/**
* DEPRECATED, please use {{#crossLink "AbstractSoundInstance/convolverBuffer:property"}}{{/crossLink}} directly as a property
*
* @deprecated
* @method setConvolverBuffer
* @param {String} The filepath to an impulse response WAV || {AudioBuffer} The audio buffer to use as the convoler's buffer.
* @return {AbstractSoundInstance} Returns reference to itself for chaining calls
*/
p.setConvolverBuffer = function (buffer) {
if (typeof buffer === 'string') {
//if a filepath is passed, must import it as an arraybuffer and decode
this._getConvolverBufferFromFilepath(buffer);
}
else {
if (typeof AudioBuffer !== 'undefined' && buffer instanceof AudioBuffer) {
//audio buffer is passed, set it directly
this._convolverBuffer = buffer;
this._updateConvolver();
}
else {
return false;
}
}
return this;
};

/**
* DEPRECATED, please use {{#crossLink "AbstractSoundInstance/convolverBuffer:property"}}{{/crossLink}} directly as a property
*
* @deprecated
* @method getConvolverBuffer
* @return {AudioBuffer} The audio buffer being used as the convolver's buffer.
*/
p.getConvolverBuffer = function () {
return this._convolverBuffer;
};


/**
* DEPRECATED, please use {{#crossLink "AbstractSoundInstance/position:property"}}{{/crossLink}} directly as a property
*
Expand Down
43 changes: 39 additions & 4 deletions src/soundjs/webaudio/WebAudioSoundInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,24 @@ this.createjs = this.createjs || {};

/**
* NOTE this is only intended for use by advanced users.
* <br />A filterNode allowing frequency filtering. Connected to WebAudioSoundInstance {{#crossLink "WebAudioSoundInstance/panNode:property"}}{{/crossLink}}.
* <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.panNode); //filter node => pan node => gain node
this.filterNode.connect(this.convolverNode); //filter node => convolver node => pan node => gain node

/**
* NOTE this is only intended for use by advanced users.
Expand All @@ -96,7 +107,7 @@ this.createjs = this.createjs || {};
* @since 0.6.?
*/
this.distortionNode = s.context.createWaveShaper();
this.distortionNode.connect(this.filterNode); //distortion node => filter node => pan node => gain node
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.
Expand Down Expand Up @@ -244,6 +255,27 @@ this.createjs = this.createjs || {};
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);
};
Expand Down Expand Up @@ -317,10 +349,13 @@ this.createjs = this.createjs || {};
* @since 0.4.1
*/
p._createAndPlayAudioNode = function(startTime, offset) {
console.log('createandplayaudionode');
var audioNode = s.context.createBufferSource();
audioNode.buffer = this.playbackResource;
//audioNode.connect(this.panNode);
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);
Expand Down