Skip to content

Commit 1ebdbe7

Browse files
author
Guillermo Montecinos
authored
Merge pull request #636 from satyasaibhushan/adding-tests-for-covered-files
(Gsoc'21) (Week-6,7)Adding tests for covered files
2 parents aed4b7f + 216427f commit 1ebdbe7

26 files changed

+3268
-436
lines changed

src/amplitude.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,10 @@ class Amplitude {
184184
} else {
185185
this.output.connect(unit);
186186
}
187-
} else {
188-
this.output.connect(this.panner.connect(p5sound.input));
189187
}
188+
// else {
189+
// this.output.connect(this.panner.connect(p5sound.input));
190+
// }
190191
}
191192

192193
disconnect() {

src/audioin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class AudioIn {
237237
*/
238238
getLevel(smoothing) {
239239
if (smoothing) {
240-
this.amplitude.smoothing = smoothing;
240+
this.amplitude.smooth(smoothing);
241241
}
242242
return this.amplitude.getLevel();
243243
}

src/eq.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class EQ extends Effect {
166166
this.bands[i / 2].gain(arguments[i + 1]);
167167
}
168168
} else {
169-
console.error(
169+
throw new Error(
170170
'Argument mismatch. .set() should be called with ' +
171171
this.bands.length * 2 +
172172
' arguments. (one frequency and gain value pair for each band of the eq)'

src/fft.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,12 @@ class FFT {
171171
*
172172
*/
173173
waveform() {
174-
var bins, mode;
174+
var mode;
175175
var normalArray = new Array();
176176

177177
for (var i = 0; i < arguments.length; i++) {
178178
if (typeof arguments[i] === 'number') {
179-
bins = arguments[i];
180-
this.analyser.fftSize = bins * 2;
179+
this.bins = arguments[i];
181180
}
182181
if (typeof arguments[i] === 'string') {
183182
mode = arguments[i];
@@ -275,7 +274,6 @@ class FFT {
275274
for (var i = 0; i < arguments.length; i++) {
276275
if (typeof arguments[i] === 'number') {
277276
this.bins = arguments[i];
278-
this.analyser.fftSize = this.bins * 2;
279277
}
280278
if (typeof arguments[i] === 'string') {
281279
mode = arguments[i];

src/helpers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ function soundFormats() {
149149
}
150150

151151
function disposeSound() {
152-
for (var i = 0; i < p5sound.soundArray.length; i++) {
152+
//looping backwards as looping forward may cause the
153+
//index of an element to change while the loop runs
154+
for (var i = p5sound.soundArray.length - 1; i >= 0; i--) {
153155
p5sound.soundArray[i].dispose();
154156
}
155157
}

test/testAudio/bx-spring.mp3

127 KB
Binary file not shown.

test/testAudio/bx-spring.ogg

146 KB
Binary file not shown.

test/tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ import('./tests/p5.Pulse.js');
2626
import('./tests/p5.Noise.js');
2727
import('./tests/p5.Panner.js');
2828
import('./tests/p5.Panner3d.js');
29-
import('./tests/p5.Listener3d.js');
29+
import('./tests/p5.Delay.js');
3030
import('./tests/p5.Reverb.js');
31+
import('./tests/p5.Listener3d.js');

test/tests/main.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
const expect = chai.expect;
22
describe('main output', function () {
3+
it('can initiate main class', function () {
4+
expect(p5.soundOut.input).to.have.property('gain');
5+
expect(p5.soundOut.input).to.have.property('context');
6+
expect(p5.soundOut.output).to.have.property('gain');
7+
expect(p5.soundOut.output).to.have.property('context');
8+
expect(p5.soundOut.limiter.threshold.value).to.equal(-3);
9+
expect(p5.soundOut.limiter.ratio.value).to.equal(20);
10+
expect(p5.soundOut.limiter.knee.value).to.equal(1);
11+
expect(p5.soundOut.audiocontext).to.have.property('audioWorklet');
12+
expect(p5.soundOut.audiocontext).to.have.property('baseLatency');
13+
expect(p5.soundOut.meter).to.have.property('gain');
14+
expect(p5.soundOut.meter).to.have.property('context');
15+
expect(p5.soundOut.fftMeter).to.have.property('gain');
16+
expect(p5.soundOut.fftMeter).to.have.property('context');
17+
expect(p5.soundOut.soundArray).to.be.an('array');
18+
expect(p5.soundOut.parts).to.be.an('array');
19+
expect(p5.soundOut.extensions).to.be.an('array');
20+
21+
expect(p5.soundOut._silentNode).to.have.property('gain');
22+
expect(p5.soundOut._silentNode).to.have.property('context');
23+
expect(p5.soundOut._silentNode.gain.value).to.equal(0);
24+
25+
console.log(p5.soundOut);
26+
});
27+
328
it('can set and return output volume', function (done) {
429
p5.prototype.outputVolume(0.6);
530

631
setTimeout(function () {
7-
expect(p5.prototype.getOutputVolume()).to.be.closeTo(0.6, 0.05);
32+
expect(p5.prototype.getOutputVolume()).to.be.approximately(0.6, 0.05);
33+
expect(p5.prototype.outputVolume().value).to.be.approximately(0.6, 0.05);
834
done();
935
}, 100);
1036
});
@@ -13,7 +39,7 @@ describe('main output', function () {
1339
p5.prototype.outputVolume(0.9, 0, t);
1440

1541
setTimeout(function () {
16-
expect(p5.prototype.getOutputVolume()).to.be.closeTo(0.9, 0.05);
42+
expect(p5.prototype.getOutputVolume()).to.be.approximately(0.9, 0.05);
1743
done();
1844
}, 1100);
1945
});
@@ -23,8 +49,13 @@ describe('main output', function () {
2349
p5.prototype.outputVolume(1, t, 0);
2450

2551
setTimeout(function () {
26-
expect(p5.prototype.getOutputVolume()).to.be.closeTo(0.5, 0.5);
52+
expect(p5.prototype.getOutputVolume()).to.be.approximately(0.5, 0.5);
2753
done();
2854
}, 500);
2955
});
56+
57+
it('can connect an audio node to p5sound output', function () {
58+
let noise = new p5.Noise();
59+
p5.prototype.outputVolume(noise);
60+
});
3061
});

test/tests/p5.Amplitude.js

Lines changed: 99 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,120 @@ const expect = chai.expect;
33
describe('p5.Amplitude', function () {
44
this.timeout(1000);
55

6-
let sf, amp, osc, oAmp;
6+
let amp;
7+
let osc = new p5.Oscillator('square');
8+
osc.amp(1);
9+
osc.start();
10+
osc.disconnect();
711

8-
it('can be created', function () {
12+
beforeEach(function () {
913
amp = new p5.Amplitude();
1014
});
1115

16+
it('can be created', function () {
17+
expect(amp).to.have.property('audiocontext').not.be.null;
18+
expect(amp).to.have.property('_workletNode').not.be.null;
19+
expect(amp.bufferSize).to.equal(2048);
20+
expect(amp).to.have.property('input');
21+
expect(amp).to.have.property('output');
22+
expect(amp.volume).to.equal(0);
23+
expect(amp.volNorm).to.equal(0);
24+
expect(amp.stereoVol).to.deep.equal([0, 0]);
25+
expect(amp.stereoVolNorm).to.deep.equal([0, 0]);
26+
});
27+
1228
after(function (done) {
13-
expect(amp.getLevel()).to.not.equal(1.0);
1429
osc.dispose();
15-
sf.dispose();
1630
done();
1731
});
18-
19-
it('accepts oscillator input', function () {
20-
osc = new p5.Oscillator('square');
21-
osc.amp(1);
22-
osc.start();
23-
osc.disconnect();
24-
oAmp = new p5.Amplitude();
25-
oAmp.setInput(osc);
32+
it('can be created with smoothing', function () {
33+
new p5.Amplitude(0.02);
2634
});
2735

28-
it('gets oscillator level', function () {
29-
setTimeout(function () {
30-
// console.log( 'unnormalized: ' + oAmp.getLevel() );
31-
expect(oAmp.getLevel()).to.be.closeTo(0.55, 0.25);
32-
}, 100);
33-
});
36+
describe('methods', function () {
37+
it('accepts oscillator input', function () {
38+
amp.setInput(osc);
39+
});
40+
it('setInput connects to main output if no argument is passed', function () {
41+
amp.setInput();
42+
});
3443

35-
it('gets normalized osc level', function (done) {
36-
setTimeout(function () {
37-
oAmp.toggleNormalize(true);
38-
// console.log( 'normalized: ' + oAmp.getLevel() );
39-
expect(oAmp.getLevel()).to.be.closeTo(1.0, 0.4);
40-
done();
41-
}, 200);
42-
});
44+
it('can be connected and disconnected from a unit', function () {
45+
let filter = new p5.Filter();
46+
47+
//if unit has input property
48+
amp.connect(filter);
49+
amp.disconnect();
50+
51+
//if unit doesnot have an input property
52+
amp = new p5.Amplitude();
53+
amp.connect(filter.input);
54+
amp.disconnect();
55+
56+
filter.dispose();
57+
});
58+
59+
it('can toggle normalization', function () {
60+
expect(amp.normalize).to.be.false;
61+
amp.toggleNormalize();
62+
expect(amp.normalize).to.be.true;
63+
64+
amp.toggleNormalize(true);
65+
expect(amp.normalize).to.be.true;
66+
amp.toggleNormalize(false);
67+
expect(amp.normalize).to.be.false;
68+
});
4369

44-
it('loop a SoundFile with params, disconnected from master, setInput()', function (done) {
45-
p5.prototype.soundFormats('ogg', 'mp3');
46-
sf = p5.prototype.loadSound('./testAudio/drum', function () {
47-
sf.disconnect();
48-
sf.loop(1, 1, 0.0, 0.05);
49-
sf.connect(amp);
70+
it('gets oscillator level', function () {
71+
amp.setInput(osc);
5072
setTimeout(function () {
51-
done();
73+
expect(amp.getLevel()).to.be.closeTo(0.55, 0.25);
5274
}, 100);
5375
});
54-
});
5576

56-
it('stop getting level', function (done) {
57-
sf.stop();
58-
setTimeout(function () {
59-
// console.log( amp.getLevel() );
60-
done();
61-
}, 10);
77+
it('gets normalized osc level', function (done) {
78+
amp.setInput(osc);
79+
setTimeout(function () {
80+
amp.toggleNormalize(true);
81+
expect(amp.getLevel()).to.be.closeTo(1.0, 0.4);
82+
done();
83+
}, 200);
84+
});
85+
it('gets stereo osc level', function (done) {
86+
amp.setInput(osc);
87+
setTimeout(function () {
88+
expect(amp.getLevel(0)).to.be.closeTo(0.55, 0.25);
89+
expect(amp.getLevel(1)).to.be.closeTo(0.55, 0.25);
90+
amp.toggleNormalize(true);
91+
expect(amp.getLevel(0)).to.be.closeTo(1, 0.4);
92+
expect(amp.getLevel(1)).to.be.closeTo(1, 0.4);
93+
done();
94+
}, 200);
95+
});
96+
97+
it('can be connected to a soundFile', function (done) {
98+
p5.prototype.soundFormats('ogg', 'mp3');
99+
let sf = p5.prototype.loadSound('./testAudio/drum', function () {
100+
sf.disconnect();
101+
sf.loop(1, 1, 0.0, 0.05);
102+
sf.connect(amp);
103+
sf.dispose();
104+
setTimeout(function () {
105+
sf.stop();
106+
done();
107+
}, 100);
108+
});
109+
});
110+
111+
it('can apply smoothing', function () {
112+
amp.smooth(0.5);
113+
});
114+
115+
it('can be disposed', function () {
116+
amp.dispose();
117+
expect(amp).to.not.have.property('input');
118+
expect(amp).to.not.have.property('output');
119+
expect(amp).to.not.have.property('_workletNode');
120+
});
62121
});
63122
});

0 commit comments

Comments
 (0)