Skip to content

Commit f690db4

Browse files
(Gsoc'21)💚Added tests to soundRecorder.js
1 parent 5ca621a commit f690db4

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

test/tests/p5.SoundRecorder.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const expect = chai.expect;
22

33
describe('p5.SoundRecorder', function () {
4-
let recorder;
54
let inputSoundFile;
65
let writeFileSub;
76

@@ -33,17 +32,34 @@ describe('p5.SoundRecorder', function () {
3332
});
3433

3534
beforeEach(function () {
36-
recorder = new p5.SoundRecorder();
3735
inputSoundFile.disconnect();
3836
inputSoundFile.stop();
3937
writeFileSub.reset();
4038
});
4139

42-
afterEach(function () {
40+
afterEach(function () {});
41+
42+
it('can be created and disposed', function () {
43+
let recorder = new p5.SoundRecorder();
44+
expect(recorder.input).to.have.property('context');
45+
expect(recorder.input).to.have.property('gain');
46+
expect(recorder.output).to.have.property('context');
47+
expect(recorder.output).to.have.property('gain');
48+
expect(recorder._inputChannels).to.equal(2);
49+
expect(recorder._outputChannels).to.equal(2);
50+
expect(recorder._workletNode).to.have.property('context');
51+
expect(recorder._workletNode).to.have.property('parameters');
52+
53+
expect(p5.soundOut.soundArray).to.include(recorder);
54+
4355
recorder.dispose();
56+
expect(p5.soundOut.soundArray).to.not.include(recorder);
57+
expect(recorder.input).to.be.null;
58+
expect(recorder._workletNode).to.be.null;
4459
});
4560

4661
it('can record input from a microphone', function (done) {
62+
let recorder = new p5.SoundRecorder();
4763
// this is the shortest possible recording duration
4864
const recordingDuration =
4965
recorder.bufferSize / p5.soundOut.audiocontext.sampleRate;
@@ -65,13 +81,15 @@ describe('p5.SoundRecorder', function () {
6581
outputSoundFile.dispose();
6682
mic.dispose();
6783
p5.prototype.outputVolume(0);
84+
recorder.dispose();
6885
done();
6986
});
70-
}, 130);
87+
}, 500);
7188
});
7289
});
7390

7491
it('can record input from a sound file', function (done) {
92+
let recorder = new p5.SoundRecorder();
7593
const sampleIndex = 0;
7694
// this is the shortest possible recording duration
7795
const recordingDuration =
@@ -91,11 +109,13 @@ describe('p5.SoundRecorder', function () {
91109
expect(outputChannel[sampleIndex]).to.eq(inputChannelSampleValue);
92110

93111
outputSoundFile.dispose();
112+
recorder.dispose();
94113
done();
95114
});
96115
});
97116

98117
it('can record the main output of a sketch', function (done) {
118+
let recorder = new p5.SoundRecorder();
99119
// this is the shortest possible recording duration
100120
const recordingDuration =
101121
recorder.bufferSize / p5.soundOut.audiocontext.sampleRate;
@@ -119,12 +139,14 @@ describe('p5.SoundRecorder', function () {
119139

120140
outputSoundFile.dispose();
121141
p5.prototype.outputVolume(0);
142+
recorder.dispose();
122143
done();
123144
});
124145
}, 20);
125146
});
126147

127-
it('can save a recorded buffer to a .wav file', function (done) {
148+
it('can save a recorded buffer to a .wav file and be stopped', function (done) {
149+
let recorder = new p5.SoundRecorder();
128150
// this is the shortest possible recording duration
129151
const recordingDuration =
130152
recorder.bufferSize / p5.soundOut.audiocontext.sampleRate;
@@ -139,6 +161,8 @@ describe('p5.SoundRecorder', function () {
139161
expect(writeFileSub.called).to.be.true;
140162

141163
outputSoundFile.dispose();
164+
recorder.stop();
165+
recorder.dispose();
142166
done();
143167
});
144168
});

0 commit comments

Comments
 (0)