Tone.js/Tone/core/Master.js

147 lines
3.7 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/signal/Signal"], function(Tone){
"use strict";
2014-06-18 19:39:10 +00:00
/**
* @class A single master output which is connected to the
* AudioDestinationNode. It provides useful conveniences
* such as the ability to set the global volume and mute
* the entire application. Additionally, it accepts
* a master send/receive for adding final compression,
2015-02-26 16:26:23 +00:00
* limiting or effects to your application. <br><br>
* Like the Transport, the Master output is created for you
* on initialization. It does not need to be created.
2014-06-18 19:39:10 +00:00
*
* @constructor
* @extends {Tone}
*/
Tone.Master = function(){
Tone.call(this);
/**
* the unmuted volume
* @type {number}
* @private
*/
this._unmutedVolume = 1;
/**
* if the master is muted
* @type {boolean}
* @private
*/
this._muted = false;
/**
* the volume of the output in decibels
* @type {Tone.Signal}
*/
this.volume = new Tone.Signal(this.output.gain, Tone.Signal.Units.Decibels);
2014-10-01 18:47:45 +00:00
//connections
this.input.chain(this.output, this.context.destination);
2014-06-18 19:39:10 +00:00
};
Tone.extend(Tone.Master);
/**
2015-05-23 22:15:39 +00:00
* @type {Object}
* @const
*/
2015-05-23 22:15:39 +00:00
Tone.Master.defaults = {
"volume" : 0,
"mute" : false
};
2014-06-21 17:06:37 +00:00
/**
2015-05-23 22:15:39 +00:00
* Set `mute` to true to stop all output
* @memberOf Tone.Master#
* @type {boolean}
* @name mute
* @example
* //mute the output
* Tone.Master.mute = true;
2014-06-21 17:06:37 +00:00
*/
2015-05-23 22:15:39 +00:00
Object.defineProperty(Tone.Master.prototype, "mute", {
get : function(){
return this._muted;
},
set : function(mute){
this._muted = mute;
if (!this._muted && mute){
this._unmutedVolume = this.volume.value;
//maybe it should ramp here?
this.volume.value = -Infinity;
} else if (this._muted && !mute){
this.volume.value = this._unmutedVolume;
}
}
2015-05-23 22:15:39 +00:00
});
2014-06-21 17:06:37 +00:00
/**
2015-05-23 22:15:39 +00:00
* Add a master effects chain. This will disconnect any nodes which were previously
* chained.
* @param {AudioNode|Tone...} args All arguments will be connected in a row
* and the Master will be routed through it
* @return {Tone.Master} `this`
* @example
* //some overall compression to keep the levels in check
* var masterCompressor = new Tone.Compressor({
* "threshold" : -6,
* "ratio" : 3,
* "attack" : 0.5,
* "release" : 0.1
* });
* //give a little boost to the lows
* var lowBump = new Tone.Filter(200, "lowshelf");
* //route everything through the filter
* //and compressor before going to the speakers
* Tone.Master.chain(lowBump, masterCompressor);
*/
2015-05-23 22:15:39 +00:00
Tone.Master.prototype.chain = function(){
this.input.disconnect();
2015-05-23 22:15:39 +00:00
this.input.chain.apply(this.input, arguments);
arguments[arguments.length - 1].connect(this.output);
};
///////////////////////////////////////////////////////////////////////////
2014-06-18 19:39:10 +00:00
// AUGMENT TONE's PROTOTYPE
///////////////////////////////////////////////////////////////////////////
2014-06-18 19:39:10 +00:00
/**
* connect 'this' to the master output
* defined in "Tone/core/Master"
* @returns {Tone} `this`
2014-06-18 19:39:10 +00:00
*/
Tone.prototype.toMaster = function(){
this.connect(Tone.Master);
return this;
2014-06-18 19:39:10 +00:00
};
2014-06-18 19:39:10 +00:00
/**
* Also augment AudioNode's prototype to include toMaster
* as a convenience
* @returns {AudioNode} `this`
2014-06-18 19:39:10 +00:00
*/
AudioNode.prototype.toMaster = function(){
this.connect(Tone.Master);
return this;
2014-06-18 19:39:10 +00:00
};
var MasterConstructor = Tone.Master;
/**
2014-07-30 17:55:24 +00:00
* initialize the module and listen for new audio contexts
*/
2014-07-30 17:55:24 +00:00
Tone._initAudioContext(function(){
2014-10-16 04:49:31 +00:00
//a single master output
if (!Tone.prototype.isUndef(Tone.Master)){
2014-10-19 20:08:40 +00:00
Tone.Master = new MasterConstructor();
2014-10-16 04:49:31 +00:00
} else {
MasterConstructor.prototype.dispose.call(Tone.Master);
2014-10-16 04:49:31 +00:00
MasterConstructor.call(Tone.Master);
}
2014-07-30 17:55:24 +00:00
});
return Tone.Master;
2014-06-18 19:46:31 +00:00
});