jenkins 64d0eb42f6 bstein: publish microphone watch and selected output test
Vendor tested Lesavka observer source a1ac564e. Reuse numeric-only receipts; no backend, routing or installed relay changes. Validated Kustomize, client dry-run, 70 Node tests plus recent-level test, and isolated fake-device Chromium capture/output/cleanup.
2026-12-03 11:20:27 -06:00

46 lines
2.1 KiB
JavaScript

// Explicit selected output only. A scheduled native audio deadline bounds tones
// even when JavaScript is delayed. No microphone is connected to this graph.
export function pulseSamples(rate) {
const data=new Float32Array(rate);
for(let i=0;i<rate;i++) {
const time=i/rate;
const envelope=time<.12?Math.min(1,time/.005,(.12-time)/.005):0;
data[i]=.025*envelope*Math.sin(2*Math.PI*1000*time);
}
return data;
}
export class OutputPulses {
constructor(Context=AudioContext){this.Context=Context;this.closed=false;this.context=null;}
async start(device,ended) {
if(!device)throw new Error('Output selection required');
const context=this.context=new this.Context({sinkId:device});
if(typeof context.setSinkId!=='function')throw new Error('Output selection unavailable');
await context.setSinkId(device);
if(this.closed)return;
this.source=context.createBufferSource();
const buffer=context.createBuffer(1,context.sampleRate,context.sampleRate);
buffer.copyToChannel(pulseSamples(context.sampleRate),0);
this.source.buffer=buffer;this.source.loop=true;
this.source.connect(context.destination);
await context.resume();
if(this.closed)return;
if(context.state!=='running')throw new Error('Output suspended');
this.started=context.currentTime;
this.source.onended=ended;
this.source.start();this.source.stop(this.started+120);
}
sample() {
if(!Number.isFinite(this.started))return null;
// Context-clock progress is not USB or acoustic playback evidence.
return {inputFrames:null,processedFrames:Math.round(Math.min(120,
Math.max(0,this.context.currentTime-this.started))*this.context.sampleRate),
exactZeroFrames:null,rms:null,peak:null,sampleRate:this.context.sampleRate};
}
stop() {
this.closed=true;
if(this.source){this.source.onended=null;try{this.source.stop();}catch{}try{this.source.disconnect();}catch{}}
if(this.context)void this.context.close().catch(()=>{});
}
}