@babylonjs/viewer
Version:
The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.
3 lines (2 loc) • 8.71 kB
JavaScript
import{O as e,b as t,L as s}from"./index-yNtOWHHe.esm.min.js";import{A as i,_ as n}from"./abstractSoundInstance-_QBiLbnZ.esm.min.js";import{_ as o,a,b as r,c as h}from"./spatialWebAudio-BbDz6Oip.esm.min.js";import{c as d}from"./audioEngine-vi2zlmHT.esm.min.js";import"./abstractSoundSource-BEzGJJiV.esm.min.js";import"./webAudioBaseSubGraph-4eia-TfF.esm.min.js";class _ extends i{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 e=>await e.preloadedPromise)))}play(e={}){if(5===this.state)return void this.resume();let t;this.preloadCompletedCount>0?(t=this._preloadedInstances[0],t.startOffset=this.startOffset,this._removePreloadedInstance(t)):t=this._createInstance();const s=()=>{3===t.state&&(this._stopExcessInstances(),t.onStateChangedObservable.removeCallback(s))};t.onStateChangedObservable.add(s),e.startOffset??=this.startOffset,e.loop??=this.loop,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);-1!==t&&this._preloadedInstances.splice(t,1)}}class l extends n{constructor(t){super(t),this.onReadyObservable=new e,this.preloadedPromise=new Promise(((e,t)=>{this._rejectPreloadedProimse=t,this._resolvePreloadedPromise=e})),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 u extends _{constructor(e,t,s){super(e,t),this._spatial=null,this._spatialAutoUpdate=!0,this._spatialMinUpdateTime=0,this._stereo=null,"boolean"==typeof s.spatialAutoUpdate&&(this._spatialAutoUpdate=s.spatialAutoUpdate),"number"==typeof s.spatialMinUpdateTime&&(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 u._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:!1!==t.outBusAutoDefault&&(await this.engine.isReadyPromise,this.outBus=this.engine.defaultMainBus),await this._subGraph.initAsync(t),o(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 a(this._subGraph))}dispose(){super.dispose(),this._spatial=null,this._stereo=null,this._subGraph.dispose(),this.engine._removeNode(this)}getClassName(){return"_WebAudioStreamingSound"}_createInstance(){return new p(this,this._options)}_connect(e){return!!super._connect(e)&&(e._inNode&&this._outNode?.connect(e._inNode),!0)}_disconnect(e){return!!super._disconnect(e)&&(e._inNode&&this._outNode?.disconnect(e._inNode),!0)}_initSpatialProperty(){return this._spatial||(this._spatial=new r(this._subGraph,this._spatialAutoUpdate,this._spatialMinUpdateTime)),this._spatial}}u._SubGraph=class extends h{get _downstreamNodes(){return this._owner._downstreamNodes??null}get _upstreamNodes(){return this._owner._upstreamNodes??null}};class p extends l{constructor(e,t){super(e),this._currentTimeChangedWhilePaused=!1,this._enginePlayTime=1/0,this._enginePauseTime=0,this._isReady=!1,this._isReadyPromise=new Promise(((e,t)=>{this._resolveIsReadyPromise=e,this._rejectIsReadyPromise=t})),this._onCanPlayThrough=()=>{this._isReady=!0,this._resolveIsReadyPromise(this._mediaElement),this.onReadyObservable.notifyObservers(this)},this._onEnded=()=>{this.onEndedObservable.notifyObservers(this),this.dispose()},this._onError=e=>{this._setState(4),this.onErrorObservable.notifyObservers(e),this._rejectIsReadyPromise(e),this.dispose()},this._onEngineStateChanged=()=>{"running"===this.engine.state&&(this._options.loop&&2===this.state&&this.play(),this.engine.stateChangedObservable.removeCallback(this._onEngineStateChanged))},this._onUserGesture=()=>{this.play()},this._options=t,this._volumeNode=new GainNode(e._audioContext),"string"==typeof e._source?this._initFromUrl(e._source):Array.isArray(e._source)?this._initFromUrls(e._source):e._source instanceof HTMLMediaElement&&this._initFromMediaElement(e._source)}get currentTime(){if(1===this._state)return 0;const e=5===this._state?0:this.engine.currentTime-this._enginePlayTime;return this._enginePauseTime+e+this._options.startOffset}set currentTime(e){const t=2===this._state||3===this._state;t&&(this._mediaElement.pause(),this._setState(1)),this._options.startOffset=e,t?this.play({startOffset:e}):5===this._state&&(this._currentTimeChangedWhilePaused=!0)}get _outNode(){return this._volumeNode}get startTime(){return 1===this._state?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(3===this._state)return;void 0!==e.loop&&(this._options.loop=e.loop),this._mediaElement.loop=this._options.loop;let t=e.startOffset;this._currentTimeChangedWhilePaused?(t=this._options.startOffset,this._currentTimeChangedWhilePaused=!1):5===this._state&&(t=this.currentTime+this._options.startOffset),t&&t>0&&(this._mediaElement.currentTime=t),this._volumeNode.gain.value=e.volume??1,this._play()}pause(){2!==this._state&&3!==this._state||(this._setState(5),this._enginePauseTime+=this.engine.currentTime-this._enginePlayTime,this._mediaElement.pause())}resume(){(5===this._state||this._currentTimeChangedWhilePaused)&&this.play()}stop(){1!==this._state&&this._stop()}getClassName(){return"_WebAudioStreamingSoundInstance"}_connect(e){return!!super._connect(e)&&(e instanceof u&&e._inNode&&this._outNode?.connect(e._inNode),!0)}_disconnect(e){return!!super._disconnect(e)&&(e instanceof u&&e._inNode&&this._outNode?.disconnect(e._inNode),!0)}_initFromMediaElement(e){if(t.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(d(e));this._initFromMediaElement(t)}_initFromUrls(e){const t=new Audio;for(const s of e){const e=document.createElement("source");e.src=d(s),t.appendChild(e)}this._initFromMediaElement(t)}_play(){if(this._setState(2),this._isReady){if(2===this._state)if("running"===this.engine.state){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))}else this._playWhenReady()}_playWhenReady(){this._isReadyPromise.then((()=>{this._play()})).catch((()=>{s.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{u as _WebAudioStreamingSound};
//# sourceMappingURL=webAudioStreamingSound-BTFSVhs5.esm.min.js.map