Tone.js/Tone/source/PWMOscillator.js

159 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-02-27 16:19:45 +00:00
define(["Tone/core/Tone", "Tone/source/Source", "Tone/source/PulseOscillator", "Tone/source/Oscillator", "Tone/signal/Multiply"],
2014-09-30 03:44:43 +00:00
function(Tone){
"use strict";
/**
* @class takes an array of Oscillator descriptions and mixes them together
* with the same detune and frequency controls.
*
2014-10-03 20:07:30 +00:00
* @extends {Tone.Oscillator}
2014-09-30 03:44:43 +00:00
* @constructor
* @param {Tone.Type.Frequency} frequency of the oscillator (meaningless for noise types)
* @param {Tone.Type.Frequency} modulationFrequency the modulation frequency of the oscillator
2015-02-27 16:19:45 +00:00
* @example
* var pwm = new Tone.PWMOscillator("Ab3", 0.3);
2014-09-30 03:44:43 +00:00
*/
Tone.PWMOscillator = function(){
var options = this.optionsObject(arguments, ["frequency", "modulationFrequency"], Tone.PWMOscillator.defaults);
2015-02-02 01:38:06 +00:00
Tone.Source.call(this, options);
2014-09-30 03:44:43 +00:00
/**
* the pulse oscillator
2015-05-05 20:40:52 +00:00
* @type {Tone.PulseOscillator}
* @private
2014-09-30 03:44:43 +00:00
*/
this._pulse = new Tone.PulseOscillator(options.modulationFrequency);
//change the pulse oscillator type
this._pulse._sawtooth.type = "sine";
2014-09-30 03:44:43 +00:00
/**
* the modulator
2015-02-27 16:19:45 +00:00
* @type {Tone.Oscillator}
* @private
2014-09-30 03:44:43 +00:00
*/
this._modulator = new Tone.Oscillator({
"frequency" : options.frequency,
"detune" : options.detune
});
2014-09-30 03:44:43 +00:00
2015-02-27 16:19:45 +00:00
/**
* Scale the oscillator so it doesn't go silent
* at the extreme values.
* @type {Tone.Multiply}
* @private
*/
this._scale = new Tone.Multiply(1.01);
2014-09-30 03:44:43 +00:00
/**
* the frequency control
2015-06-13 23:50:39 +00:00
* @type {Frequency}
* @signal
2014-09-30 03:44:43 +00:00
*/
this.frequency = this._modulator.frequency;
/**
* the detune control
2015-06-13 23:50:39 +00:00
* @type {Cents}
* @signal
2014-09-30 03:44:43 +00:00
*/
this.detune = this._modulator.detune;
/**
* the modulation rate of the oscillator
2015-06-13 23:50:39 +00:00
* @type {Frequency}
* @signal
*/
this.modulationFrequency = this._pulse.frequency;
2014-09-30 03:44:43 +00:00
//connections
2015-02-27 16:19:45 +00:00
this._modulator.chain(this._scale, this._pulse.width);
2014-09-30 03:44:43 +00:00
this._pulse.connect(this.output);
2015-04-05 18:53:27 +00:00
this._readOnly(["modulationFrequency", "frequency", "detune"]);
2014-09-30 03:44:43 +00:00
};
2014-10-03 20:07:30 +00:00
Tone.extend(Tone.PWMOscillator, Tone.Oscillator);
2014-09-30 03:44:43 +00:00
/**
* default values
* @static
* @type {Object}
* @const
*/
Tone.PWMOscillator.defaults = {
"frequency" : 440,
"detune" : 0,
"modulationFrequency" : 0.4,
};
/**
* start the oscillator
2015-05-23 23:01:05 +00:00
* @param {Tone.Type.Time} [time=now]
2015-02-02 02:32:07 +00:00
* @private
2014-09-30 03:44:43 +00:00
*/
2015-02-02 02:32:07 +00:00
Tone.PWMOscillator.prototype._start = function(time){
time = this.toSeconds(time);
this._modulator.start(time);
this._pulse.start(time);
2014-09-30 03:44:43 +00:00
};
/**
* stop the oscillator
2015-05-23 23:01:05 +00:00
* @param {Tone.Type.Time} time (optional) timing parameter
2015-02-02 02:32:07 +00:00
* @private
2014-09-30 03:44:43 +00:00
*/
2015-02-02 02:32:07 +00:00
Tone.PWMOscillator.prototype._stop = function(time){
time = this.toSeconds(time);
this._modulator.stop(time);
this._pulse.stop(time);
2014-09-30 03:44:43 +00:00
};
/**
2015-02-27 16:19:45 +00:00
* The type of the oscillator. Always returns "pwm".
* @readOnly
* @memberOf Tone.PWMOscillator#
* @type {string}
* @name type
*/
Object.defineProperty(Tone.PWMOscillator.prototype, "type", {
get : function(){
return "pwm";
}
});
2014-09-30 03:44:43 +00:00
/**
2015-02-27 16:19:45 +00:00
* The phase of the oscillator in degrees.
* @memberOf Tone.PWMOscillator#
* @type {number}
* @name phase
2014-09-30 03:44:43 +00:00
*/
Object.defineProperty(Tone.PWMOscillator.prototype, "phase", {
get : function(){
return this._modulator.phase;
},
set : function(phase){
this._modulator.phase = phase;
}
});
2014-09-30 03:44:43 +00:00
/**
* clean up
2015-02-02 03:05:24 +00:00
* @return {Tone.PWMOscillator} `this`
2014-09-30 03:44:43 +00:00
*/
2015-02-02 03:05:24 +00:00
Tone.PWMOscillator.prototype.dispose = function(){
Tone.Source.prototype.dispose.call(this);
2014-09-30 03:44:43 +00:00
this._pulse.dispose();
this._pulse = null;
2015-02-27 16:19:45 +00:00
this._scale.dispose();
this._scale = null;
this._modulator.dispose();
this._modulator = null;
2015-04-05 18:53:27 +00:00
this._writable(["modulationFrequency", "frequency", "detune"]);
this.frequency = null;
this.detune = null;
this.modulationFrequency = null;
2015-02-02 03:05:24 +00:00
return this;
2014-09-30 03:44:43 +00:00
};
return Tone.PWMOscillator;
});