Skip to content

Commit 246fd15

Browse files
committed
Merge branch 'main' of https://github.com/processing/p5.js-sound into Abhijay007/AudioIn-fields
2 parents eaa8279 + 030270f commit 246fd15

File tree

9 files changed

+61
-40
lines changed

9 files changed

+61
-40
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);

src/monosynth.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,21 @@ class MonoSynth extends AudioVoice {
7777
* @for p5.MonoSynth
7878
*/
7979
/**
80+
* Allows user to set the decay time of the envelope (ADSR) of the MonoSynth class.
81+
* It is a getter and setter that can be used to retrieve or change the decay time.
82+
* Used in conjunction with the attack, sustain, and release fields/functions to set the full envelope of the synthesizer.
8083
* @property {Number} decay
8184
* @for p5.MonoSynth
8285
*/
8386
/**
87+
* Allows the user to retrieve and adjust the sustain level of the envelope,
88+
* which controls the level at which the sound is sustained during the sustain phase of the envelope.
89+
* The default sustain level is set to 0.15.
8490
* @property {Number} sustain
8591
* @for p5.MonoSynth
8692
*/
8793
/**
94+
* Allows the user to access and change the release time of the envelope.
8895
* @property {Number} release
8996
* @for p5.MonoSynth
9097
*/

src/soundfile.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,40 +1362,39 @@ class SoundFile {
13621362
var now = ac.currentTime;
13631363
var cNode = ac.createBufferSource();
13641364

1365-
const workletBufferSize = safeBufferSize(256);
1366-
1367-
// dispose of worklet node if it already exists
1368-
if (self._workletNode) {
1369-
self._workletNode.disconnect();
1370-
delete self._workletNode;
1371-
}
1372-
self._workletNode = new AudioWorkletNode(
1373-
ac,
1374-
processorNames.soundFileProcessor,
1375-
{
1376-
processorOptions: { bufferSize: workletBufferSize },
1377-
}
1378-
);
1379-
self._workletNode.port.onmessage = (event) => {
1380-
if (event.data.name === 'position') {
1381-
// event.data.position should only be 0 when paused
1382-
if (event.data.position === 0) {
1383-
return;
1365+
// Reuse the worklet node rather than creating a new one. Even if we
1366+
// disconnect it, it seems to leak and cause choppy audio after a
1367+
// while.
1368+
if (!self._workletNode) {
1369+
const workletBufferSize = safeBufferSize(256);
1370+
self._workletNode = new AudioWorkletNode(
1371+
ac,
1372+
processorNames.soundFileProcessor,
1373+
{
1374+
processorOptions: { bufferSize: workletBufferSize },
13841375
}
1385-
this._lastPos = event.data.position;
1376+
);
1377+
self._workletNode.port.onmessage = (event) => {
1378+
if (event.data.name === 'position') {
1379+
// event.data.position should only be 0 when paused
1380+
if (event.data.position === 0) {
1381+
return;
1382+
}
1383+
this._lastPos = event.data.position;
13861384

1387-
// do any callbacks that have been scheduled
1388-
this._onTimeUpdate(self._lastPos);
1389-
}
1390-
};
1385+
// do any callbacks that have been scheduled
1386+
this._onTimeUpdate(self._lastPos);
1387+
}
1388+
};
1389+
self._workletNode.connect(p5.soundOut._silentNode);
1390+
}
13911391

13921392
// create counter buffer of the same length as self.buffer
13931393
cNode.buffer = _createCounterBuffer(self.buffer);
13941394

13951395
cNode.playbackRate.setValueAtTime(self.playbackRate, now);
13961396

13971397
cNode.connect(self._workletNode);
1398-
self._workletNode.connect(p5.soundOut._silentNode);
13991398

14001399
return cNode;
14011400
}

test/tests/p5.Amplitude.js

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

70-
it('gets oscillator level', function () {
70+
it('gets oscillator level', function (done) {
7171
amp.setInput(osc);
7272
setTimeout(function () {
7373
expect(amp.getLevel()).to.be.closeTo(0.55, 0.25);
74+
done();
7475
}, 100);
7576
});
7677

test/tests/p5.AudioIn.js

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

9696
it('can get sources', function (done) {
9797
mic.getSources().then(function (sources) {
98-
console.log(sources);
9998
expect(sources).to.be.an('array');
10099
done();
101100
});

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
@@ -72,12 +72,23 @@ describe('p5.SoundRecorder', function () {
7272
recorder.setInput(mic);
7373
const outputSoundFile = new p5.SoundFile();
7474
setTimeout(() => {
75-
recorder.record(outputSoundFile, recordingDuration, function () {
76-
expect(outputSoundFile.duration()).to.eq(recordingDuration);
75+
recorder.record(outputSoundFile, 5 * recordingDuration, function () {
76+
expect(outputSoundFile.duration()).to.be.approximately(
77+
5 * recordingDuration,
78+
0.01
79+
);
7780

7881
const outputChannel = outputSoundFile.buffer.getChannelData(0);
79-
expect(outputChannel[0]).to.not.eq(0);
82+
let isAllZero = true;
8083

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

0 commit comments

Comments
 (0)