From bbe76c0f3d7564069872c22d029bd45b00c2e78d Mon Sep 17 00:00:00 2001 From: Divyanshu Raj Date: Wed, 28 Apr 2021 13:13:33 +0530 Subject: [PATCH 1/2] added unit tests for p5.fft --- src/looper.js | 4 ++-- test/tests/p5.FFT.js | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/looper.js b/src/looper.js index 2dfaf9e5..c02e1f36 100644 --- a/src/looper.js +++ b/src/looper.js @@ -387,7 +387,7 @@ class Score { constructor() { // for all of the arguments this.parts = []; - this.currentPart = new Array(arguments.length);; + this.currentPart = new Array(arguments.length); var thisScore = this; for (var i in arguments) { @@ -396,7 +396,7 @@ class Score { this.parts[i].onended = function () { thisScore.resetPart(i); playNextPart(thisScore); - }; + }; } this.looping = false; } diff --git a/test/tests/p5.FFT.js b/test/tests/p5.FFT.js index fd83baaa..c19db02f 100644 --- a/test/tests/p5.FFT.js +++ b/test/tests/p5.FFT.js @@ -55,4 +55,55 @@ describe('p5.FFT', function () { expect(fft.smoothing).to.equal(0.8); expect(fft.smooth()).to.equal(0.8); }); + + describe('can set input correctly', function () { + let fft; + + beforeEach(function () { + fft = new p5.FFT(); + }); + + afterEach(function () { + fft.dispose(); + }); + + let soundFile, oscillator; + it('can set Oscillator as input', function (done) { + this.timeout(1000); + oscillator = new p5.Oscillator(); + fft.setInput(oscillator); + oscillator.start(); + + setTimeout(() => { + let spectrum = fft.analyze(); + const someFrequencyIsNotZero = spectrum.some( + (frequency) => frequency !== 0 + ); + expect(someFrequencyIsNotZero).to.equal(true); + oscillator.dispose(); + done(); + }, 500); + }); + it('can set soundFile as input', function () { + soundFile = p5.prototype.loadSound('./testAudio/drum.mp3', () => { + fft.setInput(soundFile); + soundFile.play(); + let spectrum = fft.analyze(); + const someFrequencyIsNotZero = spectrum.some( + (frequency) => frequency !== 0 + ); + expect(someFrequencyIsNotZero).to.equal(true); + done(); + }); + }); + it('can handle any Unknown sources', function () { + let UnknownSource = { UnknownSource: 'this is a unknown object' }; + fft.setInput(UnknownSource); + let spectrum = fft.analyze(); + const someFrequencyIsNotZero = spectrum.some( + (frequency) => frequency !== 0 + ); + expect(someFrequencyIsNotZero).to.equal(false); + }); + }); }); From a624f435bfa3311c8c8759873d2c8cf82931b5fe Mon Sep 17 00:00:00 2001 From: Divyanshu Raj Date: Wed, 28 Apr 2021 13:37:42 +0530 Subject: [PATCH 2/2] fixed async bugs --- test/tests/p5.FFT.js | 70 ++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/test/tests/p5.FFT.js b/test/tests/p5.FFT.js index c19db02f..578f963c 100644 --- a/test/tests/p5.FFT.js +++ b/test/tests/p5.FFT.js @@ -56,54 +56,60 @@ describe('p5.FFT', function () { expect(fft.smooth()).to.equal(0.8); }); - describe('can set input correctly', function () { - let fft; - - beforeEach(function () { - fft = new p5.FFT(); - }); - - afterEach(function () { - fft.dispose(); - }); - - let soundFile, oscillator; - it('can set Oscillator as input', function (done) { - this.timeout(1000); - oscillator = new p5.Oscillator(); + let soundFile, oscillator; + it('can set Oscillator as input', function (done) { + oscillator = new p5.Oscillator(); + try { fft.setInput(oscillator); - oscillator.start(); + } catch (err) { + return expect.fail(); + } + oscillator.start(); + setTimeout(() => { + let spectrum = fft.analyze(); + const someFrequencyIsNotZero = spectrum.some( + (frequency) => frequency !== 0 + ); + expect(someFrequencyIsNotZero).to.equal(true); + oscillator.dispose(); + done(); + }, 500); + }); + it('can set soundFile as input', function (done) { + soundFile = p5.prototype.loadSound('./testAudio/drum.mp3', () => { + soundFile.disconnect(); + try { + fft.setInput(soundFile); + } catch (err) { + return done(err); + } + soundFile.play(); setTimeout(() => { let spectrum = fft.analyze(); const someFrequencyIsNotZero = spectrum.some( (frequency) => frequency !== 0 ); expect(someFrequencyIsNotZero).to.equal(true); - oscillator.dispose(); + soundFile.dispose(); done(); }, 500); }); - it('can set soundFile as input', function () { - soundFile = p5.prototype.loadSound('./testAudio/drum.mp3', () => { - fft.setInput(soundFile); - soundFile.play(); - let spectrum = fft.analyze(); - const someFrequencyIsNotZero = spectrum.some( - (frequency) => frequency !== 0 - ); - expect(someFrequencyIsNotZero).to.equal(true); - done(); - }); - }); - it('can handle any Unknown sources', function () { - let UnknownSource = { UnknownSource: 'this is a unknown object' }; + }); + it('can handle any Unknown sources', function (done) { + let UnknownSource = { UnknownSource: 'this is a unknown object' }; + try { fft.setInput(UnknownSource); + } catch (err) { + return done(err); + } + setTimeout(() => { let spectrum = fft.analyze(); const someFrequencyIsNotZero = spectrum.some( (frequency) => frequency !== 0 ); expect(someFrequencyIsNotZero).to.equal(false); - }); + done(); + }, 500); }); });