// mic.html?mode=watch: passive long watch; otherwise the existing mic page. const watchMode=new URLSearchParams(location.search).get('mode')==='watch'; if(!watchMode) await import('./mic-observer.js'); else { const {WatchSession}=await import('./mic-watch.js'); const el=id=>document.getElementById(id); for(const id of ['output','tone','tone-stop','stage-label','local-review','output-status','output-report','path-link']) (el(id).closest('label')??el(id)).hidden=true; document.querySelector('h1').textContent='Passive mic watch'; el('start').textContent='Start watch';el('start').title='Holds the selected microphone open up to 24 h; numeric report every 10 s.'; let session=null,last=null; const buttons=()=>{el('start').disabled=!!session||!el('input').value;el('stop').disabled=!session; el('devices').disabled=!!session;el('input').disabled=!!session;el('save').disabled=!last;}; const show=s=>{last={watchId:s.id,events:s.events,last:{...s.last,_stats:undefined}}; el('level').value=(s.last?.peakE5??0)/1e5;el('metrics').textContent=JSON.stringify(last,null,2);buttons();}; el('devices').onclick=async()=>{ try { const stream=await navigator.mediaDevices.getUserMedia({audio:true,video:false}); for(const t of stream.getTracks())t.stop(); const select=el('input'),saved=select.value;select.replaceChildren(new Option('Select device','')); for(const d of await navigator.mediaDevices.enumerateDevices()) if(d.kind==='audioinput'&&!['default','communications'].includes(d.deviceId))select.add(new Option(d.label||'Unnamed device',d.deviceId)); select.value=saved;el('status').textContent='Select the USB microphone'; } catch {el('status').textContent='Device permission unavailable';} buttons(); }; el('input').onchange=buttons; el('start').onclick=async()=>{ if(session||!el('input').value)return; session=new WatchSession({deviceId:el('input').value,status:t=>{el('status').textContent=t;},show}); session.reports.status=t=>{el('report-status').textContent=t;}; buttons();await session.start(); }; el('stop').onclick=()=>{session?.stop();if(session)show(session);session=null;buttons();}; el('save').onclick=()=>{ const url=URL.createObjectURL(new Blob([JSON.stringify(last,null,2)],{type:'application/json'})); const a=document.createElement('a');a.href=url;a.download='mic-watch.json';a.click();setTimeout(()=>URL.revokeObjectURL(url),1000); }; window.addEventListener('pagehide',()=>session?.stop('page closed')); buttons(); }