2017-10-21 23:02:46 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/source/Source", "Tone/source/PulseOscillator",
|
|
|
|
"Tone/source/Oscillator", "Tone/signal/Multiply"], function(Tone){
|
2014-09-30 03:44:43 +00:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
2017-10-21 23:02:46 +00:00
|
|
|
* @class Tone.PWMOscillator modulates the width of a Tone.PulseOscillator
|
2015-07-04 16:43:21 +00:00
|
|
|
* at the modulationFrequency. This has the effect of continuously
|
2017-10-21 23:02:46 +00:00
|
|
|
* changing the timbre of the oscillator by altering the harmonics
|
2015-07-04 16:43:21 +00:00
|
|
|
* generated.
|
2014-09-30 03:44:43 +00:00
|
|
|
*
|
2017-04-26 04:00:01 +00:00
|
|
|
* @extends {Tone.Source}
|
2014-09-30 03:44:43 +00:00
|
|
|
* @constructor
|
2017-10-21 23:02:46 +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(){
|
2017-04-26 04:00:01 +00:00
|
|
|
var options = Tone.defaults(arguments, ["frequency", "modulationFrequency"], Tone.PWMOscillator);
|
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
|
2015-02-04 04:48:47 +00:00
|
|
|
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
|
|
|
*/
|
2014-10-03 19:16:32 +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-10-03 19:16:32 +00:00
|
|
|
});
|
2014-09-30 03:44:43 +00:00
|
|
|
|
2015-02-27 16:19:45 +00:00
|
|
|
/**
|
2017-10-21 23:02:46 +00:00
|
|
|
* Scale the oscillator so it doesn't go silent
|
2015-02-27 16:19:45 +00:00
|
|
|
* at the extreme values.
|
|
|
|
* @type {Tone.Multiply}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-06-17 18:02:24 +00:00
|
|
|
this._scale = new Tone.Multiply(2);
|
2015-02-27 16:19:45 +00:00
|
|
|
|
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;
|
|
|
|
|
2014-10-08 15:59:31 +00:00
|
|
|
/**
|
2017-10-21 23:02:46 +00:00
|
|
|
* The modulation rate of the oscillator.
|
2015-06-13 23:50:39 +00:00
|
|
|
* @type {Frequency}
|
|
|
|
* @signal
|
2014-10-08 15:59:31 +00:00
|
|
|
*/
|
2017-10-21 23:02:46 +00:00
|
|
|
this.modulationFrequency = this._pulse.frequency;
|
2014-10-08 15:59:31 +00:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-04-26 04:00:01 +00:00
|
|
|
Tone.extend(Tone.PWMOscillator, Tone.Source);
|
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
|
|
|
};
|
|
|
|
|
2018-03-06 05:02:49 +00:00
|
|
|
/**
|
|
|
|
* restart the oscillator
|
|
|
|
* @param {Time} time (optional) timing parameter
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Tone.PWMOscillator.prototype.restart = function(time){
|
|
|
|
this._modulator.restart(time);
|
|
|
|
this._pulse.restart(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
|
2015-02-04 04:48:47 +00:00
|
|
|
* @memberOf Tone.PWMOscillator#
|
|
|
|
* @type {string}
|
|
|
|
* @name type
|
2015-02-01 20:47:31 +00:00
|
|
|
*/
|
2015-02-04 04:48:47 +00:00
|
|
|
Object.defineProperty(Tone.PWMOscillator.prototype, "type", {
|
|
|
|
get : function(){
|
|
|
|
return "pwm";
|
|
|
|
}
|
|
|
|
});
|
2015-02-01 20:47:31 +00:00
|
|
|
|
2015-11-18 03:51:10 +00:00
|
|
|
/**
|
|
|
|
* The partials of the waveform. Cannot set partials for this waveform type
|
|
|
|
* @memberOf Tone.PWMOscillator#
|
|
|
|
* @type {Array}
|
|
|
|
* @name partials
|
|
|
|
* @private
|
|
|
|
*/
|
2015-11-24 04:51:36 +00:00
|
|
|
Object.defineProperty(Tone.PWMOscillator.prototype, "partials", {
|
2015-11-18 03:51:10 +00:00
|
|
|
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.
|
2015-02-04 04:48:47 +00:00
|
|
|
* @memberOf Tone.PWMOscillator#
|
|
|
|
* @type {number}
|
|
|
|
* @name phase
|
2014-09-30 03:44:43 +00:00
|
|
|
*/
|
2015-02-04 04:48:47 +00:00
|
|
|
Object.defineProperty(Tone.PWMOscillator.prototype, "phase", {
|
|
|
|
get : function(){
|
|
|
|
return this._modulator.phase;
|
2017-10-21 23:02:46 +00:00
|
|
|
},
|
2015-02-04 04:48:47 +00:00
|
|
|
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.
|
2015-06-14 00:54:29 +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();
|
2015-04-05 18:41:43 +00:00
|
|
|
this._pulse = null;
|
2015-02-27 16:19:45 +00:00
|
|
|
this._scale.dispose();
|
2015-04-05 18:41:43 +00:00
|
|
|
this._scale = null;
|
2015-02-02 01:02:13 +00:00
|
|
|
this._modulator.dispose();
|
2015-04-05 18:41:43 +00:00
|
|
|
this._modulator = null;
|
2015-04-05 18:53:27 +00:00
|
|
|
this._writable(["modulationFrequency", "frequency", "detune"]);
|
2015-04-05 18:41:43 +00:00
|
|
|
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;
|
2017-10-21 23:02:46 +00:00
|
|
|
});
|