Tone.js/Tone/instrument/Sampler.js

144 lines
3.6 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/source/Player", "Tone/component/AmplitudeEnvelope", "Tone/component/ScaledEnvelope",
"Tone/component/Filter", "Tone/instrument/Instrument"],
2014-08-20 20:52:14 +00:00
function(Tone){
"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 a filter envelope.
2014-08-20 20:52:14 +00:00
*
* @constructor
2014-09-20 22:57:44 +00:00
* @extends {Tone.Instrument}
* @param {string|object} url the url of the audio file
2014-09-30 04:29:02 +00:00
* @param {function} onload called when the sample has been loaded
2014-08-20 20:52:14 +00:00
*/
Tone.Sampler = function(){
2014-09-20 22:57:44 +00:00
Tone.Instrument.call(this);
2014-09-30 04:29:02 +00:00
var options = this.optionsObject(arguments, ["url", "onload"], Tone.Sampler.defaults);
2014-08-20 20:52:14 +00:00
/**
* the sample player
* @type {Tone.Player}
*/
this.player = new Tone.Player({
url : options.url,
onload : options.onload,
retrigger : true
});
this.player.set(options.player);
2014-08-20 20:52:14 +00:00
/**
* the amplitude envelope
* @type {Tone.Envelope}
*/
this.envelope = new Tone.AmplitudeEnvelope(options.envelope);
2014-09-12 00:36:07 +00:00
2014-08-20 20:52:14 +00:00
/**
* the filter envelope
* @type {Tone.Envelope}
*/
this.filterEnvelope = new Tone.ScaledEnvelope(options.filterEnvelope);
2014-08-20 20:52:14 +00:00
/**
* the filter
* @type {BiquadFilterNode}
*/
this.filter = new Tone.Filter(options.filter);
2014-08-20 20:52:14 +00:00
//connections
2014-12-01 02:32:09 +00:00
this.player.chain(this.filter, this.envelope, this.output);
2014-08-20 20:52:14 +00:00
this.filterEnvelope.connect(this.filter.frequency);
};
2014-09-20 22:57:44 +00:00
Tone.extend(Tone.Sampler, Tone.Instrument);
2014-08-20 20:52:14 +00:00
/**
* the default parameters
*
* @static
*/
Tone.Sampler.defaults = {
2014-09-30 04:29:02 +00:00
"url" : undefined,
"onload" : function(){},
"player" : {
"loop" : false,
},
"envelope" : {
"attack" : 0.001,
"decay" : 0,
"sustain" : 1,
"release" : 0.1
},
"filterEnvelope" : {
"attack" : 0.001,
"decay" : 0.001,
"sustain" : 1,
"release" : 0.5,
2014-09-12 00:36:07 +00:00
"min" : 20,
"max" : 20000,
"exponent" : 2,
},
"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.player)) this.player.set(params.player);
if (!this.isUndef(params.filter)) this.filter.set(params.filter);
};
2014-08-20 20:52:14 +00:00
/**
* start the sample
*
* @param {number} [note=0] the interval in the sample should be played at 0 = no change
* @param {Tone.Time} [time=now] the time when the note should start
* @param {number} [velocity=1] the velocity of the note
2014-08-20 20:52:14 +00:00
*/
Tone.Sampler.prototype.triggerAttack = function(note, time, velocity){
time = this.toSeconds(time);
note = this.defaultArg(note, 0);
this.player.setPlaybackRate(this.intervalToFrequencyRatio(note), time);
this.player.start(time, 0);
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
2014-08-20 20:52:14 +00:00
*/
Tone.Sampler.prototype.triggerRelease = function(time){
time = this.toSeconds(time);
2014-08-20 20:52:14 +00:00
this.filterEnvelope.triggerRelease(time);
this.envelope.triggerRelease(time);
this.player.stop(this.toSeconds(this.envelope.release) + time);
2014-08-20 20:52:14 +00:00
};
/**
* clean up
*/
Tone.Sampler.prototype.dispose = function(){
2014-09-20 22:57:44 +00:00
Tone.Instrument.prototype.dispose.call(this);
2014-08-20 20:52:14 +00:00
this.player.dispose();
this.filterEnvelope.dispose();
this.envelope.dispose();
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;
});