ブロック崩しを作る(1)

公開:2015-08-23 16:49
更新:2017-11-15 21:58
カテゴリ:ブロック崩し,ゲーム製作,html5,webgl,three.js,js

ブロック崩しを作る

お盆休み中にブロック崩しを作ってみていたが、HDDの修復に時間を取られ、公開できないでいた。ゲームの内容としては、

である。

このゲームの面白さは隅の列のブロックを全部崩して、そこにボールを入れ込んで大量にブロックを崩すことにある。今のバージョンではその面白さがうまく再現できていない。パドルのボールコントロール方法がまだいまいちなせいであろうと思う。改良が必要であろう。

コーディングに関して

squashゲームのソースコードをもとに作っている。変更点はブロックの追加と、パドルとボールの反射のアルゴリズムを修正している。 パドルとボールの当たる位置によって反射角度を変えるように変更している。ある程度狙ったブロックを崩せるようにね。

プレイ動画

Shadow Playで撮ったのだけれども、Youtubeにアップするとカクカクしてしまう。Windows 10の「映画&テレビ」アプリだとスムーズに再生される。Media Playerでもカクカクするので、Youtubeのせいではないと思うけど、なんでだろうね。

動作サンプル

新しいウィンドウで開く

ソースコード・リソース

/dev/break/0001/audio.js

/// <reference path="song.js" />
/// <reference path="dsp.js" />

//// Web Audio API ラッパークラス ////
function initAudio(global) {
  global.SF = global.SF || {};
  var SF = global.SF;
  var fft = new FFT(4096, 44100);
  var BUFFER_SIZE = 1024;
  var TIME_BASE = 96;

  noteFreq = [];
  for (var i = -81; i < 46; ++i) {
    noteFreq.push(Math.pow(2, i / 12));
  }

  var SquareWave = {
    bits: 4,
    wavedata: [0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0, 0, 0, 0, 0, 0, 0, 0]
  };// 4bit wave form

  var SawWave = {
    bits: 4,
    wavedata: [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf]
  };// 4bit wave form

  var TriWave = {
    bits: 4,
    wavedata: [0x0, 0x2, 0x4, 0x6, 0x8, 0xA, 0xC, 0xE, 0xF, 0xE, 0xC, 0xA, 0x8, 0x6, 0x4, 0x2]
  };

  function decodeStr(bits, wavestr) {
    var arr = [];
    var n = bits / 4 | 0;
    var c = 0;
    var zeropos = 1 << (bits - 1);
    while (c < wavestr.length) {
      var d = 0;
      for (var i = 0; i < n; ++i) {
        eval("d = (d << 4) + 0x" + wavestr.charAt(c++) + ";");
      }
      arr.push((d - zeropos) / zeropos);
    }
    return arr;
  }

  var waves = [
      decodeStr(4, 'EEEEEEEEEEEEEEEE0000000000000000'),
      decodeStr(4, '00112233445566778899AABBCCDDEEFF'),
      decodeStr(4, '023466459AA8A7A977965656ACAACDEF'),
      decodeStr(4, 'BDCDCA999ACDCDB94212367776321247'),
      decodeStr(4, '7ACDEDCA742101247BDEDB7320137E78'),
      decodeStr(4, 'ACCA779BDEDA66679994101267742247'),
      decodeStr(4, '7EC9CEA7CFD8AB728D94572038513531'),
      decodeStr(4, 'EE77EE77EE77EE770077007700770077'),
      decodeStr(4, 'EEEE8888888888880000888888888888')//ノイズ用のダミー波形
  ];

  var waveSamples = [];
  function WaveSample(audioctx, ch, sampleLength, sampleRate) {

    this.sample = audioctx.createBuffer(ch, sampleLength, sampleRate || audioctx.sampleRate);
    this.loop = false;
    this.start = 0;
    this.end = (sampleLength - 1) / (sampleRate || audioctx.sampleRate);
  }

  function createWaveSampleFromWaves(audioctx, sampleLength) {
    for (var i = 0, end = waves.length; i < end; ++i) {
      var sample = new WaveSample(audioctx, 1, sampleLength);
      waveSamples.push(sample);
      if (i != 8) {
        var wavedata = waves[i];
        var delta = 440.0 * wavedata.length / audioctx.sampleRate;
        var stime = 0;
        var output = sample.sample.getChannelData(0);
        var len = wavedata.length;
        var index = 0;
        var endsample = 0;
        for (var j = 0; j < sampleLength; ++j) {
          index = stime | 0;
          output[j] = wavedata[index];
          stime += delta;
          if (stime >= len) {
            stime = stime - len;
            endsample = j;
          }
        }
        sample.end = endsample / audioctx.sampleRate;
        sample.loop = true;
      } else {
        // ボイス8はノイズ波形とする
        var output = sample.sample.getChannelData(0);
        for (var j = 0; j < sampleLength; ++j) {
          output[j] = Math.random() * 2.0 - 1.0;
        }
        sample.end = sampleLength / audioctx.sampleRate;
        sample.loop = true;
      }
    }
  }

  /// エンベロープジェネレーター
  function EnvelopeGenerator(voice, attack, decay, sustain, release) {
    this.voice = voice;
    //this.keyon = false;
    this.attack = attack || 0.0005;
    this.decay = decay || 0.05;
    this.sustain = sustain || 0.5;
    this.release = release || 0.5;
    this.v = 1.0;

  };

  EnvelopeGenerator.prototype =
  {
    keyon: function (t, vel) {
      this.v = vel || 1.0;
      var v = this.v;
      var t0 = t || this.voice.audioctx.currentTime;
      var t1 = t0 + this.attack * v;
      var gain = this.voice.gain.gain;
      gain.cancelScheduledValues(t0);
      gain.setValueAtTime(0, t0);
      gain.linearRampToValueAtTime(v, t1);
      gain.linearRampToValueAtTime(this.sustain * v, t0 + this.decay / v);
      //gain.setTargetAtTime(this.sustain * v, t1, t1 + this.decay / v);
    },
    keyoff: function (t) {
      var voice = this.voice;
      var gain = voice.gain.gain;
      var t0 = t || voice.audioctx.currentTime;
      gain.cancelScheduledValues(t0);
      //gain.setValueAtTime(0, t0 + this.release / this.v);
      //gain.setTargetAtTime(0, t0, t0 + this.release / this.v);
      gain.linearRampToValueAtTime(0, t0 + this.release / this.v);
    }
  };

  /// ボイス
  function Voice(audioctx) {
    this.audioctx = audioctx;
    this.sample = waveSamples[6];
    this.gain = audioctx.createGain();
    this.gain.gain.value = 0.0;
    this.volume = audioctx.createGain();
    this.envelope = new EnvelopeGenerator(this);
    this.initProcessor();
    this.detune = 1.0;
    this.volume.gain.value = 1.0;
    this.gain.connect(this.volume);
    this.output = this.volume;
  };

  Voice.prototype = {
    initProcessor: function () {
      this.processor = this.audioctx.createBufferSource();
      this.processor.buffer = this.sample.sample;
      this.processor.loop = this.sample.loop;
      this.processor.loopStart = 0;
      this.processor.playbackRate.value = 1.0;
      this.processor.loopEnd = this.sample.end;
      this.processor.connect(this.gain);
    },

    setSample: function (sample) {
      this.envelope.keyoff(0);
      this.processor.disconnect(this.gain);
      this.sample = sample;
      this.initProcessor();
      this.processor.start();
    },
    start: function (startTime) {
      //   if (this.processor.playbackState == 3) {
      this.processor.disconnect(this.gain);
      this.initProcessor();
      //    } else {
      //      this.envelope.keyoff();
      //
      //    }
      this.processor.start(startTime);
    },
    stop: function (time) {
      this.processor.stop(time);
      this.reset();
    },
    keyon: function (t, note, vel) {
      this.processor.playbackRate.setValueAtTime(noteFreq[note] * this.detune, t);
      this.envelope.keyon(t, vel);
    },
    keyoff: function (t) {
      this.envelope.keyoff(t);
    },
    reset: function () {
      this.processor.playbackRate.cancelScheduledValues(0);
      this.gain.gain.cancelScheduledValues(0);
      this.gain.gain.value = 0;
    }
  }

  function Audio() {
    this.enable = false;
    this.audioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;

    if (this.audioContext) {
      this.audioctx = new this.audioContext();
      this.enable = true;
    }

    this.voices = [];
    if (this.enable) {
      createWaveSampleFromWaves(this.audioctx, BUFFER_SIZE);
      this.filter = this.audioctx.createBiquadFilter();
      this.filter.type = 'lowpass';
      this.filter.frequency.value = 20000;
      this.filter.Q.value = 0.0001;
      this.noiseFilter = this.audioctx.createBiquadFilter();
      this.noiseFilter.type = 'lowpass';
      this.noiseFilter.frequency.value = 1000;
      this.noiseFilter.Q.value = 1.8;
      this.comp = this.audioctx.createDynamicsCompressor();
      this.filter.connect(this.comp);
      this.noiseFilter.connect(this.comp);
      this.comp.connect(this.audioctx.destination);
      for (var i = 0, end = this.VOICES; i < end; ++i) {
        var v = new Voice(this.audioctx);
        this.voices.push(v);
        if (i == (this.VOICES - 1)) {
          v.output.connect(this.noiseFilter);
        } else {
          v.output.connect(this.filter);
        }
      }
      //  this.started = false;

      //this.voices[0].output.connect();
    }

  }

  Audio.prototype = {
    start: function () {
      //  if (this.started) return;

      var voices = this.voices;
      for (var i = 0, end = voices.length; i < end; ++i) {
        voices[i].start(0);
      }
      //this.started = true;
    },
    stop: function () {
      //if(this.started)
      //{
      var voices = this.voices;
      for (var i = 0, end = voices.length; i < end; ++i) {
        voices[i].stop(0);
      }
      //  this.started = false;
      //}
    },
    VOICES: 12
  }

  SF.audio = new Audio();

  /**********************************************/
  /* シーケンサーコマンド                       */
  /**********************************************/

  function Note(no, name) {
    this.no = no;
    this.name = name;
  }

  Note.prototype = {
    process: function (track) {
      var back = track.back;
      var note = this;
      var oct = this.oct || back.oct;
      var step = this.step || back.step;
      var gate = this.gate || back.gate;
      var vel = this.vel || back.vel;
      setQueue(track, note, oct, step, gate, vel);

    }
  }

  var
    C = new Note(0, 'C '),
    Db = new Note(1, 'Db'),
    D = new Note(2, 'D '),
    Eb = new Note(3, 'Eb'),
    E = new Note(4, 'E '),
    F = new Note(5, 'F '),
    Gb = new Note(6, 'Gb'),
    G = new Note(7, 'G '),
    Ab = new Note(8, 'Ab'),
    A = new Note(9, 'A '),
    Bb = new Note(10, 'Bb'),
    B = new Note(11, 'B ');

  // R = new Rest();

  function SeqData(note, oct, step, gate, vel) {
    this.note = note;
    this.oct = oct;
    //this.no = note.no + oct * 12;
    this.step = step;
    this.gate = gate;
    this.vel = vel;
  }

  function setQueue(track, note, oct, step, gate, vel) {
    var no = note.no + oct * 12;
    var step_time = track.playingTime;
    var gate_time = ((gate >= 0) ? gate * 60 : step * gate * 60 * -1.0) / (TIME_BASE * track.sequencer.tempo) + track.playingTime;
    var voice = track.audio.voices[track.channel];
    voice.keyon(step_time, no, vel);
    voice.keyoff(gate_time);
    track.playingTime = (step * 60) / (TIME_BASE * track.sequencer.tempo) + track.playingTime;
    var back = track.back;
    back.note = note;
    back.oct = oct;
    back.step = step;
    back.gate = gate;
    back.vel = vel;
  }

  SeqData.prototype = {
    process: function (track) {

      var back = track.back;
      var note = this.note || back.note;
      var oct = this.oct || back.oct;
      var step = this.step || back.step;
      var gate = this.gate || back.gate;
      var vel = this.vel || back.vel;
      setQueue(track, note, oct, step, gate, vel);
    }
  }

  function S(note, oct, step, gate, vel) {
    var args = Array.prototype.slice.call(arguments);
    if (S.length != args.length) {
      if (typeof (args[args.length - 1]) == 'object' && !(args[args.length - 1] instanceof Note)) {
        var args1 = args[args.length - 1];
        var l = args.length - 1;
        return new SeqData(
        ((l != 0) ? note : false) || args1.note || args1.n || null,
        ((l != 1) ? oct : false) || args1.oct || args1.o || null,
        ((l != 2) ? step : false) || args1.step || args1.s || null,
        ((l != 3) ? gate : false) || args1.gate || args1.g || null,
        ((l != 4) ? vel : false) || args1.vel || args1.v || null
        );
      }
    }
    return new SeqData(note || null, oct || null, step || null, gate || null, vel || null);
  }

  function S1(note, oct, step, gate, vel) {
    return S(note, oct, l(step), gate, vel);
  }

  function S2(note, len, dot, oct, gate, vel) {
    return S(note, oct, l(len, dot), gate, vel);
  }

  function S3(note, step, gate, vel, oct) {
    return S(note, oct, step, gate, vel);
  }


  /// 音符の長さ指定

  function l(len, dot) {
    var d = false;
    if (dot) d = dot;
    return (TIME_BASE * (4 + (d ? 2 : 0))) / len;
  }

  function Step(step) {
    this.step = step;
  }

  Step.prototype.process = function (track) {
    track.back.step = this.step;
  }

  function ST(step) {
    return new Step(step);
  }

  function L(len, dot) {
    return new Step(l(len, dot));
  }

  /// ゲートタイム指定

  function GateTime(gate) {
    this.gate = gate;
  }

  GateTime.prototype.process = function (track) {
    track.back.gate = this.gate;
  }

  function GT(gate) {
    return new GateTime(gate);
  }

  /// ベロシティ指定

  function Velocity(vel) {
    this.vel = vel;
  }

  Velocity.prototype.process = function (track) {
    track.back.vel = this.vel;
  }

  function V(vel) {
    return new Velocity(vel);
  }


  function Jump(pos) { this.pos = pos; };
  Jump.prototype.process = function (track) {
    track.seqPos = this.pos;
  }

  /// 音色設定
  function Tone(no) {
    this.no = no;
    //this.sample = waveSamples[this.no];
  }

  Tone.prototype =
  {
    process: function (track) {
      track.audio.voices[track.channel].setSample(waveSamples[this.no]);
    }
  }
  function TONE(no) {
    return new Tone(no);
  }

  function JUMP(pos) {
    return new Jump(pos);
  }

  function Rest(step) {
    this.step = step;
  }

  Rest.prototype.process = function (track) {
    var step = this.step || track.back.step;
    track.playingTime = track.playingTime + (this.step * 60) / (TIME_BASE * track.sequencer.tempo);
    track.back.step = this.step;
  }

  function R1(step) {
    return new Rest(step);
  }
  function R(len, dot) {
    return new Rest(l(len, dot));
  }

  function Octave(oct) {
    this.oct = oct;
  }
  Octave.prototype.process = function (track) {
    track.back.oct = this.oct;
  }

  function O(oct) {
    return new Octave(oct);
  }

  function OctaveUp(v) { this.v = v; };
  OctaveUp.prototype.process = function (track) {
    track.back.oct += this.v;
  }

  var OU = new OctaveUp(1);
  var OD = new OctaveUp(-1);

  function Tempo(tempo) {
    this.tempo = tempo;
  }

  Tempo.prototype.process = function (track) {
    track.sequencer.tempo = this.tempo;
  }

  function TEMPO(tempo) {
    return new Tempo(tempo);
  }

  function Envelope(attack, decay, sustain, release) {
    this.attack = attack;
    this.decay = decay;
    this.sustain = sustain;
    this.release = release;
  }

  Envelope.prototype.process = function (track) {
    var envelope = track.audio.voices[track.channel].envelope;
    envelope.attack = this.attack;
    envelope.decay = this.decay;
    envelope.sustain = this.sustain;
    envelope.release = this.release;
  }

  function ENV(attack, decay, sustain, release) {
    return new Envelope(attack, decay, sustain, release);
  }

  /// デチューン
  function Detune(detune) {
    this.detune = detune;
  }

  Detune.prototype.process = function (track) {
    var voice = track.audio.voices[track.channel];
    voice.detune = this.detune;
  }

  function DETUNE(detune) {
    return new Detune(detune);
  }

  function Volume(volume) {
    this.volume = volume;
  }

  Volume.prototype.process = function (track) {
    track.audio.voices[track.channel].volume.gain.setValueAtTime(this.volume, track.playingTime);
  }

  function VOLUME(volume) {
    return new Volume(volume);
  }

  function LoopData(obj, varname, count, seqPos) {
    this.varname = varname;
    this.count = count;
    this.obj = obj;
    this.seqPos = seqPos;
  }

  function Loop(varname, count) {
    this.loopData = new LoopData(this, varname, count, 0);
  }

  Loop.prototype.process = function (track) {
    var stack = track.stack;
    if (stack.length == 0 || stack[stack.length - 1].obj !== this) {
      var ld = this.loopData;
      stack.push(new LoopData(this, ld.varname, ld.count, track.seqPos));
    }
  }

  function LOOP(varname, count) {
    return new Loop(varname, count);
  }

  function LoopEnd() {
  }

  LoopEnd.prototype.process = function (track) {
    var ld = track.stack[track.stack.length - 1];
    ld.count--;
    if (ld.count > 0) {
      track.seqPos = ld.seqPos;
    } else {
      track.stack.pop();
    }
  }

  var LOOP_END = new LoopEnd();

  /// シーケンサートラック
  function Track(sequencer, seqdata) {
    this.name = '';
    this.end = false;
    this.oneshot = false;
    this.sequencer = sequencer;
    this.seqData = seqdata;
    this.seqPos = 0;
    this.mute = false;
    this.playingTime = -1;
    this.localTempo = sequencer.tempo;
    this.trackVolume = 1.0;
    this.transpose = 0;
    this.solo = false;
    this.channel = -1;
    this.track = -1;
    this.audio = SF.audio;
    this.back = {
      note: 72,
      oct: 5,
      step: 96,
      gate: 48,
      vel: 1.0
    }
    this.stack = [];
  }

  Track.prototype = {
    process: function (currentTime) {

      if (this.end) return;

      var os = false;
      if (this.oneshot) {
        this.reset();
      }

      var seqSize = this.seqData.length;
      if (this.seqPos >= seqSize) {
        if (this.sequencer.repeat) {
          this.seqPos = 0;
        } else {
          this.end = true;
          return;
        }
      }

      var seq = this.seqData;
      this.playingTime = (this.playingTime > -1) ? this.playingTime : currentTime;
      var endTime = currentTime + 0.2/*sec*/;

      while (this.seqPos < seqSize) {
        if (this.playingTime >= endTime && !this.oneshot) {
          break;
        } else {
          var d = seq[this.seqPos];
          d.process(this);
          this.seqPos++;
        }
      }
    },
    reset: function () {
      var curVoice = this.audio.voices[this.channel];
      curVoice.gain.gain.cancelScheduledValues(0);
      curVoice.processor.playbackRate.cancelScheduledValues(0);
      curVoice.gain.gain.value = 0;
      this.playingTime = -1;
      this.seqPos = 0;
      this.end = false;
    }

  }

  function loadTracks(self, tracks, trackdata) {
    for (var i = 0; i < trackdata.length; ++i) {
      var track = new Track(self, trackdata[i].data);
      track.channel = trackdata[i].channel;
      track.oneshot = (!trackdata[i].oneshot) ? false : true;
      track.track = i;
      tracks.push(track);
    }
  }

  function createTracks(trackdata) {
    var tracks = [];
    loadTracks(this, tracks, trackdata);
    return tracks;
  }

  /// シーケンサー本体
  function Sequencer() {
    this.tempo = 100.0;
    this.repeat = false;
    this.play = false;
    this.tracks = [];
    this.pauseTime = 0;
    this.status = this.STOP;
  }

  Sequencer.prototype = {
    load: function (data) {
      if (this.play) {
        this.stop();
      }
      this.tracks.length = 0;
      loadTracks(this, this.tracks, data.tracks);
    },
    start: function () {
      //    this.handle = window.setTimeout(function () { self.process() }, 50);
      this.status = this.PLAY;
      this.process();
    },
    process: function () {
      if (this.status == this.PLAY) {
        var tracks = this.tracks;
        this.playTracks(tracks);
        var self = this;
        this.handle = window.setTimeout(function () { self.process() }, 50);
      }
    },
    playTracks: function (tracks) {
      var currentTime = SF.audio.audioctx.currentTime;
      for (var i = 0, end = tracks.length; i < end; ++i) {
        tracks[i].process(currentTime);
      }
    },
    pause: function () {
      this.status = this.PAUSE;
      this.pauseTime = SF.audio.audioctx.currentTime;
    },
    resume: function () {
      if (this.status == this.PAUSE) {
        this.status = this.PLAY;
        var tracks = this.tracks;
        var adjust = SF.audio.audioctx.currentTime - this.pauseTime;
        for (var i = 0, end = tracks.length; i < end; ++i) {
          tracks[i].playingTime += adjust;
        }
        this.process();
      }
    },
    stop: function () {
      if (this.status != this.STOP) {
        clearTimeout(this.handle);
        //    clearInterval(this.handle);
        this.status = this.STOP;
        this.reset();
      }
    },
    reset: function () {
      for (var i = 0, end = this.tracks.length; i < end; ++i) {
        this.tracks[i].reset();
      }
    },
    STOP: 0 | 0,
    PLAY: 1 | 0,
    PAUSE: 2 | 0
  }

  SF.sequencer = new Sequencer();

  
  // 効果音
  function SoundEffects(sequencer) {
    this.soundEffects =
     [
     // Effect 0 ////////////////////////////////////
     createTracks.call(sequencer, [
     {
       channel: 8,
       oneshot: true,
       data: [VOLUME(0.5),
         ENV(0.0001, 0.01, 1.0, 0.0001), GT(-0.999), TONE(0), TEMPO(120), O(7), ST(48), A
       ]
     }
     ]),
     // Effect 1 ////////////////////////////////////
     createTracks.call(sequencer, [
     {
       channel: 9,
       oneshot: true,
       data: [VOLUME(0.5),
         ENV(0.0001, 0.01, 1.0, 0.0001), GT(-0.999), TONE(0), TEMPO(120), O(6), ST(48), A
       ]
     }
     ]),
     // Effect 2 ////////////////////////////////////
     createTracks.call(sequencer, [
     {
       channel: 10,
       oneshot: true,
       data: [VOLUME(0.5),
         ENV(0.0001, 0.01, 1.0, 0.0001), GT(-0.999), TONE(0), TEMPO(120), O(8), ST(48), A
       ]
     }
     ])
     ];
  }

  SF.soundEffects = (new SoundEffects(SF.sequencer)).soundEffects;

}
 



