Skip to content

Commit

Permalink
simplify recorder return type and add comments to audit.js
Browse files Browse the repository at this point in the history
  • Loading branch information
terryzfeng committed Jul 15, 2024
1 parent 50aa46d commit 17aeaba
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/tests/playwright/pages/live-suite/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/
import {convertTestFiles} from './test-file-converter.js';

window._webAudioTestSuite = true;
// Flag for live test suite environment
window._isTestSuiteMode = true;

const files = [
'realtime-sine.html',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export const convertTestFiles = async (tests) => {
script.textContent = scriptContent;
document.head.appendChild(script);

const start = performance.now();
// await when script loads in the live suite
await new Promise((resolve) => window._webAudioTestIsRunning = resolve);
const start = performance.now();
await window._webAudioTest;
const diff = performance.now() - start;

Expand Down
6 changes: 3 additions & 3 deletions src/tests/playwright/pages/realtime-sine.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
const context = new AudioContext({sampleRate: 48000});
const oscillator = new OscillatorNode(context);
const duration = 1; // second
const {recorder, bufferPromise} = await record(context, duration);
const {recorder, recordingCompletePromise} = await record(context, duration);
oscillator.connect(recorder).connect(context.destination);

oscillator.start();

const audioBuffer = await bufferPromise;
const float32Buffer = audioBuffer.getChannelData(0);
const recordedChannelBuffers = await recordingCompletePromise;
const float32Buffer = recordedChannelBuffers[0];

await context.close();
return {buffer: float32Buffer};
Expand Down
7 changes: 5 additions & 2 deletions src/tests/playwright/pages/util/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export function beCloseTo(actual, expected, threshold) {
* window._webAudioTest.
*/
export const test = (testPromise) => {
// Current web audio test to run
window._webAudioTest = testPromise;
window._webAudioTestSuite && window._webAudioTestIsRunning();
// Flag if test is run from the live suite and
// resolve that the web audio test is running
window._isTestSuiteMode && window._webAudioTestIsRunning();
};

/**
Expand All @@ -49,7 +52,7 @@ export const test = (testPromise) => {
* @return {any}
*/
export const evaluate =
(testFunction) => window.webAudioEvaluate = window._webAudioTestSuite
(testFunction) => window.webAudioEvaluate = window._isTestSuiteMode
? () => testFunction(window._webAudioTest)
: testFunction(window._webAudioTest);

Expand Down
49 changes: 17 additions & 32 deletions src/tests/playwright/pages/util/recorder/recorder-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,39 @@
* Creates an AudioWorklet recorder node to record input for a specified
* length of time (seconds). Passes audio through to output.
* @param {AudioContext} context - The AudioContext to create the recorder node.
* @param {number} recordLength - The length in seconds to record for.
* @param {number} recordDuration - The duration in seconds to record for.
* @return {Object} An object containing:
* - {AudioWorkletNode} recorder: The recorder AudioWorkletNode.
* - {Promise<AudioBuffer>} bufferPromise: A promise that resolves to an
* AudioBuffer once filled.
* - {Promise<Array<Float32Array>>} recordingCompletePromise: A promise that
* resolves to an array of float32Arrays of each recorded channel.
*/
export const record = async (context, recordLength) => {
export const record = async (context, recordDuration) => {
console.assert(context instanceof AudioContext);
console.assert(typeof recordLength === 'number' && recordLength > 0);
console.assert(typeof recordDuration === 'number' && recordDuration > 0);

const recorderBufferSize = recordLength * context.sampleRate;
const recorderBufferSize = recordDuration * context.sampleRate;

let recorder;

try {
recorder = new AudioWorkletNode(context, 'test-recorder', {
processorOptions: {
numberOfSamples: recorderBufferSize,
},
});
} catch {
await context.audioWorklet.addModule('./util/recorder/recorder-processor.js');

recorder = new AudioWorkletNode(context, 'test-recorder', {
processorOptions: {
numberOfSamples: recorderBufferSize,
},
});
}
await context.audioWorklet.addModule('./util/recorder/recorder-processor.js');

recorder = new AudioWorkletNode(context, 'test-recorder', {
processorOptions: {
numberOfSamples: recorderBufferSize,
},
});

let resolveWithRecording;
recorder.port.onmessage = (event) => {
if (event.data.message === 'RECORD_DONE') {
const channelData = event.data.channelData;
const audioBuffer = new AudioBuffer({
length: recorderBufferSize,
sampleRate: context.sampleRate,
numberOfChannels: channelData.length,
});
channelData.forEach((array, i) => audioBuffer.copyToChannel(array, i));

resolveWithRecording(audioBuffer);
// Array<Float32Array> channels of recorded samples
resolveWithRecording(event.data.channelData);
}
};

const bufferPromise = new Promise((resolve) => {
const recordingCompletePromise = new Promise((resolve) => {
resolveWithRecording = resolve;
});

return {recorder, bufferPromise};
return {recorder, recordingCompletePromise};
};

0 comments on commit 17aeaba

Please sign in to comment.