dsp.js
function Oscillator(type, frequency, amplitude, bufferSize, sampleRate) {
  this.frequency  = frequency;
  this.amplitude  = amplitude;
  this.bufferSize = bufferSize;
  this.sampleRate = sampleRate;
  //this.pulseWidth = pulseWidth;
  this.frameCount = 0;
 
  this.waveTableLength = 2048;

  this.cyclesPerSample = frequency / sampleRate;

  this.signal = new Float32Array(bufferSize);
  this.envelope = null;

  switch(parseInt(type, 10)) {
    case DSP.TRIANGLE:
      this.func = Oscillator.Triangle;
      break;

    case DSP.SAW:
      this.func = Oscillator.Saw;
      break;

    case DSP.SQUARE:
      this.func = Oscillator.Square;
      break;

    default:
    case DSP.SINE:
      this.func = Oscillator.Sine;
      break;
  }

  this.generateWaveTable = function() {
    Oscillator.waveTable[this.func] = new Float32Array(2048);
    var waveTableTime = this.waveTableLength / this.sampleRate;
    var waveTableHz = 1 / waveTableTime;

    for (var i = 0; i < this.waveTableLength; i++) {
      Oscillator.waveTable[this.func][i] = this.func(i * waveTableHz/this.sampleRate);
    }
  };

  if ( typeof Oscillator.waveTable === 'undefined' ) {
    Oscillator.waveTable = {};
  }

  if ( typeof Oscillator.waveTable[this.func] === 'undefined' ) {
    this.generateWaveTable();
  }
 
  this.waveTable = Oscillator.waveTable[this.func];
}
function Oscillator(type, frequency, amplitude, bufferSize, sampleRate) {
  this.frequency  = frequency;
  this.amplitude  = amplitude;
  this.bufferSize = bufferSize;
  this.sampleRate = sampleRate;
  //this.pulseWidth = pulseWidth;
  this.frameCount = 0;
 
  this.waveTableLength = 2048;

  this.cyclesPerSample = frequency / sampleRate;

  this.signal = new Float32Array(bufferSize);
  this.envelope = null;

  switch(parseInt(type, 10)) {
    case DSP.TRIANGLE:
      this.func = Oscillator.Triangle;
      break;

    case DSP.SAW:
      this.func = Oscillator.Saw;
      break;

    case DSP.SQUARE:
      this.func = Oscillator.Square;
      break;

    default:
    case DSP.SINE:
      this.func = Oscillator.Sine;
      break;
  }

  this.generateWaveTable = function() {
    Oscillator.waveTable[this.func] = new Float32Array(2048);
    var waveTableTime = this.waveTableLength / this.sampleRate;
    var waveTableHz = 1 / waveTableTime;

    for (var i = 0; i < this.waveTableLength; i++) {
      Oscillator.waveTable[this.func][i] = this.func(i * waveTableHz/this.sampleRate);
    }
  };

  if ( typeof Oscillator.waveTable === 'undefined' ) {
    Oscillator.waveTable = {};
  }

  if ( typeof Oscillator.waveTable[this.func] === 'undefined' ) {
    this.generateWaveTable();
  }
 
  this.waveTable = Oscillator.waveTable[this.func];
}
function Oscillator ( type , frequency , amplitude , bufferSize , sampleRate ) { this . frequency = frequency ; this . amplitude = amplitude ; this . bufferSize = bufferSize ; this . sampleRate = sampleRate ; //this.pulseWidth = pulseWidth; this . frameCount = 0 ; this . waveTableLength = 2048 ; this . cyclesPerSample = frequency / sampleRate ; this . signal = new Float32Array ( bufferSize ) ; this . envelope = null ; switch ( parseInt ( type , 10 ) ) { case DSP . TRIANGLE : this . func = Oscillator . Triangle ; break ; case DSP . SAW : this . func = Oscillator . Saw ; break ; case DSP . SQUARE : this . func = Oscillator . Square ; break ; default : case DSP . SINE : this . func = Oscillator . Sine ; break ; } this . generateWaveTable = function ( ) { Oscillator . waveTable [ this . func ] = new Float32Array ( 2048 ) ; var waveTableTime = this . waveTableLength / this . sampleRate ; var waveTableHz = 1 / waveTableTime ; for ( var i = 0 ; i < this . waveTableLength ; i ++ ) { Oscillator . waveTable [ this . func ] [ i ] = this . func ( i * waveTableHz / this . sampleRate ) ; } } ; if ( typeof Oscillator . waveTable === 'undefined' ) { Oscillator . waveTable = { } ; } if ( typeof Oscillator . waveTable [ this . func ] === 'undefined' ) { this . generateWaveTable ( ) ; } this . waveTable = Oscillator . waveTable [ this . func ] ; }
Oscillator class for generating and modifying signals

@param {Number} type       A waveform constant (eg. DSP.SINE)
@param {Number} frequency  Initial frequency of the signal
@param {Number} amplitude  Initial amplitude of the signal
@param {Number} bufferSize Size of the sample buffer to generate
@param {Number} sampleRate The sample rate of the signal

@contructor
Oscillator class for generating and modifying signals