Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cher-ami/audio-manager

Package Overview
Dependencies
Maintainers
5
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cher-ami/audio-manager - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

8

dist/AudioManager.d.ts

@@ -18,2 +18,3 @@ import { TDeferredPromise } from "@wbe/deferred-promise";

delay?: number;
onUpdate?: (time: number) => void;
}

@@ -36,8 +37,10 @@ /**

id: string;
protected nameSpace: string;
canplayPromise: TDeferredPromise<void>;
endedPromise: TDeferredPromise<void>;
protected raf: number;
constructor(audioFileUrl: string, options?: IAudioManagerOptions);
protected getNameSpace(id: any, url: any): string;
protected load(): void;
protected initEvents(): void;
destroy(): void;
protected handleMuteAll: (mute: boolean) => void;

@@ -55,2 +58,5 @@ protected handleEnded: () => void;

fadeOut(duration?: number): Promise<void>;
destroy(): void;
protected rafRender(): number;
protected cancelRaf(): void;
}

2

dist/index.modern.js

@@ -1,2 +0,2 @@

import{StateSignal as i}from"@zouloux/signal";import{deferredPromise as s}from"@wbe/deferred-promise";import{Howl as t}from"howler";import e from"@wbe/debug";import{useMemo as o,useState as d,useEffect as n}from"react";function h(){return h=Object.assign?Object.assign.bind():function(i){for(var s=1;s<arguments.length;s++){var t=arguments[s];for(var e in t)Object.prototype.hasOwnProperty.call(t,e)&&(i[e]=t[e])}return i},h.apply(this,arguments)}const a=e("AudioManager"),l=i(!1);class r{constructor(i,t={}){this.url=void 0,this.options=void 0,this.sound=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=null,this.canplayPromise=void 0,this.endedPromise=void 0,this.handleMuteAll=i=>{i?this.mute():this.unmute()},this.handleEnded=()=>{a(this.id,"ended"),this.isPlaying=!1,this.endedPromise.resolve()},this.url=i,this.options=h({},{volume:1,autoplay:!1,loop:!1,preload:!0,html5:!1,delay:0},t),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=s(),this.endedPromise=s(),this.load(),this.initEvents()}load(){this.sound=new t(h({src:[this.url]},this.options,{onload:()=>{a(this.id,"canplay handler, audio is ready"),this.isLoaded=!0,this.canplayPromise.resolve(),this.isLoading=!1,this.isLoaded=!0,this.isMuted&&this.mute()}}))}initEvents(){l.add(this.handleMuteAll),this.sound.on("end",this.handleEnded)}destroy(){a(this.id,"destroy"),this.sound.unload(),l.remove(this.handleMuteAll)}async play(){return a(this.id,"waiting for canplayPromise..."),await this.canplayPromise.promise,this.endedPromise=s(),a(this.id,"play",this.options),await new Promise(i=>setTimeout(i,this.options.delay)),this.id=this.sound.play(),this.isPlaying=!0,this.endedPromise.promise}pause(){this.isPlaying&&this.isLoaded&&this.sound.pause()}async stop(){a(this.id,"stop"),this.sound.stop(this.id),this.isPlaying=!1}replay(){a(this.id,"replay"),this.stop(),this.play()}async loop(){this.sound.loop(!0),this.isPlaying=!0}mute(){a(this.id,"mute"),this.isMuted||(this.sound.mute(!0),this.isMuted=!0)}unmute(){a(this.id,"unmute"),this.isMuted&&(this.sound.mute(!1),this.isMuted=!1)}async fade(i,s,t=1e3){return a(this.id,"fade >",i,s,this.options),this.isPlaying||this.play(),this.sound.fade(i,s,t),new Promise(i=>setTimeout(i,t))}async fadeIn(i=1e3){return this.id=this.sound.play(),a(this.id,`fadeIn 0 -> ${this.options.volume}`),this.isPlaying=!0,this.sound.fade(0,this.options.volume,i),new Promise(s=>setTimeout(()=>{a(this.id,"fadeIn ended"),s()},i))}async fadeOut(i=1e3){return a(this.id,`fadeOut ${this.options.volume} -> 0`),this.sound.fade(this.options.volume,0,i),new Promise(s=>setTimeout(()=>{a(this.id,"fadeOut ended"),this.stop(),s()},i))}}e("AudioManager:hooks");const u={},p=(i,s,t=[])=>{const e=o(()=>[null!=s&&s.id?`__${s.id}__`:null,i.split("/")[i.split("/").length-1].replaceAll(" ","/")].filter(i=>i).join(""),[i,s]),[h]=d(()=>{var t;return null!=(t=null==u?void 0:u[e])?t:new r(i,s)});return n(()=>{u[e]||(u[e]=h)},t),h},m=()=>{const[i,s]=d(l.state);return n(()=>l.add(i=>{s(i)}),[]),[i,i=>{l.dispatch(i)}]};export{r as AudioManager,l as MUTE_AUDIO_SIGNAL,p as useAudio,m as useMuteAllAudio};
import{StateSignal as i}from"@zouloux/signal";import{deferredPromise as s}from"@wbe/deferred-promise";import{Howl as t}from"howler";import e from"@wbe/debug";import{useMemo as a,useState as n,useEffect as o}from"react";function h(){return h=Object.assign?Object.assign.bind():function(i){for(var s=1;s<arguments.length;s++){var t=arguments[s];for(var e in t)Object.prototype.hasOwnProperty.call(t,e)&&(i[e]=t[e])}return i},h.apply(this,arguments)}const d=e("AudioManager"),r=i(!1);class l{constructor(i,t={}){this.url=void 0,this.options=void 0,this.sound=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=null,this.nameSpace=void 0,this.canplayPromise=void 0,this.endedPromise=void 0,this.raf=void 0,this.handleMuteAll=i=>{i?this.mute():this.unmute()},this.handleEnded=()=>{d(this.nameSpace,"ended"),this.isPlaying=!1,this.endedPromise.resolve(),this.raf&&this.cancelRaf()},this.url=i,this.options=h({},{volume:1,autoplay:!1,loop:!1,preload:!0,html5:!1,delay:0,onUpdate:null},t),this.nameSpace=this.getNameSpace(this.id,this.url),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=s(),this.endedPromise=s(),this.load(),this.initEvents()}getNameSpace(i,s){return`${i} - ${s.replace(/^.*[\\\/]/,"")} -`}load(){this.sound=new t(h({src:[this.url]},this.options,{onload:()=>{d(this.nameSpace,"canplay handler, audio is ready"),this.isLoaded=!0,this.canplayPromise.resolve(),this.isLoading=!1,this.isLoaded=!0,this.isMuted&&this.mute()}}))}initEvents(){r.add(this.handleMuteAll),this.sound.on("end",this.handleEnded)}async play(){return d(this.nameSpace,"waiting for canplayPromise..."),await this.canplayPromise.promise,this.endedPromise=s(),d(this.nameSpace,"play",this.options),await new Promise(i=>setTimeout(i,this.options.delay)),this.id=this.sound.play(),this.nameSpace=this.getNameSpace(this.id,this.url),this.isPlaying=!0,this.options.onUpdate&&(this.raf=this.rafRender()),this.endedPromise.promise}pause(){this.isPlaying&&this.isLoaded&&(this.sound.pause(),this.raf&&this.cancelRaf())}async stop(){d(this.nameSpace,"stop"),this.sound.stop(this.id),this.isPlaying=!1,this.raf&&this.cancelRaf()}replay(){d(this.nameSpace,"replay"),this.stop(),this.play()}async loop(){this.sound.loop(!0),this.isPlaying=!0}mute(){d(this.nameSpace,"mute"),this.isMuted||(this.sound.mute(!0),this.isMuted=!0)}unmute(){d(this.nameSpace,"unmute"),this.isMuted&&(this.sound.mute(!1),this.isMuted=!1)}async fade(i,s,t=1e3){return d(this.nameSpace,"fade >",i,s,this.options),this.isPlaying||this.play(),this.sound.fade(i,s,t),new Promise(i=>setTimeout(i,t))}async fadeIn(i=1e3){return this.id=this.sound.play(),d(this.nameSpace,`fadeIn 0 -> ${this.options.volume}`),this.isPlaying=!0,this.sound.fade(0,this.options.volume,i),new Promise(s=>setTimeout(()=>{d(this.nameSpace,"fadeIn ended"),s()},i))}async fadeOut(i=1e3){return d(this.nameSpace,`fadeOut ${this.options.volume} -> 0`),this.sound.fade(this.options.volume,0,i),new Promise(s=>setTimeout(()=>{d(this.nameSpace,"fadeOut ended"),this.stop(),s()},i))}destroy(){d(this.nameSpace,"destroy"),this.sound.unload(),r.remove(this.handleMuteAll)}rafRender(){return requestAnimationFrame(i=>{var s,t;null==(s=(t=this.options).onUpdate)||s.call(t,i),this.raf=this.rafRender()})}cancelRaf(){cancelAnimationFrame(this.raf),this.raf=null}}e("AudioManager:hooks");const u={},p=(i,s,t=[])=>{const e=a(()=>[null!=s&&s.id?`__${s.id}__`:null,i.split("/")[i.split("/").length-1].replaceAll(" ","/")].filter(i=>i).join(""),[i,s]),[h]=n(()=>{var t;return null!=(t=null==u?void 0:u[e])?t:new l(i,s)});return o(()=>{u[e]||(u[e]=h)},t),h},m=()=>{const[i,s]=n(r.state);return o(()=>r.add(i=>{s(i)}),[]),[i,i=>{r.dispatch(i)}]};export{l as AudioManager,r as MUTE_AUDIO_SIGNAL,p as useAudio,m 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{Howl as e}from"howler";import n from"@wbe/debug";import{useMemo as o,useState as s,useEffect as r}from"react";function u(){return u=Object.assign?Object.assign.bind():function(i){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(i[n]=e[n])}return i},u.apply(this,arguments)}var d=n("AudioManager"),a=i(!1),l=/*#__PURE__*/function(){function i(i,e){var n=this;void 0===e&&(e={}),this.url=void 0,this.options=void 0,this.sound=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=null,this.canplayPromise=void 0,this.endedPromise=void 0,this.handleMuteAll=function(i){i?n.mute():n.unmute()},this.handleEnded=function(){d(n.id,"ended"),n.isPlaying=!1,n.endedPromise.resolve()},this.url=i,this.options=u({},{volume:1,autoplay:!1,loop:!1,preload:!0,html5:!1,delay:0},e),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=t(),this.endedPromise=t(),this.load(),this.initEvents()}var n=i.prototype;return n.load=function(){var i=this;this.sound=new e(u({src:[this.url]},this.options,{onload:function(){d(i.id,"canplay handler, audio is ready"),i.isLoaded=!0,i.canplayPromise.resolve(),i.isLoading=!1,i.isLoaded=!0,i.isMuted&&i.mute()}}))},n.initEvents=function(){a.add(this.handleMuteAll),this.sound.on("end",this.handleEnded)},n.destroy=function(){d(this.id,"destroy"),this.sound.unload(),a.remove(this.handleMuteAll)},n.play=function(){try{var i=this;return d(i.id,"waiting for canplayPromise..."),Promise.resolve(i.canplayPromise.promise).then(function(){return i.endedPromise=t(),d(i.id,"play",i.options),Promise.resolve(new Promise(function(t){return setTimeout(t,i.options.delay)})).then(function(){return i.id=i.sound.play(),i.isPlaying=!0,i.endedPromise.promise})})}catch(i){return Promise.reject(i)}},n.pause=function(){this.isPlaying&&this.isLoaded&&this.sound.pause()},n.stop=function(){try{var i=this;return d(i.id,"stop"),i.sound.stop(i.id),i.isPlaying=!1,Promise.resolve()}catch(i){return Promise.reject(i)}},n.replay=function(){d(this.id,"replay"),this.stop(),this.play()},n.loop=function(){try{return this.sound.loop(!0),this.isPlaying=!0,Promise.resolve()}catch(i){return Promise.reject(i)}},n.mute=function(){d(this.id,"mute"),this.isMuted||(this.sound.mute(!0),this.isMuted=!0)},n.unmute=function(){d(this.id,"unmute"),this.isMuted&&(this.sound.mute(!1),this.isMuted=!1)},n.fade=function(i,t,e){void 0===e&&(e=1e3);try{var n=this;return d(n.id,"fade >",i,t,n.options),n.isPlaying||n.play(),n.sound.fade(i,t,e),Promise.resolve(new Promise(function(i){return setTimeout(i,e)}))}catch(i){return Promise.reject(i)}},n.fadeIn=function(i){void 0===i&&(i=1e3);try{var t=this;return t.id=t.sound.play(),d(t.id,"fadeIn 0 -> "+t.options.volume),t.isPlaying=!0,t.sound.fade(0,t.options.volume,i),Promise.resolve(new Promise(function(e){return setTimeout(function(){d(t.id,"fadeIn ended"),e()},i)}))}catch(i){return Promise.reject(i)}},n.fadeOut=function(i){void 0===i&&(i=1e3);try{var t=this;return d(t.id,"fadeOut "+t.options.volume+" -> 0"),t.sound.fade(t.options.volume,0,i),Promise.resolve(new Promise(function(e){return setTimeout(function(){d(t.id,"fadeOut ended"),t.stop(),e()},i)}))}catch(i){return Promise.reject(i)}},i}();n("AudioManager:hooks");var h={},c=function(i,t,e){void 0===e&&(e=[]);var n=o(function(){return[null!=t&&t.id?"__"+t.id+"__":null,i.split("/")[i.split("/").length-1].replaceAll(" ","/")].filter(function(i){return i}).join("")},[i,t]),u=s(function(){var e;return null!=(e=null==h?void 0:h[n])?e:new l(i,t)})[0];return r(function(){h[n]||(h[n]=u)},e),u},m=function(){var i=s(a.state),t=i[0],e=i[1];return r(function(){return a.add(function(i){e(i)})},[]),[t,function(i){a.dispatch(i)}]};export{l as AudioManager,a as MUTE_AUDIO_SIGNAL,c as useAudio,m as useMuteAllAudio};
import{StateSignal as e}from"@zouloux/signal";import{deferredPromise as n}from"@wbe/deferred-promise";import{Howl as t}from"howler";import i from"@wbe/debug";import{useMemo as o,useState as s,useEffect as r}from"react";function a(){return a=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i])}return e},a.apply(this,arguments)}var u=i("AudioManager"),d=e(!1),c=/*#__PURE__*/function(){function e(e,t){var i=this;void 0===t&&(t={}),this.url=void 0,this.options=void 0,this.sound=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=null,this.nameSpace=void 0,this.canplayPromise=void 0,this.endedPromise=void 0,this.raf=void 0,this.handleMuteAll=function(e){e?i.mute():i.unmute()},this.handleEnded=function(){u(i.nameSpace,"ended"),i.isPlaying=!1,i.endedPromise.resolve(),i.raf&&i.cancelRaf()},this.url=e,this.options=a({},{volume:1,autoplay:!1,loop:!1,preload:!0,html5:!1,delay:0,onUpdate:null},t),this.nameSpace=this.getNameSpace(this.id,this.url),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=n(),this.endedPromise=n(),this.load(),this.initEvents()}var i=e.prototype;return i.getNameSpace=function(e,n){return e+" - "+n.replace(/^.*[\\\/]/,"")+" -"},i.load=function(){var e=this;this.sound=new t(a({src:[this.url]},this.options,{onload:function(){u(e.nameSpace,"canplay handler, audio is ready"),e.isLoaded=!0,e.canplayPromise.resolve(),e.isLoading=!1,e.isLoaded=!0,e.isMuted&&e.mute()}}))},i.initEvents=function(){d.add(this.handleMuteAll),this.sound.on("end",this.handleEnded)},i.play=function(){try{var e=this;return u(e.nameSpace,"waiting for canplayPromise..."),Promise.resolve(e.canplayPromise.promise).then(function(){return e.endedPromise=n(),u(e.nameSpace,"play",e.options),Promise.resolve(new Promise(function(n){return setTimeout(n,e.options.delay)})).then(function(){return e.id=e.sound.play(),e.nameSpace=e.getNameSpace(e.id,e.url),e.isPlaying=!0,e.options.onUpdate&&(e.raf=e.rafRender()),e.endedPromise.promise})})}catch(e){return Promise.reject(e)}},i.pause=function(){this.isPlaying&&this.isLoaded&&(this.sound.pause(),this.raf&&this.cancelRaf())},i.stop=function(){try{var e=this;return u(e.nameSpace,"stop"),e.sound.stop(e.id),e.isPlaying=!1,e.raf&&e.cancelRaf(),Promise.resolve()}catch(e){return Promise.reject(e)}},i.replay=function(){u(this.nameSpace,"replay"),this.stop(),this.play()},i.loop=function(){try{return this.sound.loop(!0),this.isPlaying=!0,Promise.resolve()}catch(e){return Promise.reject(e)}},i.mute=function(){u(this.nameSpace,"mute"),this.isMuted||(this.sound.mute(!0),this.isMuted=!0)},i.unmute=function(){u(this.nameSpace,"unmute"),this.isMuted&&(this.sound.mute(!1),this.isMuted=!1)},i.fade=function(e,n,t){void 0===t&&(t=1e3);try{var i=this;return u(i.nameSpace,"fade >",e,n,i.options),i.isPlaying||i.play(),i.sound.fade(e,n,t),Promise.resolve(new Promise(function(e){return setTimeout(e,t)}))}catch(e){return Promise.reject(e)}},i.fadeIn=function(e){void 0===e&&(e=1e3);try{var n=this;return n.id=n.sound.play(),u(n.nameSpace,"fadeIn 0 -> "+n.options.volume),n.isPlaying=!0,n.sound.fade(0,n.options.volume,e),Promise.resolve(new Promise(function(t){return setTimeout(function(){u(n.nameSpace,"fadeIn ended"),t()},e)}))}catch(e){return Promise.reject(e)}},i.fadeOut=function(e){void 0===e&&(e=1e3);try{var n=this;return u(n.nameSpace,"fadeOut "+n.options.volume+" -> 0"),n.sound.fade(n.options.volume,0,e),Promise.resolve(new Promise(function(t){return setTimeout(function(){u(n.nameSpace,"fadeOut ended"),n.stop(),t()},e)}))}catch(e){return Promise.reject(e)}},i.destroy=function(){u(this.nameSpace,"destroy"),this.sound.unload(),d.remove(this.handleMuteAll)},i.rafRender=function(){var e=this;return requestAnimationFrame(function(n){null==e.options.onUpdate||e.options.onUpdate(n),e.raf=e.rafRender()})},i.cancelRaf=function(){cancelAnimationFrame(this.raf),this.raf=null},e}();i("AudioManager:hooks");var l={},h=function(e,n,t){void 0===t&&(t=[]);var i=o(function(){return[null!=n&&n.id?"__"+n.id+"__":null,e.split("/")[e.split("/").length-1].replaceAll(" ","/")].filter(function(e){return e}).join("")},[e,n]),a=s(function(){var t;return null!=(t=null==l?void 0:l[i])?t:new c(e,n)})[0];return r(function(){l[i]||(l[i]=a)},t),a},m=function(){var e=s(d.state),n=e[0],t=e[1];return r(function(){return d.add(function(e){t(e)})},[]),[n,function(e){d.dispatch(e)}]};export{c as AudioManager,d as MUTE_AUDIO_SIGNAL,h as useAudio,m as useMuteAllAudio};
//# sourceMappingURL=index.module.js.map

