// ?test=digital only: listening clips kept in this page. Never uploaded; the // numeric receipts are unchanged. Not loaded by the passive watch mode. import {ClipStore, wav, zip, clipName, clock} from './clip-store.js'; const el=id=>document.getElementById(id); let player=null, playerUrl=null; function save(blob, name) { const url=URL.createObjectURL(blob), a=document.createElement('a'); a.href=url; a.download=name; a.click(); setTimeout(()=>URL.revokeObjectURL(url),2000); } function resetPlayer() { if (!playerUrl) return; player.pause(); player.removeAttribute('src'); player.load(); URL.revokeObjectURL(playerUrl); playerUrl=null; } function play(clip) { player??=new Audio(); resetPlayer(); playerUrl=URL.createObjectURL(new Blob([wav(clip.pcm,clip.rate)],{type:'audio/wav'})); player.src=playerUrl; void player.play().catch(()=>{}); } const phaseText=ms=>ms===null?'':' @'+(ms/1000).toFixed(2)+' s'; function render(store) { const list=el('clip-list'); list.replaceChildren(...store.clips.map(clip=>{ const li=document.createElement('li'); const label=document.createElement('span'); label.textContent=clock(clip.eventMs??clip.startMs)+' '+clip.type+phaseText(clip.phaseMs)+(clip.tapGap?' · gap':''); if (clip.event) label.title='kind '+clip.event[1]+' · '+clip.event[3]+' ms · value '+clip.event[4]; const button=document.createElement('button'); button.textContent='▶'; button.title='Play'; button.onclick=()=>play(clip); const link=document.createElement('a'); link.href='#'; link.textContent='WAV'; link.title=clipName(clip); link.onclick=e=>{e.preventDefault(); save(new Blob([wav(clip.pcm,clip.rate)],{type:'audio/wav'}),clipName(clip));}; li.append(label,' ',button,' ',link); return li; })); el('clips-count').textContent=store.clips.length+' clips'+(store.lost?' · '+store.lost+' dropped':''); el('clips-all').disabled=!store.clips.length; } export async function attachClips(run, minutes, isCurrent) { await run.context.audioWorklet.addModule(new URL('./clip-worklet.js',import.meta.url).href); if (!isCurrent()) return; const store=new ClipStore({rate:run.context.sampleRate}); const tap=new AudioWorkletNode(run.context,'clip-tap'); let collectQueued=false, stopped=false; const collect=()=>{ if (collectQueued) return; collectQueued=true; setTimeout(()=>{collectQueued=false; store.collect();},0); }; tap.port.onmessage=({data})=>{ store.ring.write(data.frame,data.samples); tap.port.postMessage(data.samples,[data.samples.buffer]); if (store.pending.length) collect(); }; run.source.connect(tap); tap.connect(run.context.destination); resetPlayer(); store.onChange=render; el('clips').hidden=false; render(store); el('clips-all').onclick=()=>{ const index=store.index(); index.minutes=minutes; const files=store.clips.map(c=>({name:clipName(c),data:wav(c.pcm,c.rate)})); files.push({name:'index.json',data:new TextEncoder().encode(JSON.stringify(index,null,2))}); save(new Blob(zip(files),{type:'application/zip'}),'rct-clips-'+new Date().toISOString().slice(0,19).replace(/:/g,'')+'.zip'); }; run.clips={ store, observe(path, data, detector) { if (store.observe(path,data.endMs,detector)) collect(); }, stop() { if (stopped) return; stopped=true; tap.port.onmessage=null; tap.port.postMessage('stop'); try { run.source?.disconnect(tap); tap.disconnect(); } catch {} // Keep what was captured: shorten pending post-roll to the last delivered frame. for (const p of store.pending) if (store.ring.head!==null && p.to>store.ring.head) { p.to=store.ring.head; p.toMs=p.fromMs+(p.to-p.from)*1000/store.rate; } store.pending=store.pending.filter(p=>p.to>p.from); store.collect(); }, }; }