UNPKG

ts-audio

Version:

3 lines (2 loc) 9.38 kB
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var s=1;s<arguments.length;s++){var e=arguments[s];for(var i in e)({}).hasOwnProperty.call(e,i)&&(t[i]=e[i])}return t},t.apply(null,arguments)}const s=(t,e=3,i)=>{fetch(t).then(i).catch(()=>{e&&s(t,e-1)})},e={isDecoded:!1,isPlaying:!1,hasStarted:!1,source:null,gainNode:null};class i{constructor(){this.events=void 0,this.events={}}listener(t,s){this.events[t]=s}emit(t,s){this.events[t]&&this.events[t](s)}removeAllListeners(){this.events={}}}class a{constructor(t,s){this.emitter=void 0,this.audioCtx=void 0,this.emitter=t,this.audioCtx=s}ready(t){this.emitter.listener("decoded",t)}start(t){this.emitter.listener("start",t)}end(t){this.emitter.listener("end",t)}state(t){this.audioCtx&&(this.audioCtx.onstatechange=()=>{var s;return t({data:null==(s=this.audioCtx)?void 0:s.state})})}dispose(){this.audioCtx&&(this.audioCtx.onstatechange=null)}}const o=({audioCtx:t,volume:s,emitter:e,states:i})=>{const a=i.source=t.createBufferSource(),o=i.gainNode=t.createGain();o.gain.value=s,o.connect(t.destination),a.connect(o),a.onended=()=>{i.hasStarted=!1,i.isPlaying=!1,e.emit("end",{data:null})}},r=(t,s,e)=>"suspended"===t.state?t.resume().then(()=>s.start(0,e)):s.start(0,e);class h{constructor({file:o,volume:r=1,time:h=0,autoPlay:n=!1,loop:u=!1,preload:l=!1}){this._file=void 0,this._initialVolume=void 0,this._initialTime=void 0,this._autoPlay=void 0,this._initialLoop=void 0,this._audioCtx=void 0,this._states=void 0,this._emitter=void 0,this._eventHandler=void 0,this._startTime=0,this._pauseTime=0,this._hasSeekedWhilePaused=!1,this._pendingSeekTime=null,this._isDestroyed=!1,this._file=o,this._initialVolume=r,this._initialTime=h,this._autoPlay=n,this._initialLoop=u,this._audioCtx=(()=>{const t=window.AudioContext||window.webkitAudioContext;return t||(t=>{throw new Error("`ts-audio`: Your browser doesn't support AudioContext - https://bit.ly/2YWmpnX")})(),new t})(),this._states=t({},e),this._emitter=new i,this._eventHandler=new a(this._emitter,this._audioCtx),l&&s(o)}recreateAndStart(t,s){try{var e,i;this._states.source&&(this._states.source.stop(0),this._states.source.onended=null),o({audioCtx:this._audioCtx,volume:null!=(e=null==(i=this._states.gainNode)?void 0:i.gain.value)?e:this._initialVolume,emitter:this._emitter,states:this._states});const{source:a}=this._states;a&&(a.buffer=s,a.loop=this._initialLoop,r(this._audioCtx,a,t),this._startTime=this._audioCtx.currentTime,this._pauseTime=t,this._states.isPlaying=!0,this._states.hasStarted=!0)}catch(t){console.error("Failed to recreate audio source:",t),this._states.isPlaying=!1}}curryGetBuffer(t){var s;this._states.isDecoded=!1,(s=this._file,fetch(s).then(t=>{if(!t.ok)throw new Error(`HTTP error, status = ${t.status}`);return t.arrayBuffer()})).then(s=>{(({audioCtx:t,source:s,arrayBuffer:e,autoPlay:i,loop:a,states:o,emitter:r})=>{t.decodeAudioData(e,t=>{s.buffer=t,s.loop=a,o.isDecoded=!0,r.emit("decoded",{data:t}),i&&(s.start(0),o.isPlaying=!0)},console.error)})({audioCtx:this._audioCtx,source:t,arrayBuffer:s,autoPlay:this._autoPlay,loop:this._initialLoop,states:this._states,emitter:this._emitter})}).catch(console.error)}play(){var t;if(this._isDestroyed)return;if(this._states.hasStarted&&!this._hasSeekedWhilePaused)return this._audioCtx.resume(),this._startTime=this._audioCtx.currentTime,void(this._states.isPlaying=!0);if(this._hasSeekedWhilePaused&&null!=(t=this._states.source)&&t.buffer)return this.recreateAndStart(this._pauseTime,this._states.source.buffer),void(this._hasSeekedWhilePaused=!1);o({audioCtx:this._audioCtx,volume:this._initialVolume,emitter:this._emitter,states:this._states}),null!==this._pendingSeekTime&&(this._pauseTime=this._pendingSeekTime,this._pendingSeekTime=null);const{source:s}=this._states;var e;s&&(this.curryGetBuffer(s),this._states.isDecoded?(r(this._audioCtx,s,null!=(e=this._pauseTime)?e:this._initialTime),this._startTime=this._audioCtx.currentTime):this._emitter.listener("decoded",()=>{var t;r(this._audioCtx,s,null!=(t=this._pauseTime)?t:this._initialTime),this._startTime=this._audioCtx.currentTime}),this._states.hasStarted=!0,this._states.isPlaying=!0,this._emitter.emit("start",{data:null}))}pause(){this._isDestroyed||(this._states.isPlaying&&(this._pauseTime=this.currentTime),this._audioCtx.suspend(),this._states.isPlaying=!1,this._hasSeekedWhilePaused=!1)}toggle(){this._isDestroyed||(this._states.isPlaying?this.pause():this.play())}stop(){var t;this._isDestroyed||this._states.hasStarted&&(null==(t=this._states.source)||t.stop(0),this._states.isPlaying=!1)}on(t,s){var e,i;this._isDestroyed||null==(e=(i=this._eventHandler)[t])||e.call(i,s)}get volume(){var t,s;return null!=(t=null==(s=this._states.gainNode)?void 0:s.gain.value)?t:0}set volume(t){this._isDestroyed||this._states.gainNode&&(this._states.gainNode.gain.value=t)}get loop(){var t,s;return null!=(t=null==(s=this._states.source)?void 0:s.loop)&&t}set loop(t){this._isDestroyed||this._states.source&&(this._states.source.loop=t)}get state(){return this._audioCtx.state}get audioCtx(){return this._audioCtx}get duration(){var t,s,e;return null!=(t=null==(s=this._states.source)||null==(e=s.buffer)?void 0:e.duration)?t:0}get currentTime(){return this._states.hasStarted?this._states.isPlaying?this._pauseTime+(this._audioCtx.currentTime-this._startTime):this._pauseTime:0}get isPlaying(){return this._states.isPlaying}seek(t){var s;if(this._isDestroyed)return;const e=this.duration>0?Math.max(0,Math.min(t,this.duration)):Math.max(0,t);if(!this._states.isDecoded||null==(s=this._states.source)||!s.buffer)return void(this._pendingSeekTime=e);this._pendingSeekTime=null;const i=Math.max(0,Math.min(e,this.duration)),a=this._states.source.buffer;if(this._states.isPlaying&&this._states.source){this._states.source.onended=null;try{this._states.source.stop(0)}catch(t){console.error("Error stopping audio source:",t)}this.recreateAndStart(i,a)}else this._pauseTime=i,this._hasSeekedWhilePaused=!0}destroy(){var t;if(!this._isDestroyed){if(this._states.source){this._states.source.onended=null;try{this._states.source.stop(0)}catch(t){}this._states.source.disconnect()}null==(t=this._states.gainNode)||t.disconnect(),this._eventHandler.dispose(),this._emitter.removeAllListeners(),this._states.source=null,this._states.gainNode=null,this._states.isPlaying=!1,this._states.hasStarted=!1,this._states.isDecoded=!1,this._isDestroyed=!0}}}var n=t=>new h(t);const u={volume:1,loop:!1,audio:null,isStopped:!1,isPlaying:!1,audioIndex:0};class l{constructor({files:s,volume:e=1,loop:a=!1,shuffle:o=!1,preload:r=!1,preloadLimit:h=3}){this.emiter=void 0,this.states=void 0,this.copiedFiles=void 0,this.shouldLoop=void 0,this.curryPlayAudio=void 0,this.isDestroyed=!1,this.emiter=new i,this.states=t({},u,{volume:e,loop:a});const l=!Array.isArray(s);this.shouldLoop=a||l;const d=l?(t=>Object.entries(t).flatMap(([t,s])=>Array(Math.floor(s)).fill(t)))(s):s;this.copiedFiles=o||l?(t=>{const s=t.slice();let e=t.length-1;for(;e>=0;){const t=Math.floor(Math.random()*e+1),i=s[e];s[e]=s[t],s[t]=i,e--}return s})(d):d.slice(),this.curryPlayAudio=((t,s)=>{const e=(i,a)=>{const o=n({file:i[t.audioIndex],volume:t.volume});t.audio=o,o.on("start",t=>{s.emit("start",t)}),o.on("end",()=>{t.isStopped||(i.length===t.audioIndex+1?(t.audio=null,t.audioIndex=0,t.loop?e(i):(s.emit("end",{data:null}),t.isPlaying=!1)):(t.audioIndex++,e(i)))}),o.play()};return e})(this.states,this.emiter),r&&((t,s,e=fetch,i)=>{const a=t.slice(s).reverse();let o=!1;const r=()=>{a.length?h(a.pop()):o||(null==i||i(),o=!0)},h=t=>{e(t).then(r).catch(r)};for(let e=0;e<s;e++)h(t[e])})(this.copiedFiles,h)}play(){if(this.isDestroyed)return;const{audio:t}=this.states;if(this.states.isPlaying=!0,!t||this.states.isStopped)return this.curryPlayAudio(this.copiedFiles,this.shouldLoop),void(this.states.isStopped=!1);t.play()}toggle(){this.isDestroyed||(this.states.isPlaying?this.pause():this.play())}pause(){var t;this.isDestroyed||(null==(t=this.states.audio)||t.pause(),this.states.isPlaying=!1)}stop(){var t;this.isDestroyed||(this.states.isPlaying=!1,this.states.isStopped=!0,null==(t=this.states.audio)||t.stop())}next(){var t;if(this.isDestroyed)return;this.states.audioIndex=this.states.audioIndex===this.copiedFiles.length-1?0:this.states.audioIndex+1,null==(t=this.states.audio)||t.pause();const s=n({file:this.copiedFiles[this.states.audioIndex],volume:this.states.volume});this.states.audio=s,s.play()}prev(){var t;if(this.isDestroyed)return;this.states.audioIndex=0===this.states.audioIndex?this.copiedFiles.length-1:this.states.audioIndex-1,null==(t=this.states.audio)||t.pause();const s=n({file:this.copiedFiles[this.states.audioIndex],volume:this.states.volume});this.states.audio=s,s.play()}on(t,s){this.isDestroyed||this.emiter.listener(t,s)}get volume(){return this.states.volume}set volume(t){this.isDestroyed||(this.states.volume=t,this.states.audio&&(this.states.audio.volume=t))}get loop(){return this.states.loop}set loop(t){this.isDestroyed||(this.states.loop=t)}get audioCtx(){var t;return null==(t=this.states.audio)?void 0:t.audioCtx}destroy(){var t;this.isDestroyed||(this.stop(),null==(t=this.states.audio)||t.destroy(),this.emiter.removeAllListeners(),this.states.audio=null,this.copiedFiles=[],this.isDestroyed=!0)}}var d=t=>new l(t);export{n as Audio,d as AudioPlaylist}; //# sourceMappingURL=index.modern.js.map