/dev/break/0001/dsp.js

/* 
 *  DSP.js - a comprehensive digital signal processing  library for javascript
 * 
 *  Created by Corban Brook <corbanbrook@gmail.com> on 2010-01-01.
 *  Copyright 2010 Corban Brook. All rights reserved.
 *
 */

////////////////////////////////////////////////////////////////////////////////
//                                  CONSTANTS                                 //
////////////////////////////////////////////////////////////////////////////////

/**
 * DSP is an object which contains general purpose utility functions and constants
 */
var DSP = {
  // Channels
  LEFT:           0,
  RIGHT:          1,
  MIX:            2,

  // Waveforms
  SINE:           1,
  TRIANGLE:       2,
  SAW:            3,
  SQUARE:         4,

  // Filters
  LOWPASS:        0,
  HIGHPASS:       1,
  BANDPASS:       2,
  NOTCH:          3,

  // Window functions
  BARTLETT:       1,
  BARTLETTHANN:   2,
  BLACKMAN:       3,
  COSINE:         4,
  GAUSS:          5,
  HAMMING:        6,
  HANN:           7,
  LANCZOS:        8,
  RECTANGULAR:    9,
  TRIANGULAR:     10,

  // Loop modes
  OFF:            0,
  FW:             1,
  BW:             2,
  FWBW:           3,

  // Math
  TWO_PI:         2*Math.PI
};

// Setup arrays for platforms which do not support byte arrays
function setupTypedArray(name, fallback) {
  // check if TypedArray exists
  // typeof on Minefield and Chrome return function, typeof on Webkit returns object.
  if (typeof this[name] !== "function" && typeof this[name] !== "object") {
    // nope.. check if WebGLArray exists
    if (typeof this[fallback] === "function" && typeof this[fallback] !== "object") {
      this[name] = this[fallback];
    } else {
      // nope.. set as Native JS array
      this[name] = function(obj) {
        if (obj instanceof Array) {
          return obj;
        } else if (typeof obj === "number") {
          return new Array(obj);
        }
      };
    }
  }
}

setupTypedArray("Float32Array", "WebGLFloatArray");
setupTypedArray("Int32Array",   "WebGLIntArray");
setupTypedArray("Uint16Array",  "WebGLUnsignedShortArray");
setupTypedArray("Uint8Array",   "WebGLUnsignedByteArray");


////////////////////////////////////////////////////////////////////////////////
//                            DSP UTILITY FUNCTIONS                           //
////////////////////////////////////////////////////////////////////////////////

/**
 * Inverts the phase of a signal
 *
 * @param {Array} buffer A sample buffer
 *
 * @returns The inverted sample buffer
 */
DSP.invert = function(buffer) {
  for (var i = 0, len = buffer.length; i < len; i++) {
    buffer[i] *= -1;
  }

  return buffer;
};

/**
 * Converts split-stereo (dual mono) sample buffers into a stereo interleaved sample buffer
 *
 * @param {Array} left  A sample buffer
 * @param {Array} right A sample buffer
 *
 * @returns The stereo interleaved buffer
 */
DSP.interleave = function(left, right) {
  if (left.length !== right.length) {
    throw "Can not interleave. Channel lengths differ.";
  }
 
  var stereoInterleaved = new Float32Array(left.length * 2);
 
  for (var i = 0, len = left.length; i < len; i++) {
    stereoInterleaved[2*i]   = left[i];
    stereoInterleaved[2*i+1] = right[i];
  }
 
  return stereoInterleaved;
};

/**
 * Converts a stereo-interleaved sample buffer into split-stereo (dual mono) sample buffers
 *
 * @param {Array} buffer A stereo-interleaved sample buffer
 *
 * @returns an Array containing left and right channels
 */
DSP.deinterleave = (function() {
  var left, right, mix, deinterleaveChannel = []; 

  deinterleaveChannel[DSP.MIX] = function(buffer) {
    for (var i = 0, len = buffer.length/2; i < len; i++) {
      mix[i] = (buffer[2*i] + buffer[2*i+1]) / 2;
    }
    return mix;
  };

  deinterleaveChannel[DSP.LEFT] = function(buffer) {
    for (var i = 0, len = buffer.length/2; i < len; i++) {
      left[i]  = buffer[2*i];
    }
    return left;
  };

  deinterleaveChannel[DSP.RIGHT] = function(buffer) {
    for (var i = 0, len = buffer.length/2; i < len; i++) {
      right[i]  = buffer[2*i+1];
    }
    return right;
  };

  return function(channel, buffer) { 
    left  = left  || new Float32Array(buffer.length/2);
    right = right || new Float32Array(buffer.length/2);
    mix   = mix   || new Float32Array(buffer.length/2);

    if (buffer.length/2 !== left.length) {
      left  = new Float32Array(buffer.length/2);
      right = new Float32Array(buffer.length/2);
      mix   = new Float32Array(buffer.length/2);
    }

    return deinterleaveChannel[channel](buffer);
  };
}());

/**
 * Separates a channel from a stereo-interleaved sample buffer
 *
 * @param {Array}  buffer A stereo-interleaved sample buffer
 * @param {Number} channel A channel constant (LEFT, RIGHT, MIX)
 *
 * @returns an Array containing a signal mono sample buffer
 */
DSP.getChannel = DSP.deinterleave;

/**
 * Helper method (for Reverb) to mix two (interleaved) samplebuffers. It's possible
 * to negate the second buffer while mixing and to perform a volume correction
 * on the final signal.
 *
 * @param {Array} sampleBuffer1 Array containing Float values or a Float32Array
 * @param {Array} sampleBuffer2 Array containing Float values or a Float32Array
 * @param {Boolean} negate When true inverts/flips the audio signal
 * @param {Number} volumeCorrection When you add multiple sample buffers, use this to tame your signal ;)
 *
 * @returns A new Float32Array interleaved buffer.
 */
DSP.mixSampleBuffers = function(sampleBuffer1, sampleBuffer2, negate, volumeCorrection){
  var outputSamples = new Float32Array(sampleBuffer1);

  for(var i = 0; i<sampleBuffer1.length; i++){
    outputSamples[i] += (negate ? -sampleBuffer2[i] : sampleBuffer2[i]) / volumeCorrection;
  }
 
  return outputSamples;
}; 

// Biquad filter types
DSP.LPF = 0;                // H(s) = 1 / (s^2 + s/Q + 1)
DSP.HPF = 1;                // H(s) = s^2 / (s^2 + s/Q + 1)
DSP.BPF_CONSTANT_SKIRT = 2; // H(s) = s / (s^2 + s/Q + 1)  (constant skirt gain, peak gain = Q)
DSP.BPF_CONSTANT_PEAK = 3;  // H(s) = (s/Q) / (s^2 + s/Q + 1)      (constant 0 dB peak gain)
DSP.NOTCH = 4;              // H(s) = (s^2 + 1) / (s^2 + s/Q + 1)
DSP.APF = 5;                // H(s) = (s^2 - s/Q + 1) / (s^2 + s/Q + 1)
DSP.PEAKING_EQ = 6;         // H(s) = (s^2 + s*(A/Q) + 1) / (s^2 + s/(A*Q) + 1)
DSP.LOW_SHELF = 7;          // H(s) = A * (s^2 + (sqrt(A)/Q)*s + A)/(A*s^2 + (sqrt(A)/Q)*s + 1)
DSP.HIGH_SHELF = 8;         // H(s) = A * (A*s^2 + (sqrt(A)/Q)*s + 1)/(s^2 + (sqrt(A)/Q)*s + A)

// Biquad filter parameter types
DSP.Q = 1;
DSP.BW = 2; // SHARED with BACKWARDS LOOP MODE
DSP.S = 3;

// Find RMS of signal
DSP.RMS = function(buffer) {
  var total = 0;
  
  for (var i = 0, n = buffer.length; i < n; i++) {
    total += buffer[i] * buffer[i];
  }
  
  return Math.sqrt(total / n);
};

// Find Peak of signal
DSP.Peak = function(buffer) {
  var peak = 0;
  
  for (var i = 0, n = buffer.length; i < n; i++) {
    peak = (Math.abs(buffer[i]) > peak) ? Math.abs(buffer[i]) : peak; 
  }
  
  return peak;
};

// Fourier Transform Module used by DFT, FFT, RFFT
function FourierTransform(bufferSize, sampleRate) {
  this.bufferSize = bufferSize;
  this.sampleRate = sampleRate;
  this.bandwidth  = 2 / bufferSize * sampleRate / 2;

  this.spectrum   = new Float32Array(bufferSize/2);
  this.real       = new Float32Array(bufferSize);
  this.imag       = new Float32Array(bufferSize);

  this.peakBand   = 0;
  this.peak       = 0;

  /**
   * Calculates the *middle* frequency of an FFT band.
   *
   * @param {Number} index The index of the FFT band.
   *
   * @returns The middle frequency in Hz.
   */
  this.getBandFrequency = function(index) {
    return this.bandwidth * index + this.bandwidth / 2;
  };

  this.calculateSpectrum = function() {
    var spectrum  = this.spectrum,
        real      = this.real,
        imag      = this.imag,
        bSi       = 2 / this.bufferSize,
        sqrt      = Math.sqrt,
        rval, 
        ival,
        mag;

    for (var i = 0, N = bufferSize/2; i < N; i++) {
      rval = real[i];
      ival = imag[i];
      mag = bSi * sqrt(rval * rval + ival * ival);

      if (mag > this.peak) {
        this.peakBand = i;
        this.peak = mag;
      }

      spectrum[i] = mag;
    }
  };
}

/**
 * DFT is a class for calculating the Discrete Fourier Transform of a signal.
 *
 * @param {Number} bufferSize The size of the sample buffer to be computed
 * @param {Number} sampleRate The sampleRate of the buffer (eg. 44100)
 *
 * @constructor
 */
function DFT(bufferSize, sampleRate) {
  FourierTransform.call(this, bufferSize, sampleRate);

  var N = bufferSize/2 * bufferSize;
  var TWO_PI = 2 * Math.PI;

  this.sinTable = new Float32Array(N);
  this.cosTable = new Float32Array(N);

  for (var i = 0; i < N; i++) {
    this.sinTable[i] = Math.sin(i * TWO_PI / bufferSize);
    this.cosTable[i] = Math.cos(i * TWO_PI / bufferSize);
  }
}

