2017-02-19 16:50:15 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/component/Volume", "Tone/core/Context"], 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
|
|
|
/**
|
2015-02-20 05:58:29 +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
|
|
|
*/
|
2014-09-11 17:00:59 +00:00
|
|
|
Tone.Master = function(){
|
2016-09-20 03:02:42 +00:00
|
|
|
|
2017-04-25 03:23:15 +00:00
|
|
|
Tone.call(this);
|
2017-08-14 01:37:32 +00:00
|
|
|
Tone.getContext(function(){
|
|
|
|
this.createInsOuts(1, 0);
|
2015-02-20 05:58:29 +00:00
|
|
|
|
2017-08-14 01:37:32 +00:00
|
|
|
/**
|
|
|
|
* The private volume node
|
|
|
|
* @type {Tone.Volume}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._volume = this.output = new Tone.Volume();
|
2015-11-16 16:10:12 +00:00
|
|
|
|
2017-08-14 01:37:32 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
2014-09-11 17:00:59 +00:00
|
|
|
Tone.extend(Tone.Master);
|
2014-04-05 22:05:42 +00:00
|
|
|
|
2014-06-19 02:35:31 +00:00
|
|
|
/**
|
2015-05-23 22:15:39 +00:00
|
|
|
* @type {Object}
|
|
|
|
* @const
|
2014-06-19 02:35:31 +00:00
|
|
|
*/
|
2015-05-23 22:15:39 +00:00
|
|
|
Tone.Master.defaults = {
|
|
|
|
"volume" : 0,
|
|
|
|
"mute" : false
|
2014-06-19 02:35:31 +00:00
|
|
|
};
|
|
|
|
|
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-04-24 21:45:32 +00:00
|
|
|
}
|
2015-05-23 22:15:39 +00:00
|
|
|
});
|
2014-06-21 17:06:37 +00:00
|
|
|
|
2014-12-19 21:32:59 +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.
|
2015-06-14 00:54:29 +00:00
|
|
|
* @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);
|
2014-12-19 21:32:59 +00:00
|
|
|
*/
|
2015-05-23 22:15:39 +00:00
|
|
|
Tone.Master.prototype.chain = function(){
|
2014-12-19 21:32:59 +00:00
|
|
|
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-12-19 21:32:59 +00:00
|
|
|
};
|
|
|
|
|
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");
|
2015-11-16 16:10:12 +00:00
|
|
|
this._volume.dispose();
|
|
|
|
this._volume = null;
|
2015-08-16 18:23:14 +00:00
|
|
|
this.volume = null;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
/**
|
2015-06-14 03:56:32 +00:00
|
|
|
* Connect 'this' to the master output. Shorthand for this.connect(Tone.Master)
|
2015-06-14 00:54:29 +00:00
|
|
|
* @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
|
|
|
*/
|
2014-04-05 22:05:42 +00:00
|
|
|
Tone.prototype.toMaster = function(){
|
|
|
|
this.connect(Tone.Master);
|
2015-01-06 03:46:19 +00:00
|
|
|
return this;
|
2014-06-18 19:39:10 +00:00
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
2017-07-27 22:06:05 +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-04-05 22:05:42 +00:00
|
|
|
|
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
|
|
|
*/
|
2017-02-19 16:50:15 +00:00
|
|
|
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 {
|
2017-02-19 16:50:15 +00:00
|
|
|
Tone.Master = new MasterConstructor();
|
2014-10-16 04:49:31 +00:00
|
|
|
}
|
2017-02-19 16:50:15 +00:00
|
|
|
context.Master = Tone.Master;
|
2014-07-30 17:55:24 +00:00
|
|
|
});
|
2014-04-11 23:17:01 +00:00
|
|
|
|
2017-06-19 21:51:59 +00:00
|
|
|
Tone.Context.on("close", function(context){
|
|
|
|
if (context.Master instanceof MasterConstructor){
|
|
|
|
context.Master.dispose();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
return Tone.Master;
|
2014-06-18 19:46:31 +00:00
|
|
|
});
|