2014-06-15 21:36:52 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/core/Transport", "Tone/signal/Signal"],
|
|
|
|
function(Tone){
|
2014-04-05 22:05:42 +00:00
|
|
|
|
2014-06-15 21:36:52 +00:00
|
|
|
/**
|
|
|
|
* Oscillator
|
|
|
|
*
|
|
|
|
* Oscilator with start, pause, stop and sync to Transport
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {number=} freq starting frequency
|
|
|
|
* @param {string=} type type of oscillator (sine|square|triangle|sawtooth)
|
|
|
|
*/
|
2014-04-05 22:05:42 +00:00
|
|
|
Tone.Oscillator = function(freq, type){
|
|
|
|
|
|
|
|
//components
|
2014-06-15 21:36:52 +00:00
|
|
|
this.output = this.context.createGain();
|
2014-04-05 22:05:42 +00:00
|
|
|
this.oscillator = this.context.createOscillator();
|
2014-06-15 21:36:52 +00:00
|
|
|
this.control = new Tone.Signal(this.defaultArg(this.toFrequency(freq), 440));
|
|
|
|
|
2014-04-05 22:05:42 +00:00
|
|
|
//connections
|
2014-06-15 21:36:52 +00:00
|
|
|
this.oscillator.connect(this.output);
|
|
|
|
|
|
|
|
//parameters
|
|
|
|
this.oscillator.type = this.defaultArg(type, "sine");
|
|
|
|
this.isSynced = false;
|
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
|
|
|
Tone.extend(Tone.Oscillator);
|
|
|
|
|
2014-06-15 21:36:52 +00:00
|
|
|
/**
|
|
|
|
* start the oscillator
|
|
|
|
*
|
|
|
|
* @param {Tone.Time} time
|
|
|
|
*/
|
2014-04-05 22:05:42 +00:00
|
|
|
Tone.Oscillator.prototype.start = function(time){
|
2014-06-15 21:36:52 +00:00
|
|
|
//get previous values
|
|
|
|
var type = this.oscillator.type;
|
|
|
|
var detune = this.oscillator.frequency.value;
|
|
|
|
//new oscillator with previous values
|
|
|
|
this.oscillator = this.context.createOscillator();
|
|
|
|
this.oscillator.type = type;
|
|
|
|
this.oscillator.detune.value = detune;
|
|
|
|
//connect the control signal to the oscillator frequency
|
|
|
|
this.oscillator.connect(this.output);
|
|
|
|
this.control.connect(this.oscillator.frequency);
|
|
|
|
this.oscillator.frequency.value = 0;
|
|
|
|
//start the oscillator
|
|
|
|
this.oscillator.start(this.toSeconds(time));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync the oscillator to the transport
|
|
|
|
*
|
|
|
|
* the current ratio between the oscillator and the Transport BPM
|
|
|
|
* is fixed and any change to the Transport BPM will change this
|
|
|
|
* oscillator in that same ratio
|
|
|
|
*
|
|
|
|
* Transport start/pause/stop will also start/pause/stop the oscillator
|
|
|
|
*/
|
|
|
|
Tone.Oscillator.prototype.sync = function(){
|
|
|
|
Tone.Transport.sync(this, this.control);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* unsync the oscillator from the Transport
|
|
|
|
*/
|
|
|
|
Tone.Oscillator.prototype.unsync = function(){
|
|
|
|
Tone.Transport.unsync(this);
|
|
|
|
this.control.unsync();
|
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
2014-06-15 21:36:52 +00:00
|
|
|
/**
|
|
|
|
* stop the oscillator
|
|
|
|
* @param {Tone.Time=} time (optional) timing parameter
|
|
|
|
*/
|
2014-04-05 22:05:42 +00:00
|
|
|
Tone.Oscillator.prototype.stop = function(time){
|
2014-06-15 21:36:52 +00:00
|
|
|
this.oscillator.stop(this.toSeconds(time));
|
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
2014-06-15 21:36:52 +00:00
|
|
|
/**
|
|
|
|
* exponentially ramp the frequency of the oscillator over the rampTime
|
|
|
|
*
|
|
|
|
* @param {number} val the frequency
|
|
|
|
* @param {Tone.Time} rampTime when the oscillator will arrive at the frequency
|
|
|
|
*/
|
2014-04-05 22:05:42 +00:00
|
|
|
Tone.Oscillator.prototype.setFrequency = function(val, rampTime){
|
2014-06-15 21:36:52 +00:00
|
|
|
this.control.exponentialRampToValueAtTime(this.toFrequency(val), this.toSeconds(rampTime));
|
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
2014-06-15 21:36:52 +00:00
|
|
|
/**
|
|
|
|
* set the oscillator type
|
|
|
|
* @param {string} type (sine|square|triangle|sawtooth)
|
|
|
|
*/
|
2014-04-05 22:05:42 +00:00
|
|
|
Tone.Oscillator.prototype.setType = function(type){
|
|
|
|
this.oscillator.type = type;
|
2014-06-15 21:36:52 +00:00
|
|
|
};
|
2014-04-05 22:05:42 +00:00
|
|
|
|
|
|
|
return Tone.Oscillator;
|
|
|
|
});
|