(function(root){ function createRecordedSpeech(o){ const clock=o.clock||{set:(fn,ms)=>setTimeout(fn,ms),clear:id=>clearTimeout(id)}; let context, generation=0, active=null; const cache=new Map(), getContext=()=>context||(context=o.createContext()); async function unlock(){const ctx=getContext();if(ctx.state!=='running')await ctx.resume();} function prepare(i){ if(cache.has(i))return cache.get(i); const pending=(async()=>{ const controller=new AbortController(); const timeout=clock.set(()=>controller.abort(),12000); try{const response=await o.fetch(o.src(i),{signal:controller.signal});if(!response.ok)throw Error('audio-fetch');return await getContext().decodeAudioData(await response.arrayBuffer());} finally{clock.clear(timeout);} })(); cache.set(i,pending);pending.catch(()=>cache.delete(i));return pending; } function cancel(){generation++;if(active)active.finish();} async function play(i){ cancel();const ticket=generation;if(!o.enabled())return; await unlock();if(ticket!==generation)return; const buffer=await prepare(i);if(ticket!==generation)return; const ctx=getContext();if(ctx.state!=='running')throw Error('audio-blocked'); return new Promise((resolve,reject)=>{ const source=ctx.createBufferSource();source.buffer=buffer;source.playbackRate.value=1;source.connect(ctx.destination); let done=false,timer=null; const finish=error=>{if(done)return;done=true;if(timer!==null)clock.clear(timer);source.onended=null;try{source.stop();}catch{}source.disconnect();if(active&&active.source===source)active=null;if(error)reject(error);else resolve();}; active={source,finish};source.onended=()=>finish();timer=clock.set(()=>finish(Error('audio-timeout')),Math.max(12000,(buffer.duration+5)*1000)); try{source.start();}catch(error){finish(error);} }); } return {play,cancel,unlock,prepare}; } if(typeof module!=='undefined'&&module.exports)module.exports={createRecordedSpeech};else root.createRecordedSpeech=createRecordedSpeech; })(typeof window!=='undefined'?window:globalThis);