mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 14:02:29 -05:00
add a play while playing test
This commit is contained in:
parent
18cb9787ae
commit
8110885a62
1 changed files with 58 additions and 5 deletions
|
@ -1,4 +1,4 @@
|
||||||
/* global Uint8Array */
|
/* global Uint8Array Promise */
|
||||||
const tap = require('tap');
|
const tap = require('tap');
|
||||||
const {AudioContext} = require('web-audio-test-api');
|
const {AudioContext} = require('web-audio-test-api');
|
||||||
|
|
||||||
|
@ -11,11 +11,19 @@ tap.test('SoundPlayer', suite => {
|
||||||
let audioEngine;
|
let audioEngine;
|
||||||
let soundPlayer;
|
let soundPlayer;
|
||||||
|
|
||||||
|
const help = {
|
||||||
|
get engineInputs () {
|
||||||
|
return audioEngine.inputNode.toJSON().inputs;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
suite.beforeEach(async () => {
|
suite.beforeEach(async () => {
|
||||||
audioContext = new AudioContext();
|
audioContext = new AudioContext();
|
||||||
audioEngine = new AudioEngine(audioContext);
|
audioEngine = new AudioEngine(audioContext);
|
||||||
audioEngine.DECODE_AUDIO_DATA_RESULT = audioContext.createBuffer(2, 1024, 44100);
|
// sound will be 0.1 seconds long
|
||||||
const data = new Uint8Array(1024);
|
audioContext.DECODE_AUDIO_DATA_RESULT = audioContext.createBuffer(2, 4410, 44100);
|
||||||
|
audioContext.DECODE_AUDIO_DATA_FAILED = false;
|
||||||
|
const data = new Uint8Array(44100);
|
||||||
soundPlayer = await audioEngine.decodeSoundPlayer({data});
|
soundPlayer = await audioEngine.decodeSoundPlayer({data});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -27,14 +35,14 @@ tap.test('SoundPlayer', suite => {
|
||||||
audioContext = null;
|
audioContext = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
suite.plan(3);
|
suite.plan(4);
|
||||||
|
|
||||||
suite.test('play initializes and creates chain', t => {
|
suite.test('play initializes and creates chain', t => {
|
||||||
t.plan(3);
|
t.plan(3);
|
||||||
t.equal(soundPlayer.initialized, false, 'not yet initialized');
|
t.equal(soundPlayer.initialized, false, 'not yet initialized');
|
||||||
soundPlayer.play();
|
soundPlayer.play();
|
||||||
t.equal(soundPlayer.initialized, true, 'now is initialized');
|
t.equal(soundPlayer.initialized, true, 'now is initialized');
|
||||||
let buffer = audioEngine.DECODE_AUDIO_DATA_RESULT.toJSON();
|
let buffer = audioContext.DECODE_AUDIO_DATA_RESULT.toJSON();
|
||||||
t.deepEqual(soundPlayer.outputNode.toJSON(), {
|
t.deepEqual(soundPlayer.outputNode.toJSON(), {
|
||||||
buffer,
|
buffer,
|
||||||
inputs: [],
|
inputs: [],
|
||||||
|
@ -98,5 +106,50 @@ tap.test('SoundPlayer', suite => {
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
suite.test('play while playing', async t => {
|
||||||
|
t.plan(14);
|
||||||
|
const log = [];
|
||||||
|
soundPlayer.play();
|
||||||
|
soundPlayer.finished().then(() => log.push('play 1 finished'));
|
||||||
|
soundPlayer.connect(audioEngine);
|
||||||
|
|
||||||
|
|
||||||
|
audioContext.$processTo(0.005);
|
||||||
|
t.equal(soundPlayer.outputNode.$state, 'PLAYING');
|
||||||
|
|
||||||
|
const oldPlayerNode = soundPlayer.outputNode;
|
||||||
|
soundPlayer.play();
|
||||||
|
soundPlayer.finished().then(() => log.push('play 2 finished'));
|
||||||
|
|
||||||
|
// wait for a micro-task loop to fire our previous events
|
||||||
|
await Promise.resolve();
|
||||||
|
t.equal(log[0], 'play 1 finished');
|
||||||
|
t.notEqual(soundPlayer.outputNode, oldPlayerNode, 'created new player node');
|
||||||
|
|
||||||
|
t.equal(help.engineInputs.length, 2, 'there should be 2 players connected');
|
||||||
|
t.equal(oldPlayerNode.$state, 'PLAYING');
|
||||||
|
t.equal(soundPlayer.outputNode.$state, 'PLAYING');
|
||||||
|
t.equal(help.engineInputs[0].gain.value, 1, 'old sound connectect to gain node with volume 1');
|
||||||
|
|
||||||
|
audioContext.$processTo(audioContext.currentTime + 0.001);
|
||||||
|
t.notEqual(help.engineInputs[0].gain.value, 1,
|
||||||
|
'old sound connected to gain node which will fade');
|
||||||
|
|
||||||
|
audioContext.$processTo(audioContext.currentTime + audioEngine.DECAY_TIME + 0.001);
|
||||||
|
t.equal(soundPlayer.outputNode.$state, 'PLAYING');
|
||||||
|
t.equal(oldPlayerNode.$state, 'FINISHED');
|
||||||
|
|
||||||
|
t.equal(help.engineInputs[0].gain.value, 0, 'faded old sound to 0');
|
||||||
|
|
||||||
|
t.equal(log.length, 1);
|
||||||
|
audioContext.$processTo(audioContext.currentTime + 0.2);
|
||||||
|
|
||||||
|
await Promise.resolve();
|
||||||
|
t.equal(log[1], 'play 2 finished');
|
||||||
|
t.equal(log.length, 2);
|
||||||
|
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
suite.end();
|
suite.end();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue