Tone.js/Tone/source/Oscillator.js

257 lines
6.3 KiB
JavaScript
Raw Normal View History

2014-12-19 21:39:50 +00:00
define(["Tone/core/Tone", "Tone/signal/Signal", "Tone/source/Source", "Tone/core/Transport"],
2014-06-15 21:36:52 +00:00
function(Tone){
"use strict";
2014-06-15 21:36:52 +00:00
/**
2014-07-20 22:17:24 +00:00
* @class Oscilator with start, pause, stop and sync to Transport methods
2014-06-15 21:36:52 +00:00
*
* @constructor
2014-06-16 01:00:22 +00:00
* @extends {Tone.Source}
2014-12-02 06:42:08 +00:00
* @param {number|string} [frequency=440] starting frequency
* @param {string} [type="sine"] type of oscillator (sine|square|triangle|sawtooth)
2014-06-15 21:36:52 +00:00
*/
Tone.Oscillator = function(){
2014-06-16 23:38:46 +00:00
Tone.Source.call(this);
var options = this.optionsObject(arguments, ["frequency", "type"], Tone.Oscillator.defaults);
2014-06-17 16:15:10 +00:00
/**
* the main oscillator
* @type {OscillatorNode}
* @private
2014-06-17 16:15:10 +00:00
*/
this._oscillator = null;
2014-08-21 05:03:51 +00:00
2014-06-17 16:15:10 +00:00
/**
* the frequency control signal
* @type {Tone.Signal}
*/
this.frequency = new Tone.Signal(this.toFrequency(options.frequency));
2014-06-15 21:36:52 +00:00
2014-07-20 22:17:24 +00:00
/**
* the detune control signal
* @type {Tone.Signal}
*/
this.detune = new Tone.Signal(options.detune);
2014-07-20 22:17:24 +00:00
2014-06-19 05:40:16 +00:00
/**
* callback which is invoked when the oscillator is stoped
2014-06-19 05:40:16 +00:00
* @type {function()}
*/
this.onended = options.onended;
2014-06-19 05:40:16 +00:00
/**
* the periodic wave
* @type {PeriodicWave}
* @private
*/
this._wave = null;
/**
* the phase of the oscillator
* between 0 - 360
* @type {number}
* @private
*/
this._phase = options.phase;
/**
* the type of the oscillator
* @type {string}
* @private
*/
this._type = options.type;
2014-06-17 16:15:10 +00:00
//setup
this.setPhase(this._phase);
2014-06-15 21:36:52 +00:00
};
2014-06-16 01:00:22 +00:00
Tone.extend(Tone.Oscillator, Tone.Source);
/**
* the default parameters
*
* @static
2014-09-30 03:45:31 +00:00
* @const
* @type {Object}
*/
Tone.Oscillator.defaults = {
"type" : "sine",
"frequency" : 440,
"onended" : function(){},
"detune" : 0,
"phase" : 0
};
2014-06-15 21:36:52 +00:00
/**
* start the oscillator
*
2014-12-02 06:42:08 +00:00
* @param {Tone.Time} [time=now]
2014-06-15 21:36:52 +00:00
*/
Tone.Oscillator.prototype.start = function(time){
2014-06-19 05:40:16 +00:00
if (this.state === Tone.Source.State.STOPPED){
this.state = Tone.Source.State.STARTED;
//get previous values
//new oscillator with previous values
2014-10-03 20:06:42 +00:00
this._oscillator = this.context.createOscillator();
this._oscillator.setPeriodicWave(this._wave);
2014-07-20 22:17:24 +00:00
//connect the control signal to the oscillator frequency & detune
2014-10-03 20:06:42 +00:00
this._oscillator.connect(this.output);
this.frequency.connect(this._oscillator.frequency);
this.detune.connect(this._oscillator.detune);
2014-06-19 05:40:16 +00:00
//start the oscillator
this._oscillator.onended = this.onended;
2014-10-03 20:06:42 +00:00
this._oscillator.start(this.toSeconds(time));
2014-06-19 05:40:16 +00:00
}
};
/**
* stop the oscillator
2014-12-02 06:42:08 +00:00
* @param {Tone.Time} [time=now] (optional) timing parameter
2014-06-19 05:40:16 +00:00
*/
Tone.Oscillator.prototype.stop = function(time){
if (this.state === Tone.Source.State.STARTED){
this.state = Tone.Source.State.STOPPED;
2014-10-03 20:06:42 +00:00
this._oscillator.stop(this.toSeconds(time));
2014-06-19 05:40:16 +00:00
}
2014-06-15 21:36:52 +00:00
};
/**
* exponentially ramp the frequency of the oscillator over the rampTime
*
2014-06-16 23:38:46 +00:00
* @param {Tone.Time} val
* @param {Tone.Time=} rampTime when the oscillator will arrive at the frequency
2014-06-15 21:36:52 +00:00
*/
Tone.Oscillator.prototype.setFrequency = function(val, rampTime){
val = this.toFrequency(val);
2014-06-19 05:40:16 +00:00
if (rampTime){
this.frequency.exponentialRampToValueAtTime(val, this.toSeconds(rampTime));
2014-06-19 05:40:16 +00:00
} else {
this.frequency.setValue(val);
2014-06-19 05:40:16 +00:00
}
2014-06-15 21:36:52 +00:00
};
2014-06-15 21:36:52 +00:00
/**
* set the oscillator type
*
* uses PeriodicWave even for native types so that it can set the phase
*
* the the PeriodicWave equations are from the Web Audio Source code
* here: https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/modules/webaudio/PeriodicWave.cpp&sq=package:chromium
2014-06-16 01:00:22 +00:00
*
2014-06-15 21:36:52 +00:00
* @param {string} type (sine|square|triangle|sawtooth)
*/
Tone.Oscillator.prototype.setType = function(type){
var fftSize = 4096;
var halfSize = fftSize / 2;
var real = new Float32Array(halfSize);
var imag = new Float32Array(halfSize);
// Clear DC and Nyquist.
real[0] = 0;
imag[0] = 0;
var shift = this._phase;
for (var n = 1; n < halfSize; ++n) {
var piFactor = 2 / (n * Math.PI);
var b;
switch (type) {
case "sine":
b = (n === 1) ? 1 : 0;
break;
case "square":
b = (n & 1) ? 2 * piFactor : 0;
break;
case "sawtooth":
b = piFactor * ((n & 1) ? 1 : -1);
break;
case "triangle":
if (n & 1) {
b = 2 * (piFactor * piFactor) * ((((n - 1) >> 1) & 1) ? -1 : 1);
} else {
b = 0;
}
break;
default:
throw new TypeError("invalid oscillator type: "+type);
}
if (b !== 0){
real[n] = -b * Math.sin(shift);
imag[n] = b * Math.cos(shift);
} else {
real[n] = 0;
imag[n] = 0;
}
}
var periodicWave = this.context.createPeriodicWave(real, imag);
this._wave = periodicWave;
if (this._oscillator !== null){
this._oscillator.setPeriodicWave(this._wave);
}
this._type = type;
};
2014-09-02 04:25:20 +00:00
/**
* @return {string} the type of oscillator
*/
Tone.Oscillator.prototype.getType = function() {
return this._type;
};
/**
* set the phase of the oscillator (in degrees)
* @param {number} degrees the phase in degrees
*/
Tone.Oscillator.prototype.setPhase = function(phase) {
this._phase = phase * Math.PI / 180;
this.setType(this._type);
2014-06-15 21:36:52 +00:00
};
2014-12-19 21:39:50 +00:00
/**
* sync the signal to the Transport's bpm
*/
Tone.Oscillator.prototype.syncFrequency = function(){
Tone.Transport.syncSignal(this.frequency);
};
/**
* unsync the oscillator's frequency from teh transprot
*/
Tone.Oscillator.prototype.unsyncFrequency = function(){
this.frequency.unsync();
};
/**
* set the parameters at once
* @param {Object} params
*/
Tone.Oscillator.prototype.set = function(params){
if (!this.isUndef(params.type)) this.setType(params.type);
if (!this.isUndef(params.phase)) this.setPhase(params.phase);
if (!this.isUndef(params.frequency)) this.frequency.setValue(params.frequency);
if (!this.isUndef(params.onended)) this.onended = params.onended;
if (!this.isUndef(params.detune)) this.detune.setValue(params.detune);
Tone.Source.prototype.set.call(this, params);
};
2014-06-19 05:40:16 +00:00
/**
* dispose and disconnect
*/
Tone.Oscillator.prototype.dispose = function(){
Tone.Source.prototype.dispose.call(this);
2014-08-23 19:18:51 +00:00
this.stop();
2014-10-03 20:06:42 +00:00
if (this._oscillator !== null){
this._oscillator.disconnect();
this._oscillator = null;
2014-06-19 05:40:16 +00:00
}
this.frequency.dispose();
this.detune.dispose();
this._wave = null;
this.detune = null;
2014-06-20 01:48:16 +00:00
this.frequency = null;
2014-06-19 05:40:16 +00:00
};
return Tone.Oscillator;
});