@cher-ami/audio-manager
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -1,2 +0,2 @@ | ||
export { AudioManager } from './AudioManager'; | ||
export { AudioManager, MUTE_AUDIO_SIGNAL } from './AudioManager'; | ||
export { useAudio, useMuteAllAudio } from './hooks'; |
@@ -1,2 +0,2 @@ | ||
import{StateSignal as i}from"@zouloux/signal";import{deferredPromise as t}from"@wbe/deferred-promise";import s from"@wbe/debug";import{gsap as o}from"gsap";import{useMemo as e,useState as a,useEffect as n}from"react";function d(){return d=Object.assign?Object.assign.bind():function(i){for(var t=1;t<arguments.length;t++){var s=arguments[t];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(i[o]=s[o])}return i},d.apply(this,arguments)}const h=s("AudioManager"),l=i(!1);let u=0;class r{constructor(i,s={}){var o,e;this.audioFileUrl=void 0,this.options=void 0,this.audioCtx=void 0,this.panner=void 0,this.listener=void 0,this.$audio=void 0,this.track=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=void 0,this.canplayPromise=void 0,this.handleCanplay=()=>{h(this.id,"canplay handler, audio is ready"),this.canplayPromise.resolve(),this.isLoading=!1,this.isLoaded=!0},this.handleEnded=()=>{h(this.id,"ended"),this.isPlaying=!1,this.options.loop&&this.play()},this.handleMuteAll=i=>{i?this.mute():this.unmute()},this._volumeIsInProcess=void 0,this.audioFileUrl=i,this.options=d({},{volume:1,loop:!1},s),u++,this.id=[u+".",(null==(o=this.options)?void 0:o.id)&&`${null==(e=this.options)?void 0:e.id}__`,i.split("/")[i.split("/").length-1]," - "].filter(i=>i).join(""),h(this.id,"options",this.options),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=t(),this.load(),this.initEvent()}load(){const i=window.AudioContext||window.webkitAudioContext;this.audioCtx=new i,this.panner=new StereoPannerNode(this.audioCtx,{pan:0}),this.$audio=new Audio(this.audioFileUrl),this.$audio.crossOrigin="anonymous",this.$audio.volume=this.options.volume,this.track=this.audioCtx.createMediaElementSource(this.$audio),this.track.connect(this.panner).connect(this.audioCtx.destination)}initEvent(){this.$audio&&(this.$audio.addEventListener("canplay",this.handleCanplay),this.$audio.addEventListener("ended",this.handleEnded),l.add(this.handleMuteAll),this.$audio.load())}async play(){h(this.id,"play",this.options),await this.canplayPromise.promise,"suspended"===this.audioCtx.state&&this.audioCtx.resume(),this.isPlaying?h(this.id,"play > is already playIn, return"):(this.$audio.play(),this.isPlaying=!0)}pause(){this.isPlaying&&(this.$audio.pause(),this.isPlaying=!1)}stop(){h(this.id,"stop"),this.$audio.pause(),this.$audio.currentTime=0,this.isPlaying=!1}replay(){h(this.id,"replay"),this.stop(),this.play()}mute(){h(this.id,"mute",this.$audio.volume),this.isMuted||(this.$audio.volume=0,this.isMuted=!0)}unmute(){h(this.id,"unmute",this.$audio.volume),this.isMuted&&(this.$audio.volume=this.options.volume,this.isMuted=!1)}enableLoop(){h(this.id,"loop"),this.options.loop=!0}disableLoop(){h(this.id,"disable loop"),this.options.loop=!1}pan(i){h(this.id,"pan",i),this.panner.pan.value=i}async fade(i,t,s=1,o="none"){h(this.id,"fade >",i,t,this.options),this.isPlaying||this.play(),await this.processVolume(i,t,s,o),h(this.id,"fade ended!",this.$audio.volume)}async fadeIn(i=1,t="none"){h(this.id,"fadeIn"),this.play(),await this.processVolume(0,this.options.volume,i,t),h(this.id,"fadeIn ended!")}async fadeOut(i=1,t="none"){h(this.id,"fadeOut"),await this.processVolume(this.options.volume,0,i,t),h(this.id,"fadeOut ended!")}destroy(){var i,t,s;h(this.id,"destroy"),this.pause(),null==(i=this.track)||i.disconnect(),this.$audio=null,null==(t=this.$audio)||t.removeEventListener("canplay",this.handleCanplay),null==(s=this.$audio)||s.removeEventListener("ended",this.handleEnded),l.remove(this.handleMuteAll)}processVolume(i,t,s=1,e="none"){const a=Math.max(0,Math.min(i,1)),n=Math.max(0,Math.min(t,1));return new Promise(i=>{o.fromTo(this.$audio,{volume:this._volumeIsInProcess?this.$audio.volume:a},{volume:n,overwrite:!0,ease:e,duration:s,onUpdate:()=>{this._volumeIsInProcess=!0,h(this.id,"this.$audio.volume",this.$audio.volume)},onComplete:()=>{this._volumeIsInProcess=!1,i()}})})}}s("AudioManager:hooks");const p={},m=(i,t,s=[])=>{const o=e(()=>[null!=t&&t.id?`__${t.id}__`:null,i.split("/")[i.split("/").length-1].replaceAll(" ","/")].filter(i=>i).join(""),[i,t]),[d]=a(()=>{var s;return null!=(s=null==p?void 0:p[o])?s:new r(i,t)});return n(()=>{p[o]||(p[o]=d)},s),d},v=()=>{const[i,t]=a(l.state);return n(()=>l.add(i=>{t(i)}),[]),[i,i=>{l.dispatch(i)}]};export{r as AudioManager,m as useAudio,v as useMuteAllAudio}; | ||
import{StateSignal as i}from"@zouloux/signal";import{deferredPromise as t}from"@wbe/deferred-promise";import s from"@wbe/debug";import{gsap as o}from"gsap";import{useMemo as e,useState as a,useEffect as n}from"react";function d(){return d=Object.assign?Object.assign.bind():function(i){for(var t=1;t<arguments.length;t++){var s=arguments[t];for(var o in s)Object.prototype.hasOwnProperty.call(s,o)&&(i[o]=s[o])}return i},d.apply(this,arguments)}const h=s("AudioManager"),l=i(!1);let u=0;class r{constructor(i,s={}){var o,e;this.audioFileUrl=void 0,this.options=void 0,this.audioCtx=void 0,this.panner=void 0,this.listener=void 0,this.$audio=void 0,this.track=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=void 0,this.canplayPromise=void 0,this.handleCanplay=()=>{h(this.id,"canplay handler, audio is ready"),this.canplayPromise.resolve(),this.isLoading=!1,this.isLoaded=!0},this.handleEnded=()=>{h(this.id,"ended"),this.isPlaying=!1,this.options.loop&&this.play()},this.handleMuteAll=i=>{i?this.mute():this.unmute()},this._volumeIsInProcess=void 0,this.audioFileUrl=i,this.options=d({},{volume:1,loop:!1},s),u++,this.id=[u+".",(null==(o=this.options)?void 0:o.id)&&`${null==(e=this.options)?void 0:e.id}__`,i.split("/")[i.split("/").length-1]," - "].filter(i=>i).join(""),h(this.id,"options",this.options),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=t(),this.load(),this.initEvent()}load(){const i=window.AudioContext||window.webkitAudioContext;this.audioCtx=new i,this.panner=new StereoPannerNode(this.audioCtx,{pan:0}),this.$audio=new Audio(this.audioFileUrl),this.$audio.crossOrigin="anonymous",this.$audio.volume=this.options.volume,this.track=this.audioCtx.createMediaElementSource(this.$audio),this.track.connect(this.panner).connect(this.audioCtx.destination)}initEvent(){this.$audio&&(this.$audio.addEventListener("canplay",this.handleCanplay),this.$audio.addEventListener("ended",this.handleEnded),l.add(this.handleMuteAll),this.$audio.load())}async play(){h(this.id,"play",this.options),await this.canplayPromise.promise,"suspended"===this.audioCtx.state&&this.audioCtx.resume(),this.isPlaying?h(this.id,"play > is already playIn, return"):(this.$audio.play(),this.isPlaying=!0)}pause(){this.isPlaying&&(this.$audio.pause(),this.isPlaying=!1)}stop(){h(this.id,"stop"),this.$audio.pause(),this.$audio.currentTime=0,this.isPlaying=!1}replay(){h(this.id,"replay"),this.stop(),this.play()}mute(){h(this.id,"mute",this.$audio.volume),this.isMuted||(this.$audio.volume=0,this.isMuted=!0)}unmute(){h(this.id,"unmute",this.$audio.volume),this.isMuted&&(this.$audio.volume=this.options.volume,this.isMuted=!1)}enableLoop(){h(this.id,"loop"),this.options.loop=!0}disableLoop(){h(this.id,"disable loop"),this.options.loop=!1}pan(i){h(this.id,"pan",i),this.panner.pan.value=i}async fade(i,t,s=1,o="none"){h(this.id,"fade >",i,t,this.options),this.isPlaying||this.play(),await this.processVolume(i,t,s,o),h(this.id,"fade ended!",this.$audio.volume)}async fadeIn(i=1,t="none"){h(this.id,"fadeIn"),this.play(),await this.processVolume(0,this.options.volume,i,t),h(this.id,"fadeIn ended!")}async fadeOut(i=1,t="none"){h(this.id,"fadeOut"),await this.processVolume(this.options.volume,0,i,t),h(this.id,"fadeOut ended!")}destroy(){var i,t,s;h(this.id,"destroy"),this.pause(),null==(i=this.track)||i.disconnect(),this.$audio=null,null==(t=this.$audio)||t.removeEventListener("canplay",this.handleCanplay),null==(s=this.$audio)||s.removeEventListener("ended",this.handleEnded),l.remove(this.handleMuteAll)}processVolume(i,t,s=1,e="none"){const a=Math.max(0,Math.min(i,1)),n=Math.max(0,Math.min(t,1));return new Promise(i=>{o.fromTo(this.$audio,{volume:this._volumeIsInProcess?this.$audio.volume:a},{volume:n,overwrite:!0,ease:e,duration:s,onUpdate:()=>{this._volumeIsInProcess=!0,h(this.id,"this.$audio.volume",this.$audio.volume)},onComplete:()=>{this._volumeIsInProcess=!1,i()}})})}}s("AudioManager:hooks");const p={},m=(i,t,s=[])=>{const o=e(()=>[null!=t&&t.id?`__${t.id}__`:null,i.split("/")[i.split("/").length-1].replaceAll(" ","/")].filter(i=>i).join(""),[i,t]),[d]=a(()=>{var s;return null!=(s=null==p?void 0:p[o])?s:new r(i,t)});return n(()=>{p[o]||(p[o]=d)},s),d},v=()=>{const[i,t]=a(l.state);return n(()=>l.add(i=>{t(i)}),[]),[i,i=>{l.dispatch(i)}]};export{r as AudioManager,l as MUTE_AUDIO_SIGNAL,m as useAudio,v as useMuteAllAudio}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
import{StateSignal as i}from"@zouloux/signal";import{deferredPromise as t}from"@wbe/deferred-promise";import o from"@wbe/debug";import{gsap as n}from"gsap";import{useMemo as e,useState as s,useEffect as a}from"react";function d(){return d=Object.assign?Object.assign.bind():function(i){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(i[n]=o[n])}return i},d.apply(this,arguments)}var u=o("AudioManager"),r=i(!1),l=0,h=/*#__PURE__*/function(){function i(i,o){var n,e,s=this;void 0===o&&(o={}),this.audioFileUrl=void 0,this.options=void 0,this.audioCtx=void 0,this.panner=void 0,this.listener=void 0,this.$audio=void 0,this.track=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=void 0,this.canplayPromise=void 0,this.handleCanplay=function(){u(s.id,"canplay handler, audio is ready"),s.canplayPromise.resolve(),s.isLoading=!1,s.isLoaded=!0},this.handleEnded=function(){u(s.id,"ended"),s.isPlaying=!1,s.options.loop&&s.play()},this.handleMuteAll=function(i){i?s.mute():s.unmute()},this._volumeIsInProcess=void 0,this.audioFileUrl=i,this.options=d({},{volume:1,loop:!1},o),l++,this.id=[l+".",(null==(n=this.options)?void 0:n.id)&&(null==(e=this.options)?void 0:e.id)+"__",i.split("/")[i.split("/").length-1]," - "].filter(function(i){return i}).join(""),u(this.id,"options",this.options),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=t(),this.load(),this.initEvent()}var o=i.prototype;return o.load=function(){var i=window.AudioContext||window.webkitAudioContext;this.audioCtx=new i,this.panner=new StereoPannerNode(this.audioCtx,{pan:0}),this.$audio=new Audio(this.audioFileUrl),this.$audio.crossOrigin="anonymous",this.$audio.volume=this.options.volume,this.track=this.audioCtx.createMediaElementSource(this.$audio),this.track.connect(this.panner).connect(this.audioCtx.destination)},o.initEvent=function(){this.$audio&&(this.$audio.addEventListener("canplay",this.handleCanplay),this.$audio.addEventListener("ended",this.handleEnded),r.add(this.handleMuteAll),this.$audio.load())},o.play=function(){try{var i=this;return u(i.id,"play",i.options),Promise.resolve(i.canplayPromise.promise).then(function(){"suspended"===i.audioCtx.state&&i.audioCtx.resume(),i.isPlaying?u(i.id,"play > is already playIn, return"):(i.$audio.play(),i.isPlaying=!0)})}catch(i){return Promise.reject(i)}},o.pause=function(){this.isPlaying&&(this.$audio.pause(),this.isPlaying=!1)},o.stop=function(){u(this.id,"stop"),this.$audio.pause(),this.$audio.currentTime=0,this.isPlaying=!1},o.replay=function(){u(this.id,"replay"),this.stop(),this.play()},o.mute=function(){u(this.id,"mute",this.$audio.volume),this.isMuted||(this.$audio.volume=0,this.isMuted=!0)},o.unmute=function(){u(this.id,"unmute",this.$audio.volume),this.isMuted&&(this.$audio.volume=this.options.volume,this.isMuted=!1)},o.enableLoop=function(){u(this.id,"loop"),this.options.loop=!0},o.disableLoop=function(){u(this.id,"disable loop"),this.options.loop=!1},o.pan=function(i){u(this.id,"pan",i),this.panner.pan.value=i},o.fade=function(i,t,o,n){void 0===o&&(o=1),void 0===n&&(n="none");try{var e=this;return u(e.id,"fade >",i,t,e.options),e.isPlaying||e.play(),Promise.resolve(e.processVolume(i,t,o,n)).then(function(){u(e.id,"fade ended!",e.$audio.volume)})}catch(i){return Promise.reject(i)}},o.fadeIn=function(i,t){void 0===i&&(i=1),void 0===t&&(t="none");try{var o=this;return u(o.id,"fadeIn"),o.play(),Promise.resolve(o.processVolume(0,o.options.volume,i,t)).then(function(){u(o.id,"fadeIn ended!")})}catch(i){return Promise.reject(i)}},o.fadeOut=function(i,t){void 0===i&&(i=1),void 0===t&&(t="none");try{var o=this;return u(o.id,"fadeOut"),Promise.resolve(o.processVolume(o.options.volume,0,i,t)).then(function(){u(o.id,"fadeOut ended!")})}catch(i){return Promise.reject(i)}},o.destroy=function(){var i,t,o;u(this.id,"destroy"),this.pause(),null==(i=this.track)||i.disconnect(),this.$audio=null,null==(t=this.$audio)||t.removeEventListener("canplay",this.handleCanplay),null==(o=this.$audio)||o.removeEventListener("ended",this.handleEnded),r.remove(this.handleMuteAll)},o.processVolume=function(i,t,o,e){var s=this;void 0===o&&(o=1),void 0===e&&(e="none");var a=Math.max(0,Math.min(i,1)),d=Math.max(0,Math.min(t,1));return new Promise(function(i){n.fromTo(s.$audio,{volume:s._volumeIsInProcess?s.$audio.volume:a},{volume:d,overwrite:!0,ease:e,duration:o,onUpdate:function(){s._volumeIsInProcess=!0,u(s.id,"this.$audio.volume",s.$audio.volume)},onComplete:function(){s._volumeIsInProcess=!1,i()}})})},i}();o("AudioManager:hooks");var c={},p=function(i,t,o){void 0===o&&(o=[]);var n=e(function(){return[null!=t&&t.id?"__"+t.id+"__":null,i.split("/")[i.split("/").length-1].replaceAll(" ","/")].filter(function(i){return i}).join("")},[i,t]),d=s(function(){var o;return null!=(o=null==c?void 0:c[n])?o:new h(i,t)})[0];return a(function(){c[n]||(c[n]=d)},o),d},v=function(){var i=s(r.state),t=i[0],o=i[1];return a(function(){return r.add(function(i){o(i)})},[]),[t,function(i){r.dispatch(i)}]};export{h as AudioManager,p as useAudio,v as useMuteAllAudio}; | ||
import{StateSignal as i}from"@zouloux/signal";import{deferredPromise as t}from"@wbe/deferred-promise";import o from"@wbe/debug";import{gsap as n}from"gsap";import{useMemo as e,useState as s,useEffect as a}from"react";function d(){return d=Object.assign?Object.assign.bind():function(i){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(i[n]=o[n])}return i},d.apply(this,arguments)}var u=o("AudioManager"),r=i(!1),l=0,h=/*#__PURE__*/function(){function i(i,o){var n,e,s=this;void 0===o&&(o={}),this.audioFileUrl=void 0,this.options=void 0,this.audioCtx=void 0,this.panner=void 0,this.listener=void 0,this.$audio=void 0,this.track=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=void 0,this.canplayPromise=void 0,this.handleCanplay=function(){u(s.id,"canplay handler, audio is ready"),s.canplayPromise.resolve(),s.isLoading=!1,s.isLoaded=!0},this.handleEnded=function(){u(s.id,"ended"),s.isPlaying=!1,s.options.loop&&s.play()},this.handleMuteAll=function(i){i?s.mute():s.unmute()},this._volumeIsInProcess=void 0,this.audioFileUrl=i,this.options=d({},{volume:1,loop:!1},o),l++,this.id=[l+".",(null==(n=this.options)?void 0:n.id)&&(null==(e=this.options)?void 0:e.id)+"__",i.split("/")[i.split("/").length-1]," - "].filter(function(i){return i}).join(""),u(this.id,"options",this.options),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=t(),this.load(),this.initEvent()}var o=i.prototype;return o.load=function(){var i=window.AudioContext||window.webkitAudioContext;this.audioCtx=new i,this.panner=new StereoPannerNode(this.audioCtx,{pan:0}),this.$audio=new Audio(this.audioFileUrl),this.$audio.crossOrigin="anonymous",this.$audio.volume=this.options.volume,this.track=this.audioCtx.createMediaElementSource(this.$audio),this.track.connect(this.panner).connect(this.audioCtx.destination)},o.initEvent=function(){this.$audio&&(this.$audio.addEventListener("canplay",this.handleCanplay),this.$audio.addEventListener("ended",this.handleEnded),r.add(this.handleMuteAll),this.$audio.load())},o.play=function(){try{var i=this;return u(i.id,"play",i.options),Promise.resolve(i.canplayPromise.promise).then(function(){"suspended"===i.audioCtx.state&&i.audioCtx.resume(),i.isPlaying?u(i.id,"play > is already playIn, return"):(i.$audio.play(),i.isPlaying=!0)})}catch(i){return Promise.reject(i)}},o.pause=function(){this.isPlaying&&(this.$audio.pause(),this.isPlaying=!1)},o.stop=function(){u(this.id,"stop"),this.$audio.pause(),this.$audio.currentTime=0,this.isPlaying=!1},o.replay=function(){u(this.id,"replay"),this.stop(),this.play()},o.mute=function(){u(this.id,"mute",this.$audio.volume),this.isMuted||(this.$audio.volume=0,this.isMuted=!0)},o.unmute=function(){u(this.id,"unmute",this.$audio.volume),this.isMuted&&(this.$audio.volume=this.options.volume,this.isMuted=!1)},o.enableLoop=function(){u(this.id,"loop"),this.options.loop=!0},o.disableLoop=function(){u(this.id,"disable loop"),this.options.loop=!1},o.pan=function(i){u(this.id,"pan",i),this.panner.pan.value=i},o.fade=function(i,t,o,n){void 0===o&&(o=1),void 0===n&&(n="none");try{var e=this;return u(e.id,"fade >",i,t,e.options),e.isPlaying||e.play(),Promise.resolve(e.processVolume(i,t,o,n)).then(function(){u(e.id,"fade ended!",e.$audio.volume)})}catch(i){return Promise.reject(i)}},o.fadeIn=function(i,t){void 0===i&&(i=1),void 0===t&&(t="none");try{var o=this;return u(o.id,"fadeIn"),o.play(),Promise.resolve(o.processVolume(0,o.options.volume,i,t)).then(function(){u(o.id,"fadeIn ended!")})}catch(i){return Promise.reject(i)}},o.fadeOut=function(i,t){void 0===i&&(i=1),void 0===t&&(t="none");try{var o=this;return u(o.id,"fadeOut"),Promise.resolve(o.processVolume(o.options.volume,0,i,t)).then(function(){u(o.id,"fadeOut ended!")})}catch(i){return Promise.reject(i)}},o.destroy=function(){var i,t,o;u(this.id,"destroy"),this.pause(),null==(i=this.track)||i.disconnect(),this.$audio=null,null==(t=this.$audio)||t.removeEventListener("canplay",this.handleCanplay),null==(o=this.$audio)||o.removeEventListener("ended",this.handleEnded),r.remove(this.handleMuteAll)},o.processVolume=function(i,t,o,e){var s=this;void 0===o&&(o=1),void 0===e&&(e="none");var a=Math.max(0,Math.min(i,1)),d=Math.max(0,Math.min(t,1));return new Promise(function(i){n.fromTo(s.$audio,{volume:s._volumeIsInProcess?s.$audio.volume:a},{volume:d,overwrite:!0,ease:e,duration:o,onUpdate:function(){s._volumeIsInProcess=!0,u(s.id,"this.$audio.volume",s.$audio.volume)},onComplete:function(){s._volumeIsInProcess=!1,i()}})})},i}();o("AudioManager:hooks");var c={},p=function(i,t,o){void 0===o&&(o=[]);var n=e(function(){return[null!=t&&t.id?"__"+t.id+"__":null,i.split("/")[i.split("/").length-1].replaceAll(" ","/")].filter(function(i){return i}).join("")},[i,t]),d=s(function(){var o;return null!=(o=null==c?void 0:c[n])?o:new h(i,t)})[0];return a(function(){c[n]||(c[n]=d)},o),d},v=function(){var i=s(r.state),t=i[0],o=i[1];return a(function(){return r.add(function(i){o(i)})},[]),[t,function(i){r.dispatch(i)}]};export{h as AudioManager,r as MUTE_AUDIO_SIGNAL,p as useAudio,v as useMuteAllAudio}; | ||
//# sourceMappingURL=index.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(i,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@zouloux/signal"),require("@wbe/deferred-promise"),require("@wbe/debug"),require("gsap"),require("react")):"function"==typeof define&&define.amd?define(["exports","@zouloux/signal","@wbe/deferred-promise","@wbe/debug","gsap","react"],e):e((i||self).audioManager={},i.signal,i.deferredPromise,i.debug,i.gsap,i.react)}(this,function(i,e,t,o,n,s){function a(i){return i&&"object"==typeof i&&"default"in i?i:{default:i}}var d=/*#__PURE__*/a(o);function u(){return u=Object.assign?Object.assign.bind():function(i){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(i[o]=t[o])}return i},u.apply(this,arguments)}var r=d.default("AudioManager"),l=e.StateSignal(!1),h=0,c=/*#__PURE__*/function(){function i(i,e){var o,n,s=this;void 0===e&&(e={}),this.audioFileUrl=void 0,this.options=void 0,this.audioCtx=void 0,this.panner=void 0,this.listener=void 0,this.$audio=void 0,this.track=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=void 0,this.canplayPromise=void 0,this.handleCanplay=function(){r(s.id,"canplay handler, audio is ready"),s.canplayPromise.resolve(),s.isLoading=!1,s.isLoaded=!0},this.handleEnded=function(){r(s.id,"ended"),s.isPlaying=!1,s.options.loop&&s.play()},this.handleMuteAll=function(i){i?s.mute():s.unmute()},this._volumeIsInProcess=void 0,this.audioFileUrl=i,this.options=u({},{volume:1,loop:!1},e),h++,this.id=[h+".",(null==(o=this.options)?void 0:o.id)&&(null==(n=this.options)?void 0:n.id)+"__",i.split("/")[i.split("/").length-1]," - "].filter(function(i){return i}).join(""),r(this.id,"options",this.options),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=t.deferredPromise(),this.load(),this.initEvent()}var e=i.prototype;return e.load=function(){var i=window.AudioContext||window.webkitAudioContext;this.audioCtx=new i,this.panner=new StereoPannerNode(this.audioCtx,{pan:0}),this.$audio=new Audio(this.audioFileUrl),this.$audio.crossOrigin="anonymous",this.$audio.volume=this.options.volume,this.track=this.audioCtx.createMediaElementSource(this.$audio),this.track.connect(this.panner).connect(this.audioCtx.destination)},e.initEvent=function(){this.$audio&&(this.$audio.addEventListener("canplay",this.handleCanplay),this.$audio.addEventListener("ended",this.handleEnded),l.add(this.handleMuteAll),this.$audio.load())},e.play=function(){try{var i=this;return r(i.id,"play",i.options),Promise.resolve(i.canplayPromise.promise).then(function(){"suspended"===i.audioCtx.state&&i.audioCtx.resume(),i.isPlaying?r(i.id,"play > is already playIn, return"):(i.$audio.play(),i.isPlaying=!0)})}catch(i){return Promise.reject(i)}},e.pause=function(){this.isPlaying&&(this.$audio.pause(),this.isPlaying=!1)},e.stop=function(){r(this.id,"stop"),this.$audio.pause(),this.$audio.currentTime=0,this.isPlaying=!1},e.replay=function(){r(this.id,"replay"),this.stop(),this.play()},e.mute=function(){r(this.id,"mute",this.$audio.volume),this.isMuted||(this.$audio.volume=0,this.isMuted=!0)},e.unmute=function(){r(this.id,"unmute",this.$audio.volume),this.isMuted&&(this.$audio.volume=this.options.volume,this.isMuted=!1)},e.enableLoop=function(){r(this.id,"loop"),this.options.loop=!0},e.disableLoop=function(){r(this.id,"disable loop"),this.options.loop=!1},e.pan=function(i){r(this.id,"pan",i),this.panner.pan.value=i},e.fade=function(i,e,t,o){void 0===t&&(t=1),void 0===o&&(o="none");try{var n=this;return r(n.id,"fade >",i,e,n.options),n.isPlaying||n.play(),Promise.resolve(n.processVolume(i,e,t,o)).then(function(){r(n.id,"fade ended!",n.$audio.volume)})}catch(i){return Promise.reject(i)}},e.fadeIn=function(i,e){void 0===i&&(i=1),void 0===e&&(e="none");try{var t=this;return r(t.id,"fadeIn"),t.play(),Promise.resolve(t.processVolume(0,t.options.volume,i,e)).then(function(){r(t.id,"fadeIn ended!")})}catch(i){return Promise.reject(i)}},e.fadeOut=function(i,e){void 0===i&&(i=1),void 0===e&&(e="none");try{var t=this;return r(t.id,"fadeOut"),Promise.resolve(t.processVolume(t.options.volume,0,i,e)).then(function(){r(t.id,"fadeOut ended!")})}catch(i){return Promise.reject(i)}},e.destroy=function(){var i,e,t;r(this.id,"destroy"),this.pause(),null==(i=this.track)||i.disconnect(),this.$audio=null,null==(e=this.$audio)||e.removeEventListener("canplay",this.handleCanplay),null==(t=this.$audio)||t.removeEventListener("ended",this.handleEnded),l.remove(this.handleMuteAll)},e.processVolume=function(i,e,t,o){var s=this;void 0===t&&(t=1),void 0===o&&(o="none");var a=Math.max(0,Math.min(i,1)),d=Math.max(0,Math.min(e,1));return new Promise(function(i){n.gsap.fromTo(s.$audio,{volume:s._volumeIsInProcess?s.$audio.volume:a},{volume:d,overwrite:!0,ease:o,duration:t,onUpdate:function(){s._volumeIsInProcess=!0,r(s.id,"this.$audio.volume",s.$audio.volume)},onComplete:function(){s._volumeIsInProcess=!1,i()}})})},i}();d.default("AudioManager:hooks");var p={};i.AudioManager=c,i.useAudio=function(i,e,t){void 0===t&&(t=[]);var o=s.useMemo(function(){return[null!=e&&e.id?"__"+e.id+"__":null,i.split("/")[i.split("/").length-1].replaceAll(" ","/")].filter(function(i){return i}).join("")},[i,e]),n=s.useState(function(){var t;return null!=(t=null==p?void 0:p[o])?t:new c(i,e)})[0];return s.useEffect(function(){p[o]||(p[o]=n)},t),n},i.useMuteAllAudio=function(){var i=s.useState(l.state),e=i[0],t=i[1];return s.useEffect(function(){return l.add(function(i){t(i)})},[]),[e,function(i){l.dispatch(i)}]}}); | ||
!function(i,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@zouloux/signal"),require("@wbe/deferred-promise"),require("@wbe/debug"),require("gsap"),require("react")):"function"==typeof define&&define.amd?define(["exports","@zouloux/signal","@wbe/deferred-promise","@wbe/debug","gsap","react"],e):e((i||self).audioManager={},i.signal,i.deferredPromise,i.debug,i.gsap,i.react)}(this,function(i,e,t,o,n,s){function a(i){return i&&"object"==typeof i&&"default"in i?i:{default:i}}var d=/*#__PURE__*/a(o);function u(){return u=Object.assign?Object.assign.bind():function(i){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(i[o]=t[o])}return i},u.apply(this,arguments)}var r=d.default("AudioManager"),l=e.StateSignal(!1),h=0,c=/*#__PURE__*/function(){function i(i,e){var o,n,s=this;void 0===e&&(e={}),this.audioFileUrl=void 0,this.options=void 0,this.audioCtx=void 0,this.panner=void 0,this.listener=void 0,this.$audio=void 0,this.track=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=void 0,this.canplayPromise=void 0,this.handleCanplay=function(){r(s.id,"canplay handler, audio is ready"),s.canplayPromise.resolve(),s.isLoading=!1,s.isLoaded=!0},this.handleEnded=function(){r(s.id,"ended"),s.isPlaying=!1,s.options.loop&&s.play()},this.handleMuteAll=function(i){i?s.mute():s.unmute()},this._volumeIsInProcess=void 0,this.audioFileUrl=i,this.options=u({},{volume:1,loop:!1},e),h++,this.id=[h+".",(null==(o=this.options)?void 0:o.id)&&(null==(n=this.options)?void 0:n.id)+"__",i.split("/")[i.split("/").length-1]," - "].filter(function(i){return i}).join(""),r(this.id,"options",this.options),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=t.deferredPromise(),this.load(),this.initEvent()}var e=i.prototype;return e.load=function(){var i=window.AudioContext||window.webkitAudioContext;this.audioCtx=new i,this.panner=new StereoPannerNode(this.audioCtx,{pan:0}),this.$audio=new Audio(this.audioFileUrl),this.$audio.crossOrigin="anonymous",this.$audio.volume=this.options.volume,this.track=this.audioCtx.createMediaElementSource(this.$audio),this.track.connect(this.panner).connect(this.audioCtx.destination)},e.initEvent=function(){this.$audio&&(this.$audio.addEventListener("canplay",this.handleCanplay),this.$audio.addEventListener("ended",this.handleEnded),l.add(this.handleMuteAll),this.$audio.load())},e.play=function(){try{var i=this;return r(i.id,"play",i.options),Promise.resolve(i.canplayPromise.promise).then(function(){"suspended"===i.audioCtx.state&&i.audioCtx.resume(),i.isPlaying?r(i.id,"play > is already playIn, return"):(i.$audio.play(),i.isPlaying=!0)})}catch(i){return Promise.reject(i)}},e.pause=function(){this.isPlaying&&(this.$audio.pause(),this.isPlaying=!1)},e.stop=function(){r(this.id,"stop"),this.$audio.pause(),this.$audio.currentTime=0,this.isPlaying=!1},e.replay=function(){r(this.id,"replay"),this.stop(),this.play()},e.mute=function(){r(this.id,"mute",this.$audio.volume),this.isMuted||(this.$audio.volume=0,this.isMuted=!0)},e.unmute=function(){r(this.id,"unmute",this.$audio.volume),this.isMuted&&(this.$audio.volume=this.options.volume,this.isMuted=!1)},e.enableLoop=function(){r(this.id,"loop"),this.options.loop=!0},e.disableLoop=function(){r(this.id,"disable loop"),this.options.loop=!1},e.pan=function(i){r(this.id,"pan",i),this.panner.pan.value=i},e.fade=function(i,e,t,o){void 0===t&&(t=1),void 0===o&&(o="none");try{var n=this;return r(n.id,"fade >",i,e,n.options),n.isPlaying||n.play(),Promise.resolve(n.processVolume(i,e,t,o)).then(function(){r(n.id,"fade ended!",n.$audio.volume)})}catch(i){return Promise.reject(i)}},e.fadeIn=function(i,e){void 0===i&&(i=1),void 0===e&&(e="none");try{var t=this;return r(t.id,"fadeIn"),t.play(),Promise.resolve(t.processVolume(0,t.options.volume,i,e)).then(function(){r(t.id,"fadeIn ended!")})}catch(i){return Promise.reject(i)}},e.fadeOut=function(i,e){void 0===i&&(i=1),void 0===e&&(e="none");try{var t=this;return r(t.id,"fadeOut"),Promise.resolve(t.processVolume(t.options.volume,0,i,e)).then(function(){r(t.id,"fadeOut ended!")})}catch(i){return Promise.reject(i)}},e.destroy=function(){var i,e,t;r(this.id,"destroy"),this.pause(),null==(i=this.track)||i.disconnect(),this.$audio=null,null==(e=this.$audio)||e.removeEventListener("canplay",this.handleCanplay),null==(t=this.$audio)||t.removeEventListener("ended",this.handleEnded),l.remove(this.handleMuteAll)},e.processVolume=function(i,e,t,o){var s=this;void 0===t&&(t=1),void 0===o&&(o="none");var a=Math.max(0,Math.min(i,1)),d=Math.max(0,Math.min(e,1));return new Promise(function(i){n.gsap.fromTo(s.$audio,{volume:s._volumeIsInProcess?s.$audio.volume:a},{volume:d,overwrite:!0,ease:o,duration:t,onUpdate:function(){s._volumeIsInProcess=!0,r(s.id,"this.$audio.volume",s.$audio.volume)},onComplete:function(){s._volumeIsInProcess=!1,i()}})})},i}();d.default("AudioManager:hooks");var p={};i.AudioManager=c,i.MUTE_AUDIO_SIGNAL=l,i.useAudio=function(i,e,t){void 0===t&&(t=[]);var o=s.useMemo(function(){return[null!=e&&e.id?"__"+e.id+"__":null,i.split("/")[i.split("/").length-1].replaceAll(" ","/")].filter(function(i){return i}).join("")},[i,e]),n=s.useState(function(){var t;return null!=(t=null==p?void 0:p[o])?t:new c(i,e)})[0];return s.useEffect(function(){p[o]||(p[o]=n)},t),n},i.useMuteAllAudio=function(){var i=s.useState(l.state),e=i[0],t=i[1];return s.useEffect(function(){return l.add(function(i){t(i)})},[]),[e,function(i){l.dispatch(i)}]}}); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "@cher-ami/audio-manager", | ||
"description": "A simple web audio manager", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"author": "cher-ami", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
93767