/**
 * Performs a forward transform on the sample buffer.
 * Converts a time domain signal to frequency domain spectra.
 *
 * @param {Array} buffer The sample buffer
 *
 * @returns The frequency spectrum array
 */
DFT.prototype.forward = function(buffer) {
  var real = this.real, 
      imag = this.imag,
      rval,
      ival;

  for (var k = 0; k < this.bufferSize/2; k++) {
    rval = 0.0;
    ival = 0.0;

    for (var n = 0; n < buffer.length; n++) {
      rval += this.cosTable[k*n] * buffer[n];
      ival += this.sinTable[k*n] * buffer[n];
    }

    real[k] = rval;
    imag[k] = ival;
  }

  return this.calculateSpectrum();
};


/**
 * FFT is a class for calculating the Discrete Fourier Transform of a signal
 * with the Fast Fourier Transform algorithm.
 *
 * @param {Number} bufferSize The size of the sample buffer to be computed. Must be power of 2
 * @param {Number} sampleRate The sampleRate of the buffer (eg. 44100)
 *
 * @constructor
 */
function FFT(bufferSize, sampleRate) {
  FourierTransform.call(this, bufferSize, sampleRate);
   
  this.reverseTable = new Uint32Array(bufferSize);

  var limit = 1;
  var bit = bufferSize >> 1;

  var i;

  while (limit < bufferSize) {
    for (i = 0; i < limit; i++) {
      this.reverseTable[i + limit] = this.reverseTable[i] + bit;
    }

    limit = limit << 1;
    bit = bit >> 1;
  }

  this.sinTable = new Float32Array(bufferSize);
  this.cosTable = new Float32Array(bufferSize);

  for (i = 0; i < bufferSize; i++) {
    this.sinTable[i] = Math.sin(-Math.PI/i);
    this.cosTable[i] = Math.cos(-Math.PI/i);
  }
}

/**
 * Performs a forward transform on the sample buffer.
 * Converts a time domain signal to frequency domain spectra.
 *
 * @param {Array} buffer The sample buffer. Buffer Length must be power of 2
 *
 * @returns The frequency spectrum array
 */
FFT.prototype.forward = function(buffer) {
  // Locally scope variables for speed up
  var bufferSize      = this.bufferSize,
      cosTable        = this.cosTable,
      sinTable        = this.sinTable,
      reverseTable    = this.reverseTable,
      real            = this.real,
      imag            = this.imag,
      spectrum        = this.spectrum;

  var k = Math.floor(Math.log(bufferSize) / Math.LN2);

  if (Math.pow(2, k) !== bufferSize) { throw "Invalid buffer size, must be a power of 2."; }
  if (bufferSize !== buffer.length)  { throw "Supplied buffer is not the same size as defined FFT. FFT Size: " + bufferSize + " Buffer Size: " + buffer.length; }

  var halfSize = 1,
      phaseShiftStepReal,
      phaseShiftStepImag,
      currentPhaseShiftReal,
      currentPhaseShiftImag,
      off,
      tr,
      ti,
      tmpReal,
      i;

  for (i = 0; i < bufferSize; i++) {
    real[i] = buffer[reverseTable[i]];
    imag[i] = 0;
  }

  while (halfSize < bufferSize) {
    //phaseShiftStepReal = Math.cos(-Math.PI/halfSize);
    //phaseShiftStepImag = Math.sin(-Math.PI/halfSize);
    phaseShiftStepReal = cosTable[halfSize];
    phaseShiftStepImag = sinTable[halfSize];
    
    currentPhaseShiftReal = 1;
    currentPhaseShiftImag = 0;

    for (var fftStep = 0; fftStep < halfSize; fftStep++) {
      i = fftStep;

      while (i < bufferSize) {
        off = i + halfSize;
        tr = (currentPhaseShiftReal * real[off]) - (currentPhaseShiftImag * imag[off]);
        ti = (currentPhaseShiftReal * imag[off]) + (currentPhaseShiftImag * real[off]);

        real[off] = real[i] - tr;
        imag[off] = imag[i] - ti;
        real[i] += tr;
        imag[i] += ti;

        i += halfSize << 1;
      }

      tmpReal = currentPhaseShiftReal;
      currentPhaseShiftReal = (tmpReal * phaseShiftStepReal) - (currentPhaseShiftImag * phaseShiftStepImag);
      currentPhaseShiftImag = (tmpReal * phaseShiftStepImag) + (currentPhaseShiftImag * phaseShiftStepReal);
    }

    halfSize = halfSize << 1;
  }

  return this.calculateSpectrum();
};

FFT.prototype.inverse = function(real, imag) {
  // Locally scope variables for speed up
  var bufferSize      = this.bufferSize,
      cosTable        = this.cosTable,
      sinTable        = this.sinTable,
      reverseTable    = this.reverseTable,
      spectrum        = this.spectrum;
     
      real = real || this.real;
      imag = imag || this.imag;

  var halfSize = 1,
      phaseShiftStepReal,
      phaseShiftStepImag,
      currentPhaseShiftReal,
      currentPhaseShiftImag,
      off,
      tr,
      ti,
      tmpReal,
      i;

  for (i = 0; i < bufferSize; i++) {
    imag[i] *= -1;
  }

  var revReal = new Float32Array(bufferSize);
  var revImag = new Float32Array(bufferSize);
 
  for (i = 0; i < real.length; i++) {
    revReal[i] = real[reverseTable[i]];
    revImag[i] = imag[reverseTable[i]];
  }
 
  real = revReal;
  imag = revImag;

  while (halfSize < bufferSize) {
    phaseShiftStepReal = cosTable[halfSize];
    phaseShiftStepImag = sinTable[halfSize];
    currentPhaseShiftReal = 1;
    currentPhaseShiftImag = 0;

    for (var fftStep = 0; fftStep < halfSize; fftStep++) {
      i = fftStep;

      while (i < bufferSize) {
        off = i + halfSize;
        tr = (currentPhaseShiftReal * real[off]) - (currentPhaseShiftImag * imag[off]);
        ti = (currentPhaseShiftReal * imag[off]) + (currentPhaseShiftImag * real[off]);

        real[off] = real[i] - tr;
        imag[off] = imag[i] - ti;
        real[i] += tr;
        imag[i] += ti;

        i += halfSize << 1;
      }

      tmpReal = currentPhaseShiftReal;
      currentPhaseShiftReal = (tmpReal * phaseShiftStepReal) - (currentPhaseShiftImag * phaseShiftStepImag);
      currentPhaseShiftImag = (tmpReal * phaseShiftStepImag) + (currentPhaseShiftImag * phaseShiftStepReal);
    }

    halfSize = halfSize << 1;
  }

  var buffer = new Float32Array(bufferSize); // this should be reused instead
  for (i = 0; i < bufferSize; i++) {
    buffer[i] = real[i] / bufferSize;
  }

  return buffer;
};

/**
 * RFFT is a class for calculating the Discrete Fourier Transform of a signal
 * with the Fast Fourier Transform algorithm.
 *
 * This method currently only contains a forward transform but is highly optimized.
 *
 * @param {Number} bufferSize The size of the sample buffer to be computed. Must be power of 2
 * @param {Number} sampleRate The sampleRate of the buffer (eg. 44100)
 *
 * @constructor
 */

// lookup tables don't really gain us any speed, but they do increase
// cache footprint, so don't use them in here

// also we don't use sepearate arrays for real/imaginary parts

// this one a little more than twice as fast as the one in FFT
// however I only did the forward transform

// the rest of this was translated from C, see http://www.jjj.de/fxt/
// this is the real split radix FFT

function RFFT(bufferSize, sampleRate) {
  FourierTransform.call(this, bufferSize, sampleRate);

  this.trans = new Float32Array(bufferSize);

  this.reverseTable = new Uint32Array(bufferSize);

  // don't use a lookup table to do the permute, use this instead
  this.reverseBinPermute = function (dest, source) {
    var bufferSize  = this.bufferSize, 
        halfSize    = bufferSize >>> 1, 
        nm1         = bufferSize - 1, 
        i = 1, r = 0, h;

    dest[0] = source[0];

    do {
      r += halfSize;
      dest[i] = source[r];
      dest[r] = source[i];
      
      i++;

      h = halfSize << 1;
      while (h = h >> 1, !((r ^= h) & h));

      if (r >= i) { 
        dest[i]     = source[r]; 
        dest[r]     = source[i];

        dest[nm1-i] = source[nm1-r]; 
        dest[nm1-r] = source[nm1-i];
      }
      i++;
    } while (i < halfSize);
    dest[nm1] = source[nm1];
  };

  this.generateReverseTable = function () {
    var bufferSize  = this.bufferSize, 
        halfSize    = bufferSize >>> 1, 
        nm1         = bufferSize - 1, 
        i = 1, r = 0, h;

    this.reverseTable[0] = 0;

    do {
      r += halfSize;
      
      this.reverseTable[i] = r;
      this.reverseTable[r] = i;

      i++;

      h = halfSize << 1;
      while (h = h >> 1, !((r ^= h) & h));

      if (r >= i) { 
        this.reverseTable[i] = r;
        this.reverseTable[r] = i;

        this.reverseTable[nm1-i] = nm1-r;
        this.reverseTable[nm1-r] = nm1-i;
      }
      i++;
    } while (i < halfSize);

    this.reverseTable[nm1] = nm1;
  };

  this.generateReverseTable();
}


// Ordering of output:
//
// trans[0]     = re[0] (==zero frequency, purely real)
// trans[1]     = re[1]
//             ...
// trans[n/2-1] = re[n/2-1]
// trans[n/2]   = re[n/2]    (==nyquist frequency, purely real)
//
// trans[n/2+1] = im[n/2-1]
// trans[n/2+2] = im[n/2-2]
//             ...
// trans[n-1]   = im[1] 

RFFT.prototype.forward = function(buffer) {
  var n         = this.bufferSize, 
      spectrum  = this.spectrum,
      x         = this.trans, 
      TWO_PI    = 2*Math.PI,
      sqrt      = Math.sqrt,
      i         = n >>> 1,
      bSi       = 2 / n,
      n2, n4, n8, nn, 
      t1, t2, t3, t4, 
      i1, i2, i3, i4, i5, i6, i7, i8, 
      st1, cc1, ss1, cc3, ss3,
      e, 
      a,
      rval, ival, mag; 

  this.reverseBinPermute(x, buffer);

  /*
  var reverseTable = this.reverseTable;

  for (var k = 0, len = reverseTable.length; k < len; k++) {
    x[k] = buffer[reverseTable[k]];
  }
  */

  for (var ix = 0, id = 4; ix < n; id *= 4) {
    for (var i0 = ix; i0 < n; i0 += id) {
      //sumdiff(x[i0], x[i0+1]); // {a, b}  <--| {a+b, a-b}
      st1 = x[i0] - x[i0+1];
      x[i0] += x[i0+1];
      x[i0+1] = st1;
    } 
    ix = 2*(id-1);
  }

  n2 = 2;
  nn = n >>> 1;

  while((nn = nn >>> 1)) {
    ix = 0;
    n2 = n2 << 1;
    id = n2 << 1;
    n4 = n2 >>> 2;
    n8 = n2 >>> 3;
    do {
      if(n4 !== 1) {
        for(i0 = ix; i0 < n; i0 += id) {
          i1 = i0;
          i2 = i1 + n4;
          i3 = i2 + n4;
          i4 = i3 + n4;
     
          //diffsum3_r(x[i3], x[i4], t1); // {a, b, s} <--| {a, b-a, a+b}
          t1 = x[i3] + x[i4];
          x[i4] -= x[i3];
          //sumdiff3(x[i1], t1, x[i3]);   // {a, b, d} <--| {a+b, b, a-b}
          x[i3] = x[i1] - t1; 
          x[i1] += t1;
     
          i1 += n8;
          i2 += n8;
          i3 += n8;
          i4 += n8;
         
          //sumdiff(x[i3], x[i4], t1, t2); // {s, d}  <--| {a+b, a-b}
          t1 = x[i3] + x[i4];
          t2 = x[i3] - x[i4];
         
          t1 = -t1 * Math.SQRT1_2;
          t2 *= Math.SQRT1_2;
     
          // sumdiff(t1, x[i2], x[i4], x[i3]); // {s, d}  <--| {a+b, a-b}
          st1 = x[i2];
          x[i4] = t1 + st1; 
          x[i3] = t1 - st1;
          
          //sumdiff3(x[i1], t2, x[i2]); // {a, b, d} <--| {a+b, b, a-b}
          x[i2] = x[i1] - t2;
          x[i1] += t2;
        }
      } else {
        for(i0 = ix; i0 < n; i0 += id) {
          i1 = i0;
          i2 = i1 + n4;
          i3 = i2 + n4;
          i4 = i3 + n4;
     
          //diffsum3_r(x[i3], x[i4], t1); // {a, b, s} <--| {a, b-a, a+b}
          t1 = x[i3] + x[i4]; 
          x[i4] -= x[i3];
          
          //sumdiff3(x[i1], t1, x[i3]);   // {a, b, d} <--| {a+b, b, a-b}
          x[i3] = x[i1] - t1; 
          x[i1] += t1;
        }
      }
   
      ix = (id << 1) - n2;
      id = id << 2;
    } while (ix < n);
 
    e = TWO_PI / n2;

    for (var j = 1; j < n8; j++) {
      a = j * e;
      ss1 = Math.sin(a);
      cc1 = Math.cos(a);

      //ss3 = sin(3*a); cc3 = cos(3*a);
      cc3 = 4*cc1*(cc1*cc1-0.75);
      ss3 = 4*ss1*(0.75-ss1*ss1);
   
      ix = 0; id = n2 << 1;
      do {
        for (i0 = ix; i0 < n; i0 += id) {
          i1 = i0 + j;
          i2 = i1 + n4;
          i3 = i2 + n4;
          i4 = i3 + n4;
       
          i5 = i0 + n4 - j;
          i6 = i5 + n4;
          i7 = i6 + n4;
          i8 = i7 + n4;
       
          //cmult(c, s, x, y, &u, &v)
          //cmult(cc1, ss1, x[i7], x[i3], t2, t1); // {u,v} <--| {x*c-y*s, x*s+y*c}
          t2 = x[i7]*cc1 - x[i3]*ss1; 
          t1 = x[i7]*ss1 + x[i3]*cc1;
          
          //cmult(cc3, ss3, x[i8], x[i4], t4, t3);
          t4 = x[i8]*cc3 - x[i4]*ss3; 
          t3 = x[i8]*ss3 + x[i4]*cc3;
       
          //sumdiff(t2, t4);   // {a, b} <--| {a+b, a-b}
          st1 = t2 - t4;
          t2 += t4;
          t4 = st1;
          
          //sumdiff(t2, x[i6], x[i8], x[i3]); // {s, d}  <--| {a+b, a-b}
          //st1 = x[i6]; x[i8] = t2 + st1; x[i3] = t2 - st1;
          x[i8] = t2 + x[i6]; 
          x[i3] = t2 - x[i6];
         
          //sumdiff_r(t1, t3); // {a, b} <--| {a+b, b-a}
          st1 = t3 - t1;
          t1 += t3;
          t3 = st1;
          
          //sumdiff(t3, x[i2], x[i4], x[i7]); // {s, d}  <--| {a+b, a-b}
          //st1 = x[i2]; x[i4] = t3 + st1; x[i7] = t3 - st1;
          x[i4] = t3 + x[i2]; 
          x[i7] = t3 - x[i2];
         
          //sumdiff3(x[i1], t1, x[i6]);   // {a, b, d} <--| {a+b, b, a-b}
          x[i6] = x[i1] - t1; 
          x[i1] += t1;
          
          //diffsum3_r(t4, x[i5], x[i2]); // {a, b, s} <--| {a, b-a, a+b}
          x[i2] = t4 + x[i5]; 
          x[i5] -= t4;
        }
     
        ix = (id << 1) - n2;
        id = id << 2;
   
      } while (ix < n);
    }
  }

  while (--i) {
    rval = x[i];
    ival = x[n-i-1];
    mag = bSi * sqrt(rval * rval + ival * ival);

    if (mag > this.peak) {
      this.peakBand = i;
      this.peak = mag;
    }

    spectrum[i] = mag;
  }

  spectrum[0] = bSi * x[0];

  return spectrum;
};

