TeaWeb/generated/js/client.min.js

1 line
165 KiB
JavaScript
Raw Normal View History

2018-04-19 17:46:47 +00:00
var PlayerState;!function(PlayerState){PlayerState[PlayerState.PREBUFFERING=0]="PREBUFFERING",PlayerState[PlayerState.PLAYING=1]="PLAYING",PlayerState[PlayerState.BUFFERING=2]="BUFFERING",PlayerState[PlayerState.STOPPING=3]="STOPPING",PlayerState[PlayerState.STOPPED=4]="STOPPED"}(PlayerState||(PlayerState={}));class AudioController{constructor(){this.playerState=PlayerState.STOPPED,this.audioCache=[],this.playingAudioCache=[],this._volume=1,this._codecCache=[],this._timeIndex=0,this.allowBuffering=!0,this.speakerContext=AudioController.globalContext,this.onSpeaking=function(){},this.onSilence=function(){}}static get globalContext(){return this._globalContext?this._globalContext:(this._globalContext=new AudioContext,this._globalContext)}static initializeAudioController(){}initialize(){AudioController._audioInstances.push(this)}close(){AudioController._audioInstances.remove(this)}playBuffer(buffer){switch(buffer.sampleRate!=this.speakerContext.sampleRate&&console.warn("[AudioController] Source sample rate isn't equal to playback sample rate! ("+buffer.sampleRate+" | "+this.speakerContext.sampleRate+")"),this.applayVolume(buffer),this.audioCache.push(buffer),this.playerState!=PlayerState.STOPPED&&this.playerState!=PlayerState.STOPPING||(console.log("[Audio] Starting new playback"),this.playerState=PlayerState.PREBUFFERING),this.playerState){case PlayerState.PREBUFFERING:case PlayerState.BUFFERING:if(this.audioCache.length<3){if(this.playerState!=PlayerState.BUFFERING)break;if(this.allowBuffering)break}this.playerState==PlayerState.PREBUFFERING?(console.log("[Audio] Prebuffering succeeded (Replaying now)"),this.onSpeaking()):this.allowBuffering&&console.log("[Audio] Buffering succeeded (Replaying now)"),this.playerState=PlayerState.PLAYING;case PlayerState.PLAYING:this.playQueue()}}playQueue(){let buffer;for(;buffer=this.audioCache.pop_front();){this._timeIndex<this.speakerContext.currentTime&&(this._timeIndex=this.speakerContext.currentTime);let player=this.speakerContext.createBufferSource();player.buffer=buffer,player.onended=(()=>this.removeNode(player)),this.playingAudioCache.push(player),player.connect(this.speakerContext.destination),player.start(this._timeIndex),this._timeIndex+=buffer.duration}}removeNode(node){this.playingAudioCache.remove(node),this.testBufferQueue()}stopAudio(now=!1){if(this.playerState=PlayerState.STOPPING,now){this.playerState=PlayerState.STOPPED,this.audioCache=[];for(let entry of this.playingAudioCache)entry.stop(0);this.playingAudioCache=[]}this.testBufferQueue()}testBufferQueue(){0==this.audioCache.length&&0==this.playingAudioCache.length&&(this.playerState!=PlayerState.STOPPING?(this.playerState=PlayerState.BUFFERING,this.allowBuffering||console.warn("[Audio] Detected a buffer underflow!")):(this.playerState=PlayerState.STOPPED,this.onSilence()))}get volume(){return this._volume}set volume(val){if(this._volume!=val){this._volume=val;for(let buffer of this.audioCache)this.applayVolume(buffer)}}applayVolume(buffer){for(let channel=0;channel<buffer.numberOfChannels;channel++){let data=buffer.getChannelData(channel);for(let sample=0;sample<data.length;sample++){let lane=data[sample];lane*=this._volume,data[sample]=lane}}}codecCache(codec){for(;this._codecCache.length<=codec;)this._codecCache.push(new CodecClientCache);return this._codecCache[codec]}}function concatenate(resultConstructor,...arrays){let totalLength=0;for(const arr of arrays)totalLength+=arr.length;const result=new resultConstructor(totalLength);let offset=0;for(const arr of arrays)result.set(arr,offset),offset+=arr.length;return result}function formatDate(secs){let years=Math.floor(secs/31536e3),days=Math.floor(secs/86400)%365,hours=Math.floor(secs/3600)%24,minutes=Math.floor(secs/60)%60,seconds=Math.floor(secs%60),result="";return years>0&&(result+=years+" years "),(years>0||days>0)&&(result+=days+" days "),(years>0||days>0||hours>0)&&(result+=hours+" hours "),(years>0||days>0||hours>0||minutes>0)&&(result+=minutes+" minutes "),years>0||days>0||hours>0||minutes>0||seconds>0?result+=seconds+" seconds ":result="now ",result.sub