From a3fd0cacc6900b9e6293778dc79d48653b66d547 Mon Sep 17 00:00:00 2001 From: Corban Brook Date: Wed, 5 May 2010 20:40:55 +0800 Subject: [PATCH] Updated synthesizer and dsp.js --- public/synth/dsp.js | 40 ++- .../{synthesizer2.html => synthesizer.html} | 263 +++++++++++++++--- 2 files changed, 262 insertions(+), 41 deletions(-) rename public/synth/examples/{synthesizer2.html => synthesizer.html} (70%) diff --git a/public/synth/dsp.js b/public/synth/dsp.js index 3ec1d40..f4feb1c 100644 --- a/public/synth/dsp.js +++ b/public/synth/dsp.js @@ -142,7 +142,7 @@ FFT.prototype.forward = function(buffer) { return this.spectrum; }; - + Oscillator = function Oscillator(type, frequency, amplitude, bufferSize, sampleRate) { this.frequency = frequency; this.amplitude = amplitude; @@ -150,6 +150,8 @@ Oscillator = function Oscillator(type, frequency, amplitude, bufferSize, sampleR this.sampleRate = sampleRate; this.frameCount = 0; + this.waveTableLength = 2048; + this.cyclesPerSample = frequency / sampleRate; this.signal = new Array(bufferSize); @@ -175,9 +177,30 @@ Oscillator = function Oscillator(type, frequency, amplitude, bufferSize, sampleR break; } + this.generateWaveTable = function() { + Oscillator.waveTable[this.func] = Array(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]; + //this.generate(); }; + Oscillator.prototype.setAmp = function(amplitude) { if (amplitude >= 0 && amplitude <= 1) { this.amplitude = amplitude; @@ -227,16 +250,21 @@ Oscillator.prototype.addEnvelope = function(envelope) { }; Oscillator.prototype.valueAt = function(offset) { - return this.waveLength[offset % this.waveLength.length]; + //return this.waveLength[offset % this.waveLength.length]; + return this.waveTable[offset % this.waveTableLength]; }; Oscillator.prototype.generate = function() { - var frameOffset = this.frameCount * this.bufferSize, step; + var frameOffset = this.frameCount * this.bufferSize; + var step = this.waveTableLength * this.frequency / this.sampleRate; + var offset; for ( var i = 0; i < this.bufferSize; i++ ) { - step = (frameOffset + i) * this.cyclesPerSample % 1; - - this.signal[i] = this.func(step) * this.amplitude; + //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++; diff --git a/public/synth/examples/synthesizer2.html b/public/synth/examples/synthesizer.html similarity index 70% rename from public/synth/examples/synthesizer2.html rename to public/synth/examples/synthesizer.html index 575e7d0..2ceadeb 100644 --- a/public/synth/examples/synthesizer2.html +++ b/public/synth/examples/synthesizer.html @@ -1,7 +1,6 @@ - Synthesizer 2 @@ -35,7 +34,7 @@ $('#FR' ).slider({ orientation: 'vertical', range: 'min', value: 0.8, min: 0, max: 5.0, step: 0.01, slide: changeFilterEnvelope }); // Osc Mix - $('#osc_mix').slider({ orientation: 'vertical', range: 'min', value: 0.5, min: 0, max: 1.0, step: 0.01, slide: changeAmplitude }); + $('#osc_mix').slider({ orientation: 'vertical', range: 'min', value: 0.5, min: -0.1, max: 1.1, step: 0.01, slide: changeAmplitude }); changeAmplitude(); // reset amp and mix @@ -44,6 +43,20 @@ $("#debug").html(""); }); + // curve automation toggles + $("#curve_cutoff_toggle").click(function() { + if ( $("#curve_cutoff_toggle").attr("checked") ) { + SHOW_SEQUENCER = true; + CURVE_EDITOR = true; + } + }); + $("#curve_res_toggle").click(function() { + if ( $("#curve_res_toggle").attr("checked") ) { + SHOW_SEQUENCER = true; + CURVE_EDITOR = true; + } + }); + // Filter type radio selection $("input[name='filter_type']").change(changeFilter); changeFilter(); // reset filter to slider defaults @@ -55,6 +68,10 @@ // Oscillator osctave select $("#osc1_octave").change(changeOsc1); $("#osc2_octave").change(changeOsc2); + + // Oscillator osctave select + $("#osc1_semi").change(changeOsc1); + $("#osc2_semi").change(changeOsc2); changeOsc1(); // reset oscillator to select defaults changeOsc2(); @@ -104,10 +121,11 @@ @@ -454,48 +605,66 @@

Sequencer

-

Debug

+

Debug

-
+

Oscillators

- +
- - + - + - + +

Osc 1

- Waveform

Osc 1

+ - Octave + Oct + Semi +

Osc 2

Osc 2

- Waveform @@ -503,18 +672,35 @@

Oscillators

- Octave - - + + + Semi +
@@ -522,7 +708,7 @@

Oscillators

Mix

- +
@@ -560,8 +746,15 @@

Filter



+
+

Automate

+ F
+ Q +
+ +