function Sampler(file, bufferSize, sampleRate, playStart, playEnd, loopStart, loopEnd, loopMode) {
  this.file = file;
  this.bufferSize = bufferSize;
  this.sampleRate = sampleRate;
  this.playStart  = playStart || 0; // 0%
  this.playEnd    = playEnd   || 1; // 100%
  this.loopStart  = loopStart || 0;
  this.loopEnd    = loopEnd   || 1;
  this.loopMode   = loopMode  || DSP.OFF;
  this.loaded     = false;
  this.samples    = [];
  this.signal     = new Float32Array(bufferSize);
  this.frameCount = 0;
  this.envelope   = null;
  this.amplitude  = 1;
  this.rootFrequency = 110; // A2 110
  this.frequency  = 550;
  this.step       = this.frequency / this.rootFrequency;
  this.duration   = 0;
  this.samplesProcessed = 0;
  this.playhead   = 0;
 
  var audio = /* new Audio();*/ document.createElement("AUDIO");
  var self = this;
 
  this.loadSamples = function(event) {
    var buffer = DSP.getChannel(DSP.MIX, event.frameBuffer);
    for ( var i = 0; i < buffer.length; i++) {
      self.samples.push(buffer[i]);
    }
  };
 
  this.loadComplete = function() {
    // convert flexible js array into a fast typed array
    self.samples = new Float32Array(self.samples);
    self.loaded = true;
  };
 
  this.loadMetaData = function() {
    self.duration = audio.duration;
  };
 
  audio.addEventListener("MozAudioAvailable", this.loadSamples, false);
  audio.addEventListener("loadedmetadata", this.loadMetaData, false);
  audio.addEventListener("ended", this.loadComplete, false);
  audio.muted = true;
  audio.src = file;
  audio.play();
}

Sampler.prototype.applyEnvelope = function() {
  this.envelope.process(this.signal);
  return this.signal;
};

Sampler.prototype.generate = function() {
  var frameOffset = this.frameCount * this.bufferSize;
 
  var loopWidth = this.playEnd * this.samples.length - this.playStart * this.samples.length;
  var playStartSamples = this.playStart * this.samples.length; // ie 0.5 -> 50% of the length
  var playEndSamples = this.playEnd * this.samples.length; // ie 0.5 -> 50% of the length
  var offset;

  for ( var i = 0; i < this.bufferSize; i++ ) {
    switch (this.loopMode) {
      case DSP.OFF:
        this.playhead = Math.round(this.samplesProcessed * this.step + playStartSamples);
        if (this.playhead < (this.playEnd * this.samples.length) ) {
          this.signal[i] = this.samples[this.playhead] * this.amplitude;
        } else {
          this.signal[i] = 0;
        }
        break;
     
      case DSP.FW:
        this.playhead = Math.round((this.samplesProcessed * this.step) % loopWidth + playStartSamples);
        if (this.playhead < (this.playEnd * this.samples.length) ) {
          this.signal[i] = this.samples[this.playhead] * this.amplitude;
        }
        break;
       
      case DSP.BW:
        this.playhead = playEndSamples - Math.round((this.samplesProcessed * this.step) % loopWidth);
        if (this.playhead < (this.playEnd * this.samples.length) ) {
          this.signal[i] = this.samples[this.playhead] * this.amplitude;
        }
        break;
       
      case DSP.FWBW:
        if ( Math.floor(this.samplesProcessed * this.step / loopWidth) % 2 === 0 ) {
          this.playhead = Math.round((this.samplesProcessed * this.step) % loopWidth + playStartSamples);
        } else {
          this.playhead = playEndSamples - Math.round((this.samplesProcessed * this.step) % loopWidth);
        }  
        if (this.playhead < (this.playEnd * this.samples.length) ) {
          this.signal[i] = this.samples[this.playhead] * this.amplitude;
        }
        break;
    }
    this.samplesProcessed++;
  }

  this.frameCount++;

  return this.signal;
};

Sampler.prototype.setFreq = function(frequency) {
    var totalProcessed = this.samplesProcessed * this.step;
    this.frequency = frequency;
    this.step = this.frequency / this.rootFrequency;
    this.samplesProcessed = Math.round(totalProcessed/this.step);
};

Sampler.prototype.reset = function() {
  this.samplesProcessed = 0;
  this.playhead = 0;
};

/**
 * 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
 */
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];
}

/**
 * Set the amplitude of the signal
 *
 * @param {Number} amplitude The amplitude of the signal (between 0 and 1)
 */
Oscillator.prototype.setAmp = function(amplitude) {
  if (amplitude >= 0 && amplitude <= 1) {
    this.amplitude = amplitude;
  } else {
    throw "Amplitude out of range (0..1).";
  }
};
  
/**
 * Set the frequency of the signal
 *
 * @param {Number} frequency The frequency of the signal
 */  
Oscillator.prototype.setFreq = function(frequency) {
  this.frequency = frequency;
  this.cyclesPerSample = frequency / this.sampleRate;
};
     
// Add an oscillator
Oscillator.prototype.add = function(oscillator) {
  for ( var i = 0; i < this.bufferSize; i++ ) {
    //this.signal[i] += oscillator.valueAt(i);
    this.signal[i] += oscillator.signal[i];
  }
 
  return this.signal;
};
     
// Add a signal to the current generated osc signal
Oscillator.prototype.addSignal = function(signal) {
  for ( var i = 0; i < signal.length; i++ ) {
    if ( i >= this.bufferSize ) {
      break;
    }
    this.signal[i] += signal[i];
   
    /*
    // Constrain amplitude
    if ( this.signal[i] > 1 ) {
      this.signal[i] = 1;
    } else if ( this.signal[i] < -1 ) {
      this.signal[i] = -1;
    }
    */
  }
  return this.signal;
};
     
// Add an envelope to the oscillator
Oscillator.prototype.addEnvelope = function(envelope) {
  this.envelope = envelope;
};

Oscillator.prototype.applyEnvelope = function() {
  this.envelope.process(this.signal);
};
     
Oscillator.prototype.valueAt = function(offset) {
  return this.waveTable[offset % this.waveTableLength];
};
     
Oscillator.prototype.generate = function() {
  var frameOffset = this.frameCount * this.bufferSize;
  var step = this.waveTableLength * this.frequency / this.sampleRate;
  var offset;

  for ( var i = 0; i < this.bufferSize; i++ ) {
    //var step = (frameOffset + i) * this.cyclesPerSample % 1;
    //this.signal[i] = this.func(step) * this.amplitude;
    //this.signal[i] = this.valueAt(Math.round((frameOffset + i) * step)) * this.amplitude;
    offset = Math.round((frameOffset + i) * step);
    this.signal[i] = this.waveTable[offset % this.waveTableLength] * this.amplitude;
  }

  this.frameCount++;

  return this.signal;
};

Oscillator.Sine = function(step) {
  return Math.sin(DSP.TWO_PI * step);
};

Oscillator.Square = function(step) {
  return step < 0.5 ? 1 : -1;
};

Oscillator.Saw = function(step) {
  return 2 * (step - Math.round(step));
};

Oscillator.Triangle = function(step) {
  return 1 - 4 * Math.abs(Math.round(step) - step);
};

Oscillator.Pulse = function(step) {
  // stub
};
 
function ADSR(attackLength, decayLength, sustainLevel, sustainLength, releaseLength, sampleRate) {
  this.sampleRate = sampleRate;
  // Length in seconds
  this.attackLength  = attackLength;
  this.decayLength   = decayLength;
  this.sustainLevel  = sustainLevel;
  this.sustainLength = sustainLength;
  this.releaseLength = releaseLength;
  this.sampleRate    = sampleRate;
 
  // Length in samples
  this.attackSamples  = attackLength  * sampleRate;
  this.decaySamples   = decayLength   * sampleRate;
  this.sustainSamples = sustainLength * sampleRate;
  this.releaseSamples = releaseLength * sampleRate;
 
  // Updates the envelope sample positions
  this.update = function() {
    this.attack         =                this.attackSamples;
    this.decay          = this.attack  + this.decaySamples;
    this.sustain        = this.decay   + this.sustainSamples;
    this.release        = this.sustain + this.releaseSamples;
  };
 
  this.update();
 
  this.samplesProcessed = 0;
}

ADSR.prototype.noteOn = function() {
  this.samplesProcessed = 0;
  this.sustainSamples = this.sustainLength * this.sampleRate;
  this.update();
};

// Send a note off when using a sustain of infinity to let the envelope enter the release phase
ADSR.prototype.noteOff = function() {
  this.sustainSamples = this.samplesProcessed - this.decaySamples;
  this.update();
};

ADSR.prototype.processSample = function(sample) {
  var amplitude = 0;

  if ( this.samplesProcessed <= this.attack ) {
    amplitude = 0 + (1 - 0) * ((this.samplesProcessed - 0) / (this.attack - 0));
  } else if ( this.samplesProcessed > this.attack && this.samplesProcessed <= this.decay ) {
    amplitude = 1 + (this.sustainLevel - 1) * ((this.samplesProcessed - this.attack) / (this.decay - this.attack));
  } else if ( this.samplesProcessed > this.decay && this.samplesProcessed <= this.sustain ) {
    amplitude = this.sustainLevel;
  } else if ( this.samplesProcessed > this.sustain && this.samplesProcessed <= this.release ) {
    amplitude = this.sustainLevel + (0 - this.sustainLevel) * ((this.samplesProcessed - this.sustain) / (this.release - this.sustain));
  }
 
  return sample * amplitude;
};

ADSR.prototype.value = function() {
  var amplitude = 0;

  if ( this.samplesProcessed <= this.attack ) {
    amplitude = 0 + (1 - 0) * ((this.samplesProcessed - 0) / (this.attack - 0));
  } else if ( this.samplesProcessed > this.attack && this.samplesProcessed <= this.decay ) {
    amplitude = 1 + (this.sustainLevel - 1) * ((this.samplesProcessed - this.attack) / (this.decay - this.attack));
  } else if ( this.samplesProcessed > this.decay && this.samplesProcessed <= this.sustain ) {
    amplitude = this.sustainLevel;
  } else if ( this.samplesProcessed > this.sustain && this.samplesProcessed <= this.release ) {
    amplitude = this.sustainLevel + (0 - this.sustainLevel) * ((this.samplesProcessed - this.sustain) / (this.release - this.sustain));
  }
 
  return amplitude;
};
     
ADSR.prototype.process = function(buffer) {
  for ( var i = 0; i < buffer.length; i++ ) {
    buffer[i] *= this.value();

    this.samplesProcessed++;
  }
 
  return buffer;
};
     
     
ADSR.prototype.isActive = function() {
  if ( this.samplesProcessed > this.release || this.samplesProcessed === -1 ) {
    return false;
  } else {
    return true;
  }
};

ADSR.prototype.disable = function() {
  this.samplesProcessed = -1;
};
 
function IIRFilter(type, cutoff, resonance, sampleRate) {
  this.sampleRate = sampleRate;

  switch(type) {
    case DSP.LOWPASS:
    case DSP.LP12:
      this.func = new IIRFilter.LP12(cutoff, resonance, sampleRate);
      break;
  }
}

IIRFilter.prototype.__defineGetter__('cutoff',
  function() {
    return this.func.cutoff;
  }
);

IIRFilter.prototype.__defineGetter__('resonance',
  function() {
    return this.func.resonance;
  }
);

IIRFilter.prototype.set = function(cutoff, resonance) {
  this.func.calcCoeff(cutoff, resonance);
};

IIRFilter.prototype.process = function(buffer) {
  this.func.process(buffer);
};

// Add an envelope to the filter
IIRFilter.prototype.addEnvelope = function(envelope) {
  if ( envelope instanceof ADSR ) {
    this.func.addEnvelope(envelope);
  } else {
    throw "Not an envelope.";
  }
};

IIRFilter.LP12 = function(cutoff, resonance, sampleRate) {
  this.sampleRate = sampleRate;
  this.vibraPos   = 0;
  this.vibraSpeed = 0;
  this.envelope = false;
 
  this.calcCoeff = function(cutoff, resonance) {
    this.w = 2.0 * Math.PI * cutoff / this.sampleRate;
    this.q = 1.0 - this.w / (2.0 * (resonance + 0.5 / (1.0 + this.w)) + this.w - 2.0);
    this.r = this.q * this.q;
    this.c = this.r + 1.0 - 2.0 * Math.cos(this.w) * this.q;
   
    this.cutoff = cutoff;
    this.resonance = resonance;
  };

  this.calcCoeff(cutoff, resonance);

  this.process = function(buffer) {
    for ( var i = 0; i < buffer.length; i++ ) {
      this.vibraSpeed += (buffer[i] - this.vibraPos) * this.c;
      this.vibraPos   += this.vibraSpeed;
      this.vibraSpeed *= this.r;
   
      /*
      var temp = this.vibraPos;
     
      if ( temp > 1.0 ) {
        temp = 1.0;
      } else if ( temp < -1.0 ) {
        temp = -1.0;
      } else if ( temp != temp ) {
        temp = 1;
      }
     
      buffer[i] = temp;
      */

      if (this.envelope) {
        buffer[i] = (buffer[i] * (1 - this.envelope.value())) + (this.vibraPos * this.envelope.value());
        this.envelope.samplesProcessed++;
      } else {
        buffer[i] = this.vibraPos;
      }
    }
  };
}; 

IIRFilter.LP12.prototype.addEnvelope = function(envelope) {
  this.envelope = envelope;
};

function IIRFilter2(type, cutoff, resonance, sampleRate) {
  this.type = type;
  this.cutoff = cutoff;
  this.resonance = resonance;
  this.sampleRate = sampleRate;

  this.f = Float32Array(4);
  this.f[0] = 0.0; // lp
  this.f[1] = 0.0; // hp
  this.f[2] = 0.0; // bp
  this.f[3] = 0.0; // br 
 
  this.calcCoeff = function(cutoff, resonance) {
    this.freq = 2 * Math.sin(Math.PI * Math.min(0.25, cutoff/(this.sampleRate*2)));  
    this.damp = Math.min(2 * (1 - Math.pow(resonance, 0.25)), Math.min(2, 2/this.freq - this.freq * 0.5));
  };

  this.calcCoeff(cutoff, resonance);
}

IIRFilter2.prototype.process = function(buffer) {
  var input, output;
  var f = this.f;

  for ( var i = 0; i < buffer.length; i++ ) {
    input = buffer[i];

    // first pass
    f[3] = input - this.damp * f[2];
    f[0] = f[0] + this.freq * f[2];
    f[1] = f[3] - f[0];
    f[2] = this.freq * f[1] + f[2];
    output = 0.5 * f[this.type];

    // second pass
    f[3] = input - this.damp * f[2];
    f[0] = f[0] + this.freq * f[2];
    f[1] = f[3] - f[0];
    f[2] = this.freq * f[1] + f[2];
    output += 0.5 * f[this.type];

    if (this.envelope) {
      buffer[i] = (buffer[i] * (1 - this.envelope.value())) + (output * this.envelope.value());
      this.envelope.samplesProcessed++;
    } else {
      buffer[i] = output;
    }
  }
};

IIRFilter2.prototype.addEnvelope = function(envelope) {
  if ( envelope instanceof ADSR ) {
    this.envelope = envelope;
  } else {
    throw "This is not an envelope.";
  }
};

IIRFilter2.prototype.set = function(cutoff, resonance) {
  this.calcCoeff(cutoff, resonance);
};



function WindowFunction(type, alpha) {
  this.alpha = alpha;
 
  switch(type) {
    case DSP.BARTLETT:
      this.func = WindowFunction.Bartlett;
      break;
     
    case DSP.BARTLETTHANN:
      this.func = WindowFunction.BartlettHann;
      break;
     
    case DSP.BLACKMAN:
      this.func = WindowFunction.Blackman;
      this.alpha = this.alpha || 0.16;
      break;
   
    case DSP.COSINE:
      this.func = WindowFunction.Cosine;
      break;
     
    case DSP.GAUSS:
      this.func = WindowFunction.Gauss;
      this.alpha = this.alpha || 0.25;
      break;
     
    case DSP.HAMMING:
      this.func = WindowFunction.Hamming;
      break;
     
    case DSP.HANN:
      this.func = WindowFunction.Hann;
      break;
   
    case DSP.LANCZOS:
      this.func = WindowFunction.Lanczoz;
      break;
     
    case DSP.RECTANGULAR:
      this.func = WindowFunction.Rectangular;
      break;
     
    case DSP.TRIANGULAR:
      this.func = WindowFunction.Triangular;
      break;
  }
}

WindowFunction.prototype.process = function(buffer) {
  var length = buffer.length;
  for ( var i = 0; i < length; i++ ) {
    buffer[i] *= this.func(length, i, this.alpha);
  }
  return buffer;
};