@@ -1,2 +0,2 @@

!function(e,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports,require("@zouloux/signal"),require("@wbe/deferred-promise"),require("howler"),require("@wbe/debug"),require("react")):"function"==typeof define&&define.amd?define(["exports","@zouloux/signal","@wbe/deferred-promise","howler","@wbe/debug","react"],i):i((e||self).audioManager={},e.signal,e.deferredPromise,e.howler,e.debug,e.react)}(this,function(e,i,t,n,o,s){function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var u=/*#__PURE__*/r(o);function d(){return d=Object.assign?Object.assign.bind():function(e){for(var i=1;i<arguments.length;i++){var t=arguments[i];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},d.apply(this,arguments)}var a=u.default("AudioManager"),l=i.StateSignal(!1),f=/*#__PURE__*/function(){function e(e,i){var n=this;void 0===i&&(i={}),this.url=void 0,this.options=void 0,this.sound=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=null,this.canplayPromise=void 0,this.endedPromise=void 0,this.handleMuteAll=function(e){e?n.mute():n.unmute()},this.handleEnded=function(){a(n.id,"ended"),n.isPlaying=!1,n.endedPromise.resolve()},this.url=e,this.options=d({},{volume:1,autoplay:!1,loop:!1,preload:!0,html5:!1,delay:0},i),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=t.deferredPromise(),this.endedPromise=t.deferredPromise(),this.load(),this.initEvents()}var i=e.prototype;return i.load=function(){var e=this;this.sound=new n.Howl(d({src:[this.url]},this.options,{onload:function(){a(e.id,"canplay handler, audio is ready"),e.isLoaded=!0,e.canplayPromise.resolve(),e.isLoading=!1,e.isLoaded=!0,e.isMuted&&e.mute()}}))},i.initEvents=function(){l.add(this.handleMuteAll),this.sound.on("end",this.handleEnded)},i.destroy=function(){a(this.id,"destroy"),this.sound.unload(),l.remove(this.handleMuteAll)},i.play=function(){try{var e=this;return a(e.id,"waiting for canplayPromise..."),Promise.resolve(e.canplayPromise.promise).then(function(){return e.endedPromise=t.deferredPromise(),a(e.id,"play",e.options),Promise.resolve(new Promise(function(i){return setTimeout(i,e.options.delay)})).then(function(){return e.id=e.sound.play(),e.isPlaying=!0,e.endedPromise.promise})})}catch(e){return Promise.reject(e)}},i.pause=function(){this.isPlaying&&this.isLoaded&&this.sound.pause()},i.stop=function(){try{var e=this;return a(e.id,"stop"),e.sound.stop(e.id),e.isPlaying=!1,Promise.resolve()}catch(e){return Promise.reject(e)}},i.replay=function(){a(this.id,"replay"),this.stop(),this.play()},i.loop=function(){try{return this.sound.loop(!0),this.isPlaying=!0,Promise.resolve()}catch(e){return Promise.reject(e)}},i.mute=function(){a(this.id,"mute"),this.isMuted||(this.sound.mute(!0),this.isMuted=!0)},i.unmute=function(){a(this.id,"unmute"),this.isMuted&&(this.sound.mute(!1),this.isMuted=!1)},i.fade=function(e,i,t){void 0===t&&(t=1e3);try{var n=this;return a(n.id,"fade >",e,i,n.options),n.isPlaying||n.play(),n.sound.fade(e,i,t),Promise.resolve(new Promise(function(e){return setTimeout(e,t)}))}catch(e){return Promise.reject(e)}},i.fadeIn=function(e){void 0===e&&(e=1e3);try{var i=this;return i.id=i.sound.play(),a(i.id,"fadeIn 0 -> "+i.options.volume),i.isPlaying=!0,i.sound.fade(0,i.options.volume,e),Promise.resolve(new Promise(function(t){return setTimeout(function(){a(i.id,"fadeIn ended"),t()},e)}))}catch(e){return Promise.reject(e)}},i.fadeOut=function(e){void 0===e&&(e=1e3);try{var i=this;return a(i.id,"fadeOut "+i.options.volume+" -> 0"),i.sound.fade(i.options.volume,0,e),Promise.resolve(new Promise(function(t){return setTimeout(function(){a(i.id,"fadeOut ended"),i.stop(),t()},e)}))}catch(e){return Promise.reject(e)}},e}();u.default("AudioManager:hooks");var h={};e.AudioManager=f,e.MUTE_AUDIO_SIGNAL=l,e.useAudio=function(e,i,t){void 0===t&&(t=[]);var n=s.useMemo(function(){return[null!=i&&i.id?"__"+i.id+"__":null,e.split("/")[e.split("/").length-1].replaceAll(" ","/")].filter(function(e){return e}).join("")},[e,i]),o=s.useState(function(){var t;return null!=(t=null==h?void 0:h[n])?t:new f(e,i)})[0];return s.useEffect(function(){h[n]||(h[n]=o)},t),o},e.useMuteAllAudio=function(){var e=s.useState(l.state),i=e[0],t=e[1];return s.useEffect(function(){return l.add(function(e){t(e)})},[]),[i,function(e){l.dispatch(e)}]}});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@zouloux/signal"),require("@wbe/deferred-promise"),require("howler"),require("@wbe/debug"),require("react")):"function"==typeof define&&define.amd?define(["exports","@zouloux/signal","@wbe/deferred-promise","howler","@wbe/debug","react"],t):t((e||self).audioManager={},e.signal,e.deferredPromise,e.howler,e.debug,e.react)}(this,function(e,t,i,n,o,r){function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=/*#__PURE__*/s(o);function u(){return u=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var i=arguments[t];for(var n in i)Object.prototype.hasOwnProperty.call(i,n)&&(e[n]=i[n])}return e},u.apply(this,arguments)}var d=a.default("AudioManager"),l=t.StateSignal(!1),c=/*#__PURE__*/function(){function e(e,t){var n=this;void 0===t&&(t={}),this.url=void 0,this.options=void 0,this.sound=void 0,this.isLoading=void 0,this.isLoaded=void 0,this.isPlaying=void 0,this.isMuted=void 0,this.id=null,this.nameSpace=void 0,this.canplayPromise=void 0,this.endedPromise=void 0,this.raf=void 0,this.handleMuteAll=function(e){e?n.mute():n.unmute()},this.handleEnded=function(){d(n.nameSpace,"ended"),n.isPlaying=!1,n.endedPromise.resolve(),n.raf&&n.cancelRaf()},this.url=e,this.options=u({},{volume:1,autoplay:!1,loop:!1,preload:!0,html5:!1,delay:0,onUpdate:null},t),this.nameSpace=this.getNameSpace(this.id,this.url),this.isPlaying=!1,this.isLoading=!0,this.isLoaded=!1,this.isMuted=!1,this.canplayPromise=i.deferredPromise(),this.endedPromise=i.deferredPromise(),this.load(),this.initEvents()}var t=e.prototype;return t.getNameSpace=function(e,t){return e+" - "+t.replace(/^.*[\\\/]/,"")+" -"},t.load=function(){var e=this;this.sound=new n.Howl(u({src:[this.url]},this.options,{onload:function(){d(e.nameSpace,"canplay handler, audio is ready"),e.isLoaded=!0,e.canplayPromise.resolve(),e.isLoading=!1,e.isLoaded=!0,e.isMuted&&e.mute()}}))},t.initEvents=function(){l.add(this.handleMuteAll),this.sound.on("end",this.handleEnded)},t.play=function(){try{var e=this;return d(e.nameSpace,"waiting for canplayPromise..."),Promise.resolve(e.canplayPromise.promise).then(function(){return e.endedPromise=i.deferredPromise(),d(e.nameSpace,"play",e.options),Promise.resolve(new Promise(function(t){return setTimeout(t,e.options.delay)})).then(function(){return e.id=e.sound.play(),e.nameSpace=e.getNameSpace(e.id,e.url),e.isPlaying=!0,e.options.onUpdate&&(e.raf=e.rafRender()),e.endedPromise.promise})})}catch(e){return Promise.reject(e)}},t.pause=function(){this.isPlaying&&this.isLoaded&&(this.sound.pause(),this.raf&&this.cancelRaf())},t.stop=function(){try{var e=this;return d(e.nameSpace,"stop"),e.sound.stop(e.id),e.isPlaying=!1,e.raf&&e.cancelRaf(),Promise.resolve()}catch(e){return Promise.reject(e)}},t.replay=function(){d(this.nameSpace,"replay"),this.stop(),this.play()},t.loop=function(){try{return this.sound.loop(!0),this.isPlaying=!0,Promise.resolve()}catch(e){return Promise.reject(e)}},t.mute=function(){d(this.nameSpace,"mute"),this.isMuted||(this.sound.mute(!0),this.isMuted=!0)},t.unmute=function(){d(this.nameSpace,"unmute"),this.isMuted&&(this.sound.mute(!1),this.isMuted=!1)},t.fade=function(e,t,i){void 0===i&&(i=1e3);try{var n=this;return d(n.nameSpace,"fade >",e,t,n.options),n.isPlaying||n.play(),n.sound.fade(e,t,i),Promise.resolve(new Promise(function(e){return setTimeout(e,i)}))}catch(e){return Promise.reject(e)}},t.fadeIn=function(e){void 0===e&&(e=1e3);try{var t=this;return t.id=t.sound.play(),d(t.nameSpace,"fadeIn 0 -> "+t.options.volume),t.isPlaying=!0,t.sound.fade(0,t.options.volume,e),Promise.resolve(new Promise(function(i){return setTimeout(function(){d(t.nameSpace,"fadeIn ended"),i()},e)}))}catch(e){return Promise.reject(e)}},t.fadeOut=function(e){void 0===e&&(e=1e3);try{var t=this;return d(t.nameSpace,"fadeOut "+t.options.volume+" -> 0"),t.sound.fade(t.options.volume,0,e),Promise.resolve(new Promise(function(i){return setTimeout(function(){d(t.nameSpace,"fadeOut ended"),t.stop(),i()},e)}))}catch(e){return Promise.reject(e)}},t.destroy=function(){d(this.nameSpace,"destroy"),this.sound.unload(),l.remove(this.handleMuteAll)},t.rafRender=function(){var e=this;return requestAnimationFrame(function(t){null==e.options.onUpdate||e.options.onUpdate(t),e.raf=e.rafRender()})},t.cancelRaf=function(){cancelAnimationFrame(this.raf),this.raf=null},e}();a.default("AudioManager:hooks");var f={};e.AudioManager=c,e.MUTE_AUDIO_SIGNAL=l,e.useAudio=function(e,t,i){void 0===i&&(i=[]);var n=r.useMemo(function(){return[null!=t&&t.id?"__"+t.id+"__":null,e.split("/")[e.split("/").length-1].replaceAll(" ","/")].filter(function(e){return e}).join("")},[e,t]),o=r.useState(function(){var i;return null!=(i=null==f?void 0:f[n])?i:new c(e,t)})[0];return r.useEffect(function(){f[n]||(f[n]=o)},i),o},e.useMuteAllAudio=function(){var e=r.useState(l.state),t=e[0],i=e[1];return r.useEffect(function(){return l.add(function(e){i(e)})},[]),[t,function(e){l.dispatch(e)}]}});
//# sourceMappingURL=index.umd.js.map
{
"name": "@cher-ami/audio-manager",
"description": "A simple web audio manager",
"version": "0.2.0",
"version": "0.3.0",
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc