Tone.js/Tone/source/PWMOscillator.js

176 lines
4.1 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 Tone.PWMOscillator modulates the width of a Tone.PulseOscillator
* at the modulationFrequency. This has the effect of continuously
* changing the timbre of the oscillator by altering the harmonics
* generated.
2014-09-30 03:44:43 +00:00
*
2014-10-03 20:07:30 +00:00
* @extends {Tone.Oscillator}
2014-09-30 03:44:43 +00:00
* @constructor
2015-06-15 15:27:13 +00:00
* @param {Frequency} frequency The starting frequency of the oscillator.
* @param {Frequency} modulationFrequency The modulation frequency of the width of the pulse.
2015-02-27 16:19:45 +00:00
* @example
2015-06-20 19:50:57 +00:00
* var pwm = new Tone.PWMOscillator("Ab3", 0.3).toMaster().start();
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,
2015-08-21 21:04:29 +00:00
"detune" : options.detune,
"phase" : options.phase
});
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
/**
2015-06-14 02:30:33 +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;
/**
2015-06-14 02:30:33 +00:00
* The detune of the oscillator.
2015-06-13 23:50:39 +00:00
* @type {Cents}
* @signal
2014-09-30 03:44:43 +00:00
*/
this.detune = this._modulator.detune;
/**
2015-06-14 02:30:33 +00:00
* 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,
2015-08-21 21:04:29 +00:00
"phase" : 0,
2014-09-30 03:44:43 +00:00
"modulationFrequency" : 0.4,
};
/**
* start the oscillator
2015-06-14 00:20:36 +00:00
* @param {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-06-14 00:20:36 +00:00
* @param {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";
}
});
/**
* The partials of the waveform. Cannot set partials for this waveform type
* @memberOf Tone.PWMOscillator#
* @type {Array}
* @name partials
* @private
*/
Object.defineProperty(Tone.OmniOscillator.prototype, "partials", {
get : function(){
return [];
}
});
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
/**
2015-06-14 02:30:33 +00:00
* Clean up.
* @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;
});