WindowFunction.Bartlett = function(length, index) {
  return 2 / (length - 1) * ((length - 1) / 2 - Math.abs(index - (length - 1) / 2));
};

WindowFunction.BartlettHann = function(length, index) {
  return 0.62 - 0.48 * Math.abs(index / (length - 1) - 0.5) - 0.38 * Math.cos(DSP.TWO_PI * index / (length - 1));
};

WindowFunction.Blackman = function(length, index, alpha) {
  var a0 = (1 - alpha) / 2;
  var a1 = 0.5;
  var a2 = alpha / 2;

  return a0 - a1 * Math.cos(DSP.TWO_PI * index / (length - 1)) + a2 * Math.cos(4 * Math.PI * index / (length - 1));
};

WindowFunction.Cosine = function(length, index) {
  return Math.cos(Math.PI * index / (length - 1) - Math.PI / 2);
};

WindowFunction.Gauss = function(length, index, alpha) {
  return Math.pow(Math.E, -0.5 * Math.pow((index - (length - 1) / 2) / (alpha * (length - 1) / 2), 2));
};

WindowFunction.Hamming = function(length, index) {
  return 0.54 - 0.46 * Math.cos(DSP.TWO_PI * index / (length - 1));
};

WindowFunction.Hann = function(length, index) {
  return 0.5 * (1 - Math.cos(DSP.TWO_PI * index / (length - 1)));
};

WindowFunction.Lanczos = function(length, index) {
  var x = 2 * index / (length - 1) - 1;
  return Math.sin(Math.PI * x) / (Math.PI * x);
};

WindowFunction.Rectangular = function(length, index) {
  return 1;
};

WindowFunction.Triangular = function(length, index) {
  return 2 / length * (length / 2 - Math.abs(index - (length - 1) / 2));
};

function sinh (arg) {
  // Returns the hyperbolic sine of the number, defined as (exp(number) - exp(-number))/2 
  //
  // version: 1004.2314
  // discuss at: http://phpjs.org/functions/sinh    // +   original by: Onno Marsman
  // *     example 1: sinh(-0.9834330348825909);
  // *     returns 1: -1.1497971402636502
  return (Math.exp(arg) - Math.exp(-arg))/2;
}

/* 
 *  Biquad filter
 * 
 *  Created by Ricard Marxer <email@ricardmarxer.com> on 2010-05-23.
 *  Copyright 2010 Ricard Marxer. All rights reserved.
 *
 */
// Implementation based on:
// http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
function Biquad(type, sampleRate) {
  this.Fs = sampleRate;
  this.type = type;  // type of the filter
  this.parameterType = DSP.Q; // type of the parameter

  this.x_1_l = 0;
  this.x_2_l = 0;
  this.y_1_l = 0;
  this.y_2_l = 0;

  this.x_1_r = 0;
  this.x_2_r = 0;
  this.y_1_r = 0;
  this.y_2_r = 0;

  this.b0 = 1;
  this.a0 = 1;

  this.b1 = 0;
  this.a1 = 0;

  this.b2 = 0;
  this.a2 = 0;

  this.b0a0 = this.b0 / this.a0;
  this.b1a0 = this.b1 / this.a0;
  this.b2a0 = this.b2 / this.a0;
  this.a1a0 = this.a1 / this.a0;
  this.a2a0 = this.a2 / this.a0;

  this.f0 = 3000;   // "wherever it's happenin', man."  Center Frequency or
                    // Corner Frequency, or shelf midpoint frequency, depending
                    // on which filter type.  The "significant frequency".

  this.dBgain = 12; // used only for peaking and shelving filters

  this.Q = 1;       // the EE kind of definition, except for peakingEQ in which A*Q is
                    // the classic EE Q.  That adjustment in definition was made so that
                    // a boost of N dB followed by a cut of N dB for identical Q and
                    // f0/Fs results in a precisely flat unity gain filter or "wire".

  this.BW = -3;     // the bandwidth in octaves (between -3 dB frequencies for BPF
                    // and notch or between midpoint (dBgain/2) gain frequencies for
                    // peaking EQ

  this.S = 1;       // a "shelf slope" parameter (for shelving EQ only).  When S = 1,
                    // the shelf slope is as steep as it can be and remain monotonically
                    // increasing or decreasing gain with frequency.  The shelf slope, in
                    // dB/octave, remains proportional to S for all other values for a
                    // fixed f0/Fs and dBgain.

  this.coefficients = function() {
    var b = [this.b0, this.b1, this.b2];
    var a = [this.a0, this.a1, this.a2];
    return {b: b, a:a};
  };

  this.setFilterType = function(type) {
    this.type = type;
    this.recalculateCoefficients();
  };

  this.setSampleRate = function(rate) {
    this.Fs = rate;
    this.recalculateCoefficients();
  };

  this.setQ = function(q) {
    this.parameterType = DSP.Q;
    this.Q = Math.max(Math.min(q, 115.0), 0.001);
    this.recalculateCoefficients();
  };

  this.setBW = function(bw) {
    this.parameterType = DSP.BW;
    this.BW = bw;
    this.recalculateCoefficients();
  };

  this.setS = function(s) {
    this.parameterType = DSP.S;
    this.S = Math.max(Math.min(s, 5.0), 0.0001);
    this.recalculateCoefficients();
  };

  this.setF0 = function(freq) {
    this.f0 = freq;
    this.recalculateCoefficients();
  }; 
 
  this.setDbGain = function(g) {
    this.dBgain = g;
    this.recalculateCoefficients();
  };

  this.recalculateCoefficients = function() {
    var A;
    if (type === DSP.PEAKING_EQ || type === DSP.LOW_SHELF || type === DSP.HIGH_SHELF ) {
      A = Math.pow(10, (this.dBgain/40));  // for peaking and shelving EQ filters only
    } else {
      A  = Math.sqrt( Math.pow(10, (this.dBgain/20)) );   
    }

    var w0 = DSP.TWO_PI * this.f0 / this.Fs;

    var cosw0 = Math.cos(w0);
    var sinw0 = Math.sin(w0);

    var alpha = 0;
   
    switch (this.parameterType) {
      case DSP.Q:
        alpha = sinw0/(2*this.Q);
        break;
           
      case DSP.BW:
        alpha = sinw0 * sinh( Math.LN2/2 * this.BW * w0/sinw0 );
        break;

      case DSP.S:
        alpha = sinw0/2 * Math.sqrt( (A + 1/A)*(1/this.S - 1) + 2 );
        break;
    }

    /**
        FYI: The relationship between bandwidth and Q is
             1/Q = 2*sinh(ln(2)/2*BW*w0/sin(w0))     (digital filter w BLT)
        or   1/Q = 2*sinh(ln(2)/2*BW)             (analog filter prototype)

        The relationship between shelf slope and Q is
             1/Q = sqrt((A + 1/A)*(1/S - 1) + 2)
    */

    var coeff;

    switch (this.type) {
      case DSP.LPF:       // H(s) = 1 / (s^2 + s/Q + 1)
        this.b0 =  (1 - cosw0)/2;
        this.b1 =   1 - cosw0;
        this.b2 =  (1 - cosw0)/2;
        this.a0 =   1 + alpha;
        this.a1 =  -2 * cosw0;
        this.a2 =   1 - alpha;
        break;

      case DSP.HPF:       // H(s) = s^2 / (s^2 + s/Q + 1)
        this.b0 =  (1 + cosw0)/2;
        this.b1 = -(1 + cosw0);
        this.b2 =  (1 + cosw0)/2;
        this.a0 =   1 + alpha;
        this.a1 =  -2 * cosw0;
        this.a2 =   1 - alpha;
        break;

      case DSP.BPF_CONSTANT_SKIRT:       // H(s) = s / (s^2 + s/Q + 1)  (constant skirt gain, peak gain = Q)
        this.b0 =   sinw0/2;
        this.b1 =   0;
        this.b2 =  -sinw0/2;
        this.a0 =   1 + alpha;
        this.a1 =  -2*cosw0;
        this.a2 =   1 - alpha;
        break;

      case DSP.BPF_CONSTANT_PEAK:       // H(s) = (s/Q) / (s^2 + s/Q + 1)      (constant 0 dB peak gain)
        this.b0 =   alpha;
        this.b1 =   0;
        this.b2 =  -alpha;
        this.a0 =   1 + alpha;
        this.a1 =  -2*cosw0;
        this.a2 =   1 - alpha;
        break;

      case DSP.NOTCH:     // H(s) = (s^2 + 1) / (s^2 + s/Q + 1)
        this.b0 =   1;
        this.b1 =  -2*cosw0;
        this.b2 =   1;
        this.a0 =   1 + alpha;
        this.a1 =  -2*cosw0;
        this.a2 =   1 - alpha;
        break;

      case DSP.APF:       // H(s) = (s^2 - s/Q + 1) / (s^2 + s/Q + 1)
        this.b0 =   1 - alpha;
        this.b1 =  -2*cosw0;
        this.b2 =   1 + alpha;
        this.a0 =   1 + alpha;
        this.a1 =  -2*cosw0;
        this.a2 =   1 - alpha;
        break;

      case DSP.PEAKING_EQ:  // H(s) = (s^2 + s*(A/Q) + 1) / (s^2 + s/(A*Q) + 1)
        this.b0 =   1 + alpha*A;
        this.b1 =  -2*cosw0;
        this.b2 =   1 - alpha*A;
        this.a0 =   1 + alpha/A;
        this.a1 =  -2*cosw0;
        this.a2 =   1 - alpha/A;
        break;

      case DSP.LOW_SHELF:   // H(s) = A * (s^2 + (sqrt(A)/Q)*s + A)/(A*s^2 + (sqrt(A)/Q)*s + 1)
        coeff = sinw0 * Math.sqrt( (A^2 + 1)*(1/this.S - 1) + 2*A );
        this.b0 =    A*((A+1) - (A-1)*cosw0 + coeff);
        this.b1 =  2*A*((A-1) - (A+1)*cosw0);
        this.b2 =    A*((A+1) - (A-1)*cosw0 - coeff);
        this.a0 =       (A+1) + (A-1)*cosw0 + coeff;
        this.a1 =   -2*((A-1) + (A+1)*cosw0);
        this.a2 =       (A+1) + (A-1)*cosw0 - coeff;
        break;

      case DSP.HIGH_SHELF:   // H(s) = A * (A*s^2 + (sqrt(A)/Q)*s + 1)/(s^2 + (sqrt(A)/Q)*s + A)
        coeff = sinw0 * Math.sqrt( (A^2 + 1)*(1/this.S - 1) + 2*A );
        this.b0 =    A*((A+1) + (A-1)*cosw0 + coeff);
        this.b1 = -2*A*((A-1) + (A+1)*cosw0);
        this.b2 =    A*((A+1) + (A-1)*cosw0 - coeff);
        this.a0 =       (A+1) - (A-1)*cosw0 + coeff;
        this.a1 =    2*((A-1) - (A+1)*cosw0);
        this.a2 =       (A+1) - (A-1)*cosw0 - coeff;
        break;
    }
   
    this.b0a0 = this.b0/this.a0;
    this.b1a0 = this.b1/this.a0;
    this.b2a0 = this.b2/this.a0;
    this.a1a0 = this.a1/this.a0;
    this.a2a0 = this.a2/this.a0;
  };

  this.process = function(buffer) {
      //y[n] = (b0/a0)*x[n] + (b1/a0)*x[n-1] + (b2/a0)*x[n-2]
      //       - (a1/a0)*y[n-1] - (a2/a0)*y[n-2]

      var len = buffer.length;
      var output = new Float32Array(len);

      for ( var i=0; i<buffer.length; i++ ) {
        output[i] = this.b0a0*buffer[i] + this.b1a0*this.x_1_l + this.b2a0*this.x_2_l - this.a1a0*this.y_1_l - this.a2a0*this.y_2_l;
        this.y_2_l = this.y_1_l;
        this.y_1_l = output[i];
        this.x_2_l = this.x_1_l;
        this.x_1_l = buffer[i];
      }

      return output;
  };

  this.processStereo = function(buffer) {
      //y[n] = (b0/a0)*x[n] + (b1/a0)*x[n-1] + (b2/a0)*x[n-2]
      //       - (a1/a0)*y[n-1] - (a2/a0)*y[n-2]

      var len = buffer.length;
      var output = new Float32Array(len);
     
      for (var i = 0; i < len/2; i++) {
        output[2*i] = this.b0a0*buffer[2*i] + this.b1a0*this.x_1_l + this.b2a0*this.x_2_l - this.a1a0*this.y_1_l - this.a2a0*this.y_2_l;
        this.y_2_l = this.y_1_l;
        this.y_1_l = output[2*i];
        this.x_2_l = this.x_1_l;
        this.x_1_l = buffer[2*i];

        output[2*i+1] = this.b0a0*buffer[2*i+1] + this.b1a0*this.x_1_r + this.b2a0*this.x_2_r - this.a1a0*this.y_1_r - this.a2a0*this.y_2_r;
        this.y_2_r = this.y_1_r;
        this.y_1_r = output[2*i+1];
        this.x_2_r = this.x_1_r;
        this.x_1_r = buffer[2*i+1];
      }

      return output;
  };
}

/* 
 *  Magnitude to decibels
 * 
 *  Created by Ricard Marxer <email@ricardmarxer.com> on 2010-05-23.
 *  Copyright 2010 Ricard Marxer. All rights reserved.
 *
 *  @buffer array of magnitudes to convert to decibels
 *
 *  @returns the array in decibels
 *
 */
DSP.mag2db = function(buffer) {
  var minDb = -120;
  var minMag = Math.pow(10.0, minDb / 20.0);

  var log = Math.log;
  var max = Math.max;
 
  var result = Float32Array(buffer.length);
  for (var i=0; i<buffer.length; i++) {
    result[i] = 20.0*log(max(buffer[i], minMag));
  }

  return result;
};

/* 
 *  Frequency response
 * 
 *  Created by Ricard Marxer <email@ricardmarxer.com> on 2010-05-23.
 *  Copyright 2010 Ricard Marxer. All rights reserved.
 *
 *  Calculates the frequency response at the given points.
 *
 *  @b b coefficients of the filter
 *  @a a coefficients of the filter
 *  @w w points (normally between -PI and PI) where to calculate the frequency response
 *
 *  @returns the frequency response in magnitude
 *
 */
DSP.freqz = function(b, a, w) {
  var i, j;

  if (!w) {
    w = Float32Array(200);
    for (i=0;i<w.length; i++) {
      w[i] = DSP.TWO_PI/w.length * i - Math.PI;
    }
  }

  var result = Float32Array(w.length);
 
  var sqrt = Math.sqrt;
  var cos = Math.cos;
  var sin = Math.sin;
 
  for (i=0; i<w.length; i++) {
    var numerator = {real:0.0, imag:0.0};
    for (j=0; j<b.length; j++) {
      numerator.real += b[j] * cos(-j*w[i]);
      numerator.imag += b[j] * sin(-j*w[i]);
    }

    var denominator = {real:0.0, imag:0.0};
    for (j=0; j<a.length; j++) {
      denominator.real += a[j] * cos(-j*w[i]);
      denominator.imag += a[j] * sin(-j*w[i]);
    }
 
    result[i] =  sqrt(numerator.real*numerator.real + numerator.imag*numerator.imag) / sqrt(denominator.real*denominator.real + denominator.imag*denominator.imag);
  }

  return result;
};

/* 
 *  Graphical Equalizer
 *
 *  Implementation of a graphic equalizer with a configurable bands-per-octave
 *  and minimum and maximum frequencies
 * 
 *  Created by Ricard Marxer <email@ricardmarxer.com> on 2010-05-23.
 *  Copyright 2010 Ricard Marxer. All rights reserved.
 *
 */
