adds Convolver class, IR, and convolver example

This commit is contained in:
Kevin Siwoff 2014-10-22 17:58:03 -04:00
parent b45c983152
commit 1422a544f9
3 changed files with 217 additions and 0 deletions

113
Tone/effect/Convolver.js Normal file
View file

@ -0,0 +1,113 @@
define(["Tone/core/Tone", "Tone/core/Buffer", "Tone/effect/StereoEffect"], function(Tone){
"use strict";
/**
* @class Convolver wrapper for reverb and emulation.
* NB: currently, this class only supports 1 buffer member.
* Future iterations will include a this.buffers collection for multi buffer mode.
* @constructor
* @extends {Tone.Effect}
* @param {string|Object=}
* @param {function} callback function
*/
Tone.Convolver = function(){
//get all of the defaults
var options = this.optionsObject(arguments, ["url", "onload"], Tone.Convolver.defaults);
//connections
Tone.Effect.call(this, options);
/**
* convolver node
* @type {[type]}
* @private
*/
this._convolver = this.context.createConvolver();
/**
* convolution buffers
* @type {Array} Array of ArrayBuffers
* @private
*/
this._buffer = null;
//if there is a url, load it.
if (!this.isUndef(options.url)){
this.load(options.url, options.onload);
//the connections
}
this.connectEffect(this._convolver);
};
Tone.extend(Tone.Convolver, Tone.Effect);
/**
* @static
* @type {Object}
*/
Tone.Convolver.defaults = {
"url": null,
"onload": null,
};
/**
* Load the impulse response url as an audio buffer.
* Decodes the audio asynchronously and invokes
* the callback once the audio buffer loads.
* @param {string} url the url of the buffer to load.
* filetype support depends on the
* browser.
* @param {function(Tone.Convolver)=} callback
*/
Tone.Convolver.prototype.load = function(url, callback){
var self = this;
if (!self._buffer){
new Tone.Buffer({
"url" : url,
"callback" : function (buffer){
self.setBuffer(buffer);
if (callback){
callback(self);
}
}
});
} else if (callback){
callback(self);
}
};
/**
* set the buffer
*
* @param {AudioBuffer} buffer the buffer which the player will play.
* note: if you switch the buffer after
* the player is already started, it will not
* take effect until the next time the player
* is started.
*/
Tone.Convolver.prototype.setBuffer = function(buffer){
this._buffer = buffer;
this._convolver.buffer = this._buffer;
};
/**
* set multiple parameters at once with an object
* @param {Object} params the parameters as an object
*/
Tone.Convolver.prototype.set = function(params){
if (!this.isUndef(params.bypass)) this.setBypass(params.bypass);
};
/**
* dispose and disconnect
*/
Tone.Convolver.prototype.dispose = function(){
Tone.StereoEffect.prototype.dispose.call(this);
if (this._source !== null) {
this._source.disconnect();
this._source = null;
}
this._buffer = null;
};
return Tone.Convolver;
});

Binary file not shown.

104
examples/convolver.html Normal file
View file

@ -0,0 +1,104 @@
<html>
<head>
<title>Convolver</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<script type="text/javascript" src="./deps/jquery.min.js"></script>
<script type="text/javascript" src="./deps/jquery-ui.js"></script>
<script type="text/javascript" src="./deps/jquery.ui.touch-punch.js"></script>
<script type="text/javascript" src="../build/Tone.js"></script>
<script type="text/javascript" src="./Widgets.js"></script>
<script type="text/javascript" src="./ExampleList.js"></script>
<link rel="stylesheet" type="text/css" href="./style/widgets.css">
<link rel="stylesheet" type="text/css" href="./style/jquery-ui.css">
</head>
<body>
<div id="Container">
<div id="Explanation">
Convolver
<br>
<br>
Press and hold to hear a 10ms grain of audio.
</div>
<div id="Content">
<div id="Loading">Loading...</div>
</div>
</div>
<script type="text/javascript">
/* globals Tone, GUI */
var player = new Tone.Player("./audio/casio/A1.mp3", function(){
$("#Loading").remove();
});
player.loop = true;
// player.loopStart = 0.1;
// player.loopEnd = 0.11;
// player.toMaster();
var convolver = new Tone.Convolver({
url : "./audio/IRs/berlin_tunnel_ir.wav",
onload : function(buffer){
console.log('loaded buffer!');
},
wetDry: 1 // min: 0 max: +1
// bypass: false
});
//Effects sends and returns
// player.toMaster();
player.connect(convolver);
convolver.toMaster();
// GUI //
new GUI.TopBar(Tone);
var container = $("#Content");
// Momentary play
new GUI.Momentary(container, function(on){
if (on){
player.start();
} else {
player.stop();
}
}, "Start", "Stop");
// Effect bypass checkbox
new GUI.Checkbox(container, function(on){
if (on){
convolver.setBypass();
} else {
convolver.setWet(1);
}
}, "Effect bypassOff", "Effect bypassOn");
// dryWet slider
var wetnessSlider = new GUI.Slider(container, function(val){
convolver.setWet(val, 0);
return val*100;
}, 1, "wetness", "%");
wetnessSlider.render(1);
// multi choice dropdown
var impulses = new GUI.DropDown(container, ["Berlin tunnel"], function(val){
//do something with impulse val string here
});
</script>
<style type="text/css">
#Content {
width: 300px;
height: 300px;
}
.Momentary, .slider, .Value, {
width: 100%;
}
</style>
</body>
</html>