72 lines
3.9 KiB
JavaScript
72 lines
3.9 KiB
JavaScript
// Measure the v0.7.2 per-packet clock mapping and the new scheduler in actual
|
|
// OfflineAudioContexts against the same deterministic stereo PCM and clock trace.
|
|
import {readFile} from 'node:fs/promises';
|
|
const playwright=await import(process.env.AM_PLAYWRIGHT_MODULE??'playwright');
|
|
const engines=process.argv.includes('--webkit')?['chromium','firefox','webkit']:['chromium','firefox'];
|
|
const source=await readFile(new URL('../src/audio/pcm-timeline.mjs',import.meta.url),'utf8');
|
|
for(const engine of engines){
|
|
const browser=await playwright[engine].launch({headless:!process.argv.includes('--headed')});
|
|
try{
|
|
const page=await browser.newPage();
|
|
await page.route('https://fixture.invalid/**',route=>route.fulfill({
|
|
contentType:route.request().url().endsWith('.mjs')?'text/javascript':'text/html',
|
|
body:route.request().url().endsWith('.mjs')?source:'<!doctype html><title>PCM fixture</title>'
|
|
}));
|
|
await page.goto('https://fixture.invalid/');
|
|
const cases=await page.evaluate(async()=>{
|
|
const {PcmTimeline}=await import('/pcm.mjs');
|
|
async function run(legacy,inputRate,outputRate){
|
|
const context=new OfflineAudioContext(2,Math.round(outputRate*9),outputRate);
|
|
let now=0;
|
|
const playback={
|
|
get currentTime(){return now;}, state:'running',
|
|
createBuffer:(...args)=>context.createBuffer(...args),
|
|
createBufferSource:()=>context.createBufferSource()
|
|
};
|
|
const timeline=new PcmTimeline(playback,context.destination);
|
|
const length=inputRate/50;
|
|
const fraction=128/outputRate;
|
|
for(let packet=0;packet<400;packet++){
|
|
const samples=[new Float32Array(length),new Float32Array(length)];
|
|
for(let i=0;i<length;i++){
|
|
samples[0][i]=0.45*Math.cos(2*Math.PI*997*(packet*length+i)/inputRate);
|
|
samples[1][i]=0.45*Math.sin(2*Math.PI*997*(packet*length+i)/inputRate);
|
|
}
|
|
const sent=packet/50+0.367+0.012*Math.sin(packet);
|
|
now=Math.floor((sent-0.367)/fraction)*fraction;
|
|
const serverNowUs=(sent-0.367)*1e6;
|
|
const serverTimeUs=(packet/50+0.367)*1e6;
|
|
if(legacy){
|
|
const buffer=context.createBuffer(2,length,inputRate);
|
|
samples.forEach((channel,index)=>buffer.copyToChannel(channel,index));
|
|
const node=context.createBufferSource();node.buffer=buffer;node.connect(context.destination);
|
|
node.start(Math.max(now+0.01,now+(serverTimeUs-serverNowUs)/1e6));
|
|
}else timeline.push({samples,sampleRate:inputRate,serverTimeUs},serverNowUs);
|
|
}
|
|
const rendered=await context.startRendering();
|
|
const left=rendered.getChannelData(0),right=rendered.getChannelData(1);
|
|
let dropouts=0,clipped=0,nearSilenceSamples=0,run=0,largestRun=0,jumps=0;
|
|
for(let i=outputRate;i<outputRate*8;i++){
|
|
const quiet=Math.hypot(left[i],right[i])<0.05;
|
|
if(quiet){run++;nearSilenceSamples++;}
|
|
else if(run){
|
|
if(run>=outputRate*0.00075)dropouts++;
|
|
largestRun=Math.max(largestRun,run);
|
|
run=0;
|
|
}
|
|
if(Math.max(Math.abs(left[i]),Math.abs(right[i]))>=0.999)clipped++;
|
|
if(Math.hypot(left[i]-left[i-1],right[i]-right[i-1])>0.23)jumps++;
|
|
}
|
|
return {dropouts,clipped,nearSilenceSamples,largestRun,jumps};
|
|
}
|
|
const result=[];
|
|
for(const [sourceRate,contextRate] of [[48000,48000],[44100,48000],[48000,44100]]){
|
|
result.push({sourceRate,contextRate,baseline:await run(true,sourceRate,contextRate),candidate:await run(false,sourceRate,contextRate)});
|
|
}
|
|
return result;
|
|
});
|
|
console.log(JSON.stringify({engine,version:browser.version(),cases}));
|
|
if(cases.some(({baseline,candidate})=>baseline.dropouts===0||candidate.dropouts!==0||candidate.clipped!==0||candidate.jumps>=baseline.jumps))process.exitCode=1;
|
|
}finally{await browser.close();}
|
|
}
|