function GraphicalEq(sampleRate) {
  this.FS = sampleRate;
  this.minFreq = 40.0;
  this.maxFreq = 16000.0;

  this.bandsPerOctave = 1.0;

  this.filters = [];
  this.freqzs = [];

  this.calculateFreqzs = true;

  this.recalculateFilters = function() {
    var bandCount = Math.round(Math.log(this.maxFreq/this.minFreq) * this.bandsPerOctave/ Math.LN2);

    this.filters = [];
    for (var i=0; i<bandCount; i++) {
      var freq = this.minFreq*(Math.pow(2, i/this.bandsPerOctave));
      var newFilter = new Biquad(DSP.PEAKING_EQ, this.FS);
      newFilter.setDbGain(0);
      newFilter.setBW(1/this.bandsPerOctave);
      newFilter.setF0(freq);
      this.filters[i] = newFilter;
      this.recalculateFreqz(i);
    }
  };

  this.setMinimumFrequency = function(freq) {
    this.minFreq = freq;
    this.recalculateFilters();
  };

  this.setMaximumFrequency = function(freq) {
    this.maxFreq = freq;
    this.recalculateFilters();
  };

  this.setBandsPerOctave = function(bands) {
    this.bandsPerOctave = bands;
    this.recalculateFilters();
  };

  this.setBandGain = function(bandIndex, gain) {
    if (bandIndex < 0 || bandIndex > (this.filters.length-1)) {
      throw "The band index of the graphical equalizer is out of bounds.";
    }

    if (!gain) {
      throw "A gain must be passed.";
    }
   
    this.filters[bandIndex].setDbGain(gain);
    this.recalculateFreqz(bandIndex);
  };
 
  this.recalculateFreqz = function(bandIndex) {
    if (!this.calculateFreqzs) {
      return;
    }

    if (bandIndex < 0 || bandIndex > (this.filters.length-1)) {
      throw "The band index of the graphical equalizer is out of bounds. " + bandIndex + " is out of [" + 0 + ", " + this.filters.length-1 + "]";
    }
       
    if (!this.w) {
      this.w = Float32Array(400);
      for (var i=0; i<this.w.length; i++) {
         this.w[i] = Math.PI/this.w.length * i;
      }
    }
   
    var b = [this.filters[bandIndex].b0, this.filters[bandIndex].b1, this.filters[bandIndex].b2];
    var a = [this.filters[bandIndex].a0, this.filters[bandIndex].a1, this.filters[bandIndex].a2];

    this.freqzs[bandIndex] = DSP.mag2db(DSP.freqz(b, a, this.w));
  };

  this.process = function(buffer) {
    var output = buffer;

    for (var i = 0; i < this.filters.length; i++) {
      output = this.filters[i].process(output);
    }

    return output;
  };

  this.processStereo = function(buffer) {
    var output = buffer;

    for (var i = 0; i < this.filters.length; i++) {
      output = this.filters[i].processStereo(output);
    }

    return output;
  };
}

/**
 * MultiDelay effect by Almer Thie (http://code.almeros.com).
 * Copyright 2010 Almer Thie. All rights reserved.
 * Example: http://code.almeros.com/code-examples/delay-firefox-audio-api/
 *
 * This is a delay that feeds it's own delayed signal back into its circular
 * buffer. Also known as a CombFilter.
 *
 * Compatible with interleaved stereo (or more channel) buffers and
 * non-interleaved mono buffers.
 *
 * @param {Number} maxDelayInSamplesSize Maximum possible delay in samples (size of circular buffer)
 * @param {Number} delayInSamples Initial delay in samples
 * @param {Number} masterVolume Initial master volume. Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 * @param {Number} delayVolume Initial feedback delay volume. Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 *
 * @constructor
 */
function MultiDelay(maxDelayInSamplesSize, delayInSamples, masterVolume, delayVolume) {
  this.delayBufferSamples   = new Float32Array(maxDelayInSamplesSize); // The maximum size of delay
  this.delayInputPointer     = delayInSamples;
  this.delayOutputPointer   = 0;
 
  this.delayInSamples   = delayInSamples;
  this.masterVolume     = masterVolume;
  this.delayVolume     = delayVolume;
}

/**
 * Change the delay time in samples.
 *
 * @param {Number} delayInSamples Delay in samples
 */
MultiDelay.prototype.setDelayInSamples = function (delayInSamples) {
  this.delayInSamples = delayInSamples;
 
  this.delayInputPointer = this.delayOutputPointer + delayInSamples;

  if (this.delayInputPointer >= this.delayBufferSamples.length-1) {
    this.delayInputPointer = this.delayInputPointer - this.delayBufferSamples.length; 
  }
};

/**
 * Change the master volume.
 *
 * @param {Number} masterVolume Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 */
MultiDelay.prototype.setMasterVolume = function(masterVolume) {
  this.masterVolume = masterVolume;
};

/**
 * Change the delay feedback volume.
 *
 * @param {Number} delayVolume Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 */
MultiDelay.prototype.setDelayVolume = function(delayVolume) {
  this.delayVolume = delayVolume;
};

/**
 * Process a given interleaved or mono non-interleaved float value Array and adds the delayed audio.
 *
 * @param {Array} samples Array containing Float values or a Float32Array
 *
 * @returns A new Float32Array interleaved or mono non-interleaved as was fed to this function.
 */
MultiDelay.prototype.process = function(samples) {
  // NB. Make a copy to put in the output samples to return.
  var outputSamples = new Float32Array(samples.length);

  for (var i=0; i<samples.length; i++) {
    // delayBufferSamples could contain initial NULL's, return silence in that case
    var delaySample = (this.delayBufferSamples[this.delayOutputPointer] === null ? 0.0 : this.delayBufferSamples[this.delayOutputPointer]);
   
    // Mix normal audio data with delayed audio
    var sample = (delaySample * this.delayVolume) + samples[i];
   
    // Add audio data with the delay in the delay buffer
    this.delayBufferSamples[this.delayInputPointer] = sample;
   
    // Return the audio with delay mix
    outputSamples[i] = sample * this.masterVolume;
   
    // Manage circulair delay buffer pointers
    this.delayInputPointer++;
    if (this.delayInputPointer >= this.delayBufferSamples.length-1) {
      this.delayInputPointer = 0;
    }
     
    this.delayOutputPointer++;
    if (this.delayOutputPointer >= this.delayBufferSamples.length-1) {
      this.delayOutputPointer = 0; 
    } 
  }
 
  return outputSamples;
};

/**
 * SingleDelay effect by Almer Thie (http://code.almeros.com).
 * Copyright 2010 Almer Thie. All rights reserved.
 * Example: See usage in Reverb class
 *
 * This is a delay that does NOT feeds it's own delayed signal back into its 
 * circular buffer, neither does it return the original signal. Also known as
 * an AllPassFilter(?).
 *
 * Compatible with interleaved stereo (or more channel) buffers and
 * non-interleaved mono buffers.
 *
 * @param {Number} maxDelayInSamplesSize Maximum possible delay in samples (size of circular buffer)
 * @param {Number} delayInSamples Initial delay in samples
 * @param {Number} delayVolume Initial feedback delay volume. Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 *
 * @constructor
 */

function SingleDelay(maxDelayInSamplesSize, delayInSamples, delayVolume) {
  this.delayBufferSamples = new Float32Array(maxDelayInSamplesSize); // The maximum size of delay
  this.delayInputPointer  = delayInSamples;
  this.delayOutputPointer = 0;
 
  this.delayInSamples     = delayInSamples;
  this.delayVolume        = delayVolume;
}

/**
 * Change the delay time in samples.
 *
 * @param {Number} delayInSamples Delay in samples
 */
SingleDelay.prototype.setDelayInSamples = function(delayInSamples) {
  this.delayInSamples = delayInSamples;
  this.delayInputPointer = this.delayOutputPointer + delayInSamples;

  if (this.delayInputPointer >= this.delayBufferSamples.length-1) {
    this.delayInputPointer = this.delayInputPointer - this.delayBufferSamples.length; 
  }
};

/**
 * Change the return signal volume.
 *
 * @param {Number} delayVolume Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 */
SingleDelay.prototype.setDelayVolume = function(delayVolume) {
  this.delayVolume = delayVolume;
};

/**
 * Process a given interleaved or mono non-interleaved float value Array and
 * returns the delayed audio.
 *
 * @param {Array} samples Array containing Float values or a Float32Array
 *
 * @returns A new Float32Array interleaved or mono non-interleaved as was fed to this function.
 */
SingleDelay.prototype.process = function(samples) {
  // NB. Make a copy to put in the output samples to return.
  var outputSamples = new Float32Array(samples.length);

  for (var i=0; i<samples.length; i++) {

    // Add audio data with the delay in the delay buffer
    this.delayBufferSamples[this.delayInputPointer] = samples[i];
   
    // delayBufferSamples could contain initial NULL's, return silence in that case
    var delaySample = this.delayBufferSamples[this.delayOutputPointer];

    // Return the audio with delay mix
    outputSamples[i] = delaySample * this.delayVolume;

    // Manage circulair delay buffer pointers
    this.delayInputPointer++;

    if (this.delayInputPointer >= this.delayBufferSamples.length-1) {
      this.delayInputPointer = 0;
    }
     
    this.delayOutputPointer++;

    if (this.delayOutputPointer >= this.delayBufferSamples.length-1) {
      this.delayOutputPointer = 0; 
    } 
  }
 
  return outputSamples;
};

/**
 * Reverb effect by Almer Thie (http://code.almeros.com).
 * Copyright 2010 Almer Thie. All rights reserved.
 * Example: http://code.almeros.com/code-examples/reverb-firefox-audio-api/
 *
 * This reverb consists of 6 SingleDelays, 6 MultiDelays and an IIRFilter2
 * for each of the two stereo channels.
 *
 * Compatible with interleaved stereo buffers only!
 *
 * @param {Number} maxDelayInSamplesSize Maximum possible delay in samples (size of circular buffers)
 * @param {Number} delayInSamples Initial delay in samples for internal (Single/Multi)delays
 * @param {Number} masterVolume Initial master volume. Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 * @param {Number} mixVolume Initial reverb signal mix volume. Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 * @param {Number} delayVolume Initial feedback delay volume for internal (Single/Multi)delays. Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 * @param {Number} dampFrequency Initial low pass filter frequency. 0 to 44100 (depending on your maximum sampling frequency)
 *
 * @constructor
 */
function Reverb(maxDelayInSamplesSize, delayInSamples, masterVolume, mixVolume, delayVolume, dampFrequency) {
  this.delayInSamples   = delayInSamples;
  this.masterVolume     = masterVolume;
  this.mixVolume       = mixVolume;
  this.delayVolume     = delayVolume;
  this.dampFrequency     = dampFrequency;
 
  this.NR_OF_MULTIDELAYS = 6;
  this.NR_OF_SINGLEDELAYS = 6;
 
  this.LOWPASSL = new IIRFilter2(DSP.LOWPASS, dampFrequency, 0, 44100);
  this.LOWPASSR = new IIRFilter2(DSP.LOWPASS, dampFrequency, 0, 44100);
 
  this.singleDelays = [];
  
  var i, delayMultiply;

  for (i = 0; i < this.NR_OF_SINGLEDELAYS; i++) {
    delayMultiply = 1.0 + (i/7.0); // 1.0, 1.1, 1.2...
    this.singleDelays[i] = new SingleDelay(maxDelayInSamplesSize, Math.round(this.delayInSamples * delayMultiply), this.delayVolume);
  }
 
  this.multiDelays = [];

  for (i = 0; i < this.NR_OF_MULTIDELAYS; i++) {
    delayMultiply = 1.0 + (i/10.0); // 1.0, 1.1, 1.2... 
    this.multiDelays[i] = new MultiDelay(maxDelayInSamplesSize, Math.round(this.delayInSamples * delayMultiply), this.masterVolume, this.delayVolume);
  }
}

/**
 * Change the delay time in samples as a base for all delays.
 *
 * @param {Number} delayInSamples Delay in samples
 */
Reverb.prototype.setDelayInSamples = function (delayInSamples){
  this.delayInSamples = delayInSamples;

  var i, delayMultiply;
 
  for (i = 0; i < this.NR_OF_SINGLEDELAYS; i++) {
    delayMultiply = 1.0 + (i/7.0); // 1.0, 1.1, 1.2...
    this.singleDelays[i].setDelayInSamples( Math.round(this.delayInSamples * delayMultiply) );
  }
   
  for (i = 0; i < this.NR_OF_MULTIDELAYS; i++) {
    delayMultiply = 1.0 + (i/10.0); // 1.0, 1.1, 1.2...
    this.multiDelays[i].setDelayInSamples( Math.round(this.delayInSamples * delayMultiply) );
  }
};

/**
 * Change the master volume.
 *
 * @param {Number} masterVolume Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 */
Reverb.prototype.setMasterVolume = function (masterVolume){
  this.masterVolume = masterVolume;
};

/**
 * Change the reverb signal mix level.
 *
 * @param {Number} mixVolume Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 */
Reverb.prototype.setMixVolume = function (mixVolume){
  this.mixVolume = mixVolume;
};

/**
 * Change all delays feedback volume.
 *
 * @param {Number} delayVolume Float value: 0.0 (silence), 1.0 (normal), >1.0 (amplify)
 */
Reverb.prototype.setDelayVolume = function (delayVolume){
  this.delayVolume = delayVolume;
 
  var i;

  for (i = 0; i<this.NR_OF_SINGLEDELAYS; i++) {
    this.singleDelays[i].setDelayVolume(this.delayVolume);
  } 
 
  for (i = 0; i<this.NR_OF_MULTIDELAYS; i++) {
    this.multiDelays[i].setDelayVolume(this.delayVolume);
  } 
};

/**
 * Change the Low Pass filter frequency.
 *
 * @param {Number} dampFrequency low pass filter frequency. 0 to 44100 (depending on your maximum sampling frequency)
 */
Reverb.prototype.setDampFrequency = function (dampFrequency){
  this.dampFrequency = dampFrequency;
 
  this.LOWPASSL.set(dampFrequency, 0);
  this.LOWPASSR.set(dampFrequency, 0); 
};

/**
 * Process a given interleaved float value Array and copies and adds the reverb signal.
 *
 * @param {Array} samples Array containing Float values or a Float32Array
 *
 * @returns A new Float32Array interleaved buffer.
 */
Reverb.prototype.process = function (interleavedSamples){ 
  // NB. Make a copy to put in the output samples to return.
  var outputSamples = new Float32Array(interleavedSamples.length);
 
  // Perform low pass on the input samples to mimick damp
  var leftRightMix = DSP.deinterleave(interleavedSamples);
  this.LOWPASSL.process( leftRightMix[DSP.LEFT] );
  this.LOWPASSR.process( leftRightMix[DSP.RIGHT] ); 
  var filteredSamples = DSP.interleave(leftRightMix[DSP.LEFT], leftRightMix[DSP.RIGHT]);

  var i;

  // Process MultiDelays in parallel
  for (i = 0; i<this.NR_OF_MULTIDELAYS; i++) {
    // Invert the signal of every even multiDelay
    outputSamples = DSP.mixSampleBuffers(outputSamples, this.multiDelays[i].process(filteredSamples), 2%i === 0, this.NR_OF_MULTIDELAYS);
  }
 
  // Process SingleDelays in series
  var singleDelaySamples = new Float32Array(outputSamples.length);
  for (i = 0; i<this.NR_OF_SINGLEDELAYS; i++) {
    // Invert the signal of every even singleDelay
    singleDelaySamples = DSP.mixSampleBuffers(singleDelaySamples, this.singleDelays[i].process(outputSamples), 2%i === 0, 1);
  }

  // Apply the volume of the reverb signal
  for (i = 0; i<singleDelaySamples.length; i++) {
    singleDelaySamples[i] *= this.mixVolume;
  }
 
  // Mix the original signal with the reverb signal
  outputSamples = DSP.mixSampleBuffers(singleDelaySamples, interleavedSamples, 0, 1);

  // Apply the master volume to the complete signal
  for (i = 0; i<outputSamples.length; i++) {
    outputSamples[i] *= this.masterVolume;
  }
   
  return outputSamples;
};

/dev/break/0001/index.html

<!DOCTYPE html>
<html>
<head>
  <title>ブロック崩しを作る(1)</title>
  <meta name="keywords" content="WebGL,HTML5,three.js" />
  <meta name="description" content="WebGL,HTML5,three.js" />
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
  <meta charset="UTF-8">
  <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.js"></script>
  <script type="text/javascript" src="dsp.js"></script>
  <script type="text/javascript" src="audio.js"></script>
  <script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-15457703-11', 'auto');
  ga('send', 'pageview');

</script>
  <style>
    html {
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
    }

    body {
      width: 100%;
      height: 100%;
      margin: 4px;
      padding: 0;
      border: 0;
      text-align: center;
      margin-left: auto;
      margin-right: auto;
    }

    #console {
      margin-left: auto;
      margin-right: auto;
      border: 0;
      padding: 0;
    }
  </style>
