Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added check for [bins] argument in fft method of P5, along with unit Test( in es6 style) #440

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions lib/p5.sound.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/p5.sound.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/p5.sound.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/p5.sound.min.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/fft.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

define(function(require) {
var p5sound = require('master');
const {safeBins} = require('helpers');

/**
* <p>FFT (Fast Fourier Transform) is an analysis algorithm that
Expand Down Expand Up @@ -115,7 +116,7 @@ define(function(require) {

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

// default connections to p5sound fftMeter
p5sound.fftMeter.connect(this.analyser);
Expand Down
19 changes: 17 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ define(function (require) {
}

function safeBufferSize(idealBufferSize) {
let bufferSize = idealBufferSize;
let bufferSize = idealBufferSize;

// if the AudioWorkletNode is actually a ScriptProcessorNode created via polyfill,
// make sure that our chosen buffer size isn't smaller than the buffer size automatically
Expand All @@ -337,12 +337,27 @@ define(function (require) {

return bufferSize;
}
var safeBins = p5.prototype.safeBins = function(bins) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to add this to the p5 prototype, or can this be a private method that is only used internally?

If it's only added to the prototype for the purpose of writing a test, I think the test should instead be on the public methods that utilize this function behind the scenes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@therewasaguy the main problem i face was using AMD here , I was unable to use safeBins defined in src/helper.js in test/tests/helpers.test.js , so I decided to put this method in prototype object of p5 ,
and I am realising now , that it is not the good way of doing it !,
so I will add tests on public function that will wrap this function ! and remove it from p5.prototype

let safeBins = 1024;
if(typeof(bins)==="string"){
console.log("the value of bins must be power of two and between 16 and 1024");
endurance21 marked this conversation as resolved.
Show resolved Hide resolved
return safeBins
}
if(bins && bins>=16 && bins<=1024 && Math.log2(bins)%1===0 )
return bins;
else {
console.log("the value of bins must be power of two and between 16 and 1024");
return safeBins
}

}

return {
convertToWav: convertToWav,
midiToFreq: midiToFreq,
noteToFreq: noteToFreq,
safeBufferSize: safeBufferSize
safeBufferSize: safeBufferSize,
safeBins:safeBins
};

});
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require.config({
});

var allTests = [
'tests/utils/test.helpers',
'tests/p5.SoundFile',
'tests/p5.Amplitude',
'tests/p5.Oscillator',
Expand Down
35 changes: 35 additions & 0 deletions test/tests/utils/test.helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

define(['chai'],(chai)=>{
const safeBins = p5.prototype.safeBins;
let expect = chai.expect;

describe('safeBins', ()=>{
it('can handle negative input ',()=>{
let test =-Math.random()*2;
expect(safeBins(test)).to.equal(1024);
});
it('can handle value less than 16 ',()=>{
let test = 10;
expect(safeBins(test)).to.equal(1024);
});
it('can handle value greater than 1024',()=>{
let test = 1500;
expect(safeBins(test)).to.equal(1024);
});
it('can handle value other than power 2',()=>{
let test = 1500;
expect(safeBins(test)).to.equal(1024);
});
it('can handle value equal to 0',()=>{
let test = 0;
expect(safeBins(test)).to.equal(1024);
});
it('can handle strings',()=>{
let test = 'testString';
expect(safeBins(test)).to.equal(1024);
endurance21 marked this conversation as resolved.
Show resolved Hide resolved
});

});

});
endurance21 marked this conversation as resolved.
Show resolved Hide resolved