Tone.js/Tone/core/Master.js

150 lines
3.8 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
2015-06-14 01:54:20 +00:00
* AudioDestinationNode (aka your speakers).
* It provides useful conveniences such as the ability
* to set the volume and mute the entire application.
* It also gives you the ability to apply master effects like compression,
2015-02-26 16:26:23 +00:00
* limiting or effects to your application. <br><br>
2015-06-14 01:54:20 +00:00
* Like Tone.Transport, Tone.Master is created for you
2015-02-26 16:26:23 +00:00
* 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
2015-06-13 23:50:39 +00:00
* @type {Decibels}
* @signal
*/
this.volume = new Tone.Signal(this.output.gain, Tone.Type.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
2015-06-14 01:54:20 +00:00
* and the Master will be routed through it.
* @return {Tone.Master} this
2015-05-23 22:15:39 +00:00
* @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
/**
2015-06-14 03:56:32 +00:00
* Connect 'this' to the master output. Shorthand for this.connect(Tone.Master)
* @returns {Tone} this
2015-06-14 01:54:20 +00:00
* @example
* //connect an oscillator to the master output
* var osc = new Tone.Oscillator().toMaster();
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
});