Tone.js/Tone/instrument/Sampler.js

279 lines
6.5 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. Nested
2015-02-28 04:24:51 +00:00
* lists will be flattened.
2014-08-20 20:52:14 +00:00
*
* @constructor
2014-09-20 22:57:44 +00:00
* @extends {Tone.Instrument}
* @param {Object|string} urls the urls of the audio file
2015-06-14 03:15:57 +00:00
* @param {Object} [options] the options object for the synth
2015-02-28 04:24:51 +00:00
* @example
2015-06-14 04:32:17 +00:00
* var sampler = new Sampler({
* A : {
* 1 : {"./audio/casio/A1.mp3",
* 2 : "./audio/casio/A2.mp3",
* },
* "B.1" : "./audio/casio/B1.mp3",
* });
* //...once samples have loaded
* sampler.triggerAttack("A.1", time, velocity);
2014-08-20 20:52:14 +00:00
*/
Tone.Sampler = function(urls, options){
2014-09-20 22:57:44 +00:00
Tone.Instrument.call(this);
options = this.defaultArg(options, Tone.Sampler.defaults);
2014-08-20 20:52:14 +00:00
/**
* the sample player
* @type {Tone.Player}
*/
this.player = new Tone.Player(options.player);
2015-02-24 17:02:56 +00:00
this.player.retrigger = true;
/**
* the buffers
* @type {Object<Tone.Buffer>}
* @private
*/
this._buffers = {};
2014-08-20 20:52:14 +00:00
/**
2015-02-10 16:40:04 +00:00
* The amplitude envelope.
2014-08-20 20:52:14 +00:00
* @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
/**
2015-02-10 16:40:04 +00:00
* The filter envelope.
2014-08-20 20:52:14 +00:00
* @type {Tone.Envelope}
*/
this.filterEnvelope = new Tone.ScaledEnvelope(options.filterEnvelope);
2014-08-20 20:52:14 +00:00
/**
2015-02-10 16:40:04 +00:00
* The name of the current sample.
* @type {string}
2015-06-14 03:15:57 +00:00
* @private
*/
2015-02-10 16:40:04 +00:00
this._sample = options.sample;
2015-02-10 21:33:37 +00:00
/**
* the private reference to the pitch
* @type {number}
* @private
*/
this._pitch = options.pitch;
2014-08-20 20:52:14 +00:00
/**
2015-02-10 16:40:04 +00:00
* The filter.
2014-08-20 20:52:14 +00:00
* @type {BiquadFilterNode}
*/
this.filter = new Tone.Filter(options.filter);
2014-08-20 20:52:14 +00:00
2015-02-10 21:33:37 +00:00
//connections / setup
this._loadBuffers(urls);
2015-02-10 21:33:37 +00:00
this.pitch = options.pitch;
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);
2015-04-18 14:54:08 +00:00
this._readOnly(["player", "filterEnvelope", "envelope", "filter"]);
2014-08-20 20:52:14 +00:00
};
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 = {
"sample" : 0,
2015-02-10 21:33:37 +00:00
"pitch" : 0,
"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"
}
};
/**
* load the buffers
* @param {Object} urls the urls
* @private
*/
Tone.Sampler.prototype._loadBuffers = function(urls){
if (typeof urls === "string"){
2015-02-20 06:01:03 +00:00
this._buffers["0"] = new Tone.Buffer(urls, function(){
this.sample = "0";
}.bind(this));
} else {
urls = this._flattenUrls(urls);
for (var buffName in urls){
2015-02-10 16:40:04 +00:00
this._sample = buffName;
var urlString = urls[buffName];
2015-02-10 16:40:04 +00:00
this._buffers[buffName] = new Tone.Buffer(urlString);
}
}
};
/**
* flatten an object into a single depth object
* https://gist.github.com/penguinboy/762197
* @param {Object} ob
* @return {Object}
* @private
*/
Tone.Sampler.prototype._flattenUrls = function(ob) {
var toReturn = {};
for (var i in ob) {
if (!ob.hasOwnProperty(i)) continue;
if ((typeof ob[i]) == "object") {
var flatObject = this._flattenUrls(ob[i]);
for (var x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
toReturn[i + "." + x] = flatObject[x];
}
} else {
toReturn[i] = ob[i];
}
}
return toReturn;
};
/**
* start the sample.
* @param {string=} sample the name of the samle to trigger, defaults to
* the last sample used
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time when the note should start
* @param {number} [velocity=1] the velocity of the note
* @returns {Tone.Sampler} this
2014-08-20 20:52:14 +00:00
*/
Tone.Sampler.prototype.triggerAttack = function(name, time, velocity){
time = this.toSeconds(time);
if (name){
2015-02-20 06:01:03 +00:00
this.sample = name;
}
2015-05-13 03:48:13 +00:00
this.player.start(time);
2014-09-04 02:35:03 +00:00
this.envelope.triggerAttack(time, velocity);
this.filterEnvelope.triggerAttack(time);
return this;
2014-08-20 20:52:14 +00:00
};
/**
* start the release portion of the sample
*
2015-06-14 00:20:36 +00:00
* @param {Time} [time=now] the time when the note should release
* @returns {Tone.Sampler} this
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);
return this;
2014-08-20 20:52:14 +00:00
};
2015-02-10 16:40:04 +00:00
/**
2015-02-10 21:33:37 +00:00
* The name of the sample to trigger.
* @memberOf Tone.Sampler#
* @type {number|string}
* @name sample
2015-02-10 16:40:04 +00:00
*/
Object.defineProperty(Tone.Sampler.prototype, "sample", {
get : function(){
return this._sample;
},
set : function(name){
if (this._buffers.hasOwnProperty(name)){
this._sample = name;
this.player.buffer = this._buffers[name];
} else {
throw new Error("Sampler does not have a sample named "+name);
}
}
});
2015-05-13 03:48:13 +00:00
/**
* The direction the buffer should play in
* @memberOf Tone.Sampler#
* @type {boolean}
* @name reverse
*/
Object.defineProperty(Tone.Sampler.prototype, "reverse", {
get : function(){
for (var i in this._buffers){
return this._buffers[i].reverse;
}
},
set : function(rev){
for (var i in this._buffers){
this._buffers[i].reverse = rev;
}
}
});
2015-02-10 21:33:37 +00:00
/**
* Repitch the sampled note by some interval (measured
* in semi-tones).
* @memberOf Tone.Sampler#
* @type {number}
* @name pitch
2015-02-28 04:24:51 +00:00
* @example
* sampler.pitch = -12; //down one octave
* sampler.pitch = 7; //up a fifth
2015-02-10 21:33:37 +00:00
*/
Object.defineProperty(Tone.Sampler.prototype, "pitch", {
get : function(){
return this._pitch;
},
set : function(interval){
this._pitch = interval;
this.player.playbackRate = this.intervalToFrequencyRatio(interval);
}
});
2014-08-20 20:52:14 +00:00
/**
* clean up
* @returns {Tone.Sampler} this
2014-08-20 20:52:14 +00:00
*/
Tone.Sampler.prototype.dispose = function(){
2014-09-20 22:57:44 +00:00
Tone.Instrument.prototype.dispose.call(this);
2015-04-18 14:54:08 +00:00
this._writable(["player", "filterEnvelope", "envelope", "filter"]);
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;
for (var sample in this._buffers){
this._buffers[sample].dispose();
this._buffers[sample] = null;
}
this._buffers = null;
return this;
2014-08-20 20:52:14 +00:00
};
return Tone.Sampler;
});