Skip to content

Commit 7167709

Browse files
committed
added feature of safeBins along with unit Test
1 parent bdab327 commit 7167709

File tree

8 files changed

+102
-31
lines changed

8 files changed

+102
-31
lines changed

lib/p5.sound.js

Lines changed: 43 additions & 24 deletions
Large diffs are not rendered by default.

lib/p5.sound.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/p5.sound.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/p5.sound.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fft.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
define(function(require) {
44
var p5sound = require('master');
5+
const {safeBins} = require('helpers');
56

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

116117
// set default smoothing and bins
117118
this.smooth(smoothing);
118-
this.bins = bins || 1024;
119+
this.bins = safeBins(bins) || 1024;
119120

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

src/helpers.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ define(function (require) {
322322
}
323323

324324
function safeBufferSize(idealBufferSize) {
325-
let bufferSize = idealBufferSize;
325+
let bufferSize = idealBufferSize;
326326

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

338338
return bufferSize;
339339
}
340+
var safeBins = p5.prototype.safeBins = function(bins) {
341+
let safeBins = 1024;
342+
if(typeof(bins)==="string"){
343+
console.log("the value of bins must be power of two and between 16 and 1024");
344+
return safeBins
345+
}
346+
if(bins && bins>=16 && bins<=1024 && Math.log2(bins)%1===0 )
347+
return bins;
348+
else {
349+
console.log("the value of bins must be power of two and between 16 and 1024");
350+
return safeBins
351+
}
352+
353+
}
340354

341355
return {
342356
convertToWav: convertToWav,
343357
midiToFreq: midiToFreq,
344358
noteToFreq: noteToFreq,
345-
safeBufferSize: safeBufferSize
359+
safeBufferSize: safeBufferSize,
360+
safeBins:safeBins
346361
};
347362

348363
});

test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require.config({
88
});
99

1010
var allTests = [
11+
'tests/utils/test.helpers',
1112
'tests/p5.SoundFile',
1213
'tests/p5.Amplitude',
1314
'tests/p5.Oscillator',

test/tests/utils/test.helpers.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
define(['chai'],(chai)=>{
4+
const safeBins = p5.prototype.safeBins;
5+
let expect = chai.expect;
6+
7+
describe('safeBins', ()=>{
8+
it('can handle negative input ',()=>{
9+
let test =-Math.random()*2;
10+
expect(safeBins(test)).to.equal(1024);
11+
});
12+
it('can handle value less than 16 ',()=>{
13+
let test = 10;
14+
expect(safeBins(test)).to.equal(1024);
15+
});
16+
it('can handle value greater than 1024',()=>{
17+
let test = 1500;
18+
expect(safeBins(test)).to.equal(1024);
19+
});
20+
it('can handle value other than power 2',()=>{
21+
let test = 1500;
22+
expect(safeBins(test)).to.equal(1024);
23+
});
24+
it('can handle value equal to 0',()=>{
25+
let test = 0;
26+
expect(safeBins(test)).to.equal(1024);
27+
});
28+
it('can handle strings',()=>{
29+
let test = 'testString';
30+
expect(safeBins(test)).to.equal(1024);
31+
});
32+
33+
});
34+
35+
});

0 commit comments

Comments
 (0)