Permalink
Browse files

More dsp tests

  • Loading branch information...
oampo committed Feb 12, 2012
1 parent 36cc03e commit 3e58250164cbf13aef831bcf66c05db916f25684
Showing with 248 additions and 0 deletions.
  1. +45 −0 tests/dsp/CrossFade.js
  2. +43 −0 tests/dsp/Limiter.js
  3. +45 −0 tests/dsp/LinearCossFade.js
  4. +38 −0 tests/dsp/Pan.js
  5. +27 −0 tests/dsp/Reverb.js
  6. +27 −0 tests/dsp/SoftClip.js
  7. +23 −0 tests/dsp/WhiteNoise.js
View
@@ -0,0 +1,45 @@
+load('../../../../audiotest.js/trunk/audiotest.js');
+load('../../src/audiofile/audiofile.js');
+load('../../src/audiolet/Audiolet.js');
+load('../Environment.js');
+
+function testCrossFade() {
+ var audiolet = new Audiolet();
+ var square1 = new Square(audiolet);
+ var square2 = new Square(audiolet);
+ var mul = new Multiply(audiolet, 0.5);
+ var cf = new CrossFade(audiolet);
+ cf.position.setValue(0);
+
+ square1.connect(cf);
+ square2.connect(mul);
+ mul.connect(cf, 0, 1);
+
+ // Check position 0, should be all square1
+ square1.tick();
+ square2.tick();
+ mul.tick();
+ cf.tick();
+
+ Assert.assertEquals(cf.outputs[0].samples[0], 1);
+
+ // Check position 1, should be all square2
+ cf.position.setValue(1);
+ square1.tick();
+ square2.tick();
+ mul.tick();
+ cf.tick();
+
+ Assert.assertClose(cf.outputs[0].samples[0], 0.5);
+
+ // Check position 0.5, should be mixed using equal power law
+ cf.position.setValue(0.5);
+ square1.tick();
+ square2.tick();
+ mul.tick();
+ cf.tick();
+
+ Assert.assertEquals(cf.outputs[0].samples[0], (1 + 0.5) * Math.sqrt(2) / 2);
+}
+
+test("Cross Fade", testCrossFade);
View
@@ -0,0 +1,43 @@
+load('../../../../audiotest.js/trunk/audiotest.js');
+load('../../src/audiofile/audiofile.js');
+load('../../src/audiolet/Audiolet.js');
+load('../Environment.js');
+
+function testLimiter() {
+ var audiolet = new Audiolet();
+ var square = new Square(audiolet);
+ var gain = new Gain(audiolet, 2); // Make the square wave clip
+ var limiter = new Limiter(audiolet);
+ var recorder = new InputRecorder(audiolet, 1);
+
+ square.connect(gain);
+ gain.connect(limiter);
+ limiter.connect(recorder);
+
+ // Tick for a while so the envelope followers make it past the attack phase
+ for (var i=0; i<81920; i++) {
+ square.tick();
+ gain.tick();
+ limiter.tick();
+ recorder.tick();
+ }
+
+ var buffer = recorder.buffers[0][0];
+ Assert.assertAudibleValues(buffer);
+ Assert.assertValuesInRange(buffer, -2, 2);
+
+ // Check that we have limited the amplitude
+ recorder.reset();
+ for (var i=0; i<81920; i++) {
+ square.tick();
+ gain.tick();
+ limiter.tick();
+ recorder.tick();
+ }
+
+ var buffer = recorder.buffers[0][0];
+ Assert.assertAudibleValues(buffer);
+ Assert.assertValuesInRange(buffer, -1, 1);
+}
+
+test("Limiter", testLimiter);
@@ -0,0 +1,45 @@
+load('../../../../audiotest.js/trunk/audiotest.js');
+load('../../src/audiofile/audiofile.js');
+load('../../src/audiolet/Audiolet.js');
+load('../Environment.js');
+
+function testLinearCrossFade() {
+ var audiolet = new Audiolet();
+ var square1 = new Square(audiolet);
+ var square2 = new Square(audiolet);
+ var mul = new Multiply(audiolet, 0.5);
+ var cf = new LinearCrossFade(audiolet);
+ cf.position.setValue(0);
+
+ square1.connect(cf);
+ square2.connect(mul);
+ mul.connect(cf, 0, 1);
+
+ // Check position 0, should be all square1
+ square1.tick();
+ square2.tick();
+ mul.tick();
+ cf.tick();
+
+ Assert.assertEquals(cf.outputs[0].samples[0], 1);
+
+ // Check position 1, should be all square2
+ cf.position.setValue(1);
+ square1.tick();
+ square2.tick();
+ mul.tick();
+ cf.tick();
+
+ Assert.assertClose(cf.outputs[0].samples[0], 0.5);
+
+ // Check position 0.5, should be mixed using equal power law
+ cf.position.setValue(0.5);
+ square1.tick();
+ square2.tick();
+ mul.tick();
+ cf.tick();
+
+ Assert.assertEquals(cf.outputs[0].samples[0], (1 + 0.5) / 2);
+}
+
+test("Linear Cross Fade", testLinearCrossFade);
View
@@ -0,0 +1,38 @@
+load('../../../../audiotest.js/trunk/audiotest.js');
+load('../../src/audiofile/audiofile.js');
+load('../../src/audiolet/Audiolet.js');
+load('../Environment.js');
+
+function testPan() {
+ var audiolet = new Audiolet();
+ var square = new Square(audiolet);
+ var pan = new Pan(audiolet);
+
+ square.connect(pan);
+
+ // Check position 0, should be all left channel
+ pan.pan.setValue(0);
+ square.tick();
+ pan.tick();
+
+ Assert.assertEquals(pan.outputs[0].samples[0], 1);
+ Assert.assertEquals(pan.outputs[0].samples[1], 0);
+
+ // Check position 1, should be all right channel
+ pan.pan.setValue(1);
+ square.tick();
+ pan.tick();
+
+ Assert.assertClose(pan.outputs[0].samples[0], 0);
+ Assert.assertEquals(pan.outputs[0].samples[1], 1);
+
+ // Check position 0.5, should be centrally panned using equal power law
+ pan.pan.setValue(0.5);
+ square.tick();
+ pan.tick();
+
+ Assert.assertEquals(pan.outputs[0].samples[0], Math.sqrt(2) / 2);
+ Assert.assertEquals(pan.outputs[0].samples[0], Math.sqrt(2) / 2);
+}
+
+test("Pan", testPan);
View
@@ -0,0 +1,27 @@
+load('../../../../audiotest.js/trunk/audiotest.js');
+load('../../src/audiofile/audiofile.js');
+load('../../src/audiolet/Audiolet.js');
+load('../Environment.js');
+
+function testReverb() {
+ var audiolet = new Audiolet();
+ var sine = new Sine(audiolet);
+ var reverb = new Reverb(audiolet);
+ var recorder = new InputRecorder(audiolet, 1);
+
+ sine.connect(reverb);
+ reverb.connect(recorder);
+
+ for (var i=0; i<81920; i++) {
+ sine.tick();
+ reverb.tick();
+ recorder.tick();
+ }
+
+ var buffer = recorder.buffers[0][0];
+ Assert.assertContinuous(buffer);
+ Assert.assertAudibleValues(buffer);
+ Assert.assertValuesInRange(buffer);
+}
+
+test("Reverb", testReverb);
View
@@ -0,0 +1,27 @@
+load('../../../../audiotest.js/trunk/audiotest.js');
+load('../../src/audiofile/audiofile.js');
+load('../../src/audiolet/Audiolet.js');
+load('../Environment.js');
+
+function testSoftClip() {
+ var audiolet = new Audiolet();
+ var sine = new Sine(audiolet);
+ var softClip = new SoftClip(audiolet);
+ var recorder = new InputRecorder(audiolet, 1);
+
+ sine.connect(softClip);
+ softClip.connect(recorder);
+
+ for (var i=0; i<81920; i++) {
+ sine.tick();
+ softClip.tick();
+ recorder.tick();
+ }
+
+ var buffer = recorder.buffers[0][0];
+ Assert.assertContinuous(buffer);
+ Assert.assertAudibleValues(buffer);
+ Assert.assertValuesInRange(buffer, -0.75, 0.75); // Clipped from -1 -> 1
+}
+
+test("Soft Clip", testSoftClip);
View
@@ -0,0 +1,23 @@
+load('../../../../audiotest.js/trunk/audiotest.js');
+load('../../src/audiofile/audiofile.js');
+load('../../src/audiolet/Audiolet.js');
+load('../Environment.js');
+
+function testWhiteNoise() {
+ var audiolet = new Audiolet();
+ var whiteNoise = new WhiteNoise(audiolet);
+ var recorder = new InputRecorder(audiolet, 1);
+
+ whiteNoise.connect(recorder);
+
+ for (var i=0; i<81920; i++) {
+ whiteNoise.tick();
+ recorder.tick();
+ }
+
+ var buffer = recorder.buffers[0][0];
+ Assert.assertAudibleValues(buffer);
+ Assert.assertValuesInRange(buffer);
+}
+
+test("White Noise", testWhiteNoise);

0 comments on commit 3e58250

Please sign in to comment.