Skip to content

Commit 9f3561e

Browse files
committed
Merge branch 'main' of https://github.com/processing/p5.js-sound into Abhijay007/Headless-testing
2 parents 49a6f89 + 78d0865 commit 9f3561e

File tree

7 files changed

+30
-15
lines changed

7 files changed

+30
-15
lines changed

src/audioWorklet/ringBuffer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class RingBuffer {
6767
for (let i = 0; i < sourceLength; ++i) {
6868
let writeIndex = (this._writeIndex + i) % this._length;
6969
for (let channel = 0; channel < this._channelCount; ++channel) {
70-
this._channelData[channel][writeIndex] = arraySequence[channel][i];
70+
if (arraySequence[channel])
71+
this._channelData[channel][writeIndex] = arraySequence[channel][i];
7172
}
7273
}
7374

src/fft.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class FFT {
298298
* <a href="https://en.wikipedia.org/wiki/Audio_frequency" target="_blank">
299299
* frequency</a>, or the average amount of energy between two
300300
* frequencies. Accepts Number(s) corresponding
301-
* to frequency (in Hz), or a "string" corresponding to predefined
301+
* to frequency (in Hz) (frequency must be >= 0), or a "string" corresponding to predefined
302302
* frequency ranges ("bass", "lowMid", "mid", "highMid", "treble").
303303
* Returns a range between 0 (no energy/volume at that frequency) and
304304
* 255 (maximum energy).
@@ -318,8 +318,8 @@ class FFT {
318318
* will return average amount of
319319
* energy that exists between the
320320
* two frequencies.
321-
* @return {Number} Energy Energy (volume/amplitude) from
322-
* 0 and 255.
321+
* @return {Number} Energy (volume/amplitude) from
322+
* 0 and 255.
323323
*
324324
*/
325325
getEnergy(frequency1, frequency2) {
@@ -350,7 +350,9 @@ class FFT {
350350
var index = Math.round((frequency1 / nyquist) * this.freqDomain.length);
351351
return this.freqDomain[index];
352352
}
353-
353+
if (frequency1 < 0 || frequency2 < 0) {
354+
throw 'invalid input for getEnergy(), frequency cannot be a negative number';
355+
}
354356
// if two parameters:
355357
// if second is higher than first
356358
if (frequency1 > frequency2) {

src/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ p5.prototype.outputVolume = function (vol, rampTime = 0, tFromNow = 0) {
8787
var now = p5sound.audiocontext.currentTime;
8888
var currentVol = p5sound.output.gain.value;
8989
p5sound.output.gain.cancelScheduledValues(now + tFromNow);
90-
p5sound.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow);
90+
if (rampTime !== 0)
91+
p5sound.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow);
9192
p5sound.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime);
9293
} else if (vol) {
9394
vol.connect(p5sound.output.gain);

test/tests/p5.Amplitude.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ describe('p5.Amplitude', function () {
6565
expect(amp.normalize).to.be.false;
6666
});
6767

68-
it('gets oscillator level', function () {
68+
it('gets oscillator level', function (done) {
6969
amp.setInput(osc);
7070
setTimeout(function () {
7171
expect(amp.getLevel()).to.be.closeTo(0.55, 0.25);
72+
done();
7273
}, 100);
7374
});
7475

test/tests/p5.AudioIn.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ describe('p5.AudioIn', function () {
9393

9494
it('can get sources', function (done) {
9595
mic.getSources().then(function (sources) {
96-
console.log(sources);
9796
expect(sources).to.be.an('array');
9897
done();
9998
});

test/tests/p5.SoundFile.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('p5.SoundFile', function () {
6060
() => done(),
6161
() => {},
6262
(progress) => {
63-
if (progress) {
63+
if (progress && progress !== 'size unknown') {
6464
expect(progress)
6565
.to.be.a('number')
6666
.to.be.greaterThan(0)
@@ -104,7 +104,7 @@ describe('p5.SoundFile', function () {
104104
() => done(),
105105
() => {},
106106
(progress) => {
107-
if (progress) {
107+
if (progress && progress !== 'size unknown') {
108108
expect(progress)
109109
.to.be.a('number')
110110
.to.be.greaterThan(0)
@@ -185,7 +185,7 @@ describe('p5.SoundFile', function () {
185185
setTimeout(() => {
186186
expect(sf._playing).to.be.false;
187187
done();
188-
}, 500); // as play back is 2 & cued 500ms , 500ms is enough to complete playing
188+
}, 550); // as play back is 2 & cued 500ms , 500ms is enough to complete playing
189189
});
190190
});
191191
it('can play with some given duration', function (done) {
@@ -266,8 +266,8 @@ describe('p5.SoundFile', function () {
266266
setTimeout(() => {
267267
expect(sf.bufferSourceNode._playing).to.be.false;
268268
expect(sf._playing).to.be.false;
269+
done();
269270
}, 100);
270-
done();
271271
});
272272
});
273273

test/tests/p5.SoundRecorder.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,23 @@ describe('p5.SoundRecorder', function () {
7070
recorder.setInput(mic);
7171
const outputSoundFile = new p5.SoundFile();
7272
setTimeout(() => {
73-
recorder.record(outputSoundFile, recordingDuration, function () {
74-
expect(outputSoundFile.duration()).to.eq(recordingDuration);
73+
recorder.record(outputSoundFile, 5 * recordingDuration, function () {
74+
expect(outputSoundFile.duration()).to.be.approximately(
75+
5 * recordingDuration,
76+
0.01
77+
);
7578

7679
const outputChannel = outputSoundFile.buffer.getChannelData(0);
77-
expect(outputChannel[0]).to.not.eq(0);
80+
let isAllZero = true;
7881

82+
for (let i = 0; i < outputChannel.length; i++) {
83+
if (outputChannel[i] !== 0) {
84+
isAllZero = false;
85+
break;
86+
}
87+
}
88+
89+
expect(isAllZero).to.be.false;
7990
outputSoundFile.dispose();
8091
mic.dispose();
8192
p5.prototype.outputVolume(0);

0 commit comments

Comments
 (0)