gapless 4.4.1 → 4.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Queue.d.ts +72 -0
- package/dist/Queue.d.ts.map +1 -0
- package/dist/Track.d.ts +150 -0
- package/dist/Track.d.ts.map +1 -0
- package/dist/index.d.ts +4 -318
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/machines/fetchDecode.machine.d.ts +42 -0
- package/dist/machines/fetchDecode.machine.d.ts.map +1 -0
- package/dist/machines/queue.machine.d.ts +165 -0
- package/dist/machines/queue.machine.d.ts.map +1 -0
- package/dist/machines/track.machine.d.ts +291 -0
- package/dist/machines/track.machine.d.ts.map +1 -0
- package/dist/types.d.ts +99 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/audioContext.d.ts +25 -0
- package/dist/utils/audioContext.d.ts.map +1 -0
- package/dist/utils/mediaSession.d.ts +17 -0
- package/dist/utils/mediaSession.d.ts.map +1 -0
- package/dist/utils/throttle.d.ts +3 -0
- package/dist/utils/throttle.d.ts.map +1 -0
- package/package.json +10 -10
- package/src/Track.ts +5 -1
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{createActor as Z}from"xstate";var q=typeof window<"u",k;function K(){if(!q)return null;let o=window.AudioContext??window.webkitAudioContext;return o?new o:null}function T(){return k===void 0?null:k}function y(){k===void 0&&(k=K());let o=k;return o&&o.state==="suspended"?o.resume():Promise.resolve()}var _=typeof navigator<"u"&&"mediaSession"in navigator;function C(o){if(!_)return;let{mediaSession:e}=navigator;e.setActionHandler("play",o.onPlay),e.setActionHandler("pause",o.onPause),e.setActionHandler("nexttrack",o.onNext),e.setActionHandler("previoustrack",o.onPrevious),e.setActionHandler("seekto",t=>{t.seekTime!=null&&o.onSeek(t.seekTime)})}function L(o){if(_){if(!o){navigator.mediaSession.metadata=null;return}navigator.mediaSession.metadata=new MediaMetadata({title:o.title??"",artist:o.artist??"",album:o.album??"",artwork:o.artwork??[]})}}function D(o){_&&(navigator.mediaSession.playbackState=o?"playing":"paused")}function M(o,e,t=1){if(_)try{navigator.mediaSession.setPositionState({duration:o,position:e,playbackRate:t})}catch{}}import{setup as Y,assign as p}from"xstate";function I(o){return Y({types:{context:{},events:{}},guards:{hasNextTrack:({context:e})=>e.currentTrackIndex+1<e.trackCount,playImmediately:({event:e})=>!!e.playImmediately,willBeEmpty:({context:e})=>e.trackCount<=1,isRemovingCurrent:({context:e,event:t})=>t.index===e.currentTrackIndex},actions:{incrementTrackCount:p({trackCount:({context:e})=>e.trackCount+1}),decrementTrackCount:p({trackCount:({context:e})=>Math.max(0,e.trackCount-1),currentTrackIndex:({context:e,event:t})=>{let r=t,a=Math.max(0,e.trackCount-1);return r.index<e.currentTrackIndex?Math.max(0,e.currentTrackIndex-1):a>0?Math.min(e.currentTrackIndex,a-1):0}}),gotoTrackIndex:p({currentTrackIndex:({event:e})=>e.index}),advanceToNextTrack:p({currentTrackIndex:({context:e})=>{let t=e.currentTrackIndex+1;return t<e.trackCount?t:e.currentTrackIndex}}),goToPreviousTrack:p({currentTrackIndex:({context:e})=>Math.max(0,e.currentTrackIndex-1)}),advanceOnTrackEnd:p({currentTrackIndex:({context:e})=>Math.min(e.currentTrackIndex+1,e.trackCount-1)}),resetToFirstTrack:p({currentTrackIndex:()=>0}),deactivateCurrent:()=>{},deactivateEndedTrack:()=>{},activateAndPlayCurrent:()=>{},playOrContinueGapless:()=>{},cancelAllGapless:()=>{},notifyStartNewTrack:()=>{},notifyPlayNextTrack:()=>{},notifyPlayPreviousTrack:()=>{},notifyEnded:()=>{},updateMediaSessionMetadata:()=>{},preloadAhead:()=>{},playCurrent:()=>{},pauseCurrent:()=>{},seekCurrent:()=>{},seekCurrentToStartTime:()=>{},scheduleGapless:()=>{},cancelScheduledGapless:()=>{},cancelAndRescheduleGapless:()=>{}}}).createMachine({id:"queue",initial:"idle",context:o,on:{ADD_TRACK:{actions:"incrementTrackCount"},REMOVE_TRACK:{actions:"decrementTrackCount"}},states:{idle:{on:{PLAY:{target:"playing",actions:["playCurrent","updateMediaSessionMetadata","preloadAhead","scheduleGapless"]},GOTO:[{guard:"playImmediately",target:"playing",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","activateAndPlayCurrent","notifyStartNewTrack","updateMediaSessionMetadata","preloadAhead"]},{target:"paused",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","preloadAhead"]}],TRACK_LOADED:{actions:["preloadAhead"]}}},playing:{on:{REMOVE_TRACK:[{guard:"willBeEmpty",target:"idle",actions:["cancelAllGapless","decrementTrackCount"]},{guard:"isRemovingCurrent",actions:["cancelAllGapless","decrementTrackCount","activateAndPlayCurrent","notifyStartNewTrack","updateMediaSessionMetadata","preloadAhead"]},{actions:["decrementTrackCount","cancelAndRescheduleGapless","preloadAhead"]}],PAUSE:{target:"paused",actions:["cancelScheduledGapless","pauseCurrent"]},TOGGLE:{target:"paused",actions:["cancelScheduledGapless","pauseCurrent"]},NEXT:{actions:["deactivateCurrent","cancelAllGapless","advanceToNextTrack","activateAndPlayCurrent","notifyStartNewTrack","notifyPlayNextTrack","updateMediaSessionMetadata","preloadAhead"]},PREVIOUS:{actions:["deactivateCurrent","cancelAllGapless","goToPreviousTrack","activateAndPlayCurrent","notifyStartNewTrack","notifyPlayPreviousTrack","updateMediaSessionMetadata","preloadAhead"]},GOTO:[{guard:"playImmediately",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","activateAndPlayCurrent","notifyStartNewTrack","updateMediaSessionMetadata","preloadAhead"]},{actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","preloadAhead"]}],SEEK:{actions:["seekCurrent","cancelAndRescheduleGapless"]},TRACK_ENDED:[{guard:"hasNextTrack",target:"playing",actions:["deactivateEndedTrack","advanceOnTrackEnd","playOrContinueGapless","notifyStartNewTrack","notifyPlayNextTrack","updateMediaSessionMetadata","preloadAhead"]},{target:"ended",actions:["deactivateEndedTrack","notifyEnded"]}],TRACK_LOADED:[{guard:({context:e,event:t})=>t.index===e.currentTrackIndex,actions:["cancelAndRescheduleGapless","preloadAhead"]},{actions:["scheduleGapless","preloadAhead"]}]}},paused:{on:{REMOVE_TRACK:[{guard:"willBeEmpty",target:"idle",actions:["cancelAllGapless","decrementTrackCount"]},{guard:"isRemovingCurrent",actions:["cancelAllGapless","decrementTrackCount","preloadAhead"]},{actions:["decrementTrackCount","preloadAhead"]}],PLAY:{target:"playing",actions:["playCurrent","updateMediaSessionMetadata","preloadAhead","scheduleGapless"]},TOGGLE:{target:"playing",actions:["playCurrent","updateMediaSessionMetadata","preloadAhead","scheduleGapless"]},NEXT:{actions:["deactivateCurrent","cancelAllGapless","advanceToNextTrack","activateAndPlayCurrent","notifyStartNewTrack","notifyPlayNextTrack","updateMediaSessionMetadata","preloadAhead"]},PREVIOUS:{actions:["deactivateCurrent","cancelAllGapless","goToPreviousTrack","activateAndPlayCurrent","notifyStartNewTrack","notifyPlayPreviousTrack","updateMediaSessionMetadata","preloadAhead"]},GOTO:[{guard:"playImmediately",target:"playing",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","activateAndPlayCurrent","notifyStartNewTrack","updateMediaSessionMetadata","preloadAhead"]},{actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","preloadAhead"]}],SEEK:{actions:["seekCurrent"]},TRACK_ENDED:[{guard:"hasNextTrack",target:"paused",actions:["deactivateEndedTrack","advanceOnTrackEnd","notifyStartNewTrack","updateMediaSessionMetadata"]},{target:"ended",actions:["deactivateEndedTrack","notifyEnded"]}],TRACK_LOADED:{actions:["preloadAhead"]}}},ended:{on:{ADD_TRACK:{target:"paused",actions:"incrementTrackCount"},PLAY:{target:"playing",actions:["resetToFirstTrack","playCurrent","updateMediaSessionMetadata","preloadAhead","scheduleGapless"]},GOTO:[{guard:"playImmediately",target:"playing",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","activateAndPlayCurrent","notifyStartNewTrack","updateMediaSessionMetadata","preloadAhead"]},{target:"paused",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","preloadAhead"]}]}}}})}import{createActor as j,fromPromise as R}from"xstate";import{setup as X,assign as u,spawnChild as z}from"xstate";import{setup as Q,assign as b,sendParent as S,fromPromise as N}from"xstate";var x=Q({types:{context:{},input:{}},actors:{resolveUrl:N(async()=>null),fetchAudio:N(async()=>{}),decodeAudio:N(async()=>{})},guards:{shouldSkipHEAD:({context:o})=>o.skipHEAD}}).createMachine({id:"fetchDecode",initial:"resolvingUrl",context:{trackUrl:"",resolvedUrl:"",skipHEAD:!1},entry:b(({event:o})=>{let{input:e}=o;return{trackUrl:e.trackUrl,resolvedUrl:e.resolvedUrl,skipHEAD:e.skipHEAD}}),states:{resolvingUrl:{always:{guard:"shouldSkipHEAD",target:"fetching"},invoke:{id:"resolveUrl",src:"resolveUrl",input:({context:o})=>({trackUrl:o.trackUrl}),onDone:{target:"fetching",actions:[b({resolvedUrl:({event:o,context:e})=>o.output??e.resolvedUrl,skipHEAD:()=>!0}),S(({event:o,context:e})=>({type:"URL_RESOLVED",url:o.output??e.resolvedUrl}))]},onError:{target:"fetching",actions:b({skipHEAD:()=>!0})}}},fetching:{invoke:{id:"fetchAudio",src:"fetchAudio",input:({context:o})=>({resolvedUrl:o.resolvedUrl}),onDone:"decoding",onError:{target:"error",actions:S({type:"BUFFER_ERROR"})}}},decoding:{invoke:{id:"decodeAudio",src:"decodeAudio",input:()=>{},onDone:{target:"done",actions:S({type:"BUFFER_READY"})},onError:{target:"error",actions:S({type:"BUFFER_ERROR"})}}},done:{type:"final"},error:{type:"final"}}});function U(o){return X({types:{context:{},events:{}},actors:{fetchDecode:x},guards:{canPlayWebAudio:()=>!1,isWebAudioOnly:()=>!1,canStartFetch:({context:e})=>e.webAudioLoadingState==="NONE"&&!e.fetchStarted},actions:{playHtml5:()=>{},startSourceNode:()=>{},crossoverHtml5ToWebAudio:()=>{},notifyBufferReady:()=>{},startScheduledSourceNode:()=>{},startProgressLoop:()=>{},pauseHtml5:()=>{},freezePausedTime:()=>{},stopSourceNode:()=>{},stopProgressLoop:()=>{},reportProgress:()=>{},seekHtml5:()=>{},seekWebAudio:()=>{},resetHtml5Element:()=>{},resetTiming:()=>{},notifyTrackEnded:()=>{},triggerFetchForPendingPlay:()=>{},setPendingPlay:u({pendingPlay:()=>!0}),clearPendingPlay:u({pendingPlay:()=>!1}),setIsPlaying:u({isPlaying:()=>!0}),clearIsPlaying:u({isPlaying:()=>!1}),setLoadingState:u({webAudioLoadingState:()=>"LOADING"}),setLoadedState:u({webAudioLoadingState:()=>"LOADED"}),setErrorState:u({webAudioLoadingState:()=>"ERROR"}),clearScheduleAndLookahead:u({scheduledStartContextTime:()=>null,notifiedLookahead:()=>!1}),setPlayingWebAudio:u({isPlaying:()=>!0,webAudioLoadingState:()=>"LOADED",playbackType:()=>"WEBAUDIO"}),setScheduledGapless:u({isPlaying:()=>!0,webAudioLoadingState:()=>"LOADED",playbackType:()=>"WEBAUDIO",scheduledStartContextTime:({event:e})=>e.when}),clearPlayingAndSchedule:u({isPlaying:()=>!1,scheduledStartContextTime:()=>null,notifiedLookahead:()=>!1}),setNotifiedLookahead:u({notifiedLookahead:()=>!0}),setResolvedUrl:u({resolvedUrl:({event:e})=>e.url}),clearScheduledStart:u({scheduledStartContextTime:()=>null}),setPlayingWebAudioType:u({isPlaying:()=>!0,playbackType:()=>"WEBAUDIO"}),setPlaybackTypeWebAudio:u({playbackType:()=>"WEBAUDIO"}),clearNotifiedLookahead:u({notifiedLookahead:()=>!1})}}).createMachine({id:"track",initial:"idle",context:o,on:{START_FETCH:{guard:"canStartFetch",actions:[u({webAudioLoadingState:()=>"LOADING",fetchStarted:()=>!0}),z("fetchDecode",{id:"fetchDecode",input:({context:e})=>({trackUrl:e.trackUrl,resolvedUrl:e.resolvedUrl,skipHEAD:e.skipHEAD})})]}},states:{idle:{on:{HTML5_ENDED:{actions:["notifyTrackEnded"]},DEACTIVATE:{actions:["pauseHtml5","resetHtml5Element","resetTiming","stopProgressLoop","clearScheduleAndLookahead","clearPendingPlay"]},ACTIVATE:{actions:["resetTiming","resetHtml5Element","clearScheduleAndLookahead","clearPendingPlay"]},PLAY:[{guard:"canPlayWebAudio",target:"webaudio",actions:["setPlayingWebAudio","startSourceNode","startProgressLoop"]},{guard:"isWebAudioOnly",actions:["setPendingPlay","triggerFetchForPendingPlay"]},{target:"html5",actions:["setIsPlaying","playHtml5","startProgressLoop","triggerFetchForPendingPlay"]}],PLAY_WEBAUDIO:{target:"webaudio",actions:["setPlayingWebAudio","startSourceNode","startProgressLoop"]},SCHEDULE_GAPLESS:{target:"webaudio",actions:["setScheduledGapless","startScheduledSourceNode"]},PRELOAD:{target:"loading"},BUFFER_LOADING:{actions:"setLoadingState"},BUFFER_READY:[{guard:({context:e})=>e.pendingPlay,target:"webaudio",actions:["clearPendingPlay","setPlayingWebAudio","startSourceNode","startProgressLoop","notifyBufferReady"]},{actions:["setLoadedState","notifyBufferReady"]}],BUFFER_ERROR:{actions:["setErrorState","clearPendingPlay"]},URL_RESOLVED:{actions:"setResolvedUrl"}}},html5:{on:{PAUSE:{actions:["clearIsPlaying","pauseHtml5","stopProgressLoop","reportProgress"]},PLAY:{actions:["setIsPlaying","playHtml5","startProgressLoop"]},BUFFER_LOADING:{actions:"setLoadingState"},PLAY_WEBAUDIO:{target:"webaudio",actions:"setPlayingWebAudio"},BUFFER_READY:{target:"webaudio",actions:["setLoadedState","crossoverHtml5ToWebAudio","setPlaybackTypeWebAudio","clearNotifiedLookahead","notifyBufferReady"]},BUFFER_ERROR:{actions:"setErrorState"},SEEK:{actions:["seekHtml5","reportProgress"]},LOOKAHEAD_REACHED:{actions:"setNotifiedLookahead"},HTML5_ENDED:{target:"idle",actions:["clearIsPlaying","stopProgressLoop","notifyTrackEnded"]},ACTIVATE:{target:"idle",actions:["clearPlayingAndSchedule","pauseHtml5","stopProgressLoop","resetTiming","resetHtml5Element"]},URL_RESOLVED:{actions:"setResolvedUrl"},DEACTIVATE:{target:"idle",actions:["clearIsPlaying","pauseHtml5","resetHtml5Element","resetTiming","stopProgressLoop"]}}},loading:{on:{BUFFER_LOADING:{actions:"setLoadingState"},BUFFER_READY:[{guard:({context:e})=>e.pendingPlay,target:"webaudio",actions:["clearPendingPlay","setPlayingWebAudio","startSourceNode","startProgressLoop","notifyBufferReady"]},{target:"idle",actions:["setLoadedState","notifyBufferReady"]}],BUFFER_ERROR:{target:"idle",actions:["setErrorState","clearPendingPlay"]},PLAY:[{guard:"canPlayWebAudio",target:"webaudio",actions:["setPlayingWebAudio","startSourceNode","startProgressLoop"]},{guard:"isWebAudioOnly",actions:["setPendingPlay","triggerFetchForPendingPlay"]},{target:"html5",actions:["setIsPlaying","playHtml5","startProgressLoop","triggerFetchForPendingPlay"]}],PLAY_WEBAUDIO:{target:"webaudio",actions:["setPlayingWebAudio","startSourceNode","startProgressLoop"]},SCHEDULE_GAPLESS:{target:"webaudio",actions:["setScheduledGapless","startScheduledSourceNode"]},ACTIVATE:{target:"idle",actions:["clearPlayingAndSchedule","clearPendingPlay","resetTiming","resetHtml5Element"]},DEACTIVATE:{target:"idle",actions:["clearIsPlaying","clearPendingPlay","resetTiming"]},URL_RESOLVED:{actions:"setResolvedUrl"}}},webaudio:{on:{PAUSE:{actions:["clearIsPlaying","freezePausedTime","clearScheduledStart","stopSourceNode","stopProgressLoop","reportProgress"]},PLAY:[{guard:"canPlayWebAudio",actions:["setIsPlaying","startSourceNode","startProgressLoop"]},{actions:"setIsPlaying"}],PLAY_WEBAUDIO:{actions:"setPlayingWebAudioType"},SEEK:{actions:["clearScheduledStart","seekWebAudio","reportProgress"]},SET_VOLUME:{},CANCEL_GAPLESS:{target:"idle",actions:["clearPlayingAndSchedule","stopSourceNode","stopProgressLoop","resetTiming"]},LOOKAHEAD_REACHED:{actions:"setNotifiedLookahead"},WEBAUDIO_ENDED:{target:"idle",actions:["clearIsPlaying","stopProgressLoop","notifyTrackEnded"]},ACTIVATE:{target:"idle",actions:["clearPlayingAndSchedule","stopSourceNode","stopProgressLoop","resetTiming","resetHtml5Element"]},DEACTIVATE:{target:"idle",actions:["clearPlayingAndSchedule","stopSourceNode","resetTiming","resetHtml5Element","stopProgressLoop"]}}}}})}var J=5,w=15,E=.03,g=class{index;metadata;_trackUrl;_resolvedUrl;skipHEAD;_pendingArrayBuffer=null;audio;_playbackMethod;get ctx(){if(this._playbackMethod==="HTML5_ONLY")return null;let e=T();if(e&&!this.gainNode){this.gainNode=e.createGain(),this.gainNode.gain.value=this.audio.volume,this.gainNode.connect(e.destination);try{this._mediaElementSource=e.createMediaElementSource(this.audio),this._html5GainNode=e.createGain(),this._html5GainNode.gain.value=1,this._mediaElementSource.connect(this._html5GainNode),this._html5GainNode.connect(this.gainNode),this.audio.volume=1}catch{this._mediaElementSource=null,this._html5GainNode=null}}return e}gainNode=null;_mediaElementSource=null;_html5GainNode=null;sourceNode=null;audioBuffer=null;_bufferStartPaddingSec=0;_bufferAlignmentMeasured=!1;_waRefCtxTime=0;_waRefTrackTime=0;pausedAtTrackTime=0;_actor;queueRef;rafId=null;_notifiedPreloadThreshold=!1;constructor(e){this.index=e.index,this._trackUrl=e.trackUrl,this._resolvedUrl=e.trackUrl,this.skipHEAD=e.skipHEAD??!1,this.metadata=e.metadata??{},this.queueRef=e.queue,this.audio=new Audio,this.audio.preload="none",e.queue.playbackMethod!=="HTML5_ONLY"&&(this.audio.crossOrigin="anonymous"),this.audio.src=this._trackUrl,this.audio.volume=e.queue.volume,this.audio.controls=!1,this.audio.onerror=()=>{var d,c;let a=(d=this.audio.error)==null?void 0:d.code;if(a===1)return;let i=((c=this.audio.error)==null?void 0:c.message)??"unknown";this.queueRef.onError(new Error(`HTML5 audio error on track ${this.index} (code ${a}): ${i}`))},this.audio.onended=()=>{this.queueRef.onDebug(`audio.onended track=${this.index} machineState=${this._actor.getSnapshot().value} queueIdx=${this.queueRef.currentTrackIndex}`),this._actor.send({type:"HTML5_ENDED"})},this._playbackMethod=e.queue.playbackMethod;let t={trackUrl:this._trackUrl,resolvedUrl:this._trackUrl,skipHEAD:this.skipHEAD,playbackType:"HTML5",webAudioLoadingState:"NONE",isPlaying:!1,scheduledStartContextTime:null,notifiedLookahead:!1,fetchStarted:!1,pendingPlay:!1},r=U(t).provide({guards:{canPlayWebAudio:()=>!!(this.ctx&&this.audioBuffer&&this.gainNode),isWebAudioOnly:()=>this._playbackMethod==="WEBAUDIO_ONLY"},actors:{fetchDecode:x.provide({actors:{resolveUrl:R(async({signal:a})=>{let i=await fetch(this._trackUrl,{method:"HEAD",signal:a});return i.redirected&&i.url?(this._resolvedUrl=i.url,i.url):null}),fetchAudio:R(async({input:a,signal:i})=>{let{resolvedUrl:d}=a,c=await fetch(d,{signal:i});if(!c.ok)throw new Error(`HTTP ${c.status} for ${d}`);this._pendingArrayBuffer=await c.arrayBuffer()}),decodeAudio:R(async()=>{let a=this._pendingArrayBuffer;if(this._pendingArrayBuffer=null,!a||!this.ctx)throw new Error("No ArrayBuffer or AudioContext");this.audioBuffer=await this.ctx.decodeAudioData(a),this._maybeComputeBufferAlignment()})}})},actions:{triggerFetchForPendingPlay:()=>{this.preload(),y()},playHtml5:()=>this._playHtml5(),startSourceNode:()=>{this._startSourceNode(this.pausedAtTrackTime)},crossoverHtml5ToWebAudio:({context:a})=>{this._crossoverHtml5ToWebAudio(a.isPlaying)},startScheduledSourceNode:({context:a})=>{let i=a.scheduledStartContextTime;if(i===null||!this.ctx||!this.audioBuffer||!this.gainNode)return;this._maybeComputeBufferAlignment(),this._stopSourceNode(),this.sourceNode=this.ctx.createBufferSource(),this.sourceNode.buffer=this.audioBuffer,this.sourceNode.playbackRate.value=this.queueRef.playbackRate,this.sourceNode.connect(this.gainNode),this.sourceNode.onended=this._handleWebAudioEnded,this.sourceNode.start(i,this._bufferStartPaddingSec);let d=this.audioBuffer.duration-this._bufferStartPaddingSec,c=this.sourceNode.playbackRate.value||1;d>0&&this.sourceNode.stop(i+d/c),this._waRefCtxTime=i,this._waRefTrackTime=0,this.queueRef.onDebug(`startScheduledSourceNode track=${this.index} when=${i.toFixed(3)} ctxNow=${this.ctx.currentTime.toFixed(3)} delta=${(i-this.ctx.currentTime).toFixed(3)}s padding=${this._bufferStartPaddingSec.toFixed(3)}s`)},startProgressLoop:()=>this.startProgressLoop(),pauseHtml5:()=>this.audio.pause(),freezePausedTime:()=>{let a=this.currentTime;this.pausedAtTrackTime=isFinite(a)?a:0},stopSourceNode:()=>this._stopSourceNode(),stopProgressLoop:()=>this._stopProgressLoop(),reportProgress:()=>{queueMicrotask(()=>this.queueRef.onProgress(this.toInfo()))},seekHtml5:()=>this._seekHtml5(),seekWebAudio:()=>this._seekWebAudio(),resetHtml5Element:()=>{this.audio.currentTime=0},resetTiming:()=>{this._waRefCtxTime=0,this._waRefTrackTime=0,this.pausedAtTrackTime=0},notifyTrackEnded:()=>{queueMicrotask(()=>this.queueRef.onTrackEnded(this))},notifyBufferReady:()=>{queueMicrotask(()=>this.queueRef.onTrackBufferReady(this))}}});this._actor=j(r),this._actor.start()}play(){this.queueRef.onDebug(`Track.play() track=${this.index} machineState=${this._actor.getSnapshot().value} hasBuffer=${!!this.audioBuffer} hasCtx=${!!this.ctx} audioPaused=${this.audio.paused}`),this._actor.send({type:"PLAY"})}pause(){this._actor.send({type:"PAUSE"})}seek(e){if(!isFinite(e))return;let t=Math.max(0,isNaN(this.duration)?e:Math.min(e,this.duration));this.pausedAtTrackTime=t,this._actor.send({type:"SEEK",time:t})}setVolume(e){let t=Math.min(1,Math.max(0,e));this._mediaElementSource?this.audio.volume=1:this.audio.volume=t,this.gainNode&&(this.gainNode.gain.value=t),this._actor.send({type:"SET_VOLUME",volume:t})}setPlaybackRate(e){if(this.ctx&&this.sourceNode&&this._actor.getSnapshot().context.isPlaying&&this.ctx.currentTime>=this._waRefCtxTime){let t=this.sourceNode.playbackRate.value;this._waRefTrackTime=this._waRefTrackTime+(this.ctx.currentTime-this._waRefCtxTime)*t,this._waRefCtxTime=this.ctx.currentTime}if(this.audio.playbackRate=e,this.sourceNode&&(this.sourceNode.playbackRate.value=e,this.audioBuffer&&this.ctx)){let t=this.audioBuffer.duration-this._bufferStartPaddingSec-this._waRefTrackTime;if(t>0)try{this.sourceNode.stop(this._waRefCtxTime+t/e)}catch{}}}preload(){this.queueRef.onDebug(`preload() track=${this.index} state=${this._actor.getSnapshot().value} hasBuffer=${!!this.audioBuffer} hasCtx=${!!this.ctx}`),this._actor.getSnapshot().value==="idle"&&this._actor.send({type:"PRELOAD"}),!this.audioBuffer&&(y(),this.ctx&&this._actor.send({type:"START_FETCH"}))}seekToEnd(e=6){let t=this.duration;!isNaN(t)&&t>e&&this.seek(t-e)}activate(){this._actor.send({type:"ACTIVATE"})}deactivate(){this.queueRef.onDebug(`Track.deactivate() track=${this.index} machineState=${this._actor.getSnapshot().value} isPlaying=${this.isPlaying}`),this._notifiedPreloadThreshold=!1,this._actor.send({type:"DEACTIVATE"}),this.queueRef.onDebug(`Track.deactivate() done track=${this.index} machineState=${this._actor.getSnapshot().value}`)}destroy(){var e;this.deactivate(),this._pendingArrayBuffer=null,this.audioBuffer=null,(e=this.gainNode)==null||e.disconnect(),this.gainNode=null,this._actor.stop()}scheduleHtml5Mute(e){!this._html5GainNode||!this.ctx||(this._html5GainNode.gain.setValueAtTime(1,e-.005),this._html5GainNode.gain.linearRampToValueAtTime(0,e))}cancelHtml5Mute(){!this._html5GainNode||!this.ctx||(this._html5GainNode.gain.cancelScheduledValues(this.ctx.currentTime),this._html5GainNode.gain.setValueAtTime(1,this.ctx.currentTime))}cancelGaplessStart(){this._actor.getSnapshot().context.scheduledStartContextTime!==null&&this._actor.send({type:"CANCEL_GAPLESS"})}scheduleGaplessStart(e){if(!this.ctx||!this.audioBuffer||!this.gainNode)return!1;let t=this._actor.getSnapshot().value;return t!=="idle"&&t!=="loading"?!1:(this._actor.send({type:"SCHEDULE_GAPLESS",when:e}),!0)}get currentTime(){let e=this._actor.getSnapshot();return e.value==="webaudio"?e.context.isPlaying?this.ctx?Math.max(0,this._waRefTrackTime+(this.ctx.currentTime-this._waRefCtxTime)*this.queueRef.playbackRate):0:this.pausedAtTrackTime:this.audio.currentTime}get duration(){return this._actor.getSnapshot().value==="html5"&&!isNaN(this.audio.duration)?this.audio.duration:this.audioBuffer?this.audioBuffer.duration:this.audio.duration}get isPaused(){let e=this._actor.getSnapshot();return e.value==="webaudio"?!e.context.isPlaying:this.audio.paused}get isPlaying(){return this._actor.getSnapshot().context.isPlaying}get trackUrl(){return this._resolvedUrl}get playbackType(){return this._actor.getSnapshot().context.playbackType}get webAudioLoadingState(){return this._actor.getSnapshot().context.webAudioLoadingState}get hasSourceNode(){return this.sourceNode!==null}get machineState(){return this._actor.getSnapshot().value}get scheduledStartContextTime(){return this._actor.getSnapshot().context.scheduledStartContextTime}get playbackEndContextTime(){let e=this._actor.getSnapshot();if(e.value!=="webaudio"||!e.context.isPlaying||!this.sourceNode||!this.audioBuffer)return null;let t=this.sourceNode.playbackRate.value||1,r=this.audioBuffer.duration-this._bufferStartPaddingSec-this._waRefTrackTime;return this._waRefCtxTime+r/t}get isBufferLoaded(){return this.audioBuffer!==null}toInfo(){var e;return{index:this.index,currentTime:this.currentTime,duration:this.duration,isPlaying:this.isPlaying,isPaused:this.isPaused,volume:((e=this.gainNode)==null?void 0:e.gain.value)??this.audio.volume,trackUrl:this.trackUrl,playbackType:this.playbackType,webAudioLoadingState:this.webAudioLoadingState,metadata:this.metadata,playbackRate:this.queueRef.playbackRate,machineState:this.machineState}}_playHtml5(){this.audio.preload!=="auto"&&(this.audio.preload="auto"),this.audio.playbackRate=this.queueRef.playbackRate;let e=this.audio.play();e&&e.catch(t=>{t instanceof Error&&t.name==="NotAllowedError"?this.queueRef.onPlayBlocked():t instanceof Error&&t.name==="AbortError"||this.queueRef.onError(t instanceof Error?t:new Error(String(t)))})}_seekHtml5(){let e=this.pausedAtTrackTime;isFinite(e)&&(this.audio.preload!=="auto"&&(this.audio.preload="auto"),this.audio.readyState>=HTMLMediaElement.HAVE_METADATA?this.audio.currentTime=e:(this.audio.addEventListener("loadedmetadata",()=>{this.audio.currentTime=e},{once:!0}),this.audio.load()))}_crossoverHtml5ToWebAudio(e){if(!this.ctx||!this.audioBuffer||!this.gainNode)return;let t=this.audio.currentTime;if(this.pausedAtTrackTime=isFinite(t)?t:0,!e){this.audio.pause(),this.queueRef.onDebug(`crossoverHtml5ToWebAudio track=${this.index} offset=${this.pausedAtTrackTime.toFixed(3)} wasPlaying=false`);return}let r=this._startSourceNode(this.pausedAtTrackTime,E);if(r!==null&&this._html5GainNode){this._html5GainNode.gain.cancelScheduledValues(r),this._html5GainNode.gain.setValueAtTime(1,r),this._html5GainNode.gain.linearRampToValueAtTime(0,r+E);let a=this.ctx,i=this._html5GainNode,d=(r-this.ctx.currentTime+E)*1e3+5;setTimeout(()=>{this.audio.pause(),a&&i&&(i.gain.cancelScheduledValues(a.currentTime),i.gain.setValueAtTime(1,a.currentTime))},d)}else if(!this._html5GainNode)if(r!==null&&this.ctx){let a=(r-this.ctx.currentTime)*1e3;setTimeout(()=>{this.audio.volume=0,this.audio.pause(),this.audio.volume=1},Math.max(0,a))}else this.audio.volume=0,this.audio.pause(),this.audio.volume=1;this.queueRef.onDebug(`crossoverHtml5ToWebAudio track=${this.index} offset=${this.pausedAtTrackTime.toFixed(3)} wasPlaying=true fade=${E}s mediaSource=${!!this._html5GainNode} when=${(r==null?void 0:r.toFixed(3))??"null"}`)}_maybeComputeBufferAlignment(){this.audioBuffer&&(this._bufferAlignmentMeasured||(this._bufferStartPaddingSec=0,this._bufferAlignmentMeasured=!0,this.queueRef.onDebug(`_maybeComputeBufferAlignment track=${this.index} bufferDur=${this.audioBuffer.duration.toFixed(3)}s html5Dur=${isNaN(this.audio.duration)?"NaN":this.audio.duration.toFixed(3)+"s"} (alignment shift disabled \u2014 see comment)`)))}_startSourceNode(e,t=0){if(!this.ctx||!this.audioBuffer||!this.gainNode)return null;this._maybeComputeBufferAlignment(),this._stopSourceNode();let r=this.ctx.state==="suspended";r&&this.ctx.resume(),this.sourceNode=this.ctx.createBufferSource(),this.sourceNode.buffer=this.audioBuffer,this.sourceNode.playbackRate.value=this.queueRef.playbackRate;let a=r?.15:Math.max(.02,2*(this.ctx.baseLatency||0)+.01),i=this.ctx.currentTime+a,d=this.queueRef.playbackRate,c=this.audioBuffer.duration-this._bufferStartPaddingSec,h=Math.min(e+a*d,c);if(t>0){let f=this.ctx.createGain();f.gain.setValueAtTime(0,i),f.gain.linearRampToValueAtTime(1,i+t),this.sourceNode.connect(f),f.connect(this.gainNode)}else this.sourceNode.connect(this.gainNode);this.sourceNode.onended=this._handleWebAudioEnded,this._waRefCtxTime=i,this._waRefTrackTime=h,this.sourceNode.start(i,h+this._bufferStartPaddingSec);let A=this.audioBuffer.duration-this._bufferStartPaddingSec-h;return A>0&&this.sourceNode.stop(i+A/d),i}_stopSourceNode(){if(this.sourceNode){this.sourceNode.onended=null;try{this.sourceNode.stop()}catch{}try{this.sourceNode.disconnect()}catch{}this.sourceNode=null}}_seekWebAudio(){let t=this._actor.getSnapshot().context.isPlaying,r=this.pausedAtTrackTime;this._stopSourceNode(),t&&this._startSourceNode(r)}_handleWebAudioEnded=()=>{this.queueRef.onDebug(`_handleWebAudioEnded track=${this.index} sourceNode=${!!this.sourceNode} queueIdx=${this.queueRef.currentTrackIndex}`),this.sourceNode&&this._actor.send({type:"WEBAUDIO_ENDED"})};startProgressLoop(){if(this.rafId!==null)return;let e=()=>{if(this.isPaused||!this.isPlaying){this.rafId=null;return}this.queueRef.onProgress(this.toInfo());let t=this.duration-this.currentTime;!this._actor.getSnapshot().context.notifiedLookahead&&!isNaN(t)&&t<=J&&(this._actor.send({type:"LOOKAHEAD_REACHED"}),queueMicrotask(()=>this.queueRef.onTrackBufferReady(this)));let a=isNaN(this.duration)?w:Math.min(this.duration*.2,w);!this._notifiedPreloadThreshold&&this.currentTime>=a&&(this._notifiedPreloadThreshold=!0,queueMicrotask(()=>this.queueRef.onPreloadReady(this))),this.rafId=requestAnimationFrame(e)};this.rafId=requestAnimationFrame(e)}_stopProgressLoop(){this.rafId!==null&&(cancelAnimationFrame(this.rafId),this.rafId=null)}};function O(o,e){let t=0;return(...r)=>{let a=performance.now();a-t<e||(t=a,o(...r))}}var G=5,v=null;function ee(){if(v)return v;let o=8e3,e=o*10,t=44+e,r=new ArrayBuffer(t),a=new DataView(r),i=(d,c)=>{for(let h=0;h<c.length;h++)a.setUint8(d+h,c.charCodeAt(h))};return i(0,"RIFF"),a.setUint32(4,t-8,!0),i(8,"WAVE"),i(12,"fmt "),a.setUint32(16,16,!0),a.setUint16(20,1,!0),a.setUint16(22,1,!0),a.setUint32(24,o,!0),a.setUint32(28,o,!0),a.setUint16(32,1,!0),a.setUint16(34,8,!0),i(36,"data"),a.setUint32(40,e,!0),new Uint8Array(r,44).fill(128),v=URL.createObjectURL(new Blob([r],{type:"audio/wav"})),v}var P=class{_tracks=[];_actor;_onProgress;_onEnded;_onPlayNextTrack;_onPlayPreviousTrack;_onStartNewTrack;_onError;_onPlayBlocked;_onDebug;playbackMethod;_volume;_preloadNumTracks;_playbackRate;_mediaSessionAnchor=null;_scheduledNextIndex=null;_throttledUpdatePositionState=O((e,t,r)=>M(e,t,r),1e3);constructor(e={}){let{tracks:t=[],onProgress:r,onEnded:a,onPlayNextTrack:i,onPlayPreviousTrack:d,onStartNewTrack:c,onError:h,onPlayBlocked:A,onDebug:f,playbackMethod:H="HYBRID",trackMetadata:B=[],volume:F=1,preloadNumTracks:$=2,playbackRate:W=1}=e;this._volume=Math.min(1,Math.max(0,F)),this._preloadNumTracks=Math.max(0,$),this._playbackRate=Math.min(4,Math.max(.25,W)),this.playbackMethod=H,this._onProgress=r,this._onEnded=a,this._onPlayNextTrack=i,this._onPlayPreviousTrack=d,this._onStartNewTrack=c,this._onError=h,this._onPlayBlocked=A,this._onDebug=f,this._tracks=t.map((s,n)=>new g({trackUrl:s,index:n,queue:this,metadata:B[n]}));let V=I({currentTrackIndex:0,trackCount:this._tracks.length}).provide({actions:{deactivateCurrent:({context:s})=>{var n;(n=this._trackAt(s.currentTrackIndex))==null||n.deactivate()},deactivateEndedTrack:({context:s})=>{var n;(n=this._trackAt(s.currentTrackIndex))==null||n.deactivate()},activateAndPlayCurrent:({context:s})=>{let n=this._trackAt(s.currentTrackIndex);n&&(n.activate(),this._scheduledNextIndex!==n.index&&n.play())},playOrContinueGapless:({context:s})=>{let n=this._trackAt(s.currentTrackIndex);n&&(this._scheduledNextIndex!==n.index?n.play():(this._scheduledNextIndex=null,this.onDebug(`onTrackEnded: gapless track ${n.index} \u2014 sourceNode=${n.hasSourceNode} isPlaying=${n.isPlaying} machineState=${n.machineState}`),n.startProgressLoop()))},cancelAllGapless:()=>this._cancelScheduledGapless(),notifyStartNewTrack:({context:s})=>{var l;let n=this._trackAt(s.currentTrackIndex);n&&((l=this._onStartNewTrack)==null||l.call(this,n.toInfo()))},notifyPlayNextTrack:({context:s})=>{var l;let n=this._trackAt(s.currentTrackIndex);n&&((l=this._onPlayNextTrack)==null||l.call(this,n.toInfo()))},notifyPlayPreviousTrack:({context:s})=>{var l;let n=this._trackAt(s.currentTrackIndex);n&&((l=this._onPlayPreviousTrack)==null||l.call(this,n.toInfo()))},notifyEnded:()=>{var s;return(s=this._onEnded)==null?void 0:s.call(this)},updateMediaSessionMetadata:({context:s})=>{let n=this._trackAt(s.currentTrackIndex);n&&L(n.metadata)},preloadAhead:({context:s})=>{this._preloadAhead(s.currentTrackIndex)},playCurrent:({context:s})=>{var n;(n=this._trackAt(s.currentTrackIndex))==null||n.play()},pauseCurrent:({context:s})=>{var n;(n=this._trackAt(s.currentTrackIndex))==null||n.pause()},seekCurrent:({context:s,event:n})=>{var m;let l=n;(m=this._trackAt(s.currentTrackIndex))==null||m.seek(l.time)},seekCurrentToStartTime:({context:s,event:n})=>{var m;let l=n;(m=this._trackAt(s.currentTrackIndex))==null||m.seek(l.startTime??0)},scheduleGapless:({context:s})=>{this._tryScheduleGapless(s.currentTrackIndex)},cancelScheduledGapless:()=>{this._cancelScheduledGapless()},cancelAndRescheduleGapless:({context:s})=>{this._cancelScheduledGapless(),this._tryScheduleGapless(s.currentTrackIndex)}}});this._actor=Z(V),this._actor.subscribe(s=>{let n=s.value==="playing";D(n),n?this._startMediaSessionAnchor():this._stopMediaSessionAnchor()}),this._actor.start(),C({onPlay:()=>this.play(),onPause:()=>{this._actor.getSnapshot().value==="playing"&&this.pause()},onNext:()=>this.next(),onPrevious:()=>this.previous(),onSeek:s=>this.seek(s)})}play(){this._currentTrack&&this._actor.send({type:"PLAY"})}pause(){this._actor.send({type:"PAUSE"})}togglePlayPause(){this._actor.getSnapshot().value==="playing"?this.pause():this.play()}next(){this._actor.getSnapshot().context.currentTrackIndex+1>=this._tracks.length||this._actor.send({type:"NEXT"})}previous(){let e=this._currentTrack;if(e&&e.currentTime>8){this._actor.send({type:"SEEK",time:0});return}this._actor.send({type:"PREVIOUS"})}gotoTrack(e,t=!1,r){e<0||e>=this._tracks.length||(this.onDebug(`gotoTrack(${e}, playImmediately=${t}, startTime=${r}) queueState=${this._actor.getSnapshot().value} curIdx=${this._actor.getSnapshot().context.currentTrackIndex}`),this._actor.send({type:"GOTO",index:e,playImmediately:t,startTime:r}))}seek(e){this._actor.send({type:"SEEK",time:e})}setVolume(e){let t=Math.min(1,Math.max(0,e));this._volume=t;for(let r of this._tracks)r.setVolume(t)}setPlaybackRate(e){var a;let t=Math.min(4,Math.max(.25,e));this._playbackRate=t,(a=this._currentTrack)==null||a.setPlaybackRate(t),this._cancelScheduledGapless();let r=this._actor.getSnapshot();r.value==="playing"&&this._tryScheduleGapless(r.context.currentTrackIndex)}addTrack(e,t={}){let r=this._tracks.length,a=t.metadata??{};this._tracks.push(new g({trackUrl:e,index:r,queue:this,skipHEAD:t.skipHEAD,metadata:a})),this._actor.send({type:"ADD_TRACK"})}removeTrack(e){if(!(e<0||e>=this._tracks.length)){this._tracks[e].destroy(),this._tracks.splice(e,1);for(let t=e;t<this._tracks.length;t++)this._tracks[t].index=t;this._scheduledNextIndex===e?this._scheduledNextIndex=null:this._scheduledNextIndex!==null&&this._scheduledNextIndex>e&&this._scheduledNextIndex--,this._actor.send({type:"REMOVE_TRACK",index:e})}}resumeAudioContext(){return y()}destroy(){this._stopMediaSessionAnchor(),this._mediaSessionAnchor=null;for(let e of this._tracks)e.destroy();this._tracks=[],this._actor.stop()}get currentTrack(){var e;return(e=this._currentTrack)==null?void 0:e.toInfo()}get currentTrackIndex(){return this._actor.getSnapshot().context.currentTrackIndex}get tracks(){return this._tracks.map(e=>e.toInfo())}get isPlaying(){return this._actor.getSnapshot().value==="playing"}get isPaused(){return this._actor.getSnapshot().value==="paused"}get volume(){return this._volume}get preloadNumTracks(){return this._preloadNumTracks}set preloadNumTracks(e){this._preloadNumTracks=Math.max(0,e);let t=this._actor.getSnapshot();t.value==="playing"&&this._preloadAhead(t.context.currentTrackIndex)}get playbackRate(){return this._playbackRate}get queueSnapshot(){let e=this._actor.getSnapshot();return{state:e.value,context:e.context}}onTrackEnded(e){let t=this._actor.getSnapshot();if(this.onDebug(`onTrackEnded track=${e.index} queueState=${t.value} curIdx=${t.context.currentTrackIndex}`),e!==this._trackAt(t.context.currentTrackIndex))return;this._actor.send({type:"TRACK_ENDED"});let r=this._actor.getSnapshot();this.onDebug(`onTrackEnded after TRACK_ENDED \u2192 queueState=${r.value} curIdx=${r.context.currentTrackIndex}`)}onTrackBufferReady(e){this._actor.send({type:"TRACK_LOADED",index:e.index})}onProgress(e){var t;e.index===this._actor.getSnapshot().context.currentTrackIndex&&(isNaN(e.duration)||this._throttledUpdatePositionState(e.duration,e.currentTime,this._playbackRate),(t=this._onProgress)==null||t.call(this,e))}onError(e){var t;(t=this._onError)==null||t.call(this,e)}onPlayBlocked(){var e;this._actor.send({type:"PAUSE"}),(e=this._onPlayBlocked)==null||e.call(this)}onPreloadReady(e){let t=this._actor.getSnapshot();e.index===t.context.currentTrackIndex&&this._preloadAhead(t.context.currentTrackIndex)}onDebug(e){var t;(t=this._onDebug)==null||t.call(this,e)}_trackAt(e){return this._tracks[e]}get _currentTrack(){return this._tracks[this._actor.getSnapshot().context.currentTrackIndex]}_preloadAhead(e){let t=this._trackAt(e);if(t!=null&&t.isPlaying&&t.playbackType==="HTML5"&&t.webAudioLoadingState==="LOADING"){this.onDebug(`_preloadAhead(${e}): deferring all \u2014 current track buffer still loading`);return}let r=t!=null&&t.isPlaying&&(()=>{let i=isNaN(t.duration)?15:Math.min(t.duration*.2,15);return t.currentTime<i})(),a=e+this._preloadNumTracks+1;this.onDebug(`_preloadAhead(${e}) limit=${a} trackCount=${this._tracks.length} belowThreshold=${r}`);for(let i=e+1;i<this._tracks.length&&i<a;i++){let d=this._tracks[i];if(i>e+1&&r){this.onDebug(`_preloadAhead: deferring track ${i} (current at ${t.currentTime.toFixed(1)}s, below threshold)`);return}if(d.isBufferLoaded)this.onDebug(`_preloadAhead: track ${i} already loaded`);else{this.onDebug(`_preloadAhead: starting preload for track ${i}`),d.preload();return}}}_cancelScheduledGapless(){if(this._scheduledNextIndex===null)return;let e=this._trackAt(this._scheduledNextIndex);e&&(e.cancelGaplessStart(),this.onDebug(`_cancelScheduledGapless: cancelled track ${this._scheduledNextIndex}`));let t=this._trackAt(this._actor.getSnapshot().context.currentTrackIndex);t&&t.playbackType==="HTML5"&&t.cancelHtml5Mute(),this._scheduledNextIndex=null}_tryScheduleGapless(e){let t=T();if(!t||this.playbackMethod==="HTML5_ONLY")return;let r=e+1;if(r>=this._tracks.length)return;let a=this._tracks[e],i=this._tracks[r];if(!a.isBufferLoaded||!i.isBufferLoaded||this._scheduledNextIndex===r||!a.isPlaying)return;let d=this._computeTrackEndTime(a);if(d===null||d<t.currentTime+.01)return;let c=d-t.currentTime;if(a.playbackType==="HTML5"&&c>G){this.onDebug(`_tryScheduleGapless: deferring \u2014 HTML5 track ${e} has ${c.toFixed(1)}s remaining (max lookahead=${G}s)`);return}if(!i.scheduleGaplessStart(d)){this.onDebug(`_tryScheduleGapless: track ${r} rejected SCHEDULE_GAPLESS (state=${i.machineState})`);return}a.playbackType==="HTML5"&&a.scheduleHtml5Mute(d),this.onDebug(`_tryScheduleGapless: scheduled track ${r} at endTime=${d.toFixed(3)} (in ${(d-t.currentTime).toFixed(1)}s) curPlaybackType=${a.playbackType}`),this._scheduledNextIndex=r}_computeTrackEndTime(e){let t=T();if(!t||!e.isBufferLoaded)return null;let r=e.playbackEndContextTime;if(r!==null)return r>t.currentTime?r:null;let a=e.duration;if(isNaN(a))return null;let i=(a-e.currentTime)/this._playbackRate;return i<=0?null:t.currentTime+i}_startMediaSessionAnchor(){typeof Audio>"u"||(this._mediaSessionAnchor||(this._mediaSessionAnchor=new Audio(ee()),this._mediaSessionAnchor.loop=!0),this._mediaSessionAnchor.paused&&this._mediaSessionAnchor.play().catch(()=>{}))}_stopMediaSessionAnchor(){this._mediaSessionAnchor&&!this._mediaSessionAnchor.paused&&this._mediaSessionAnchor.pause()}};export{P as Queue,P as default};
|
|
1
|
+
import{createActor as Z}from"xstate";var q=typeof window<"u",T;function K(){if(!q)return null;let o=window.AudioContext??window.webkitAudioContext;return o?new o:null}function k(){return T===void 0?null:T}function y(){T===void 0&&(T=K());let o=T;return o&&o.state==="suspended"?o.resume():Promise.resolve()}var _=typeof navigator<"u"&&"mediaSession"in navigator;function C(o){if(!_)return;let{mediaSession:e}=navigator;e.setActionHandler("play",o.onPlay),e.setActionHandler("pause",o.onPause),e.setActionHandler("nexttrack",o.onNext),e.setActionHandler("previoustrack",o.onPrevious),e.setActionHandler("seekto",t=>{t.seekTime!=null&&o.onSeek(t.seekTime)})}function L(o){if(_){if(!o){navigator.mediaSession.metadata=null;return}navigator.mediaSession.metadata=new MediaMetadata({title:o.title??"",artist:o.artist??"",album:o.album??"",artwork:o.artwork??[]})}}function D(o){_&&(navigator.mediaSession.playbackState=o?"playing":"paused")}function M(o,e,t=1){if(_)try{navigator.mediaSession.setPositionState({duration:o,position:e,playbackRate:t})}catch{}}import{setup as Y,assign as p}from"xstate";function I(o){return Y({types:{context:{},events:{}},guards:{hasNextTrack:({context:e})=>e.currentTrackIndex+1<e.trackCount,playImmediately:({event:e})=>!!e.playImmediately,willBeEmpty:({context:e})=>e.trackCount<=1,isRemovingCurrent:({context:e,event:t})=>t.index===e.currentTrackIndex},actions:{incrementTrackCount:p({trackCount:({context:e})=>e.trackCount+1}),decrementTrackCount:p({trackCount:({context:e})=>Math.max(0,e.trackCount-1),currentTrackIndex:({context:e,event:t})=>{let i=t,a=Math.max(0,e.trackCount-1);return i.index<e.currentTrackIndex?Math.max(0,e.currentTrackIndex-1):a>0?Math.min(e.currentTrackIndex,a-1):0}}),gotoTrackIndex:p({currentTrackIndex:({event:e})=>e.index}),advanceToNextTrack:p({currentTrackIndex:({context:e})=>{let t=e.currentTrackIndex+1;return t<e.trackCount?t:e.currentTrackIndex}}),goToPreviousTrack:p({currentTrackIndex:({context:e})=>Math.max(0,e.currentTrackIndex-1)}),advanceOnTrackEnd:p({currentTrackIndex:({context:e})=>Math.min(e.currentTrackIndex+1,e.trackCount-1)}),resetToFirstTrack:p({currentTrackIndex:()=>0}),deactivateCurrent:()=>{},deactivateEndedTrack:()=>{},activateAndPlayCurrent:()=>{},playOrContinueGapless:()=>{},cancelAllGapless:()=>{},notifyStartNewTrack:()=>{},notifyPlayNextTrack:()=>{},notifyPlayPreviousTrack:()=>{},notifyEnded:()=>{},updateMediaSessionMetadata:()=>{},preloadAhead:()=>{},playCurrent:()=>{},pauseCurrent:()=>{},seekCurrent:()=>{},seekCurrentToStartTime:()=>{},scheduleGapless:()=>{},cancelScheduledGapless:()=>{},cancelAndRescheduleGapless:()=>{}}}).createMachine({id:"queue",initial:"idle",context:o,on:{ADD_TRACK:{actions:"incrementTrackCount"},REMOVE_TRACK:{actions:"decrementTrackCount"}},states:{idle:{on:{PLAY:{target:"playing",actions:["playCurrent","updateMediaSessionMetadata","preloadAhead","scheduleGapless"]},GOTO:[{guard:"playImmediately",target:"playing",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","activateAndPlayCurrent","notifyStartNewTrack","updateMediaSessionMetadata","preloadAhead"]},{target:"paused",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","preloadAhead"]}],TRACK_LOADED:{actions:["preloadAhead"]}}},playing:{on:{REMOVE_TRACK:[{guard:"willBeEmpty",target:"idle",actions:["cancelAllGapless","decrementTrackCount"]},{guard:"isRemovingCurrent",actions:["cancelAllGapless","decrementTrackCount","activateAndPlayCurrent","notifyStartNewTrack","updateMediaSessionMetadata","preloadAhead"]},{actions:["decrementTrackCount","cancelAndRescheduleGapless","preloadAhead"]}],PAUSE:{target:"paused",actions:["cancelScheduledGapless","pauseCurrent"]},TOGGLE:{target:"paused",actions:["cancelScheduledGapless","pauseCurrent"]},NEXT:{actions:["deactivateCurrent","cancelAllGapless","advanceToNextTrack","activateAndPlayCurrent","notifyStartNewTrack","notifyPlayNextTrack","updateMediaSessionMetadata","preloadAhead"]},PREVIOUS:{actions:["deactivateCurrent","cancelAllGapless","goToPreviousTrack","activateAndPlayCurrent","notifyStartNewTrack","notifyPlayPreviousTrack","updateMediaSessionMetadata","preloadAhead"]},GOTO:[{guard:"playImmediately",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","activateAndPlayCurrent","notifyStartNewTrack","updateMediaSessionMetadata","preloadAhead"]},{actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","preloadAhead"]}],SEEK:{actions:["seekCurrent","cancelAndRescheduleGapless"]},TRACK_ENDED:[{guard:"hasNextTrack",target:"playing",actions:["deactivateEndedTrack","advanceOnTrackEnd","playOrContinueGapless","notifyStartNewTrack","notifyPlayNextTrack","updateMediaSessionMetadata","preloadAhead"]},{target:"ended",actions:["deactivateEndedTrack","notifyEnded"]}],TRACK_LOADED:[{guard:({context:e,event:t})=>t.index===e.currentTrackIndex,actions:["cancelAndRescheduleGapless","preloadAhead"]},{actions:["scheduleGapless","preloadAhead"]}]}},paused:{on:{REMOVE_TRACK:[{guard:"willBeEmpty",target:"idle",actions:["cancelAllGapless","decrementTrackCount"]},{guard:"isRemovingCurrent",actions:["cancelAllGapless","decrementTrackCount","preloadAhead"]},{actions:["decrementTrackCount","preloadAhead"]}],PLAY:{target:"playing",actions:["playCurrent","updateMediaSessionMetadata","preloadAhead","scheduleGapless"]},TOGGLE:{target:"playing",actions:["playCurrent","updateMediaSessionMetadata","preloadAhead","scheduleGapless"]},NEXT:{actions:["deactivateCurrent","cancelAllGapless","advanceToNextTrack","activateAndPlayCurrent","notifyStartNewTrack","notifyPlayNextTrack","updateMediaSessionMetadata","preloadAhead"]},PREVIOUS:{actions:["deactivateCurrent","cancelAllGapless","goToPreviousTrack","activateAndPlayCurrent","notifyStartNewTrack","notifyPlayPreviousTrack","updateMediaSessionMetadata","preloadAhead"]},GOTO:[{guard:"playImmediately",target:"playing",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","activateAndPlayCurrent","notifyStartNewTrack","updateMediaSessionMetadata","preloadAhead"]},{actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","preloadAhead"]}],SEEK:{actions:["seekCurrent"]},TRACK_ENDED:[{guard:"hasNextTrack",target:"paused",actions:["deactivateEndedTrack","advanceOnTrackEnd","notifyStartNewTrack","updateMediaSessionMetadata"]},{target:"ended",actions:["deactivateEndedTrack","notifyEnded"]}],TRACK_LOADED:{actions:["preloadAhead"]}}},ended:{on:{ADD_TRACK:{target:"paused",actions:"incrementTrackCount"},PLAY:{target:"playing",actions:["resetToFirstTrack","playCurrent","updateMediaSessionMetadata","preloadAhead","scheduleGapless"]},GOTO:[{guard:"playImmediately",target:"playing",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","activateAndPlayCurrent","notifyStartNewTrack","updateMediaSessionMetadata","preloadAhead"]},{target:"paused",actions:["deactivateCurrent","cancelAllGapless","gotoTrackIndex","seekCurrentToStartTime","preloadAhead"]}]}}}})}import{createActor as j,fromPromise as R}from"xstate";import{setup as X,assign as u,spawnChild as z}from"xstate";import{setup as Q,assign as b,sendParent as S,fromPromise as N}from"xstate";var x=Q({types:{context:{},input:{}},actors:{resolveUrl:N(async()=>null),fetchAudio:N(async()=>{}),decodeAudio:N(async()=>{})},guards:{shouldSkipHEAD:({context:o})=>o.skipHEAD}}).createMachine({id:"fetchDecode",initial:"resolvingUrl",context:{trackUrl:"",resolvedUrl:"",skipHEAD:!1},entry:b(({event:o})=>{let{input:e}=o;return{trackUrl:e.trackUrl,resolvedUrl:e.resolvedUrl,skipHEAD:e.skipHEAD}}),states:{resolvingUrl:{always:{guard:"shouldSkipHEAD",target:"fetching"},invoke:{id:"resolveUrl",src:"resolveUrl",input:({context:o})=>({trackUrl:o.trackUrl}),onDone:{target:"fetching",actions:[b({resolvedUrl:({event:o,context:e})=>o.output??e.resolvedUrl,skipHEAD:()=>!0}),S(({event:o,context:e})=>({type:"URL_RESOLVED",url:o.output??e.resolvedUrl}))]},onError:{target:"fetching",actions:b({skipHEAD:()=>!0})}}},fetching:{invoke:{id:"fetchAudio",src:"fetchAudio",input:({context:o})=>({resolvedUrl:o.resolvedUrl}),onDone:"decoding",onError:{target:"error",actions:S({type:"BUFFER_ERROR"})}}},decoding:{invoke:{id:"decodeAudio",src:"decodeAudio",input:()=>{},onDone:{target:"done",actions:S({type:"BUFFER_READY"})},onError:{target:"error",actions:S({type:"BUFFER_ERROR"})}}},done:{type:"final"},error:{type:"final"}}});function U(o){return X({types:{context:{},events:{}},actors:{fetchDecode:x},guards:{canPlayWebAudio:()=>!1,isWebAudioOnly:()=>!1,canStartFetch:({context:e})=>e.webAudioLoadingState==="NONE"&&!e.fetchStarted},actions:{playHtml5:()=>{},startSourceNode:()=>{},crossoverHtml5ToWebAudio:()=>{},notifyBufferReady:()=>{},startScheduledSourceNode:()=>{},startProgressLoop:()=>{},pauseHtml5:()=>{},freezePausedTime:()=>{},stopSourceNode:()=>{},stopProgressLoop:()=>{},reportProgress:()=>{},seekHtml5:()=>{},seekWebAudio:()=>{},resetHtml5Element:()=>{},resetTiming:()=>{},notifyTrackEnded:()=>{},triggerFetchForPendingPlay:()=>{},setPendingPlay:u({pendingPlay:()=>!0}),clearPendingPlay:u({pendingPlay:()=>!1}),setIsPlaying:u({isPlaying:()=>!0}),clearIsPlaying:u({isPlaying:()=>!1}),setLoadingState:u({webAudioLoadingState:()=>"LOADING"}),setLoadedState:u({webAudioLoadingState:()=>"LOADED"}),setErrorState:u({webAudioLoadingState:()=>"ERROR"}),clearScheduleAndLookahead:u({scheduledStartContextTime:()=>null,notifiedLookahead:()=>!1}),setPlayingWebAudio:u({isPlaying:()=>!0,webAudioLoadingState:()=>"LOADED",playbackType:()=>"WEBAUDIO"}),setScheduledGapless:u({isPlaying:()=>!0,webAudioLoadingState:()=>"LOADED",playbackType:()=>"WEBAUDIO",scheduledStartContextTime:({event:e})=>e.when}),clearPlayingAndSchedule:u({isPlaying:()=>!1,scheduledStartContextTime:()=>null,notifiedLookahead:()=>!1}),setNotifiedLookahead:u({notifiedLookahead:()=>!0}),setResolvedUrl:u({resolvedUrl:({event:e})=>e.url}),clearScheduledStart:u({scheduledStartContextTime:()=>null}),setPlayingWebAudioType:u({isPlaying:()=>!0,playbackType:()=>"WEBAUDIO"}),setPlaybackTypeWebAudio:u({playbackType:()=>"WEBAUDIO"}),clearNotifiedLookahead:u({notifiedLookahead:()=>!1})}}).createMachine({id:"track",initial:"idle",context:o,on:{START_FETCH:{guard:"canStartFetch",actions:[u({webAudioLoadingState:()=>"LOADING",fetchStarted:()=>!0}),z("fetchDecode",{id:"fetchDecode",input:({context:e})=>({trackUrl:e.trackUrl,resolvedUrl:e.resolvedUrl,skipHEAD:e.skipHEAD})})]}},states:{idle:{on:{HTML5_ENDED:{actions:["notifyTrackEnded"]},DEACTIVATE:{actions:["pauseHtml5","resetHtml5Element","resetTiming","stopProgressLoop","clearScheduleAndLookahead","clearPendingPlay"]},ACTIVATE:{actions:["resetTiming","resetHtml5Element","clearScheduleAndLookahead","clearPendingPlay"]},PLAY:[{guard:"canPlayWebAudio",target:"webaudio",actions:["setPlayingWebAudio","startSourceNode","startProgressLoop"]},{guard:"isWebAudioOnly",actions:["setPendingPlay","triggerFetchForPendingPlay"]},{target:"html5",actions:["setIsPlaying","playHtml5","startProgressLoop","triggerFetchForPendingPlay"]}],PLAY_WEBAUDIO:{target:"webaudio",actions:["setPlayingWebAudio","startSourceNode","startProgressLoop"]},SCHEDULE_GAPLESS:{target:"webaudio",actions:["setScheduledGapless","startScheduledSourceNode"]},PRELOAD:{target:"loading"},BUFFER_LOADING:{actions:"setLoadingState"},BUFFER_READY:[{guard:({context:e})=>e.pendingPlay,target:"webaudio",actions:["clearPendingPlay","setPlayingWebAudio","startSourceNode","startProgressLoop","notifyBufferReady"]},{actions:["setLoadedState","notifyBufferReady"]}],BUFFER_ERROR:{actions:["setErrorState","clearPendingPlay"]},URL_RESOLVED:{actions:"setResolvedUrl"}}},html5:{on:{PAUSE:{actions:["clearIsPlaying","pauseHtml5","stopProgressLoop","reportProgress"]},PLAY:{actions:["setIsPlaying","playHtml5","startProgressLoop"]},BUFFER_LOADING:{actions:"setLoadingState"},PLAY_WEBAUDIO:{target:"webaudio",actions:"setPlayingWebAudio"},BUFFER_READY:{target:"webaudio",actions:["setLoadedState","crossoverHtml5ToWebAudio","setPlaybackTypeWebAudio","clearNotifiedLookahead","notifyBufferReady"]},BUFFER_ERROR:{actions:"setErrorState"},SEEK:{actions:["seekHtml5","reportProgress"]},LOOKAHEAD_REACHED:{actions:"setNotifiedLookahead"},HTML5_ENDED:{target:"idle",actions:["clearIsPlaying","stopProgressLoop","notifyTrackEnded"]},ACTIVATE:{target:"idle",actions:["clearPlayingAndSchedule","pauseHtml5","stopProgressLoop","resetTiming","resetHtml5Element"]},URL_RESOLVED:{actions:"setResolvedUrl"},DEACTIVATE:{target:"idle",actions:["clearIsPlaying","pauseHtml5","resetHtml5Element","resetTiming","stopProgressLoop"]}}},loading:{on:{BUFFER_LOADING:{actions:"setLoadingState"},BUFFER_READY:[{guard:({context:e})=>e.pendingPlay,target:"webaudio",actions:["clearPendingPlay","setPlayingWebAudio","startSourceNode","startProgressLoop","notifyBufferReady"]},{target:"idle",actions:["setLoadedState","notifyBufferReady"]}],BUFFER_ERROR:{target:"idle",actions:["setErrorState","clearPendingPlay"]},PLAY:[{guard:"canPlayWebAudio",target:"webaudio",actions:["setPlayingWebAudio","startSourceNode","startProgressLoop"]},{guard:"isWebAudioOnly",actions:["setPendingPlay","triggerFetchForPendingPlay"]},{target:"html5",actions:["setIsPlaying","playHtml5","startProgressLoop","triggerFetchForPendingPlay"]}],PLAY_WEBAUDIO:{target:"webaudio",actions:["setPlayingWebAudio","startSourceNode","startProgressLoop"]},SCHEDULE_GAPLESS:{target:"webaudio",actions:["setScheduledGapless","startScheduledSourceNode"]},ACTIVATE:{target:"idle",actions:["clearPlayingAndSchedule","clearPendingPlay","resetTiming","resetHtml5Element"]},DEACTIVATE:{target:"idle",actions:["clearIsPlaying","clearPendingPlay","resetTiming"]},URL_RESOLVED:{actions:"setResolvedUrl"}}},webaudio:{on:{PAUSE:{actions:["clearIsPlaying","freezePausedTime","clearScheduledStart","stopSourceNode","stopProgressLoop","reportProgress"]},PLAY:[{guard:"canPlayWebAudio",actions:["setIsPlaying","startSourceNode","startProgressLoop"]},{actions:"setIsPlaying"}],PLAY_WEBAUDIO:{actions:"setPlayingWebAudioType"},SEEK:{actions:["clearScheduledStart","seekWebAudio","reportProgress"]},SET_VOLUME:{},CANCEL_GAPLESS:{target:"idle",actions:["clearPlayingAndSchedule","stopSourceNode","stopProgressLoop","resetTiming"]},LOOKAHEAD_REACHED:{actions:"setNotifiedLookahead"},WEBAUDIO_ENDED:{target:"idle",actions:["clearIsPlaying","stopProgressLoop","notifyTrackEnded"]},ACTIVATE:{target:"idle",actions:["clearPlayingAndSchedule","stopSourceNode","stopProgressLoop","resetTiming","resetHtml5Element"]},DEACTIVATE:{target:"idle",actions:["clearPlayingAndSchedule","stopSourceNode","resetTiming","resetHtml5Element","stopProgressLoop"]}}}}})}var J=5,w=15,E=.03,g=class{index;metadata;_trackUrl;_resolvedUrl;skipHEAD;_pendingArrayBuffer=null;audio;_playbackMethod;get ctx(){if(this._playbackMethod==="HTML5_ONLY")return null;let e=k();if(e&&!this.gainNode){this.gainNode=e.createGain(),this.gainNode.gain.value=this.audio.volume,this.gainNode.connect(e.destination);try{this._mediaElementSource=e.createMediaElementSource(this.audio),this._html5GainNode=e.createGain(),this._html5GainNode.gain.value=1,this._mediaElementSource.connect(this._html5GainNode),this._html5GainNode.connect(this.gainNode),this.audio.volume=1}catch{this._mediaElementSource=null,this._html5GainNode=null}}return e}gainNode=null;_mediaElementSource=null;_html5GainNode=null;sourceNode=null;audioBuffer=null;_bufferStartPaddingSec=0;_bufferAlignmentMeasured=!1;_waRefCtxTime=0;_waRefTrackTime=0;pausedAtTrackTime=0;_actor;queueRef;rafId=null;_notifiedPreloadThreshold=!1;constructor(e){this.index=e.index,this._trackUrl=e.trackUrl,this._resolvedUrl=e.trackUrl,this.skipHEAD=e.skipHEAD??!1,this.metadata=e.metadata??{},this.queueRef=e.queue,this.audio=new Audio,this.audio.preload="none",e.queue.playbackMethod!=="HTML5_ONLY"&&(this.audio.crossOrigin="anonymous"),this.audio.src=this._trackUrl,this.audio.volume=e.queue.volume,this.audio.controls=!1,this.audio.onerror=()=>{var d,c;let a=(d=this.audio.error)==null?void 0:d.code;if(a===1)return;let r=((c=this.audio.error)==null?void 0:c.message)??"unknown";this.queueRef.onError(new Error(`HTML5 audio error on track ${this.index} (code ${a}): ${r}`))},this.audio.onended=()=>{this.queueRef.onDebug(`audio.onended track=${this.index} machineState=${this._actor.getSnapshot().value} queueIdx=${this.queueRef.currentTrackIndex}`),this._actor.send({type:"HTML5_ENDED"})},this._playbackMethod=e.queue.playbackMethod;let t={trackUrl:this._trackUrl,resolvedUrl:this._trackUrl,skipHEAD:this.skipHEAD,playbackType:"HTML5",webAudioLoadingState:"NONE",isPlaying:!1,scheduledStartContextTime:null,notifiedLookahead:!1,fetchStarted:!1,pendingPlay:!1},i=U(t).provide({guards:{canPlayWebAudio:()=>!!(this.ctx&&this.audioBuffer&&this.gainNode),isWebAudioOnly:()=>this._playbackMethod==="WEBAUDIO_ONLY"},actors:{fetchDecode:x.provide({actors:{resolveUrl:R(async({signal:a})=>{let r=await fetch(this._trackUrl,{method:"HEAD",signal:a});return r.redirected&&r.url?(this._resolvedUrl=r.url,r.url):null}),fetchAudio:R(async({input:a,signal:r})=>{let{resolvedUrl:d}=a,c=await fetch(d,{signal:r});if(!c.ok)throw new Error(`HTTP ${c.status} for ${d}`);this._pendingArrayBuffer=await c.arrayBuffer()}),decodeAudio:R(async()=>{let a=this._pendingArrayBuffer;if(this._pendingArrayBuffer=null,!a||!this.ctx)throw new Error("No ArrayBuffer or AudioContext");this.audioBuffer=await this.ctx.decodeAudioData(a),this._maybeComputeBufferAlignment()})}})},actions:{triggerFetchForPendingPlay:()=>{this.preload(),y()},playHtml5:()=>this._playHtml5(),startSourceNode:()=>{this._startSourceNode(this.pausedAtTrackTime)},crossoverHtml5ToWebAudio:({context:a})=>{this._crossoverHtml5ToWebAudio(a.isPlaying)},startScheduledSourceNode:({context:a})=>{let r=a.scheduledStartContextTime;if(r===null||!this.ctx||!this.audioBuffer||!this.gainNode)return;this._maybeComputeBufferAlignment(),this._stopSourceNode(),this.sourceNode=this.ctx.createBufferSource(),this.sourceNode.buffer=this.audioBuffer,this.sourceNode.playbackRate.value=this.queueRef.playbackRate,this.sourceNode.connect(this.gainNode),this.sourceNode.onended=this._handleWebAudioEnded,this.sourceNode.start(r,this._bufferStartPaddingSec);let d=this.audioBuffer.duration-this._bufferStartPaddingSec,c=this.sourceNode.playbackRate.value||1;d>0&&this.sourceNode.stop(r+d/c),this._waRefCtxTime=r,this._waRefTrackTime=0,this.queueRef.onDebug(`startScheduledSourceNode track=${this.index} when=${r.toFixed(3)} ctxNow=${this.ctx.currentTime.toFixed(3)} delta=${(r-this.ctx.currentTime).toFixed(3)}s padding=${this._bufferStartPaddingSec.toFixed(3)}s`)},startProgressLoop:()=>this.startProgressLoop(),pauseHtml5:()=>this.audio.pause(),freezePausedTime:()=>{let a=this.currentTime;this.pausedAtTrackTime=isFinite(a)?a:0},stopSourceNode:()=>this._stopSourceNode(),stopProgressLoop:()=>this._stopProgressLoop(),reportProgress:()=>{queueMicrotask(()=>this.queueRef.onProgress(this.toInfo()))},seekHtml5:()=>this._seekHtml5(),seekWebAudio:()=>this._seekWebAudio(),resetHtml5Element:()=>{this.audio.currentTime=0},resetTiming:()=>{this._waRefCtxTime=0,this._waRefTrackTime=0,this.pausedAtTrackTime=0},notifyTrackEnded:()=>{queueMicrotask(()=>this.queueRef.onTrackEnded(this))},notifyBufferReady:()=>{queueMicrotask(()=>this.queueRef.onTrackBufferReady(this))}}});this._actor=j(i),this._actor.start()}play(){this.queueRef.onDebug(`Track.play() track=${this.index} machineState=${this._actor.getSnapshot().value} hasBuffer=${!!this.audioBuffer} hasCtx=${!!this.ctx} audioPaused=${this.audio.paused}`),this._actor.send({type:"PLAY"})}pause(){this._actor.send({type:"PAUSE"})}seek(e){if(!isFinite(e))return;let t=Math.max(0,isNaN(this.duration)?e:Math.min(e,this.duration));this.pausedAtTrackTime=t,this._actor.send({type:"SEEK",time:t})}setVolume(e){let t=Math.min(1,Math.max(0,e));this._mediaElementSource?this.audio.volume=1:this.audio.volume=t,this.gainNode&&(this.gainNode.gain.value=t),this._actor.send({type:"SET_VOLUME",volume:t})}setPlaybackRate(e){if(this.ctx&&this.sourceNode&&this._actor.getSnapshot().context.isPlaying&&this.ctx.currentTime>=this._waRefCtxTime){let t=this.sourceNode.playbackRate.value;this._waRefTrackTime=this._waRefTrackTime+(this.ctx.currentTime-this._waRefCtxTime)*t,this._waRefCtxTime=this.ctx.currentTime}if(this.audio.playbackRate=e,this.sourceNode&&(this.sourceNode.playbackRate.value=e,this.audioBuffer&&this.ctx)){let t=this.audioBuffer.duration-this._bufferStartPaddingSec-this._waRefTrackTime;if(t>0)try{this.sourceNode.stop(this._waRefCtxTime+t/e)}catch{}}}preload(){this.queueRef.onDebug(`preload() track=${this.index} state=${this._actor.getSnapshot().value} hasBuffer=${!!this.audioBuffer} hasCtx=${!!this.ctx}`),this._actor.getSnapshot().value==="idle"&&this._actor.send({type:"PRELOAD"}),!this.audioBuffer&&(y(),this.ctx&&this._actor.send({type:"START_FETCH"}))}seekToEnd(e=6){let t=this.duration;!isNaN(t)&&t>e&&this.seek(t-e)}activate(){this._actor.send({type:"ACTIVATE"})}deactivate(){this.queueRef.onDebug(`Track.deactivate() track=${this.index} machineState=${this._actor.getSnapshot().value} isPlaying=${this.isPlaying}`),this._notifiedPreloadThreshold=!1,this._actor.send({type:"DEACTIVATE"}),this.queueRef.onDebug(`Track.deactivate() done track=${this.index} machineState=${this._actor.getSnapshot().value}`)}destroy(){var e;this.deactivate(),this._pendingArrayBuffer=null,this.audioBuffer=null,(e=this.gainNode)==null||e.disconnect(),this.gainNode=null,this._actor.stop()}scheduleHtml5Mute(e){!this._html5GainNode||!this.ctx||(this._html5GainNode.gain.setValueAtTime(1,e-.005),this._html5GainNode.gain.linearRampToValueAtTime(0,e))}cancelHtml5Mute(){!this._html5GainNode||!this.ctx||(this._html5GainNode.gain.cancelScheduledValues(this.ctx.currentTime),this._html5GainNode.gain.setValueAtTime(1,this.ctx.currentTime))}cancelGaplessStart(){this._actor.getSnapshot().context.scheduledStartContextTime!==null&&this._actor.send({type:"CANCEL_GAPLESS"})}scheduleGaplessStart(e){if(!this.ctx||!this.audioBuffer||!this.gainNode)return!1;let t=this._actor.getSnapshot().value;return t!=="idle"&&t!=="loading"?!1:(this._actor.send({type:"SCHEDULE_GAPLESS",when:e}),!0)}get currentTime(){let e=this._actor.getSnapshot();return e.value==="webaudio"?e.context.isPlaying?this.ctx?Math.max(0,this._waRefTrackTime+(this.ctx.currentTime-this._waRefCtxTime)*this.queueRef.playbackRate):0:this.pausedAtTrackTime:this.audio.currentTime}get duration(){return this._actor.getSnapshot().value==="html5"&&!isNaN(this.audio.duration)?this.audio.duration:this.audioBuffer?this.audioBuffer.duration:this.audio.duration}get isPaused(){let e=this._actor.getSnapshot();return e.value==="webaudio"?!e.context.isPlaying:this.audio.paused}get isPlaying(){return this._actor.getSnapshot().context.isPlaying}get trackUrl(){return this._resolvedUrl}get playbackType(){return this._actor.getSnapshot().context.playbackType}get webAudioLoadingState(){return this._actor.getSnapshot().context.webAudioLoadingState}get hasSourceNode(){return this.sourceNode!==null}get machineState(){return this._actor.getSnapshot().value}get scheduledStartContextTime(){return this._actor.getSnapshot().context.scheduledStartContextTime}get playbackEndContextTime(){let e=this._actor.getSnapshot();if(e.value!=="webaudio"||!e.context.isPlaying||!this.sourceNode||!this.audioBuffer)return null;let t=this.sourceNode.playbackRate.value||1,i=this.audioBuffer.duration-this._bufferStartPaddingSec-this._waRefTrackTime;return this._waRefCtxTime+i/t}get isBufferLoaded(){return this.audioBuffer!==null}toInfo(){var e;return{index:this.index,currentTime:this.currentTime,duration:this.duration,isPlaying:this.isPlaying,isPaused:this.isPaused,volume:((e=this.gainNode)==null?void 0:e.gain.value)??this.audio.volume,trackUrl:this.trackUrl,playbackType:this.playbackType,webAudioLoadingState:this.webAudioLoadingState,metadata:this.metadata,playbackRate:this.queueRef.playbackRate,machineState:this.machineState}}_playHtml5(){this.audio.preload!=="auto"&&(this.audio.preload="auto"),this.audio.playbackRate=this.queueRef.playbackRate;let e=this.audio.play();e&&e.catch(t=>{t instanceof Error&&t.name==="NotAllowedError"?this.queueRef.onPlayBlocked():t instanceof Error&&t.name==="AbortError"||this.queueRef.onError(t instanceof Error?t:new Error(String(t)))})}_seekHtml5(){let e=this.pausedAtTrackTime;isFinite(e)&&(this.audio.preload!=="auto"&&(this.audio.preload="auto"),this.audio.readyState>=HTMLMediaElement.HAVE_METADATA?this.audio.currentTime=e:(this.audio.addEventListener("loadedmetadata",()=>{this.audio.currentTime=e},{once:!0}),this.audio.load()))}_crossoverHtml5ToWebAudio(e){if(!this.ctx||!this.audioBuffer||!this.gainNode)return;let t=this.audio.currentTime,i=isFinite(this.pausedAtTrackTime)&&this.pausedAtTrackTime>t?this.pausedAtTrackTime:t;if(this.pausedAtTrackTime=isFinite(i)?i:0,!e){this.audio.pause(),this.queueRef.onDebug(`crossoverHtml5ToWebAudio track=${this.index} offset=${this.pausedAtTrackTime.toFixed(3)} wasPlaying=false`);return}let a=this._startSourceNode(this.pausedAtTrackTime,E);if(a!==null&&this._html5GainNode){this._html5GainNode.gain.cancelScheduledValues(a),this._html5GainNode.gain.setValueAtTime(1,a),this._html5GainNode.gain.linearRampToValueAtTime(0,a+E);let r=this.ctx,d=this._html5GainNode,c=(a-this.ctx.currentTime+E)*1e3+5;setTimeout(()=>{this.audio.pause(),r&&d&&(d.gain.cancelScheduledValues(r.currentTime),d.gain.setValueAtTime(1,r.currentTime))},c)}else if(!this._html5GainNode)if(a!==null&&this.ctx){let r=(a-this.ctx.currentTime)*1e3;setTimeout(()=>{this.audio.volume=0,this.audio.pause(),this.audio.volume=1},Math.max(0,r))}else this.audio.volume=0,this.audio.pause(),this.audio.volume=1;this.queueRef.onDebug(`crossoverHtml5ToWebAudio track=${this.index} offset=${this.pausedAtTrackTime.toFixed(3)} wasPlaying=true fade=${E}s mediaSource=${!!this._html5GainNode} when=${(a==null?void 0:a.toFixed(3))??"null"}`)}_maybeComputeBufferAlignment(){this.audioBuffer&&(this._bufferAlignmentMeasured||(this._bufferStartPaddingSec=0,this._bufferAlignmentMeasured=!0,this.queueRef.onDebug(`_maybeComputeBufferAlignment track=${this.index} bufferDur=${this.audioBuffer.duration.toFixed(3)}s html5Dur=${isNaN(this.audio.duration)?"NaN":this.audio.duration.toFixed(3)+"s"} (alignment shift disabled \u2014 see comment)`)))}_startSourceNode(e,t=0){if(!this.ctx||!this.audioBuffer||!this.gainNode)return null;this._maybeComputeBufferAlignment(),this._stopSourceNode();let i=this.ctx.state==="suspended";i&&this.ctx.resume(),this.sourceNode=this.ctx.createBufferSource(),this.sourceNode.buffer=this.audioBuffer,this.sourceNode.playbackRate.value=this.queueRef.playbackRate;let a=i?.15:Math.max(.02,2*(this.ctx.baseLatency||0)+.01),r=this.ctx.currentTime+a,d=this.queueRef.playbackRate,c=this.audioBuffer.duration-this._bufferStartPaddingSec,h=Math.min(e+a*d,c);if(t>0){let f=this.ctx.createGain();f.gain.setValueAtTime(0,r),f.gain.linearRampToValueAtTime(1,r+t),this.sourceNode.connect(f),f.connect(this.gainNode)}else this.sourceNode.connect(this.gainNode);this.sourceNode.onended=this._handleWebAudioEnded,this._waRefCtxTime=r,this._waRefTrackTime=h,this.sourceNode.start(r,h+this._bufferStartPaddingSec);let A=this.audioBuffer.duration-this._bufferStartPaddingSec-h;return A>0&&this.sourceNode.stop(r+A/d),r}_stopSourceNode(){if(this.sourceNode){this.sourceNode.onended=null;try{this.sourceNode.stop()}catch{}try{this.sourceNode.disconnect()}catch{}this.sourceNode=null}}_seekWebAudio(){let t=this._actor.getSnapshot().context.isPlaying,i=this.pausedAtTrackTime;this._stopSourceNode(),t&&this._startSourceNode(i)}_handleWebAudioEnded=()=>{this.queueRef.onDebug(`_handleWebAudioEnded track=${this.index} sourceNode=${!!this.sourceNode} queueIdx=${this.queueRef.currentTrackIndex}`),this.sourceNode&&this._actor.send({type:"WEBAUDIO_ENDED"})};startProgressLoop(){if(this.rafId!==null)return;let e=()=>{if(this.isPaused||!this.isPlaying){this.rafId=null;return}this.queueRef.onProgress(this.toInfo());let t=this.duration-this.currentTime;!this._actor.getSnapshot().context.notifiedLookahead&&!isNaN(t)&&t<=J&&(this._actor.send({type:"LOOKAHEAD_REACHED"}),queueMicrotask(()=>this.queueRef.onTrackBufferReady(this)));let a=isNaN(this.duration)?w:Math.min(this.duration*.2,w);!this._notifiedPreloadThreshold&&this.currentTime>=a&&(this._notifiedPreloadThreshold=!0,queueMicrotask(()=>this.queueRef.onPreloadReady(this))),this.rafId=requestAnimationFrame(e)};this.rafId=requestAnimationFrame(e)}_stopProgressLoop(){this.rafId!==null&&(cancelAnimationFrame(this.rafId),this.rafId=null)}};function O(o,e){let t=0;return(...i)=>{let a=performance.now();a-t<e||(t=a,o(...i))}}var G=5,v=null;function ee(){if(v)return v;let o=8e3,e=o*10,t=44+e,i=new ArrayBuffer(t),a=new DataView(i),r=(d,c)=>{for(let h=0;h<c.length;h++)a.setUint8(d+h,c.charCodeAt(h))};return r(0,"RIFF"),a.setUint32(4,t-8,!0),r(8,"WAVE"),r(12,"fmt "),a.setUint32(16,16,!0),a.setUint16(20,1,!0),a.setUint16(22,1,!0),a.setUint32(24,o,!0),a.setUint32(28,o,!0),a.setUint16(32,1,!0),a.setUint16(34,8,!0),r(36,"data"),a.setUint32(40,e,!0),new Uint8Array(i,44).fill(128),v=URL.createObjectURL(new Blob([i],{type:"audio/wav"})),v}var P=class{_tracks=[];_actor;_onProgress;_onEnded;_onPlayNextTrack;_onPlayPreviousTrack;_onStartNewTrack;_onError;_onPlayBlocked;_onDebug;playbackMethod;_volume;_preloadNumTracks;_playbackRate;_mediaSessionAnchor=null;_scheduledNextIndex=null;_throttledUpdatePositionState=O((e,t,i)=>M(e,t,i),1e3);constructor(e={}){let{tracks:t=[],onProgress:i,onEnded:a,onPlayNextTrack:r,onPlayPreviousTrack:d,onStartNewTrack:c,onError:h,onPlayBlocked:A,onDebug:f,playbackMethod:H="HYBRID",trackMetadata:B=[],volume:F=1,preloadNumTracks:$=2,playbackRate:W=1}=e;this._volume=Math.min(1,Math.max(0,F)),this._preloadNumTracks=Math.max(0,$),this._playbackRate=Math.min(4,Math.max(.25,W)),this.playbackMethod=H,this._onProgress=i,this._onEnded=a,this._onPlayNextTrack=r,this._onPlayPreviousTrack=d,this._onStartNewTrack=c,this._onError=h,this._onPlayBlocked=A,this._onDebug=f,this._tracks=t.map((s,n)=>new g({trackUrl:s,index:n,queue:this,metadata:B[n]}));let V=I({currentTrackIndex:0,trackCount:this._tracks.length}).provide({actions:{deactivateCurrent:({context:s})=>{var n;(n=this._trackAt(s.currentTrackIndex))==null||n.deactivate()},deactivateEndedTrack:({context:s})=>{var n;(n=this._trackAt(s.currentTrackIndex))==null||n.deactivate()},activateAndPlayCurrent:({context:s})=>{let n=this._trackAt(s.currentTrackIndex);n&&(n.activate(),this._scheduledNextIndex!==n.index&&n.play())},playOrContinueGapless:({context:s})=>{let n=this._trackAt(s.currentTrackIndex);n&&(this._scheduledNextIndex!==n.index?n.play():(this._scheduledNextIndex=null,this.onDebug(`onTrackEnded: gapless track ${n.index} \u2014 sourceNode=${n.hasSourceNode} isPlaying=${n.isPlaying} machineState=${n.machineState}`),n.startProgressLoop()))},cancelAllGapless:()=>this._cancelScheduledGapless(),notifyStartNewTrack:({context:s})=>{var l;let n=this._trackAt(s.currentTrackIndex);n&&((l=this._onStartNewTrack)==null||l.call(this,n.toInfo()))},notifyPlayNextTrack:({context:s})=>{var l;let n=this._trackAt(s.currentTrackIndex);n&&((l=this._onPlayNextTrack)==null||l.call(this,n.toInfo()))},notifyPlayPreviousTrack:({context:s})=>{var l;let n=this._trackAt(s.currentTrackIndex);n&&((l=this._onPlayPreviousTrack)==null||l.call(this,n.toInfo()))},notifyEnded:()=>{var s;return(s=this._onEnded)==null?void 0:s.call(this)},updateMediaSessionMetadata:({context:s})=>{let n=this._trackAt(s.currentTrackIndex);n&&L(n.metadata)},preloadAhead:({context:s})=>{this._preloadAhead(s.currentTrackIndex)},playCurrent:({context:s})=>{var n;(n=this._trackAt(s.currentTrackIndex))==null||n.play()},pauseCurrent:({context:s})=>{var n;(n=this._trackAt(s.currentTrackIndex))==null||n.pause()},seekCurrent:({context:s,event:n})=>{var m;let l=n;(m=this._trackAt(s.currentTrackIndex))==null||m.seek(l.time)},seekCurrentToStartTime:({context:s,event:n})=>{var m;let l=n;(m=this._trackAt(s.currentTrackIndex))==null||m.seek(l.startTime??0)},scheduleGapless:({context:s})=>{this._tryScheduleGapless(s.currentTrackIndex)},cancelScheduledGapless:()=>{this._cancelScheduledGapless()},cancelAndRescheduleGapless:({context:s})=>{this._cancelScheduledGapless(),this._tryScheduleGapless(s.currentTrackIndex)}}});this._actor=Z(V),this._actor.subscribe(s=>{let n=s.value==="playing";D(n),n?this._startMediaSessionAnchor():this._stopMediaSessionAnchor()}),this._actor.start(),C({onPlay:()=>this.play(),onPause:()=>{this._actor.getSnapshot().value==="playing"&&this.pause()},onNext:()=>this.next(),onPrevious:()=>this.previous(),onSeek:s=>this.seek(s)})}play(){this._currentTrack&&this._actor.send({type:"PLAY"})}pause(){this._actor.send({type:"PAUSE"})}togglePlayPause(){this._actor.getSnapshot().value==="playing"?this.pause():this.play()}next(){this._actor.getSnapshot().context.currentTrackIndex+1>=this._tracks.length||this._actor.send({type:"NEXT"})}previous(){let e=this._currentTrack;if(e&&e.currentTime>8){this._actor.send({type:"SEEK",time:0});return}this._actor.send({type:"PREVIOUS"})}gotoTrack(e,t=!1,i){e<0||e>=this._tracks.length||(this.onDebug(`gotoTrack(${e}, playImmediately=${t}, startTime=${i}) queueState=${this._actor.getSnapshot().value} curIdx=${this._actor.getSnapshot().context.currentTrackIndex}`),this._actor.send({type:"GOTO",index:e,playImmediately:t,startTime:i}))}seek(e){this._actor.send({type:"SEEK",time:e})}setVolume(e){let t=Math.min(1,Math.max(0,e));this._volume=t;for(let i of this._tracks)i.setVolume(t)}setPlaybackRate(e){var a;let t=Math.min(4,Math.max(.25,e));this._playbackRate=t,(a=this._currentTrack)==null||a.setPlaybackRate(t),this._cancelScheduledGapless();let i=this._actor.getSnapshot();i.value==="playing"&&this._tryScheduleGapless(i.context.currentTrackIndex)}addTrack(e,t={}){let i=this._tracks.length,a=t.metadata??{};this._tracks.push(new g({trackUrl:e,index:i,queue:this,skipHEAD:t.skipHEAD,metadata:a})),this._actor.send({type:"ADD_TRACK"})}removeTrack(e){if(!(e<0||e>=this._tracks.length)){this._tracks[e].destroy(),this._tracks.splice(e,1);for(let t=e;t<this._tracks.length;t++)this._tracks[t].index=t;this._scheduledNextIndex===e?this._scheduledNextIndex=null:this._scheduledNextIndex!==null&&this._scheduledNextIndex>e&&this._scheduledNextIndex--,this._actor.send({type:"REMOVE_TRACK",index:e})}}resumeAudioContext(){return y()}destroy(){this._stopMediaSessionAnchor(),this._mediaSessionAnchor=null;for(let e of this._tracks)e.destroy();this._tracks=[],this._actor.stop()}get currentTrack(){var e;return(e=this._currentTrack)==null?void 0:e.toInfo()}get currentTrackIndex(){return this._actor.getSnapshot().context.currentTrackIndex}get tracks(){return this._tracks.map(e=>e.toInfo())}get isPlaying(){return this._actor.getSnapshot().value==="playing"}get isPaused(){return this._actor.getSnapshot().value==="paused"}get volume(){return this._volume}get preloadNumTracks(){return this._preloadNumTracks}set preloadNumTracks(e){this._preloadNumTracks=Math.max(0,e);let t=this._actor.getSnapshot();t.value==="playing"&&this._preloadAhead(t.context.currentTrackIndex)}get playbackRate(){return this._playbackRate}get queueSnapshot(){let e=this._actor.getSnapshot();return{state:e.value,context:e.context}}onTrackEnded(e){let t=this._actor.getSnapshot();if(this.onDebug(`onTrackEnded track=${e.index} queueState=${t.value} curIdx=${t.context.currentTrackIndex}`),e!==this._trackAt(t.context.currentTrackIndex))return;this._actor.send({type:"TRACK_ENDED"});let i=this._actor.getSnapshot();this.onDebug(`onTrackEnded after TRACK_ENDED \u2192 queueState=${i.value} curIdx=${i.context.currentTrackIndex}`)}onTrackBufferReady(e){this._actor.send({type:"TRACK_LOADED",index:e.index})}onProgress(e){var t;e.index===this._actor.getSnapshot().context.currentTrackIndex&&(isNaN(e.duration)||this._throttledUpdatePositionState(e.duration,e.currentTime,this._playbackRate),(t=this._onProgress)==null||t.call(this,e))}onError(e){var t;(t=this._onError)==null||t.call(this,e)}onPlayBlocked(){var e;this._actor.send({type:"PAUSE"}),(e=this._onPlayBlocked)==null||e.call(this)}onPreloadReady(e){let t=this._actor.getSnapshot();e.index===t.context.currentTrackIndex&&this._preloadAhead(t.context.currentTrackIndex)}onDebug(e){var t;(t=this._onDebug)==null||t.call(this,e)}_trackAt(e){return this._tracks[e]}get _currentTrack(){return this._tracks[this._actor.getSnapshot().context.currentTrackIndex]}_preloadAhead(e){let t=this._trackAt(e);if(t!=null&&t.isPlaying&&t.playbackType==="HTML5"&&t.webAudioLoadingState==="LOADING"){this.onDebug(`_preloadAhead(${e}): deferring all \u2014 current track buffer still loading`);return}let i=t!=null&&t.isPlaying&&(()=>{let r=isNaN(t.duration)?15:Math.min(t.duration*.2,15);return t.currentTime<r})(),a=e+this._preloadNumTracks+1;this.onDebug(`_preloadAhead(${e}) limit=${a} trackCount=${this._tracks.length} belowThreshold=${i}`);for(let r=e+1;r<this._tracks.length&&r<a;r++){let d=this._tracks[r];if(r>e+1&&i){this.onDebug(`_preloadAhead: deferring track ${r} (current at ${t.currentTime.toFixed(1)}s, below threshold)`);return}if(d.isBufferLoaded)this.onDebug(`_preloadAhead: track ${r} already loaded`);else{this.onDebug(`_preloadAhead: starting preload for track ${r}`),d.preload();return}}}_cancelScheduledGapless(){if(this._scheduledNextIndex===null)return;let e=this._trackAt(this._scheduledNextIndex);e&&(e.cancelGaplessStart(),this.onDebug(`_cancelScheduledGapless: cancelled track ${this._scheduledNextIndex}`));let t=this._trackAt(this._actor.getSnapshot().context.currentTrackIndex);t&&t.playbackType==="HTML5"&&t.cancelHtml5Mute(),this._scheduledNextIndex=null}_tryScheduleGapless(e){let t=k();if(!t||this.playbackMethod==="HTML5_ONLY")return;let i=e+1;if(i>=this._tracks.length)return;let a=this._tracks[e],r=this._tracks[i];if(!a.isBufferLoaded||!r.isBufferLoaded||this._scheduledNextIndex===i||!a.isPlaying)return;let d=this._computeTrackEndTime(a);if(d===null||d<t.currentTime+.01)return;let c=d-t.currentTime;if(a.playbackType==="HTML5"&&c>G){this.onDebug(`_tryScheduleGapless: deferring \u2014 HTML5 track ${e} has ${c.toFixed(1)}s remaining (max lookahead=${G}s)`);return}if(!r.scheduleGaplessStart(d)){this.onDebug(`_tryScheduleGapless: track ${i} rejected SCHEDULE_GAPLESS (state=${r.machineState})`);return}a.playbackType==="HTML5"&&a.scheduleHtml5Mute(d),this.onDebug(`_tryScheduleGapless: scheduled track ${i} at endTime=${d.toFixed(3)} (in ${(d-t.currentTime).toFixed(1)}s) curPlaybackType=${a.playbackType}`),this._scheduledNextIndex=i}_computeTrackEndTime(e){let t=k();if(!t||!e.isBufferLoaded)return null;let i=e.playbackEndContextTime;if(i!==null)return i>t.currentTime?i:null;let a=e.duration;if(isNaN(a))return null;let r=(a-e.currentTime)/this._playbackRate;return r<=0?null:t.currentTime+r}_startMediaSessionAnchor(){typeof Audio>"u"||(this._mediaSessionAnchor||(this._mediaSessionAnchor=new Audio(ee()),this._mediaSessionAnchor.loop=!0),this._mediaSessionAnchor.paused&&this._mediaSessionAnchor.play().catch(()=>{}))}_stopMediaSessionAnchor(){this._mediaSessionAnchor&&!this._mediaSessionAnchor.paused&&this._mediaSessionAnchor.pause()}};export{P as Queue,P as default};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|