Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… Abhijay007/Headless-testing
  • Loading branch information
Abhijay007 committed Jan 22, 2023
2 parents 49a6f89 + 78d0865 commit 9f3561e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/audioWorklet/ringBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class RingBuffer {
for (let i = 0; i < sourceLength; ++i) {
let writeIndex = (this._writeIndex + i) % this._length;
for (let channel = 0; channel < this._channelCount; ++channel) {
this._channelData[channel][writeIndex] = arraySequence[channel][i];
if (arraySequence[channel])
this._channelData[channel][writeIndex] = arraySequence[channel][i];
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/fft.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class FFT {
* <a href="https://en.wikipedia.org/wiki/Audio_frequency" target="_blank">
* frequency</a>, or the average amount of energy between two
* frequencies. Accepts Number(s) corresponding
* to frequency (in Hz), or a "string" corresponding to predefined
* to frequency (in Hz) (frequency must be >= 0), or a "string" corresponding to predefined
* frequency ranges ("bass", "lowMid", "mid", "highMid", "treble").
* Returns a range between 0 (no energy/volume at that frequency) and
* 255 (maximum energy).
Expand All @@ -318,8 +318,8 @@ class FFT {
* will return average amount of
* energy that exists between the
* two frequencies.
* @return {Number} Energy Energy (volume/amplitude) from
* 0 and 255.
* @return {Number} Energy (volume/amplitude) from
* 0 and 255.
*
*/
getEnergy(frequency1, frequency2) {
Expand Down Expand Up @@ -350,7 +350,9 @@ class FFT {
var index = Math.round((frequency1 / nyquist) * this.freqDomain.length);
return this.freqDomain[index];
}

if (frequency1 < 0 || frequency2 < 0) {
throw 'invalid input for getEnergy(), frequency cannot be a negative number';
}
// if two parameters:
// if second is higher than first
if (frequency1 > frequency2) {
Expand Down
3 changes: 2 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ p5.prototype.outputVolume = function (vol, rampTime = 0, tFromNow = 0) {
var now = p5sound.audiocontext.currentTime;
var currentVol = p5sound.output.gain.value;
p5sound.output.gain.cancelScheduledValues(now + tFromNow);
p5sound.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow);
if (rampTime !== 0)
p5sound.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow);
p5sound.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime);
} else if (vol) {
vol.connect(p5sound.output.gain);
Expand Down
3 changes: 2 additions & 1 deletion test/tests/p5.Amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ describe('p5.Amplitude', function () {
expect(amp.normalize).to.be.false;
});

it('gets oscillator level', function () {
it('gets oscillator level', function (done) {
amp.setInput(osc);
setTimeout(function () {
expect(amp.getLevel()).to.be.closeTo(0.55, 0.25);
done();
}, 100);
});

Expand Down
1 change: 0 additions & 1 deletion test/tests/p5.AudioIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ describe('p5.AudioIn', function () {

it('can get sources', function (done) {
mic.getSources().then(function (sources) {
console.log(sources);
expect(sources).to.be.an('array');
done();
});
Expand Down
8 changes: 4 additions & 4 deletions test/tests/p5.SoundFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('p5.SoundFile', function () {
() => done(),
() => {},
(progress) => {
if (progress) {
if (progress && progress !== 'size unknown') {
expect(progress)
.to.be.a('number')
.to.be.greaterThan(0)
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('p5.SoundFile', function () {
() => done(),
() => {},
(progress) => {
if (progress) {
if (progress && progress !== 'size unknown') {
expect(progress)
.to.be.a('number')
.to.be.greaterThan(0)
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('p5.SoundFile', function () {
setTimeout(() => {
expect(sf._playing).to.be.false;
done();
}, 500); // as play back is 2 & cued 500ms , 500ms is enough to complete playing
}, 550); // as play back is 2 & cued 500ms , 500ms is enough to complete playing
});
});
it('can play with some given duration', function (done) {
Expand Down Expand Up @@ -266,8 +266,8 @@ describe('p5.SoundFile', function () {
setTimeout(() => {
expect(sf.bufferSourceNode._playing).to.be.false;
expect(sf._playing).to.be.false;
done();
}, 100);
done();
});
});

Expand Down
17 changes: 14 additions & 3 deletions test/tests/p5.SoundRecorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,23 @@ describe('p5.SoundRecorder', function () {
recorder.setInput(mic);
const outputSoundFile = new p5.SoundFile();
setTimeout(() => {
recorder.record(outputSoundFile, recordingDuration, function () {
expect(outputSoundFile.duration()).to.eq(recordingDuration);
recorder.record(outputSoundFile, 5 * recordingDuration, function () {
expect(outputSoundFile.duration()).to.be.approximately(
5 * recordingDuration,
0.01
);

const outputChannel = outputSoundFile.buffer.getChannelData(0);
expect(outputChannel[0]).to.not.eq(0);
let isAllZero = true;

for (let i = 0; i < outputChannel.length; i++) {
if (outputChannel[i] !== 0) {
isAllZero = false;
break;
}
}

expect(isAllZero).to.be.false;
outputSoundFile.dispose();
mic.dispose();
p5.prototype.outputVolume(0);
Expand Down

0 comments on commit 9f3561e

Please sign in to comment.