2014-08-24 16:19:25 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/source/Player", "Tone/component/Envelope", "Tone/component/Filter"],
|
2014-08-20 20:52:14 +00:00
|
|
|
function(Tone){
|
|
|
|
|
2014-09-04 04:41:40 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-08-20 20:52:14 +00:00
|
|
|
/**
|
|
|
|
* @class A simple sampler instrument which plays an audio buffer
|
|
|
|
* through an amplitude envelope and optionally a filter
|
|
|
|
* envelope.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone}
|
2014-08-24 16:19:25 +00:00
|
|
|
* @param {string|object} url the url of the audio file
|
|
|
|
* @param {function} load called when the sample has been loaded
|
2014-08-20 20:52:14 +00:00
|
|
|
*/
|
2014-08-24 23:28:42 +00:00
|
|
|
Tone.Sampler = function(){
|
2014-08-24 16:19:25 +00:00
|
|
|
|
2014-09-04 02:35:03 +00:00
|
|
|
var options = this.optionsObject(arguments, ["url", "load"], Tone.Sampler.defaults);
|
2014-08-20 20:52:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {GainNode}
|
|
|
|
*/
|
|
|
|
this.output = this.context.createGain();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the sample player
|
|
|
|
* @type {Tone.Player}
|
|
|
|
*/
|
2014-08-24 16:19:25 +00:00
|
|
|
this.player = new Tone.Player(options.url, options.load);
|
2014-08-20 20:52:14 +00:00
|
|
|
this.player.retrigger = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the amplitude envelope
|
|
|
|
* @type {Tone.Envelope}
|
|
|
|
*/
|
2014-08-24 16:19:25 +00:00
|
|
|
this.envelope = new Tone.Envelope(options.envelope);
|
2014-08-20 20:52:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the filter envelope
|
|
|
|
* @type {Tone.Envelope}
|
|
|
|
*/
|
2014-08-24 16:19:25 +00:00
|
|
|
this.filterEnvelope = new Tone.Envelope(options.filterEnvelope);
|
2014-08-20 20:52:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the filter
|
|
|
|
* @type {BiquadFilterNode}
|
|
|
|
*/
|
2014-08-24 16:19:25 +00:00
|
|
|
this.filter = new Tone.Filter(options.filter);
|
2014-08-20 20:52:14 +00:00
|
|
|
|
|
|
|
//connections
|
|
|
|
this.chain(this.player, this.filter, this.output);
|
2014-09-04 02:35:03 +00:00
|
|
|
this.envelope.connect(this.player.output.gain);
|
2014-08-20 20:52:14 +00:00
|
|
|
this.filterEnvelope.connect(this.filter.frequency);
|
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(Tone.Sampler);
|
|
|
|
|
2014-08-24 16:19:25 +00:00
|
|
|
/**
|
|
|
|
* the default parameters
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
2014-08-25 14:23:37 +00:00
|
|
|
Tone.Sampler.defaults = {
|
2014-08-24 16:19:25 +00:00
|
|
|
"url" : null,
|
|
|
|
"load" : function(){},
|
|
|
|
"envelope" : {
|
|
|
|
"attack" : 0.001,
|
|
|
|
"decay" : 0,
|
|
|
|
"sustain" : 1,
|
|
|
|
"release" : 0.1
|
|
|
|
},
|
|
|
|
"filterEnvelope" : {
|
|
|
|
"attack" : 0.001,
|
|
|
|
"decay" : 0.001,
|
|
|
|
"sustain" : 1,
|
|
|
|
"release" : 0.5,
|
|
|
|
"min" : 0,
|
|
|
|
"max" : 20000
|
|
|
|
},
|
|
|
|
"filter" : {
|
|
|
|
"type" : "lowpass"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the parameters in bulk
|
|
|
|
* @param {Object} param
|
|
|
|
*/
|
|
|
|
Tone.Sampler.prototype.set = function(params){
|
|
|
|
if (!this.isUndef(params.filterEnvelope)) this.filterEnvelope.set(params.filterEnvelope);
|
|
|
|
if (!this.isUndef(params.envelope)) this.envelope.set(params.envelope);
|
|
|
|
if (!this.isUndef(params.filter)) this.filter.set(params.filter);
|
|
|
|
};
|
|
|
|
|
2014-08-20 20:52:14 +00:00
|
|
|
/**
|
|
|
|
* start the sample
|
|
|
|
*
|
|
|
|
* @param {Tone.Time=} [time=now] the time when the note should start
|
2014-09-04 02:35:03 +00:00
|
|
|
* @param {number=} velocity the velocity of the note
|
2014-08-20 20:52:14 +00:00
|
|
|
*/
|
2014-09-04 02:35:03 +00:00
|
|
|
Tone.Sampler.prototype.triggerAttack = function(time, velocity){
|
2014-08-20 20:52:14 +00:00
|
|
|
this.player.start(time);
|
2014-09-04 02:35:03 +00:00
|
|
|
this.envelope.triggerAttack(time, velocity);
|
|
|
|
this.filterEnvelope.triggerAttack(time);
|
2014-08-20 20:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the release portion of the sample
|
|
|
|
*
|
|
|
|
* @param {Tone.Time=} [time=now] the time when the note should release
|
|
|
|
*/
|
|
|
|
Tone.Sampler.prototype.triggerRelease = function(time){
|
|
|
|
this.filterEnvelope.triggerRelease(time);
|
|
|
|
this.envelope.triggerRelease(time);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up
|
|
|
|
*/
|
|
|
|
Tone.Sampler.prototype.dispose = function(){
|
2014-08-24 16:19:25 +00:00
|
|
|
Tone.prototype.dispose.call(this);
|
2014-08-20 20:52:14 +00:00
|
|
|
this.player.dispose();
|
|
|
|
this.filterEnvelope.dispose();
|
|
|
|
this.envelope.dispose();
|
2014-08-24 16:19:25 +00:00
|
|
|
this.filter.dispose();
|
2014-08-20 20:52:14 +00:00
|
|
|
this.player = null;
|
|
|
|
this.filterEnvelope = null;
|
|
|
|
this.envelope = null;
|
|
|
|
this.filter = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.Sampler;
|
|
|
|
});
|