Skip to content

Commit 00be66e

Browse files
committed
Add first tests for p5.SoundRecorder
1 parent 3901a37 commit 00be66e

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

test/testAudio/constant.wav

1.83 MB
Binary file not shown.

test/tests/p5.SoundRecorder.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
'use strict';
2+
3+
define(['chai'], function(chai) {
4+
var expect = chai.expect;
5+
6+
describe('p5.SoundRecorder', function() {
7+
var recorder;
8+
var inputSoundFile;
9+
var originalWriteFile;
10+
var writeFileCalled;
11+
12+
before(function(done) {
13+
// clean up audio nodes from other test suites
14+
p5.soundOut.soundArray.length = 0;
15+
16+
// mock p5.prototype.writeFile
17+
originalWriteFile = p5.prototype.writeFile;
18+
p5.prototype.writeFile = function() {
19+
writeFileCalled = true;
20+
};
21+
22+
inputSoundFile = p5.prototype.loadSound('./testAudio/constant.wav', function() {
23+
done();
24+
});
25+
});
26+
27+
after(function() {
28+
inputSoundFile.dispose();
29+
p5.prototype.writeFile = originalWriteFile;
30+
});
31+
32+
beforeEach(function() {
33+
recorder = new p5.SoundRecorder();
34+
inputSoundFile.disconnect();
35+
inputSoundFile.stop();
36+
writeFileCalled = false;
37+
});
38+
39+
afterEach(function() {
40+
recorder.dispose();
41+
});
42+
43+
it('can record input from a microphone', function(done) {
44+
var recordingDuration = 100;
45+
46+
// need to enable master volume to test recording from the microphone
47+
p5.prototype.masterVolume(1);
48+
49+
var mic = new p5.AudioIn();
50+
mic.start(function() {
51+
var outputSoundFile = new p5.SoundFile();
52+
recorder.record(outputSoundFile);
53+
54+
setTimeout(function() {
55+
recorder.stop();
56+
expect(outputSoundFile.duration()).to.be.closeTo(recordingDuration / 1000, 0.1);
57+
58+
var outputChannel = outputSoundFile.buffer.getChannelData(0);
59+
expect(outputChannel[0]).to.not.eq(0);
60+
61+
outputSoundFile.dispose();
62+
mic.dispose();
63+
p5.prototype.masterVolume(0);
64+
done();
65+
}, recordingDuration);
66+
});
67+
});
68+
69+
it('can record input from a sound file', function(done) {
70+
// TODO: why does a sampleIndex of 0 break this test?
71+
var sampleIndex = 1000;
72+
var recordingDuration = 100;
73+
var inputChannel = inputSoundFile.buffer.getChannelData(0);
74+
// input SoundFile should contain all 1s
75+
expect(inputChannel[sampleIndex]).to.eq(1);
76+
77+
var outputSoundFile = new p5.SoundFile();
78+
inputSoundFile.play();
79+
recorder.setInput(inputSoundFile);
80+
recorder.record(outputSoundFile);
81+
82+
setTimeout(function() {
83+
recorder.stop();
84+
expect(outputSoundFile.duration()).to.be.closeTo(recordingDuration / 1000, 0.1);
85+
86+
var outputChannel = outputSoundFile.buffer.getChannelData(0);
87+
expect(outputChannel[sampleIndex]).to.eq(1);
88+
89+
outputSoundFile.dispose();
90+
done();
91+
}, recordingDuration);
92+
});
93+
94+
it('can record the master output of a sketch', function(done) {
95+
var recordingDuration = 100;
96+
var inputChannel = inputSoundFile.buffer.getChannelData(0);
97+
// input SoundFile should contain all 1s
98+
expect(inputChannel[0]).to.eq(1);
99+
100+
// need to enable master volume to test recording from master output
101+
p5.prototype.masterVolume(1);
102+
103+
var outputSoundFile = new p5.SoundFile();
104+
inputSoundFile.connect();
105+
inputSoundFile.play();
106+
recorder.setInput();
107+
recorder.record(outputSoundFile);
108+
109+
setTimeout(function() {
110+
recorder.stop();
111+
expect(outputSoundFile.duration()).to.be.closeTo(recordingDuration / 1000, 0.1);
112+
113+
var outputChannel = outputSoundFile.buffer.getChannelData(0);
114+
expect(outputChannel[0]).to.not.eq(0);
115+
116+
outputSoundFile.dispose();
117+
p5.prototype.masterVolume(0);
118+
done();
119+
}, recordingDuration);
120+
});
121+
122+
it('can save a recorded buffer to a .wav file', function(done) {
123+
var recordingDuration = 100;
124+
125+
var outputSoundFile = new p5.SoundFile();
126+
inputSoundFile.play();
127+
recorder.setInput(inputSoundFile);
128+
recorder.record(outputSoundFile);
129+
130+
setTimeout(function() {
131+
recorder.stop();
132+
expect(outputSoundFile.duration()).to.be.closeTo(recordingDuration / 1000, 0.1);
133+
134+
p5.prototype.saveSound(outputSoundFile, 'test.wav');
135+
expect(writeFileCalled).to.be.true;
136+
137+
outputSoundFile.dispose();
138+
done();
139+
}, recordingDuration);
140+
});
141+
});
142+
});

0 commit comments

Comments
 (0)