2014-04-06 20:51:30 +00:00
|
|
|
define(["Tone/core/Tone"], function(Tone){
|
2014-09-04 04:41:40 +00:00
|
|
|
|
|
|
|
"use strict";
|
2014-06-19 02:35:31 +00:00
|
|
|
|
2014-06-18 19:39:10 +00:00
|
|
|
/**
|
2014-09-05 15:32:33 +00:00
|
|
|
* @class A single master output.
|
|
|
|
* adds toMaster to Tone
|
2014-06-18 19:39:10 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone}
|
|
|
|
*/
|
2014-04-05 22:05:42 +00:00
|
|
|
var Master = function(){
|
|
|
|
//extend audio unit
|
|
|
|
Tone.call(this);
|
|
|
|
|
2014-06-18 19:39:10 +00:00
|
|
|
/**
|
|
|
|
* put a hard limiter on the output so we don't blow any eardrums
|
|
|
|
*
|
|
|
|
* @type {DynamicsCompressorNode}
|
|
|
|
*/
|
2014-04-05 22:05:42 +00:00
|
|
|
this.limiter = this.context.createDynamicsCompressor();
|
|
|
|
this.limiter.threshold.value = 0;
|
|
|
|
this.limiter.ratio.value = 20;
|
2014-06-18 19:39:10 +00:00
|
|
|
//connect it up
|
2014-04-05 22:05:42 +00:00
|
|
|
this.chain(this.input, this.limiter, this.output, this.context.destination);
|
2014-06-18 19:39:10 +00:00
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
|
|
|
Tone.extend(Master);
|
|
|
|
|
2014-06-19 02:35:31 +00:00
|
|
|
/**
|
|
|
|
* mute the output
|
|
|
|
* @param {boolean} muted
|
|
|
|
*/
|
|
|
|
Master.prototype.mute = function(muted){
|
|
|
|
muted = this.defaultArg(muted, true);
|
|
|
|
if (muted){
|
|
|
|
this.output.gain.value = 0;
|
|
|
|
} else {
|
|
|
|
this.output.gain.value = 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-21 17:06:37 +00:00
|
|
|
/**
|
|
|
|
* @param {number} value
|
|
|
|
* @param {Tone.Time=} fadeTime (optional) time it takes to reach the value
|
|
|
|
*/
|
|
|
|
Master.prototype.setVolume = function(value, fadeTime){
|
|
|
|
var now = this.now();
|
|
|
|
if (fadeTime){
|
|
|
|
var currentVolume = this.output.gain.value;
|
|
|
|
this.output.gain.cancelScheduledValues(now);
|
|
|
|
this.output.gain.setValueAtTime(currentVolume, now);
|
2014-07-30 17:55:24 +00:00
|
|
|
this.output.gain.linearRampToValueAtTime(value, now + this.toSeconds(fadeTime));
|
2014-06-21 17:06:37 +00:00
|
|
|
} else {
|
|
|
|
this.output.gain.setValueAtTime(value, now);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2014-06-18 19:39:10 +00:00
|
|
|
// AUGMENT TONE's PROTOTYPE
|
2014-04-05 22:05:42 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-06-18 19:39:10 +00:00
|
|
|
/**
|
|
|
|
* connect 'this' to the master output
|
|
|
|
*/
|
2014-04-05 22:05:42 +00:00
|
|
|
Tone.prototype.toMaster = function(){
|
|
|
|
this.connect(Tone.Master);
|
2014-06-18 19:39:10 +00:00
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
2014-06-18 19:39:10 +00:00
|
|
|
/**
|
|
|
|
* Also augment AudioNode's prototype to include toMaster
|
|
|
|
* as a convenience
|
|
|
|
*/
|
2014-04-05 22:05:42 +00:00
|
|
|
AudioNode.prototype.toMaster = function(){
|
|
|
|
this.connect(Tone.Master);
|
2014-06-18 19:39:10 +00:00
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
2014-09-04 23:04:16 +00:00
|
|
|
Tone.Master = new Master();
|
|
|
|
|
2014-06-19 02:35:31 +00:00
|
|
|
/**
|
2014-07-30 17:55:24 +00:00
|
|
|
* initialize the module and listen for new audio contexts
|
2014-06-19 02:35:31 +00:00
|
|
|
*/
|
2014-07-30 17:55:24 +00:00
|
|
|
Tone._initAudioContext(function(){
|
|
|
|
//a single master output
|
2014-09-04 23:04:16 +00:00
|
|
|
Master.call(Tone.Master);
|
2014-07-30 17:55:24 +00:00
|
|
|
});
|
2014-04-11 23:17:01 +00:00
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
return Tone.Master;
|
2014-06-18 19:46:31 +00:00
|
|
|
});
|