UNPKG

@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) 7.41 kB
import{A as e,a as t}from"./audioEngine-vi2zlmHT.esm.min.js";import{L as s}from"./index-yNtOWHHe.esm.min.js";class n extends e{constructor(e,t){super(e,t,3)}connect(e){if(!this._connect(e))throw new Error("Connect failed")}disconnect(e){if(!this._disconnect(e))throw new Error("Disconnect failed")}disconnectAll(){if(!this._downstreamNodes)throw new Error("Disconnect failed");const e=this._downstreamNodes.values();for(let t=e.next();!t.done;t=e.next())if(!this._disconnect(t.value))throw new Error("Disconnect failed")}}const o={volume:1};class i extends n{constructor(e){super("Volume",e)}setOptions(e){this.volume=e.volume??o.volume}}function r(e){return e.getSubNode("Volume")}const a=2048,u=-100,c=-30,l=.8;class d{get frequencyBinCount(){return this.fftSize/2}}class h extends n{constructor(e){super("Analyzer",e)}setOptions(e){this.fftSize=e.analyzerFFTSize??a,this.minDecibels=e.analyzerMinDecibels??u,this.maxDecibels=e.analyzerMaxDecibels??c,this.smoothing=e.analyzerSmoothing??l}}function _(e){return e.getSubNode("Analyzer")}function b(e,t,s){e.callOnSubNode("Analyzer",(e=>{e[t]=s}))}let y=null,m=null;function N(){return y||(y=new Uint8Array),y}function f(){return m||(m=new Float32Array),m}class p extends d{constructor(e){super(),this._fftSize=a,this._maxDecibels=c,this._minDecibels=u,this._smoothing=l,this._subGraph=e}get fftSize(){return this._fftSize}set fftSize(e){this._fftSize=e,b(this._subGraph,"fftSize",e)}get isEnabled(){return null!==_(this._subGraph)}get minDecibels(){return this._minDecibels}set minDecibels(e){this._minDecibels=e,b(this._subGraph,"minDecibels",e)}get maxDecibels(){return this._maxDecibels}set maxDecibels(e){this._maxDecibels=e,b(this._subGraph,"maxDecibels",e)}get smoothing(){return this._smoothing}set smoothing(e){this._smoothing=e,b(this._subGraph,"smoothing",e)}dispose(){const e=_(this._subGraph);e&&(this._subGraph.removeSubNodeAsync(e),e.dispose())}async enableAsync(){_(this._subGraph)||await this._subGraph.createAndAddSubNodeAsync("Analyzer")}getByteFrequencyData(){const e=_(this._subGraph);return e?e.getByteFrequencyData():(s.Warn("AudioAnalyzer not enabled"),this.enableAsync(),N())}getFloatFrequencyData(){const e=_(this._subGraph);return e?e.getFloatFrequencyData():(s.Warn("AudioAnalyzer not enabled"),this.enableAsync(),f())}}class D extends e{constructor(e,t,s){super(e,t,s),this._analyzer=null}get analyzer(){return this._analyzer??(this._analyzer=new p(this._subGraph))}get volume(){return e=this._subGraph,t="volume",r(e)?.[t]??o[t];var e,t}set volume(e){const t=r(this._subGraph);if(!t)throw new Error("No volume subnode");t.volume=e}dispose(){super.dispose(),this._analyzer?.dispose(),this._analyzer=null,this._subGraph.dispose()}setVolume(e,t=null){const s=r(this._subGraph);if(!s)throw new Error("No volume subnode");s.setVolume(e,t)}}class g{constructor(){this._createSubNodePromises={},this._isDisposed=!1,this._subNodes={},this._onSubNodeDisposed=e=>{const t=e;delete this._subNodes[t.name],this._onSubNodesChanged()}}callOnSubNode(e,t){const s=this.getSubNode(e);s?t(s):this._createSubNodePromisesResolvedAsync().then((()=>{const s=this.getSubNode(e);s?t(s):this.createAndAddSubNodeAsync(e).then((e=>{t(e)}))}))}createAndAddSubNodeAsync(e){return this._createSubNodePromises[e]||=this._createSubNode(e).then((e=>(this._addSubNode(e),e))),this._createSubNodePromises[e]}dispose(){this._isDisposed=!0;const e=Object.values(this._subNodes);for(const t of e)t.dispose();this._subNodes={},this._createSubNodePromises={}}getSubNode(e){return this._subNodes[e]??null}async removeSubNodeAsync(e){await this._createSubNodePromisesResolvedAsync();const t=e.name;this._subNodes[t]&&delete this._subNodes[t],delete this._createSubNodePromises[t],this._onSubNodesChanged()}async _createSubNodePromisesResolvedAsync(){return await Promise.all(Object.values(this._createSubNodePromises))}_addSubNode(e){this._isDisposed?e.dispose():(this._subNodes[e.name]=e,e.onDisposeObservable.addOnce(this._onSubNodeDisposed),this._onSubNodesChanged())}}class z extends i{constructor(e){super(e);const s=this.node=new GainNode(e._audioContext);this._volume=new t(e,s.gain)}dispose(){super.dispose(),this._volume.dispose()}get volume(){return this._volume.value}set volume(e){this.setVolume(e)}get _inNode(){return this.node}get _outNode(){return this.node}setVolume(e,t=null){this._volume.setTargetValue(e,t)}_connect(e){return!!super._connect(e)&&(e._inNode&&this.node.connect(e._inNode),!0)}_disconnect(e){return!!super._disconnect(e)&&(e._inNode&&this.node.disconnect(e._inNode),!0)}getClassName(){return"_VolumeWebAudioSubNode"}}class S extends h{constructor(e){super(e),this._byteFrequencyData=null,this._floatFrequencyData=null,this._analyzerNode=new AnalyserNode(e._audioContext)}get fftSize(){return this._analyzerNode.fftSize}set fftSize(e){e!==this._analyzerNode.fftSize&&(this._analyzerNode.fftSize=e,this._clearArrays())}get _inNode(){return this._analyzerNode}get minDecibels(){return this._analyzerNode.minDecibels}set minDecibels(e){this._analyzerNode.minDecibels=e}get maxDecibels(){return this._analyzerNode.maxDecibels}set maxDecibels(e){this._analyzerNode.maxDecibels=e}get smoothing(){return this._analyzerNode.smoothingTimeConstant}set smoothing(e){this._analyzerNode.smoothingTimeConstant=e}dispose(){super.dispose(),this._clearArrays(),this._byteFrequencyData=null,this._floatFrequencyData=null,this._analyzerNode.disconnect()}getClassName(){return"_WebAudioAnalyzerSubNode"}getByteFrequencyData(){return this._byteFrequencyData&&0!==this._byteFrequencyData.length||(this._byteFrequencyData=new Uint8Array(this._analyzerNode.frequencyBinCount)),this._analyzerNode.getByteFrequencyData(this._byteFrequencyData),this._byteFrequencyData}getFloatFrequencyData(){return this._floatFrequencyData&&0!==this._floatFrequencyData.length||(this._floatFrequencyData=new Float32Array(this._analyzerNode.frequencyBinCount)),this._analyzerNode.getFloatFrequencyData(this._floatFrequencyData),this._floatFrequencyData}_clearArrays(){this._byteFrequencyData?.set(N()),this._floatFrequencyData?.set(f())}}class A extends g{constructor(e){super(),this._outputNode=null,this._owner=e}async initAsync(e){const t=function(e){return e.analyzerEnabled||void 0!==e.analyzerFFTSize||void 0!==e.analyzerMinDecibels||void 0!==e.analyzerMaxDecibels||void 0!==e.analyzerSmoothing}(e);if(t&&await this.createAndAddSubNodeAsync("Analyzer"),await this.createAndAddSubNodeAsync("Volume"),await this._createSubNodePromisesResolvedAsync(),t){const t=_(this);if(!t)throw new Error("No analyzer subnode.");t.setOptions(e)}const s=r(this);if(!s)throw new Error("No volume subnode.");if(s.setOptions(e),"_VolumeWebAudioSubNode"!==s.getClassName())throw new Error("Not a WebAudio subnode.");if(this._outputNode=s.node,this._outputNode&&this._downstreamNodes){const e=this._downstreamNodes.values();for(let t=e.next();!t.done;t=e.next()){const e=t.value._inNode;e&&this._outputNode.connect(e)}}}get _inNode(){return this._outputNode}get _outNode(){return this._outputNode}_createSubNode(e){switch(e){case"Analyzer":return async function(e){return new S(e)}(this._owner.engine);case"Volume":return async function(e){return new z(e)}(this._owner.engine);default:throw new Error(`Unknown subnode name: ${e}`)}}_onSubNodesChanged(){const e=_(this),t=r(this);e&&t&&t.connect(e)}}export{D as A,n as _,A as a,r as b}; //# sourceMappingURL=webAudioBaseSubGraph-4eia-TfF.esm.min.js.map