Tone.js/Tone/instrument/Sampler.js

156 lines
3.8 KiB
JavaScript
Raw Normal View History

2016-07-07 02:32:34 +00:00
define(["Tone/core/Tone", "Tone/source/Player", "Tone/component/AmplitudeEnvelope", "Tone/instrument/Instrument"],
2014-08-20 20:52:14 +00:00
function(Tone){
"use strict";
2014-08-20 20:52:14 +00:00
/**
2016-07-07 02:32:34 +00:00
* @class Sampler wraps Tone.Player in an AmplitudeEnvelope.
2014-08-20 20:52:14 +00:00
*
* @constructor
2014-09-20 22:57:44 +00:00
* @extends {Tone.Instrument}
2016-07-07 02:32:34 +00:00
* @param {String} url the url of the audio file
* @param {Function=} onload The callback to invoke when the sample is loaded.
2015-02-28 04:24:51 +00:00
* @example
2016-07-07 02:32:34 +00:00
* var sampler = new Sampler("./audio/casio/A1.mp3", function(){
* //repitch the sample down a half step
* sampler.triggerAttack(-1);
2015-06-15 15:27:13 +00:00
* }).toMaster();
2014-08-20 20:52:14 +00:00
*/
2016-07-07 02:32:34 +00:00
Tone.Sampler = function(){
2016-07-07 02:32:34 +00:00
var options = this.optionsObject(arguments, ["url", "onload"], Tone.Sampler.defaults);
Tone.Instrument.call(this, options);
2014-08-20 20:52:14 +00:00
/**
2015-06-20 22:03:49 +00:00
* The sample player.
2014-08-20 20:52:14 +00:00
* @type {Tone.Player}
*/
2016-07-07 02:32:34 +00:00
this.player = new Tone.Player(options.url, options.onload);
2015-02-24 17:02:56 +00:00
this.player.retrigger = true;
2014-08-20 20:52:14 +00:00
/**
2015-02-10 16:40:04 +00:00
* The amplitude envelope.
2015-06-15 15:27:13 +00:00
* @type {Tone.AmplitudeEnvelope}
2014-08-20 20:52:14 +00:00
*/
this.envelope = new Tone.AmplitudeEnvelope(options.envelope);
2014-09-12 00:36:07 +00:00
2016-07-07 02:32:34 +00:00
this.player.chain(this.envelope, this.output);
this._readOnly(["player", "envelope"]);
this.loop = options.loop;
this.reverse = options.reverse;
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 = {
2016-07-07 02:32:34 +00:00
"onload" : Tone.noOp,
"loop" : false,
"reverse" : false,
"envelope" : {
"attack" : 0.001,
"decay" : 0,
"sustain" : 1,
"release" : 0.1
}
};
/**
2016-07-07 02:32:34 +00:00
* Trigger the start of the sample.
* @param {Interval} [pitch=0] The amount the sample should
* be repitched.
2015-06-20 22:03:49 +00:00
* @param {Time} [time=now] The time when the sample should start
2016-07-07 02:32:34 +00:00
* @param {NormalRange} [velocity=1] The velocity of the note
* @returns {Tone.Sampler} this
2015-06-20 22:03:49 +00:00
* @example
2016-07-07 02:32:34 +00:00
* sampler.triggerAttack(0, "+0.1", 0.5);
2014-08-20 20:52:14 +00:00
*/
2016-07-07 02:32:34 +00:00
Tone.Sampler.prototype.triggerAttack = function(pitch, time, velocity){
time = this.toSeconds(time);
2016-07-07 02:32:34 +00:00
pitch = this.defaultArg(pitch, 0);
this.player.playbackRate = this.intervalToFrequencyRatio(pitch);
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);
return this;
2014-08-20 20:52:14 +00:00
};
/**
2015-06-20 22:03:49 +00:00
* Start the release portion of the sample. Will stop the sample once the
* envelope has fully released.
2014-08-20 20:52:14 +00:00
*
2015-06-20 22:03:49 +00:00
* @param {Time} [time=now] The time when the note should release
* @returns {Tone.Sampler} this
2015-06-20 22:03:49 +00:00
* @example
* sampler.triggerRelease();
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.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
/**
2016-07-07 02:32:34 +00:00
* If the output sample should loop or not.
2015-02-10 21:33:37 +00:00
* @memberOf Tone.Sampler#
* @type {number|string}
2016-07-07 02:32:34 +00:00
* @name loop
2015-02-10 16:40:04 +00:00
*/
2016-07-07 02:32:34 +00:00
Object.defineProperty(Tone.Sampler.prototype, "loop", {
2015-02-10 16:40:04 +00:00
get : function(){
2016-07-07 02:32:34 +00:00
return this.player.loop;
2015-02-10 16:40:04 +00:00
},
2016-07-07 02:32:34 +00:00
set : function(loop){
this.player.loop = loop;
2015-02-10 16:40:04 +00:00
}
});
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(){
2016-07-07 02:32:34 +00:00
return this.player.reverse;
2015-05-13 03:48:13 +00:00
},
set : function(rev){
2016-07-07 02:32:34 +00:00
this.player.reverse = rev;
2015-02-10 21:33:37 +00:00
}
});
2016-07-20 19:42:55 +00:00
/**
* The buffer to play.
* @memberOf Tone.Sampler#
* @type {Tone.Buffer}
* @name buffer
*/
Object.defineProperty(Tone.Sampler.prototype, "buffer", {
get : function(){
return this.player.buffer;
},
set : function(buff){
this.player.buffer = buff;
}
});
2014-08-20 20:52:14 +00:00
/**
2015-06-20 22:03:49 +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);
2016-07-07 02:32:34 +00:00
this._writable(["player", "envelope"]);
2014-08-20 20:52:14 +00:00
this.player.dispose();
this.player = null;
2016-07-07 02:32:34 +00:00
this.envelope.dispose();
2014-08-20 20:52:14 +00:00
this.envelope = null;
return this;
2014-08-20 20:52:14 +00:00
};
return Tone.Sampler;
2016-07-07 02:32:34 +00:00
});