1import{aW as o,ai as h,b2 as r,L as d}from"./Layout.astro_astro_type_script_index_0_lang.D7HFstIc.js";import{A as l,_}from"./abstractSoundInstance.C1U73nUN.js";import{_ as u,a as c,b as p,c as f}from"./spatialWebAudio.CWwraS7v.js";import"./_commonjsHelpers.CqkleIqs.js";import"./abstractSoundSource.Bv6zigu5.js";import"./webAudioBaseSubGraph.wb856c9X.js";class m extends l{constructor(e,t){super(e,t),this._preloadedInstances=new Array}get preloadCount(){return this._options.preloadCount??1}get preloadCompletedCount(){return this._preloadedInstances.length}preloadInstanceAsync(){const e=this._createInstance();return this._addPreloadedInstance(e),e.preloadedPromise}async preloadInstancesAsync(e){for(let t=0;t<e;t++)this.preloadInstanceAsync();await Promise.all(this._preloadedInstances.map(async t=>await t.preloadedPromise))}play(e={}){if(this.state===5){this.resume();return}let t;this.preloadCompletedCount>0?(t=this._preloadedInstances[0],t.startOffset=this.startOffset,this._removePreloadedInstance(t)):t=this._createInstance();const s=()=>{t.state===3&&(this._stopExcessInstances(),t.onStateChangedObservable.removeCallback(s))};t.onStateChangedObservable.add(s),e.startOffset??(e.startOffset=this.startOffset),e.loop??(e.loop=this.loop),e.volume??(e.volume=1),this._beforePlay(t),t.play(e),this._afterPlay(t)}stop(){if(this._setState(1),!!this._instances)for(const e of Array.from(this._instances))e.stop()}_addPreloadedInstance(e){this._preloadedInstances.includes(e)||this._preloadedInstances.push(e)}_removePreloadedInstance(e){const t=this._preloadedInstances.indexOf(e);t!==-1&&this._preloadedInstances.splice(t,1)}}class g extends _{constructor(e){super(e),this.onReadyObservable=new o,this.preloadedPromise=new Promise((t,s)=>{this._rejectPreloadedProimse=s,this._resolvePreloadedPromise=t}),this.onErrorObservable.add(this._rejectPreloadedProimse),this.onReadyObservable.add(this._resolvePreloadedPromise)}set startOffset(e){this._options.startOffset=e}dispose(){super.dispose(),this.onErrorObservable.clear(),this.onReadyObservable.clear(),this._resolvePreloadedPromise()}}class i extends m{constructor(e,t,s){super(e,t),this._spatial=null,this._spatialAutoUpdate=!0,this._spatialMinUpdateTime=0,this._stereo=null,typeof s.spatialAutoUpdate=="boolean"&&(this._spatialAutoUpdate=s.spatialAutoUpdate),typeof s.spatialMinUpdateTime=="number"&&(this._spatialMinUpdateTime=s.spatialMinUpdateTime),this._options={autoplay:s.autoplay??!1,loop:s.loop??!1,maxInstances:s.maxInstances??1/0,preloadCount:s.preloadCount??1,startOffset:s.startOffset??0},this._subGraph=new i._SubGraph(this)}async _initAsync(e,t){const s=this.engine._audioContext;if(!(s instanceof AudioContext))throw new Error("Unsupported audio context type.");this._audioContext=s,this._source=e,t.outBus?this.outBus=t.outBus:t.outBusAutoDefault!==!1&&(await this.engine.isReadyPromise,this.outBus=this.engine.defaultMainBus),await this._subGraph.initAsync(t),u(t)&&this._initSpatialProperty(),this.preloadCount&&await this.preloadInstancesAsync(this.preloadCount),t.autoplay&&this.play(t),this.engine._addNode(this)}get _inNode(){return this._subGraph._inNode}get _outNode(){return this._subGraph._outNode}get spatial(){return this._spatial?this._spatial:this._initSpatialProperty()}get stereo(){return this._stereo??(this._stereo=new c(this._subGraph))}dispose(){super.dispose(),this._spatial=null,this._stereo=null,this._subGraph.dispose(),this.engine._removeNode(this)}getClassName(){return"_WebAudioStreamingSound"}_createInstance(){return new y(this,this._options)}_connect(e){return super._connect(e)?(e._inNode&&this._outNode?.connect(e._inNode),!0):!1}_disconnect(e){return super._disconnect(e)?(e._inNode&&this._outNode?.disconnect(e._inNode),!0):!1}_initSpatialProperty(){return this._spatial||(this._spatial=new p(this._subGraph,this._spatialAutoUpdate,this._spatialMinUpdateTime)),this._spatial}}i._SubGraph=class extends f{get _downstreamNodes(){return this._owner._downstreamNodes??null}get _upstreamNodes(){return this._owner._upstreamNodes??null}};class y extends g{constructor(e,t){super(e),this._currentTimeChangedWhilePaused=!1,this._enginePlayTime=1/0,this._enginePauseTime=0,this._isReady=!1,this._isReadyPromise=new Promise((s,n)=>{this._resolveIsReadyPromise=s,this._rejectIsReadyPromise=n}),this._onCanPlayThrough=()=>{this._isReady=!0,this._resolveIsReadyPromise(this._mediaElement),this.onReadyObservable.notifyObservers(this)},this._onEnded=()=>{this.onEndedObservable.notifyObservers(this),this.dispose()},this._onError=s=>{this._setState(4),this.onErrorObservable.notifyObservers(s),this._rejectIsReadyPromise(s),this.dispose()},this._onEngineStateChanged=()=>{this.engine.state==="running"&&(this._options.loop&&this.state===2&&this.play(),this.engine.stateChangedObservable.removeCallback(this._onEngineStateChanged))},this._onUserGesture=()=>{this.play()},this._options=t,this._volumeNode=new GainNode(e._audioContext),typeof e._source=="string"?this._initFromUrl(e._source):Array.isArray(e._source)?this._initFromUrls(e._source):e._source instanceof HTMLMediaElement&&this._initFromMediaElement(e._source)}get currentTime(){if(this._state===1)return 0;const e=this._state===5?0:this.engine.currentTime-this._enginePlayTime;return this._enginePauseTime+e+this._options.startOffset}set currentTime(e){const t=this._state===2||this._state===3;t&&(this._mediaElement.pause(),this._setState(1)),this._options.startOffset=e,t?this.play({startOffset:e}):this._state===5&&(this._currentTimeChangedWhilePaused=!0)}get _outNode(){return this._volumeNode}get startTime(){return this._state===1?0:this._enginePlayTime}dispose(){super.dispose(),this.stop(),this._sourceNode?.disconnect(this._volumeNode),this._sourceNode=null,this._mediaElement.removeEventListener("error",this._onError),this._mediaElement.removeEventListener("ended",this._onEnded),this._mediaElement.removeEventListener("canplaythrough",this._onCanPlayThrough);for(const e of Array.from(this._mediaElement.children))this._mediaElement.removeChild(e);this.engine.stateChangedObservable.removeCallback(this._onEngineStateChanged),this.engine.userGestureObservable.removeCallback(this._onUserGesture)}play(e={}){if(this._state===3)return;e.loop!==void 0&&(this._options.loop=e.loop),this._mediaElement.loop=this._options.loop;let t=e.startOffset;this._currentTimeChangedWhilePaused?(t=this._options.startOffset,this._currentTimeChangedWhilePaused=!1):this._state===5&&(t=this.currentTime+this._options.startOffset),t&&t>0&&(this._mediaElement.currentTime=t),this._volumeNode.gain.value=e.volume??1,this._play()}pause(){this._state!==2&&this._state!==3||(this._setState(5),this._enginePauseTime+=this.engine.currentTime-this._enginePlayTime,this._mediaElement.pause())}resume(){this._state===5?this.play():this._currentTimeChangedWhilePaused&&this.play()}stop(){this._state!==1&&this._stop()}getClassName(){return"_WebAudioStreamingSoundInstance"}_connect(e){return super._connect(e)?(e instanceof i&&e._inNode&&this._outNode?.connect(e._inNode),!0):!1}_disconnect(e){return super._disconnect(e)?(e instanceof i&&e._inNode&&this._outNode?.disconnect(e._inNode),!0):!1}_initFromMediaElement(e){if(h.SetCorsBehavior(e.currentSrc,e),e.controls=!1,e.loop=this._options.loop,e.preload="auto",e.addEventListener("canplaythrough",this._onCanPlayThrough,{once:!0}),e.addEventListener("ended",this._onEnded,{once:!0}),e.addEventListener("error",this._onError,{once:!0}),e.load(),this._sourceNode=new MediaElementAudioSourceNode(this._sound._audioContext,{mediaElement:e}),this._sourceNode.connect(this._volumeNode),!this._connect(this._sound))throw new Error("Connect failed");this._mediaElement=e}_initFromUrl(e){const t=new Audio(r(e));this._initFromMediaElement(t)}_initFromUrls(e){const t=new Audio;for(const s of e){const n=document.createElement("source");
1n.src=r(s),t.appendChild(n)}this._initFromMediaElement(t)}_play(){if(this._setState(2),!this._isReady){this._playWhenReady();return}if(this._state===2)if(this.engine.state==="running"){const e=this._mediaElement.play();this._enginePlayTime=this.engine.currentTime,this._setState(3),e.catch(()=>{this._setState(4),this._options.loop&&this.engine.userGestureObservable.addOnce(this._onUserGesture)})}else this._options.loop?this.engine.stateChangedObservable.add(this._onEngineStateChanged):(this.stop(),this._setState(4))}_playWhenReady(){this._isReadyPromise.then(()=>{this._play()}).catch(()=>{d.Error("Streaming sound instance failed to play"),this._setState(4)})}_stop(){this._mediaElement.pause(),this._setState(1),this._onEnded(),this.engine.stateChangedObservable.removeCallback(this._onEngineStateChanged)}}export{i as _WebAudioStreamingSound};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.