2015-02-02 01:38:06 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/source/Source", "Tone/source/Oscillator",
|
|
|
|
"Tone/source/PulseOscillator", "Tone/source/PWMOscillator"],
|
2014-09-30 03:44:59 +00:00
|
|
|
function(Tone){
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
2014-12-04 02:38:24 +00:00
|
|
|
* @class OmniOscillator aggregates Tone.Oscillator, Tone.PulseOscillator,
|
|
|
|
* and Tone.PWMOscillator which allows it to have the types:
|
|
|
|
* sine, square, triangle, sawtooth, pulse or pwm.
|
2014-09-30 03:44:59 +00:00
|
|
|
*
|
2014-10-03 20:07:30 +00:00
|
|
|
* @extends {Tone.Oscillator}
|
2014-09-30 03:44:59 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {frequency} frequency frequency of the oscillator (meaningless for noise types)
|
|
|
|
* @param {string} type the type of the oscillator
|
|
|
|
*/
|
|
|
|
Tone.OmniOscillator = function(){
|
|
|
|
var options = this.optionsObject(arguments, ["frequency", "type"], Tone.OmniOscillator.defaults);
|
2015-02-02 01:38:06 +00:00
|
|
|
Tone.Source.call(this, options);
|
2014-09-30 03:44:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the frequency control
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
*/
|
2015-02-04 04:48:47 +00:00
|
|
|
this.frequency = new Tone.Signal(options.frequency, Tone.Signal.Units.Frequency);
|
2014-09-30 03:44:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the detune control
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
*/
|
|
|
|
this.detune = new Tone.Signal(options.detune);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the type of the oscillator source
|
|
|
|
* @type {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._sourceType = undefined;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the oscillator
|
|
|
|
* @type {Tone.Oscillator|Tone.PWMOscillator|Tone.PulseOscillator}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._oscillator = null;
|
|
|
|
|
|
|
|
//set the oscillator
|
2015-02-04 04:48:47 +00:00
|
|
|
this.type = options.type;
|
2014-09-30 03:44:59 +00:00
|
|
|
};
|
|
|
|
|
2014-10-03 20:07:30 +00:00
|
|
|
Tone.extend(Tone.OmniOscillator, Tone.Oscillator);
|
2014-09-30 03:44:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* default values
|
|
|
|
* @static
|
|
|
|
* @type {Object}
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
Tone.OmniOscillator.defaults = {
|
|
|
|
"frequency" : 440,
|
|
|
|
"detune" : 0,
|
|
|
|
"type" : "sine",
|
|
|
|
"width" : 0.4, //only applies if the oscillator is set to "pulse",
|
|
|
|
"modulationFrequency" : 0.4, //only applies if the oscillator is set to "pwm",
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the oscillator
|
|
|
|
* @param {Tone.Time} [time=now] the time to start the oscillator
|
2015-02-02 02:32:07 +00:00
|
|
|
* @private
|
2014-09-30 03:44:59 +00:00
|
|
|
*/
|
2015-02-02 02:32:07 +00:00
|
|
|
Tone.OmniOscillator.prototype._start = function(time){
|
|
|
|
this._oscillator.start(time);
|
2014-09-30 03:44:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the oscillator
|
|
|
|
* @param {Tone.Time} [time=now] the time to start the oscillator
|
2015-02-02 02:32:07 +00:00
|
|
|
* @private
|
2014-09-30 03:44:59 +00:00
|
|
|
*/
|
2015-02-02 02:32:07 +00:00
|
|
|
Tone.OmniOscillator.prototype._stop = function(time){
|
|
|
|
this._oscillator.stop(time);
|
2014-09-30 03:44:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-02-04 04:48:47 +00:00
|
|
|
* The type of the oscillator. sine, square, triangle, sawtooth, pwm, or pulse.
|
|
|
|
*
|
|
|
|
* @memberOf Tone.OmniOscillator#
|
|
|
|
* @type {string}
|
|
|
|
* @name type
|
2014-09-30 03:44:59 +00:00
|
|
|
*/
|
2015-02-04 04:48:47 +00:00
|
|
|
Object.defineProperty(Tone.OmniOscillator.prototype, "type", {
|
|
|
|
get : function(){
|
|
|
|
return this._oscillator.type;
|
|
|
|
},
|
|
|
|
set : function(type){
|
|
|
|
if (type === "sine" || type === "square" || type === "triangle" || type === "sawtooth"){
|
|
|
|
if (this._sourceType !== OmniOscType.Oscillator){
|
|
|
|
this._sourceType = OmniOscType.Oscillator;
|
|
|
|
this._createNewOscillator(Tone.Oscillator);
|
|
|
|
}
|
|
|
|
this._oscillator.type = type;
|
|
|
|
} else if (type === "pwm"){
|
|
|
|
if (this._sourceType !== OmniOscType.PWMOscillator){
|
|
|
|
this._sourceType = OmniOscType.PWMOscillator;
|
|
|
|
this._createNewOscillator(Tone.PWMOscillator);
|
|
|
|
}
|
|
|
|
} else if (type === "pulse"){
|
|
|
|
if (this._sourceType !== OmniOscType.PulseOscillator){
|
|
|
|
this._sourceType = OmniOscType.PulseOscillator;
|
|
|
|
this._createNewOscillator(Tone.PulseOscillator);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new TypeError("Tone.OmniOscillator does not support type "+type);
|
2014-09-30 03:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-04 04:48:47 +00:00
|
|
|
});
|
2014-10-01 02:48:21 +00:00
|
|
|
|
2014-09-30 03:44:59 +00:00
|
|
|
/**
|
|
|
|
* connect the oscillator to the frequency and detune signals
|
|
|
|
* @private
|
|
|
|
*/
|
2014-10-03 05:00:37 +00:00
|
|
|
Tone.OmniOscillator.prototype._createNewOscillator = function(OscillatorConstructor){
|
2014-10-16 18:52:08 +00:00
|
|
|
//short delay to avoid clicks on the change
|
|
|
|
var now = this.now() + this.bufferTime;
|
2014-10-03 05:00:37 +00:00
|
|
|
if (this._oscillator !== null){
|
|
|
|
var oldOsc = this._oscillator;
|
2014-10-16 18:52:08 +00:00
|
|
|
oldOsc.stop(now);
|
2014-10-03 05:00:37 +00:00
|
|
|
oldOsc.onended = function(){
|
|
|
|
oldOsc.dispose();
|
|
|
|
oldOsc = null;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
this._oscillator = new OscillatorConstructor();
|
2014-09-30 03:44:59 +00:00
|
|
|
this.frequency.connect(this._oscillator.frequency);
|
|
|
|
this.detune.connect(this._oscillator.detune);
|
|
|
|
this._oscillator.connect(this.output);
|
|
|
|
if (this.state === Tone.Source.State.STARTED){
|
2014-10-16 18:52:08 +00:00
|
|
|
this._oscillator.start(now);
|
2014-09-30 03:44:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-01 20:47:31 +00:00
|
|
|
/**
|
2015-02-04 04:48:47 +00:00
|
|
|
* the phase of the oscillator in degrees
|
|
|
|
* @memberOf Tone.OmniOscillator#
|
|
|
|
* @type {number}
|
|
|
|
* @name phase
|
2015-02-01 20:47:31 +00:00
|
|
|
*/
|
2015-02-04 04:48:47 +00:00
|
|
|
Object.defineProperty(Tone.OmniOscillator.prototype, "phase", {
|
|
|
|
get : function(){
|
|
|
|
return this._oscillator.phase;
|
|
|
|
},
|
|
|
|
set : function(phase){
|
|
|
|
this._oscillator.phase = phase;
|
2014-09-30 03:44:59 +00:00
|
|
|
}
|
2015-02-04 04:48:47 +00:00
|
|
|
});
|
2014-09-30 03:44:59 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-04 04:48:47 +00:00
|
|
|
* The width Signal of the oscillator (only if the oscillator is set to pulse)
|
|
|
|
* @memberOf Tone.PulseOscillator#
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
* @name width
|
2014-09-30 03:44:59 +00:00
|
|
|
*/
|
2015-02-04 04:48:47 +00:00
|
|
|
Object.defineProperty(Tone.OmniOscillator.prototype, "width", {
|
|
|
|
get : function(){
|
|
|
|
if (this._sourceType === OmniOscType.PulseOscillator){
|
|
|
|
return this._oscillator.width;
|
2015-02-06 22:47:26 +00:00
|
|
|
}
|
2014-09-30 03:44:59 +00:00
|
|
|
}
|
2015-02-04 04:48:47 +00:00
|
|
|
});
|
2014-09-30 03:44:59 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-04 04:48:47 +00:00
|
|
|
* The modulationFrequency Signal of the oscillator
|
|
|
|
* (only if the oscillator type is set to pwm)
|
|
|
|
* @memberOf Tone.PulseOscillator#
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
* @name width
|
2014-09-30 03:44:59 +00:00
|
|
|
*/
|
2015-02-04 04:48:47 +00:00
|
|
|
Object.defineProperty(Tone.OmniOscillator.prototype, "modulationFrequency", {
|
|
|
|
get : function(){
|
|
|
|
if (this._sourceType === OmniOscType.PWMOscillator){
|
|
|
|
return this._oscillator.modulationFrequency;
|
2015-02-06 22:47:26 +00:00
|
|
|
}
|
2015-02-04 04:48:47 +00:00
|
|
|
}
|
|
|
|
});
|
2014-09-30 03:44:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up
|
2015-02-02 01:02:13 +00:00
|
|
|
* @private
|
2015-02-02 03:05:24 +00:00
|
|
|
* @return {Tone.OmniOscillator} `this`
|
2014-09-30 03:44:59 +00:00
|
|
|
*/
|
2015-02-02 03:05:24 +00:00
|
|
|
Tone.OmniOscillator.prototype.dispose = function(){
|
|
|
|
Tone.Source.prototype.dispose.call(this);
|
2014-09-30 03:44:59 +00:00
|
|
|
this.detune.dispose();
|
|
|
|
this.detune = null;
|
2014-12-04 02:38:24 +00:00
|
|
|
this.frequency.dispose();
|
2014-09-30 03:44:59 +00:00
|
|
|
this.frequency = null;
|
2014-12-04 02:38:24 +00:00
|
|
|
this._oscillator.dispose();
|
2014-09-30 03:44:59 +00:00
|
|
|
this._oscillator = null;
|
|
|
|
this._sourceType = null;
|
2015-02-01 19:40:47 +00:00
|
|
|
return this;
|
2014-09-30 03:44:59 +00:00
|
|
|
};
|
|
|
|
|
2014-10-01 02:48:21 +00:00
|
|
|
/**
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
var OmniOscType = {
|
|
|
|
PulseOscillator : "PulseOscillator",
|
|
|
|
PWMOscillator : "PWMOscillator",
|
|
|
|
Oscillator : "Oscillator"
|
|
|
|
};
|
|
|
|
|
2014-09-30 03:44:59 +00:00
|
|
|
return Tone.OmniOscillator;
|
|
|
|
});
|