Skip to content

Commit

Permalink
add fft tests for smoothing and bins (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
therewasaguy authored Jun 20, 2017
1 parent 90918a3 commit 9788312
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
20 changes: 10 additions & 10 deletions src/fft.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,34 +99,34 @@ define(function (require) {
return this.analyser.fftSize / 2;
},
set: function(b) {
this.analyser.fftSize = (b * 2) || 1024;
this.analyser.fftSize = (b * 2);
},
configurable: true,
enumerable: true
},
'smoothing': {
get: function() {
return this.analyser.smoothing;
return this.analyser.smoothingTimeConstant;
},
set: function(s) {
this.analyser.smoothingTimeConstant = s || 0.8;
this.analyser.smoothingTimeConstant = s;
},
configurable: true,
enumerable: true
}
});
//
this.smoothing = smoothing;
this.bins = bins;

// set default smoothing and bins
this.smooth(smoothing);
this.bins = bins || 1024;

// default connections to p5sound fftMeter
p5sound.fftMeter.connect(this.analyser);

this.freqDomain = new Uint8Array(this.analyser.frequencyBinCount);
this.timeDomain = new Uint8Array(this.analyser.frequencyBinCount);

// predefined frequency ranages, these will be tweakable
// predefined frequency ranges, these will be tweakable
this.bass = [20, 140];
this.lowMid = [140, 400];
this.mid = [400, 2600];
Expand Down Expand Up @@ -488,10 +488,10 @@ define(function (require) {
* Defaults to 0.8.
*/
p5.FFT.prototype.smooth = function(s) {
if (s) {
if (typeof s !== 'undefined') {
this.smoothing = s;
}
this.analyser.smoothingTimeConstant = s;
return this.smoothing;
};

p5.FFT.prototype.dispose = function() {
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ require.config({
baseUrl:'./',
paths : {
'lib' : '../lib',
'chai' : './testDeps/chai',
'chai' : './testDeps/chai'
}
});

var allTests = ['tests/p5.SoundFile', 'tests/p5.Amplitude',
'tests/p5.Oscillator', 'tests/p5.Distortion',
'tests/p5.Effect','tests/p5.Filter'];
'tests/p5.Effect','tests/p5.Filter', 'tests/p5.FFT'];

p5.prototype.masterVolume(0);

Expand Down
62 changes: 62 additions & 0 deletions test/tests/p5.FFT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
define(['chai'],
function(chai) {

var expect = chai.expect;

describe('p5.FFT', function() {
var fft;

beforeEach(function() {
fft = new p5.FFT();
});

afterEach(function() {
fft.dispose();
});

it('has default bins of 1024', function() {
expect(fft.bins).to.equal(1024);
});

it('has default smoothing of 0.8', function() {
expect(fft.smooth()).to.equal(0.8);
expect(fft.smoothing).to.equal(0.8);
});

it('accepts smoothing and bins as args', function() {
fft.dispose();
fft = new p5.FFT(0, 128);
expect(fft.smoothing).to.equal(0);
expect(fft.bins).to.equal(128);
});

it('can set smoothing to zero', function() {
fft.smooth(0);
expect(fft.smoothing).to.equal(0);
expect(fft.smooth()).to.equal(0);
fft.smooth(0.9);
expect(fft.smoothing).to.equal(0.9);
expect(fft.smooth()).to.equal(0.9);
});

it('handles smoothing values out of range', function() {
expect(fft.smooth()).to.equal(0.8);
try {
fft.smooth(-1);
expect.fail();
} catch(e) {
expect(e).to.be.an.instanceof(Error);
}
expect(fft.smoothing).to.equal(0.8);
expect(fft.smooth()).to.equal(0.8);
try {
fft.smooth('some bad param');
expect.fail();
} catch(e) {
expect(e).to.be.an.instanceof(Error);
}
expect(fft.smoothing).to.equal(0.8);
expect(fft.smooth()).to.equal(0.8);
});
});
});

0 comments on commit 9788312

Please sign in to comment.