Tone.js/Tone/core/Master.js

165 lines
4.3 KiB
JavaScript
Raw Normal View History

define(["Tone/core/Tone", "Tone/component/Volume", "Tone/core/Context"], 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.
2015-06-20 19:50:57 +00:00
* It also gives you the ability to apply master effects to your application.
* <br><br>
* Like Tone.Transport, A single Tone.Master is created
* on initialization and you do not need to explicitly construct one.
2014-06-18 19:39:10 +00:00
*
* @constructor
* @extends {Tone}
2015-06-20 19:50:57 +00:00
* @singleton
* @example
* //the audio will go from the oscillator to the speakers
* oscillator.connect(Tone.Master);
* //a convenience for connecting to the master output is also provided:
* oscillator.toMaster();
* //the above two examples are equivalent.
2014-06-18 19:39:10 +00:00
*/
Tone.Master = function(){
2016-09-20 03:02:42 +00:00
Tone.call(this);
Tone.getContext(function(){
this.createInsOuts(1, 0);
/**
* The private volume node
* @type {Tone.Volume}
* @private
*/
this._volume = this.output = new Tone.Volume();
/**
* The volume of the master output.
* @type {Decibels}
* @signal
*/
this.volume = this._volume.volume;
this._readOnly("volume");
//connections
this.input.chain(this.output, this.context.destination);
}.bind(this));
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-06-20 19:50:57 +00:00
* Mute the output.
2015-05-23 22:15:39 +00:00
* @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(){
2016-05-14 21:34:23 +00:00
return this._volume.mute;
2015-05-23 22:15:39 +00:00
},
set : function(mute){
2016-05-14 21:34:23 +00:00
this._volume.mute = mute;
}
2015-05-23 22:15:39 +00:00
});
2014-06-21 17:06:37 +00:00
/**
2015-06-20 19:50:57 +00:00
* Add a master effects chain. NOTE: this will disconnect any nodes which were previously
* chained in the master effects chain.
2017-06-21 14:22:00 +00:00
* @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);
};
2015-08-16 18:23:14 +00:00
/**
* Clean up
* @return {Tone.Master} this
*/
Tone.Master.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
this._writable("volume");
this._volume.dispose();
this._volume = null;
2015-08-16 18:23:14 +00:00
this.volume = null;
};
///////////////////////////////////////////////////////////////////////////
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
};
if (window.AudioNode){
// Also augment AudioNode's prototype to include toMaster as a convenience
AudioNode.prototype.toMaster = function(){
this.connect(Tone.Master);
return this;
};
}
/**
2014-07-30 17:55:24 +00:00
* initialize the module and listen for new audio contexts
*/
var MasterConstructor = Tone.Master;
Tone.Master = new MasterConstructor();
Tone.Context.on("init", function(context){
// if it already exists, just restore it
if (context.Master instanceof MasterConstructor){
Tone.Master = context.Master;
2014-10-16 04:49:31 +00:00
} else {
Tone.Master = new MasterConstructor();
2014-10-16 04:49:31 +00:00
}
context.Master = Tone.Master;
2014-07-30 17:55:24 +00:00
});
Tone.Context.on("close", function(context){
if (context.Master instanceof MasterConstructor){
context.Master.dispose();
}
});
return Tone.Master;
2014-06-18 19:46:31 +00:00
});