</head>
<body>
  <div id="content"></div>
  <script type="text/javascript">
    const ASCII_CHARS = [
        [
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,0,0,0
        ],
        [
            0,0,1,0,0,
            0,0,1,0,0,
            0,0,1,0,0,
            0,0,0,0,0,
            0,0,1,0,0
        ],
        [
            0,1,0,1,0,
            0,1,0,1,0,
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,0,0,0
        ],
        [
            0,1,0,1,0,
            1,1,1,1,1,
            0,1,0,1,0,
            1,1,1,1,1,
            0,1,0,1,0
        ],
        [
            1,1,1,1,1,
            1,0,1,0,0,
            1,1,1,1,1,
            0,0,1,0,1,
            1,1,1,1,1
        ],
        [
            1,1,0,0,1,
            1,1,0,1,0,
            0,0,1,0,0,
            0,1,0,1,1,
            1,0,0,1,1
        ],
        [
            1,1,1,0,0,
            1,0,1,0,0,
            0,1,1,0,1,
            1,0,0,1,0,
            1,1,1,0,1
        ],
        [
            0,0,1,0,0,
            0,0,1,0,0,
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,0,0,0
        ],
        [
            0,0,1,1,0,
            0,1,0,0,0,
            0,1,0,0,0,
            0,1,0,0,0,
            0,0,1,1,0
        ],
        [
            0,1,1,0,0,
            0,0,0,1,0,
            0,0,0,1,0,
            0,0,0,1,0,
            0,1,1,0,0
        ],
        [
            1,0,1,0,1,
            0,1,1,1,0,
            1,1,1,1,1,
            0,1,1,1,0,
            1,0,1,0,1
        ],
        [
            0,0,1,0,0,
            0,0,1,0,0,
            1,1,1,1,1,
            0,0,1,0,0,
            0,0,1,0,0
        ],
        [
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,1,0,0,
            0,1,0,0,0
        ],
        [
            0,0,0,0,0,
            0,0,0,0,0,
            1,1,1,1,1,
            0,0,0,0,0,
            0,0,0,0,0
        ],
        [
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,1,0,0
        ],
        [
            0,0,0,0,1,
            0,0,0,1,0,
            0,0,1,0,0,
            0,1,0,0,0,
            1,0,0,0,0
        ],
        [1,1,1,1,1,
         1,1,0,0,1,
         1,0,1,0,1,
         1,0,0,1,1,
         1,1,1,1,1],
        [0,1,1,0,0,
         0,0,1,0,0,
         0,0,1,0,0,
         0,0,1,0,0,
         0,1,1,1,0],
        [1,1,1,1,1,
         1,0,0,0,1,
         0,0,1,1,0,
         0,1,0,0,0,
         1,1,1,1,1],
        [1,1,1,1,1,
         0,0,0,0,1,
         1,1,1,1,1,
         0,0,0,0,1,
         1,1,1,1,1],
        [1,0,0,1,0,
         1,0,0,1,0,
         1,0,0,1,0,
         1,1,1,1,1,
         0,0,0,1,0],
        [1,1,1,1,1,
         1,0,0,0,0,
         1,1,1,1,1,
         0,0,0,0,1,
         1,1,1,1,1],
        [1,1,1,1,1,
         1,0,0,0,0,
         1,1,1,1,1,
         1,0,0,0,1,
         1,1,1,1,1],
        [1,1,1,1,1,
         1,0,0,1,0,
         0,0,1,0,0,
         0,1,0,0,0,
         1,0,0,0,0],
        [1,1,1,1,1,
         1,0,0,0,1,
         1,1,1,1,1,
         1,0,0,0,1,
         1,1,1,1,1],
        [1,1,1,1,1,
         1,0,0,0,1,
         1,1,1,1,1,
         0,0,0,0,1,
         1,1,1,1,1],
        [
            0,0,0,0,0,
            0,0,1,0,0,
            0,0,0,0,0,
            0,0,1,0,0,
            0,0,0,0,0
        ],
        [
            0,0,0,0,0,
            0,0,1,0,0,
            0,0,0,0,0,
            0,0,1,0,0,
            0,1,0,0,0
        ],
        [
            0,0,0,1,0,
            0,0,1,0,0,
            0,1,0,0,0,
            0,0,1,0,0,
            0,0,0,1,0
        ],
        [
            0,0,0,0,0,
            1,1,1,1,1,
            0,0,0,0,0,
            1,1,1,1,1,
            0,0,0,0,0
        ],
        [
            0,1,0,0,0,
            0,0,1,0,0,
            0,0,0,1,0,
            0,0,1,0,0,
            0,1,0,0,0
        ],
        [
            1,1,1,1,1,
            1,0,0,0,1,
            0,0,1,1,1,
            0,0,0,0,0,
            0,0,1,0,0
        ],
        [
            0,1,1,1,0,
            1,0,0,0,1,
            1,0,1,1,1,
            1,0,0,0,0,
            0,1,1,1,1
        ],
        [
            1,1,1,1,1,
            1,0,0,0,1,
            1,1,1,1,1,
            1,0,0,0,1,
            1,0,0,0,1
        ],
        [
            1,1,1,1,1,
            1,0,0,0,1,
            1,1,1,1,0,
            1,0,0,0,1,
            1,1,1,1,1
        ],
        [
            1,1,1,1,1,
            1,0,0,0,0,
            1,0,0,0,0,
            1,0,0,0,0,
            1,1,1,1,1
        ],
        [
            1,1,1,1,0,
            1,0,0,0,1,
            1,0,0,0,1,
            1,0,0,0,1,
            1,1,1,1,0
        ],
        [
            1,1,1,1,1,
            1,0,0,0,0,
            1,1,1,1,1,
            1,0,0,0,0,
            1,1,1,1,1
        ],
        [
            1,1,1,1,1,
            1,0,0,0,0,
            1,1,1,1,1,
            1,0,0,0,0,
            1,0,0,0,0
        ],
        [
            1,1,1,1,1,
            1,0,0,0,0,
            1,0,1,1,1,
            1,0,0,0,1,
            1,1,1,1,1
        ],
        [
            1,0,0,0,1,
            1,0,0,0,1,
            1,1,1,1,1,
            1,0,0,0,1,
            1,0,0,0,1
        ],
        [
            0,0,1,0,0,
            0,0,1,0,0,
            0,0,1,0,0,
            0,0,1,0,0,
            0,0,1,0,0
        ],
        [
            1,1,1,1,1,
            0,0,1,0,0,
            0,0,1,0,0,
            1,0,1,0,0,
            1,1,1,0,0
        ],
        [
            1,0,0,,1,
            1,0,0,1,0,
            1,1,1,0,0,
            1,0,0,1,0,
            1,0,0,0,1
        ],
        [
            1,0,0,0,0,
            1,0,0,0,0,
            1,0,0,0,0,
            1,0,0,0,0,
            1,1,1,1,1
        ],
        [
            1,1,0,1,1,
            1,0,1,0,1,
            1,0,0,0,1,
            1,0,0,0,1,
            1,0,0,0,1
        ],
        [
            1,0,0,0,1,
            1,1,0,0,1,
            1,0,1,0,1,
            1,0,0,1,1,
            1,0,0,0,1
        ],
        [
            1,1,1,1,1,
            1,0,0,0,1,
            1,0,0,0,1,
            1,0,0,0,1,
            1,1,1,1,1
        ],
        [
            1,1,1,1,1,
            1,0,0,0,1,
            1,1,1,1,1,
            1,0,0,0,0,
            1,0,0,0,0
        ],
        [
            1,1,1,1,1,
            1,0,0,0,1,
            1,0,0,0,1,
            1,0,0,1,1,
            1,1,1,1,1
        ],
        [
            1,1,1,1,1,
            1,0,0,0,1,
            1,1,1,1,0,
            1,0,0,0,1,
            1,0,0,0,1
        ],
        [
            1,1,1,1,1,
            1,0,0,0,0,
            1,1,1,1,1,
            0,0,0,0,1,
            1,1,1,1,1
        ],
        [
            1,1,1,1,1,
            0,0,1,0,0,
            0,0,1,0,0,
            0,0,1,0,0,
            0,0,1,0,0
        ],
        [
            1,0,0,0,1,
            1,0,0,0,1,
            1,0,0,0,1,
            1,0,0,0,1,
            1,1,1,1,1
        ],
        [
            1,0,0,0,1,
            1,0,0,0,1,
            0,1,0,1,0,
            0,1,0,1,0,
            0,0,1,0,0
        ],
        [
            1,0,1,0,1,
            1,0,1,0,1,
            1,0,1,0,1,
            1,0,1,0,1,
            1,1,1,1,1
        ],
        [
            1,0,0,0,1,
            0,1,0,1,0,
            0,0,1,0,0,
            0,1,0,1,0,
            1,0,0,0,1
        ],
        [
            1,0,0,0,1,
            0,1,0,1,0,
            0,0,1,0,0,
            0,0,1,0,0,
            0,0,1,0,0
        ],
        [
            1,1,1,1,1,
            0,0,0,1,0,
            0,0,1,0,0,
            0,1,0,0,0,
            1,1,1,1,1
        ]
    ];

    window.addEventListener('load',
    function () {

      'use strict';
      const WIDTH = 192;
      const HEIGHT = 256;
      var screen_width;
      var screen_height;
      const INITIAL_REMAINS = 9;
      var remains = INITIAL_REMAINS;
      var score = 0;
      var stage = 0;
      var renderer;
      var x = 0;
      var y = 0;
      var px;// paddle x pos
      var speed = 2;
      var dx = Math.cos(Math.PI / 4 ) * speed;
      var dy = Math.sin(Math.PI / 4 ) * speed;

      function calcScreenSize() {
        screen_width = document.body.clientWidth - 8;
        screen_height = document.body.clientHeight - 8;
        if (screen_width >= screen_height) {
          screen_width = screen_height * WIDTH / HEIGHT;
        } else {
          screen_height = screen_width * HEIGHT / WIDTH;
        }
      }

      calcScreenSize();

      initAudio(window);
      SF.audio.start();

      renderer = new THREE.WebGLRenderer({ antialias: false /*, sortObjects: true */ });
      renderer.setSize(screen_width, screen_height);
      renderer.setClearColor(0x000000, 1);
      renderer.domElement.id = 'console';
      renderer.domElement.style.zIndex = 0;
      document.body.appendChild(renderer.domElement);
      renderer.clear();
      // Pointer Lock API
      var isPointerLocked = false;
      var isPointerRequesting = false;
      var elm = renderer.domElement;//document.body;
      function pointerLockChange() {
        if (
            document.pointerLockElement === elm ||
            document.mozPointerLockElement === elm ||
            document.webkitPointerLockElement === elm) {
          isPointerLocked = true;
        } else {
          isPointerLocked = false;
        }
        isPointerRequesting = false;

      }
      document.addEventListener('pointerlockchange',pointerLockChange,false);
      document.addEventListener('mozpointerlockchange',pointerLockChange,false);
      document.addEventListener('webkitpointerlockchange',pointerLockChange,false);
      elm.requestPointerLock = elm.requestPointerLock    ||
                                elm.mozRequestPointerLock ||
                                elm.webkitRequestPointerLock;



      // カメラを工夫し、Z座標が0の時座標指定が仮想画面サイズの位置となるようにする
      var camera = new THREE.PerspectiveCamera(90, WIDTH / HEIGHT, 0.1, 1000);
      camera.position.z = HEIGHT / 2;
      var scene = new THREE.Scene();
      var geometry = new THREE.PlaneBufferGeometry(4, 4);
      var material = new THREE.MeshBasicMaterial({ color: 0xffffff });
      var ball = new THREE.Mesh(geometry, material);
      var ballSegment = {s:new THREE.Vector2(),v:new THREE.Vector2()};
      var paddleWidth = 32;
      var paddle = new THREE.Mesh(new THREE.PlaneBufferGeometry(paddleWidth, 4), new THREE.MeshBasicMaterial({ color: 0xffffff }));
      var paddleSegment = { s:new THREE.Vector2(),v:new THREE.Vector2(32,0)};
      var paddlePosY = -100;

      var blocks = [];
      paddle.position.y = paddlePosY;

      // ブロックの生成
      var blockWidth = 16;
      var blockHeight = 5;
      var blockGeometry = new THREE.PlaneBufferGeometry(blockWidth-2,blockHeight-1);
      var blockMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });

      for(var y = 70;y < 110;y += 5){
        for(var x = (-96 + 8);x < (-96 + 16 * 12 + 8);x += 16){
          var mesh = new THREE.Mesh(blockGeometry,blockMaterial);
          mesh.position.x = x;
          mesh.position.y = y;
          mesh.visible = false;
          blocks.push({
            mesh:mesh,
            pos:new THREE.Vector2(x,y),
            rect:{left:x - blockWidth/2,top:y + blockHeight/2,right:x + blockWidth/2,bottom:y - blockHeight/2},
            segment:[
              { s: new THREE.Vector2(x - blockWidth/2,y + blockHeight/2),v:new THREE.Vector2(blockWidth,0)},
              { s: new THREE.Vector2(x - blockWidth/2,y - blockHeight/2),v:new THREE.Vector2(blockWidth,0)},
              { s: new THREE.Vector2(x - blockWidth/2,y + blockHeight/2),v:new THREE.Vector2(0,blockHeight)},
              { s: new THREE.Vector2(x + blockWidth/2,y + blockHeight/2),v:new THREE.Vector2(0,blockHeight)},
            ]
          });
          scene.add(mesh);
        }
      }

      blocks.rect = {left:-96+8,top:110+5,right:-96+16 * 12 + 8,bottom:60-5};

      blocks.show = function(){
        this.forEach(function(elm){
          elm.mesh.visible = true;
        });
      }

      blocks.hide = function(){
        this.forEach(function(elm){
          elm.mesh.visible = false;
        });
      }

      // 文字コード -> mesh 変換
      var asciiCharObjs4 = [];
      var asciiCharObjs2 = [];
      var asciiGeometry2 = new THREE.PlaneBufferGeometry(2, 2);
      var asciiGeometry4 = new THREE.PlaneBufferGeometry(4,4);

      for(var i = 0,l = ASCII_CHARS.length;i < l;++i ){
        var c2 = new THREE.Object3D();
        var c4 = new THREE.Object3D();
        asciiCharObjs2.push(c2);
        asciiCharObjs4.push(c4);
        for(var cy = 0;cy < 5;++cy){
          for(var cx = 0;cx < 5;++cx){
            if(ASCII_CHARS[i][cy * 5 + cx]){
              var mesh = new THREE.Mesh(asciiGeometry2,material);
              mesh.position.x = cx * 2;
              mesh.position.y = 10 - cy * 2;
              c2.add(mesh);
              var mesh = new THREE.Mesh(asciiGeometry4,material);
              mesh.position.x = cx * 4;
              mesh.position.y = 10 - cy * 4;
              c4.add(mesh);

            }
          }
        }
      }

      function createStringMesh(str,size){
        if(!size)  size = 2;
        var strObj = new THREE.Object3D();
        var asciiChars = size == 4?asciiCharObjs4:asciiCharObjs2;
        for(var i = 0,l = str.length;i < l;++i){
          var sx = i * 6 * size;
          var c = str.charCodeAt(i) - 0x20;
          var co = asciiChars[c].clone();
          co.position.x = sx;
          strObj.add(co);
        }
        return strObj;
      }


      // PRESS_MOUSE 文字列
      const PRESS_MOUSE = 'PRESS MOUSE BTN';
      var pressMouse = createStringMesh(PRESS_MOUSE);
      pressMouse.position.x = - PRESS_MOUSE.length  * 2 * 6 / 2;
      pressMouse.position.y = 0;
      scene.add(pressMouse);

      // Title
      const TITLE = 'BLOCKS';
      var titleObj = createStringMesh(TITLE,4);
      titleObj.position.x = - TITLE.length  * 4 * 6 / 2;
      titleObj.position.y = 70;
      scene.add(titleObj);

      // GAME OVER
      const GAME_OVER = 'GAME OVER';
      var gameOverObj = createStringMesh(GAME_OVER,2);
      gameOverObj.position.x = - GAME_OVER.length  * 2 * 6 / 2;
      gameOverObj.position.y = 40;
      scene.add(gameOverObj);

      // CLEAR
      const CLEAR = 'CLEAR';
      var clearObj = createStringMesh(CLEAR,2);
      clearObj.position.x = - CLEAR.length  * 2 * 6 / 2;
      clearObj.position.y = 40;
      scene.add(clearObj);



      // スコア表示用
      var scoreObj = new THREE.Object3D();
      for(var i = 0;i < 5;++i){
        var sx = i * 6 * 2;
        var digit = new THREE.Object3D();
        scoreObj.add(digit);
        for(var j = 0;j < 10;++j){
          var n = asciiCharObjs2[0x10 + j].clone();
          n.position.x = sx;
          n.visible = false;
          digit.add(n);
        }
      }
      scoreObj.position.y = 110;
      scoreObj.position.x = - 6 * 2 * 5 / 2;
      scoreObj.children[0].children[0].visible = true;
      scoreObj.children[1].children[0].visible = true;
      scoreObj.children[2].children[0].visible = true;
      scoreObj.children[3].children[0].visible = true;
      scoreObj.children[4].children[0].visible = true;

      scene.add(scoreObj);
      var scoreBackup = score;
      function updateScore(){
        if(score > 99999){
          score = 99999;
        }

        var c5 = parseInt(score / 10000) % 10;
        var c4 = parseInt(score / 1000) % 10;
        var c3 = parseInt(score / 100) % 10;
        var c2 = parseInt(score / 10) % 10;
        var c1 = parseInt(score) % 10;

        var b5 = parseInt(scoreBackup / 10000) % 10;
        var b4 = parseInt(scoreBackup / 1000) % 10;
        var b3 = parseInt(scoreBackup / 100) % 10;
        var b2 = parseInt(scoreBackup / 10) % 10;
        var b1 = parseInt(scoreBackup) % 10;

        scoreObj.children[0].children[b5].visible = false;
        scoreObj.children[0].children[c5].visible = true;
        scoreObj.children[1].children[b4].visible = false;
        scoreObj.children[1].children[c4].visible = true;
        scoreObj.children[2].children[b3].visible = false;
        scoreObj.children[2].children[c3].visible = true;
        scoreObj.children[3].children[b2].visible = false;
        scoreObj.children[3].children[c2].visible = true;
        scoreObj.children[4].children[b1].visible = false;
        scoreObj.children[4].children[c1].visible = true;

        scoreBackup = score;

      }

      // 残数表示
      var remainObj = new THREE.Object3D();
      for(var j = 0;j < 10;++j){
        var n = asciiCharObjs2[j + 0x10].clone();
        n.visible = false;
        remainObj.add(n);
      }
      remainObj.position.y = -124;
      remainObj.position.x = 70;
      var remainBackup = 0;
      function updateRemain(){
        remainObj.children[remains].visible = true;
        remainObj.children[remainBackup].visible = false;
        remainBackup = remains;
      }
      scene.add(remainObj);

      var dpx = 0;
      elm.addEventListener('mousemove', function (e) {
        if(isPointerLocked){
          var movementX = e.movementX       ||
                          e.mozMovementX    ||
                          e.webkitMovementX ||
                          0,
              movementY = e.movementY       ||
                          e.mozMovementY    ||
                          e.webkitMovementY ||
                          0;
          dpx = movementX;
          px += movementX;
          if(px < ( -WIDTH / 2)) px = - WIDTH / 2;
          if(px > (  WIDTH  / 2)) px = WIDTH / 2;
        } else {
          var ex = e.clientX;
          var ey = e.clientY;
          var rect = e.target.getBoundingClientRect();
          ex -= rect.left;
          ey -= rect.top;
          dpx = px;
          px = ex * WIDTH / screen_width - WIDTH / 2;
          dpx = px - dpx;

        }
        //paddle.position.x = x * WIDTH / screen_width - WIDTH / 2;
      });

      var click = false;
      elm.addEventListener('click',function(){
        click = true;
        if((!isPointerLocked) &&  elm.requestPointerLock && window == parent){
          try {
            isPointerRequesting = true;
            elm.requestPointerLock();
          } catch (e){
            isPointerRequesting = false;
          }
        } else {
          isPointerRequesting = false;
        }
      });

      function mouseCheck(){
        var ret = click;
        click = false;
        return ret;
      }


      window.addEventListener('resize', function () {
        calcScreenSize();
        renderer.setSize(screen_width, screen_height);
      });

      scene.add(ball);
      scene.add(paddle);

      //
      // SF.sequencer.load(SF.seqData);
      SF.sequencer.start();

      // ジェネレータによるゲームメインの実装
      function* game(){
        while(true){
          // init
          remains = INITIAL_REMAINS;
          updateRemain();
          stage = 1;
          x = 0;
          y = 0;
          speed = 2;
          dx = Math.cos(Math.PI / 4 ) * speed;
          dy = Math.sin(Math.PI / 4 ) * speed;
          click =  false;
          titleObj.visible = true;
          paddle.visible = false;
          ball.visible = false;
          pressMouse.visible = true;
          remainObj.visible = false;
          gameOverObj.visible = false;
          clearObj.visible = false;
          blocks.hide();
          blocks.remains = blocks.length;
          // game start wait
          var start = false;
          while(!mouseCheck() && !start){
            for(var i = 0;i < 10;++i ){
              if(mouseCheck()){
                start = true;
                while(isPointerRequesting){
                  yield;
                }
                break;
              }
              yield;
            }
            pressMouse.visible = !pressMouse.visible;
          }
          score = 0;
          updateScore();
          titleObj.visible = false;
          pressMouse.visible = false;
          paddle.visible = true;
          ball.visible = true;
          remainObj.visible = true;
          blocks.show();

          // game play
          while(remains > 0){
            if(!play()){
              x = 0;
              y = 0;
              dx = Math.abs(dx);
              dy = Math.abs(dy);
              remains--;
              updateRemain();
            } else {
              if(blocks.remains == 0){
                // ゲームクリア
                ++stage;
                clearObj.visible = true;
                var t = 60 * 3;
                while(--t){
                  if(!(t % 15)){
                    clearObj.visible = !clearObj.visible;
                  }
                  yield;
                }

                paddleWidth = 32 - stage * 2;
                if(paddleWidth < 12) paddleWidth = 12;
                scene.remove(paddle);
                paddle = new THREE.Mesh(new THREE.PlaneBufferGeometry(paddleWidth, 4), new THREE.MeshBasicMaterial({ color: 0xffffff }));
                paddleSegment = { s:new THREE.Vector2(),v:new THREE.Vector2(paddleWidth,0)};
                paddle.position.y = paddlePosY;
                scene.add(paddle);

                clearObj.visible = false;
                blocks.remains = blocks.length;
                blocks.show();
                speed = 2;
                x = 0;
                y = 0;
                dx = Math.cos(Math.PI / 4 ) * speed;
                dy = Math.sin(Math.PI / 4 ) * speed;
                yield;
              } else {
                yield;
              }
            };
          }
          // game over
          gameOverObj.visible = true;
          for(var i = 0;i < 5 * 20;++i){
            yield;
          }
          gameOverObj.visible =false;
          continue;
        }
      }

        // ゲームプレイ
        function play(){

          // ボールの動き
          var bx = x, by = y;
          x += dx;
          y += dy;
          if (x > (WIDTH / 2) || x < (-WIDTH / 2)) {
            SF.sequencer.playTracks(SF.soundEffects[1]);
            dx = -dx;
            x += dx;
          }

          if (y > (HEIGHT / 2) ) {
            SF.sequencer.playTracks(SF.soundEffects[1]);
            dy = -dy;
            y += dy;
          }

          if(y < (-HEIGHT / 2)){
            return false;
          }

          ball.position.x = x;
          ball.position.y = y;
          ballSegment.s.x = bx;
          ballSegment.s.y = by;
          ballSegment.v.x = dx;
          ballSegment.v.y = dy;

          paddle.position.x = px;
          paddleSegment.s.x = px - dpx - paddleWidth / 2 ;
          paddleSegment.s.y = paddlePosY;
          paddleSegment.v.x = paddleWidth + dpx;


          // パドルとの衝突判定
          var result = colSegments(ballSegment,paddleSegment);
          if(result){
            SF.sequencer.playTracks(SF.soundEffects[0]);
            dy = - dy;
            dx = (result.pos.x - paddle.position.x)/4;
            var v = speed / Math.sqrt(dx*dx + dy*dy);

            dy *= v;
            dx *= v;
            y = result.pos.y + (1.0 - result.t1) * dy;
            x = result.pos.x + (1.0 - result.t1) * dx;
            ball.position.y = y;
            ball.position.x = x;
          }

          dpx = 0;

          // ブロックとの衝突判定
          function colBlock(result,block,topBottom){

            SF.sequencer.playTracks(SF.soundEffects[2]);
            // 徐々に難易度を上げていく
            if (speed < 4.0){
              speed += stage / 40;
            }
            if(topBottom){
              dy = -dy;
            } else {
              dx = -dx;
            }
            var v = speed / Math.sqrt(dx*dx + dy*dy);

            dy *= v;
            dx *= v;

            y = result.pos.y + (1.0 - result.t1) * dy;
            x = result.pos.x + (1.0 - result.t1) * dx;
            ball.position.y = y;
            ball.position.x = x;
            block.mesh.visible = false;
            ++score;
            updateScore();
            blocks.remains--;
          }

          if(ball.position.y > blocks.rect.bottom){
            for(var i = 0,l = blocks.length;i < l;++i){
              var block = blocks[i];
              var result;
              if(block.mesh.visible){
                if(dy > 0){
                  result = colSegments(ballSegment,block.segment[0]);
                  if(result){
                    colBlock(result,block,true);
                    break;
                  }
                  result = colSegments(ballSegment,block.segment[1]);
                  if(result){
                    colBlock(result,block,true);
                    break;
                  }
                } else {
                  result = colSegments(ballSegment,block.segment[1]);
                  if(result){
                    colBlock(result,block,true);
                    break;
                  }
                  result = colSegments(ballSegment,block.segment[0]);
                  if(result){
                    colBlock(result,block,true);
                    break;
                  }
                }
                if(dx > 0){
                  result = colSegments(ballSegment,block.segment[2]);
                  if(result){
                    colBlock(result,block,false);
                    break;
                  }
                  result = colSegments(ballSegment,block.segment[3]);
                  if(result){
                    colBlock(result,block,false);
                    break;
                  }

                } else {
                  result = colSegments(ballSegment,block.segment[3]);
                  if(result){
                    colBlock(result,block,false);
                    break;
                  }
                  result = colSegments(ballSegment,block.segment[2]);
                  if(result){
                    colBlock(result,block,false);
                    break;
                  }

                }

              }
            }
          }

          return true;
        }

          // 2Dベクトルの外積
          function crossProduct(v1, v2 ) {
            return v1.x * v2.y - v1.y * v2.x;
          }

          // 線分の衝突
          // 参考:http://marupeke296.com/COL_2D_No10_SegmentAndSegment.html
          function colSegments(
             seg1,          // 線分1
             seg2          // 線分2
          ) {

            var v = new THREE.Vector2();
            v.copy(seg2.s);
            v.sub(seg1.s);
            var crsV1V2 = crossProduct( seg1.v, seg2.v );
            if ( crsV1V2 == 0 ) {
              // 平行状態
              return false;
            }

            var crsVV1 = crossProduct( v, seg1.v );
            var crsVV2 = crossProduct( v, seg2.v );

            var t1 = crsVV2 / crsV1V2;
            var t2 = crsVV1 / crsV1V2;

            const eps = 0.00002;
            if ( t1 + eps < 0 || t1 - eps > 1 || t2 + eps < 0 || t2 - eps > 1 ) {
              // 交差していない
              return false;
            }
            var pos = new THREE.Vector2();
            var vec = new THREE.Vector2();
            pos.copy(seg1.s);
            vec.copy(seg1.v);
            return {
              pos : pos.add(vec.multiplyScalar(t1)),
              t1 : t1,
              t2 : t2
            };
          }

          //
          var g = game();

          function render() {
            requestAnimationFrame(render);
            renderer.render(scene, camera);
            g.next();
          }
          render();
        });
  </script>
