2014-08-31 20:29:56 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/source/Source", "Tone/source/Oscillator", "Tone/signal/Signal", "Tone/signal/Threshold"],
|
|
|
|
function(Tone){
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @private
|
|
|
|
* @type {Float32Array}
|
|
|
|
*/
|
|
|
|
var pulseCurve = new Float32Array(256);
|
|
|
|
for(var i=0; i < 128; i++) {
|
|
|
|
pulseCurve[i] = -1;
|
|
|
|
pulseCurve[i+128] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Pulse Oscillator with control over width
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends {Tone.Source}
|
|
|
|
* @param {number=} frequency the frequency of the oscillator
|
2014-09-30 03:44:35 +00:00
|
|
|
* @param {number} [width = 0.5] the width of the pulse
|
2014-08-31 20:29:56 +00:00
|
|
|
*/
|
2014-09-30 03:44:35 +00:00
|
|
|
Tone.PulseOscillator = function(){
|
2014-08-31 20:29:56 +00:00
|
|
|
|
|
|
|
Tone.Source.call(this);
|
2014-09-30 03:44:35 +00:00
|
|
|
var options = this.optionsObject(arguments, ["frequency", "width"], Tone.Oscillator.defaults);
|
2014-08-31 20:29:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the width of the pulse
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
*/
|
2014-09-30 03:44:35 +00:00
|
|
|
this.width = new Tone.Signal(options.width);
|
2014-08-31 20:29:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the sawtooth oscillator
|
|
|
|
* @type {Tone.Oscillator}
|
2014-09-06 19:56:41 +00:00
|
|
|
* @private
|
2014-08-31 20:29:56 +00:00
|
|
|
*/
|
2014-09-30 03:44:35 +00:00
|
|
|
this._sawtooth = new Tone.Oscillator({
|
|
|
|
frequency : options.frequency,
|
|
|
|
detune : options.detune,
|
|
|
|
type : "sawtooth",
|
|
|
|
phase : options.phase
|
|
|
|
});
|
2014-08-31 20:29:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the oscillators frequency
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
*/
|
|
|
|
this.frequency = this._sawtooth.frequency;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the oscillators detune
|
|
|
|
* @type {Tone.Signal}
|
|
|
|
*/
|
|
|
|
this.detune = this._sawtooth.detune;
|
|
|
|
|
2014-09-30 03:44:35 +00:00
|
|
|
/**
|
|
|
|
* callback which is invoked when the oscillator is stoped
|
|
|
|
* @type {function()}
|
|
|
|
*/
|
|
|
|
this.onended = options.onended;
|
|
|
|
|
2014-08-31 20:29:56 +00:00
|
|
|
/**
|
|
|
|
* threshold the signal to turn it into a square
|
|
|
|
*
|
|
|
|
* @type {WaveShaperNode}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._thresh = this.context.createWaveShaper();
|
|
|
|
this._thresh.curve = pulseCurve;
|
|
|
|
|
2014-09-30 03:44:35 +00:00
|
|
|
//connections
|
2014-08-31 20:29:56 +00:00
|
|
|
this.chain(this._sawtooth, this._thresh, this.output);
|
|
|
|
this.width.connect(this._thresh);
|
2014-09-30 03:44:35 +00:00
|
|
|
this._sawtooth.onended = this._onended.bind(this);
|
2014-08-31 20:29:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(Tone.PulseOscillator, Tone.Source);
|
|
|
|
|
2014-09-30 03:44:35 +00:00
|
|
|
/**
|
|
|
|
* the default parameters
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @const
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
Tone.PulseOscillator.defaults = {
|
|
|
|
"frequency" : 440,
|
|
|
|
"detune" : 0,
|
|
|
|
"phase" : 0,
|
|
|
|
"width" : 0.5,
|
|
|
|
"onended" : function(){},
|
|
|
|
};
|
|
|
|
|
2014-08-31 20:29:56 +00:00
|
|
|
/**
|
|
|
|
* set the width of the oscillators
|
|
|
|
* @param {number} width
|
|
|
|
*/
|
|
|
|
Tone.PulseOscillator.prototype.setWidth = function(width){
|
|
|
|
this.width.setValue(width);
|
|
|
|
};
|
|
|
|
|
2014-09-30 03:44:35 +00:00
|
|
|
/**
|
|
|
|
* set the phase of the oscillator
|
|
|
|
* @param {number} phase
|
|
|
|
*/
|
|
|
|
Tone.PulseOscillator.prototype.setPhase = function(phase){
|
|
|
|
this._sawtooth.setPhase(phase);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* exponentially ramp the frequency of the oscillator over the rampTime
|
|
|
|
* inherited from Tone.Oscillator
|
|
|
|
*
|
|
|
|
* @param {Tone.Time} val
|
|
|
|
* @param {Tone.Time=} rampTime when the oscillator will arrive at the frequency
|
|
|
|
*/
|
|
|
|
Tone.PulseOscillator.prototype.setFrequency = Tone.Oscillator.prototype.setFrequency;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bulk setter
|
|
|
|
* @param {Object} params
|
|
|
|
*/
|
|
|
|
Tone.PulseOscillator.prototype.set = function(params){
|
|
|
|
if (!this.isUndef(params.width)) this.setWidth(params.width);
|
|
|
|
this._sawtooth.set({
|
|
|
|
"phase" : params.phase,
|
|
|
|
"frequency" : params.frequency,
|
|
|
|
"detune" : params.detune
|
2014-09-30 04:28:48 +00:00
|
|
|
});
|
|
|
|
Tone.Source.prototype.set.call(this, params);
|
2014-09-30 03:44:35 +00:00
|
|
|
};
|
|
|
|
|
2014-08-31 20:29:56 +00:00
|
|
|
/**
|
|
|
|
* start the oscillator
|
|
|
|
*
|
|
|
|
* @param {Tone.Time} time
|
|
|
|
*/
|
|
|
|
Tone.PulseOscillator.prototype.start = function(time){
|
|
|
|
if (this.state === Tone.Source.State.STOPPED){
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
this._sawtooth.start(time);
|
|
|
|
this.width.output.gain.setValueAtTime(1, time);
|
|
|
|
this.state = Tone.Source.State.STARTED;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stop the oscillator
|
|
|
|
*
|
|
|
|
* @param {Tone.Time} time
|
|
|
|
*/
|
|
|
|
Tone.PulseOscillator.prototype.stop = function(time){
|
|
|
|
if (this.state === Tone.Source.State.STARTED){
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
this._sawtooth.stop(time);
|
|
|
|
//the width is still connected to the output.
|
|
|
|
//that needs to be stopped also
|
|
|
|
this.width.output.gain.setValueAtTime(0, time);
|
|
|
|
this.state = Tone.Source.State.STOPPED;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-30 03:44:35 +00:00
|
|
|
/**
|
|
|
|
* internal on end call
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Tone.PulseOscillator.prototype._onended = function(){
|
|
|
|
this.state = Tone.Source.State.STOPPED;
|
|
|
|
this.onended();
|
|
|
|
};
|
|
|
|
|
2014-08-31 20:29:56 +00:00
|
|
|
/**
|
|
|
|
* clean up method
|
|
|
|
*/
|
|
|
|
Tone.PulseOscillator.prototype.dispose = function(){
|
2014-09-06 22:55:11 +00:00
|
|
|
Tone.Source.prototype.dispose.call(this);
|
2014-08-31 20:29:56 +00:00
|
|
|
this._sawtooth.dispose();
|
|
|
|
this.width.dispose();
|
2014-09-06 22:55:11 +00:00
|
|
|
this._thresh.disconnect();
|
|
|
|
this._sawtooth = null;
|
|
|
|
this.frequency = null;
|
|
|
|
this.detune = null;
|
2014-08-31 20:29:56 +00:00
|
|
|
this.width = null;
|
2014-09-06 22:55:11 +00:00
|
|
|
this._thresh = null;
|
2014-08-31 20:29:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Tone.PulseOscillator;
|
|
|
|
});
|