</body>
</html>

/dev/break/0001/README.md

### ブロック崩しを作る

* あまり面白くない。
* 下記URLでプレーするほうがま多少面白い。
 [http://bl.ocks.org/sfpgmr/raw/39dfb82a46dda45ea597/](http://bl.ocks.org/sfpgmr/raw/39dfb82a46dda45ea597/)


### 今のところ

* Chrome,Firefoxでは動作
* Edgeではabout:flagsにて「試験的なJavaScript機能を有効にする」にチェックを入れると動作する。またPointer Lockができない。


/dev/break/0001/song.js

/// <reference path="../../../scripts/dsp.js" />
/// <reference path="../../../scripts/three/three.js" />
/// <reference path="graphics.js" />
/// <reference path="io.js" />
/// <reference path="song.js" />
/// <reference path="audio.js" />
/// <reference path="text.js" />
/// <reference path="util.js" />
/// <reference path="gameobj.js" />
/// <reference path="enemies.js" />
/// <reference path="effectobj.js" />
/// <reference path="myship.js" />
/// <reference path="game.js" />

//var seq = [T(120), O(5), l(4), LS(128), V(0.5), N(C({}), D, E, F, G, A, B),o(6)];
var seqData = {
  name: 'Test',
  tracks: [
    {
      name: 'part1',
      channel: 0,
      data:
      [
        ENV(0.01, 0.02, 0.5, 0.07),
        TEMPO(180), TONE(0), VOLUME(0.5), L(8), GT(-0.5),O(4),
        LOOP('i',4),
        C, C, C, C, C, C, C, C,
        LOOP_END,
        JUMP(6)
      ]
    },
    {
      name: 'part2',
      channel: 1,
      data:
        [
        ENV(0.01, 0.05, 0.6, 0.07),
        TONE(6), VOLUME(0.2), L(8), GT(-0.8),O(6),
        R(1), R(1),
        L(1), F,
        E,
        OD, L(8, true), Bb, G, L(4), Bb, OU, L(4), F, L(8), D,
        L(4, true), E, L(2), C,R(8),
        JUMP(7)
        ]
    },
    {
      name: 'part3',
      channel: 2,
      data:
        [
        ENV(0.01, 0.05, 0.6, 0.07),
        TONE(6), VOLUME(0.1), L(8), GT(-0.5), O(6),DETUNE(0.992),
        R(1), R(1),
        O(6),L(1), C,
        C,
        OD, L(8, true), G, D, L(4), G, OU, L(4), D, L(8),OD, G,
        L(4, true), OU,C, L(2),OD, G, R(8),
        JUMP(8)
        ]
    }
  ]
}

var soundEffects;
 function SoundEffects(sequencer) {
   this.soundEffects =
    [
    // Effect 0 ////////////////////////////////////
    createTracks.call(sequencer,[
    {
      channel: 8,
      oneshot:true,
      data: [VOLUME(0.5),
        ENV(0.0001, 0.01, 1.0, 0.0001),GT(-0.999),TONE(0), TEMPO(200), O(8),ST(3), C, D, E, F, G, A, B, OU, C, D, E, G, A, B,B,B,B
      ]
    },
    {
      channel: 9,
      oneshot: true,
      data: [VOLUME(0.5),
        ENV(0.0001, 0.01, 1.0, 0.0001), DETUNE(0.9), GT(-0.999), TONE(0), TEMPO(200), O(5), ST(3), C, D, E, F, G, A, B, OU, C, D, E, G, A, B,B,B,B
      ]
    }
    ]),
    // Effect 1 /////////////////////////////////////
    createTracks.call(sequencer,
      [
        {
          channel: 10,
          oneshot: true,
          data: [
           TONE(4), TEMPO(150), ST(4), GT(-0.9999), ENV(0.0001, 0.0001, 1.0, 0.0001),
           O(6), G, A, B, O(7), B, A, G, F, E, D, C, E, G, A, B, OD, B, A, G, F, E, D, C, OD, B, A, G, F, E, D, C
          ]
        }
      ]),
    // Effect 2//////////////////////////////////////
    createTracks.call(sequencer,
      [
        {
          channel: 10,
          oneshot: true,
          data: [
           TONE(0), TEMPO(150), ST(2), GT(-0.9999), ENV(0.0001, 0.0001, 1.0, 0.0001),
           O(8), C,D,E,F,G,A,B,OU,C,D,E,F,OD,G,OU,A,OD,B,OU,A,OD,G,OU,F,OD,E,OU,E
          ]
        }
      ]),
      // Effect 3 ////////////////////////////////////
      createTracks.call(sequencer,
        [
          {
            channel: 10,
            oneshot: true,
            data: [
             TONE(5), TEMPO(150), L(64), GT(-0.9999), ENV(0.0001, 0.0001, 1.0, 0.0001),
             O(6),C,OD,C,OU,C,OD,C,OU,C,OD,C,OU,C,OD
            ]
          }
        ]),
      // Effect 4 ////////////////////////////////////////
      createTracks.call(sequencer,
        [
          {
            channel: 11,
            oneshot: true,
            data: [
             TONE(8), VOLUME(2.0),TEMPO(120), L(2), GT(-0.9999), ENV(0.0001, 0.0001, 1.0, 0.25),
             O(1), C
            ]
          }
        ])
   ];
 }

/dev/break/0001/thumbnail.png