UNPKG

buzz

Version:

Buzz, a Javascript HTML5 Audio library

3 lines (2 loc) 10.1 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).buzz=e()}(this,function(){"use strict";const t=window.AudioContext||window.webkitAudioContext,e={defaults:{autoplay:!1,crossOrigin:null,duration:5e3,formats:[],loop:!1,placeholder:"--",preload:"metadata",volume:80,webAudioApi:!1,document:window.document},types:{mp3:"audio/mpeg",ogg:"audio/ogg",wav:"audio/wav",aac:"audio/aac",m4a:"audio/x-m4a"},sounds:[],el:document.createElement("audio"),getAudioContext(){if(void 0===this.audioCtx)try{this.audioCtx=t?new t:null}catch(t){this.audioCtx=null}return this.audioCtx},sound:function(t,n){const i=(n=n||{}).document||e.defaults.document;let s=0;const u=[],o={},r=e.isSupported();function h(t){const e=[],n=t.length-1;for(let i=0;i<=n;i++)e.push({start:t.start(i),end:t.end(i)});return e}function a(t){return t.split(".").pop()}if(this.load=function(){return r?(this.sound.load(),this):this},this.play=function(){return r?(this.sound.play().catch(()=>{}),this):this},this.togglePlay=function(){return r?(this.sound.paused?this.sound.play().catch(()=>{}):this.sound.pause(),this):this},this.pause=function(){return r?(this.sound.pause(),this):this},this.isPaused=function(){return r?this.sound.paused:null},this.stop=function(){return r?(this.sound.pause(),this.setTime(0),this):this},this.isEnded=function(){return r?this.sound.ended:null},this.loop=function(){return r?(this.sound.loop="loop",this.bind("ended.buzzloop",function(){this.currentTime=0,this.play()}),this):this},this.unloop=function(){return r?(this.sound.removeAttribute("loop"),this.unbind("ended.buzzloop"),this):this},this.mute=function(){return r?(this.sound.muted=!0,this):this},this.unmute=function(){return r?(this.sound.muted=!1,this):this},this.toggleMute=function(){return r?(this.sound.muted=!this.sound.muted,this):this},this.isMuted=function(){return r?this.sound.muted:null},this.setVolume=function(t){return r?(t<0&&(t=0),t>100&&(t=100),this.volume=t,this.sound.volume=t/100,this):this},this.getVolume=function(){return r?this.volume:this},this.increaseVolume=function(t){return this.setVolume(this.volume+(t||1))},this.decreaseVolume=function(t){return this.setVolume(this.volume-(t||1))},this.setTime=function(t){if(!r)return this;let e=!0;return this.whenReady(function(){!0===e&&(e=!1,this.sound.currentTime=t)}),this},this.getTime=function(){if(!r)return null;const t=Math.round(100*this.sound.currentTime)/100;return isNaN(t)?e.defaults.placeholder:t},this.setPercent=function(t){return r?this.setTime(e.fromPercent(t,this.sound.duration)):this},this.getPercent=function(){if(!r)return null;const t=Math.round(e.toPercent(this.sound.currentTime,this.sound.duration));return isNaN(t)?e.defaults.placeholder:t},this.setSpeed=function(t){return r?(this.sound.playbackRate=t,this):this},this.getSpeed=function(){return r?this.sound.playbackRate:null},this.getDuration=function(){if(!r)return null;const t=Math.round(100*this.sound.duration)/100;return isNaN(t)?e.defaults.placeholder:t},this.getPlayed=function(){return r?h(this.sound.played):null},this.getBuffered=function(){return r?h(this.sound.buffered):null},this.getSeekable=function(){return r?h(this.sound.seekable):null},this.getErrorCode=function(){return r&&this.sound.error?this.sound.error.code:0},this.getErrorMessage=function(){if(!r)return null;switch(this.getErrorCode()){case 1:return"MEDIA_ERR_ABORTED";case 2:return"MEDIA_ERR_NETWORK";case 3:return"MEDIA_ERR_DECODE";case 4:return"MEDIA_ERR_SRC_NOT_SUPPORTED";default:return null}},this.getStateCode=function(){return r?this.sound.readyState:null},this.getStateMessage=function(){if(!r)return null;switch(this.getStateCode()){case 0:return"HAVE_NOTHING";case 1:return"HAVE_METADATA";case 2:return"HAVE_CURRENT_DATA";case 3:return"HAVE_FUTURE_DATA";case 4:return"HAVE_ENOUGH_DATA";default:return null}},this.getNetworkStateCode=function(){return r?this.sound.networkState:null},this.getNetworkStateMessage=function(){if(!r)return null;switch(this.getNetworkStateCode()){case 0:return"NETWORK_EMPTY";case 1:return"NETWORK_IDLE";case 2:return"NETWORK_LOADING";case 3:return"NETWORK_NO_SOURCE";default:return null}},this.set=function(t,e){return r?(this.sound[t]=e,this):this},this.get=function(t){return r?t?this.sound[t]:this.sound:null},this.bind=function(t,e){if(!r)return this;t=t.split(" ");const n=this,i=t=>{e.call(n,t)};for(let e=0;e<t.length;e++){const n=t[e],s=n.split(".")[0];u.push({idx:n,func:i}),this.sound.addEventListener(s,i,!0)}return this},this.unbind=function(t){if(!r)return this;t=t.split(" ");for(let e=0;e<t.length;e++){const n=t[e],i=n.split(".")[0];for(let t=0;t<u.length;t++){const e=u[t].idx.split(".");(u[t].idx===n||e[1]&&e[1]===n.replace(".",""))&&(this.sound.removeEventListener(i,u[t].func,!0),u.splice(t,1))}}return this},this.bindOnce=function(t,e){if(!r)return this;const n=this;return o[s++]=!1,this.bind(`${t}.${s}`,function(){o[s]||(o[s]=!0,e.call(n)),n.unbind(`${t}.${s}`)}),this},this.trigger=function(t,e){if(!r)return this;t=t.split(" ");for(let n=0;n<t.length;n++){const s=t[n];for(let t=0;t<u.length;t++){const n=u[t].idx.split(".");if(u[t].idx===s||n[0]&&n[0]===s.replace(".","")){const t=i.createEvent("HTMLEvents");t.initEvent(n[0],!1,!0),t.originalEvent=e,this.sound.dispatchEvent(t)}}}return this},this.fadeTo=function(t,n,i){if(!r)return this;n instanceof Function?(i=n,n=e.defaults.duration):n=n||e.defaults.duration;const s=this.volume,u=n/Math.abs(s-t),o=this;let h;const a=i instanceof Function;let l=null;if(!a)var d=new Promise(t=>{l=t});function c(){clearTimeout(h),h=setTimeout(()=>{s<t&&o.volume<t?(o.setVolume(o.volume+=1),c()):s>t&&o.volume>t?(o.setVolume(o.volume-=1),c()):a?i.apply(o):l&&l(o)},u)}return this.play(),this.whenReady(function(){c()}),a?this:d},this.fadeIn=function(t,e){if(!r)return this;const n=this.setVolume(0).fadeTo(100,t,e);return n&&"function"==typeof n.then?n:this},this.fadeOut=function(t,e){return r?this.fadeTo(0,t,e):this},this.fadeWith=function(t,e){return r?(this.fadeOut(e,function(){this.stop()}),t.play().fadeIn(e),this):this},this.whenReady=function(t){if(!r)return null;const e=this;if(!(t instanceof Function))return new Promise(t=>{0===e.sound.readyState?e.bind("canplay.buzzwhenready",function(){t(e)}):t(e)});0===this.sound.readyState?this.bind("canplay.buzzwhenready",function(){t.call(e)}):t.call(e)},this.addSource=function(t){const n=this,s=i.createElement("source");return s.src=t,e.types[a(t)]&&(s.type=e.types[a(t)]),this.sound.appendChild(s),s.addEventListener("error",t=>{n.trigger("sourceerror",t)}),s},r&&t){for(const t in e.defaults)e.defaults.hasOwnProperty(t)&&void 0===n[t]&&(n[t]=e.defaults[t]);if(this.sound=i.createElement("audio"),null!==n.crossOrigin&&(this.sound.crossOrigin=n.crossOrigin),n.webAudioApi){const t=e.getAudioContext();t&&(this.source=t.createMediaElementSource(this.sound),this.source.connect(t.destination))}if(t instanceof Array)for(const e in t)t.hasOwnProperty(e)&&this.addSource(t[e]);else if(n.formats.length)for(const e in n.formats)n.formats.hasOwnProperty(e)&&this.addSource(`${t}.${n.formats[e]}`);else this.addSource(t);n.loop&&this.loop(),n.autoplay&&(this.sound.autoplay="autoplay"),!0===n.preload?this.sound.preload="auto":!1===n.preload?this.sound.preload="none":this.sound.preload=n.preload,this.setVolume(n.volume),e.sounds.push(this)}},group:function(t){function e(){const e=n(null,arguments),i=e.shift();for(let n=0;n<t.length;n++)t[n][i].apply(t[n],e)}function n(t,e){return t instanceof Array?t:Array.prototype.slice.call(e)}t=n(t,arguments),this.getSounds=()=>t,this.add=function(e){e=n(e,arguments);for(let n=0;n<e.length;n++)t.push(e[n])},this.remove=function(e){e=n(e,arguments);for(let n=0;n<e.length;n++)for(let i=0;i<t.length;i++)if(t[i]===e[n]){t.splice(i,1);break}},this.load=function(){return e("load"),this},this.play=function(){return e("play"),this},this.togglePlay=function(){return e("togglePlay"),this},this.pause=function(t){return e("pause",t),this},this.stop=function(){return e("stop"),this},this.mute=function(){return e("mute"),this},this.unmute=function(){return e("unmute"),this},this.toggleMute=function(){return e("toggleMute"),this},this.setVolume=function(t){return e("setVolume",t),this},this.increaseVolume=function(t){return e("increaseVolume",t),this},this.decreaseVolume=function(t){return e("decreaseVolume",t),this},this.loop=function(){return e("loop"),this},this.unloop=function(){return e("unloop"),this},this.setSpeed=function(t){return e("setSpeed",t),this},this.setTime=function(t){return e("setTime",t),this},this.set=function(t,n){return e("set",t,n),this},this.bind=function(t,n){return e("bind",t,n),this},this.unbind=function(t){return e("unbind",t),this},this.bindOnce=function(t,n){return e("bindOnce",t,n),this},this.trigger=function(t){return e("trigger",t),this},this.fade=function(t,n,i,s){return e("fade",t,n,i,s),this},this.fadeIn=function(t,n){return e("fadeIn",t,n),this},this.fadeOut=function(t,n){return e("fadeOut",t,n),this}},all:()=>new e.group(e.sounds),isSupported:()=>!!e.el.canPlayType,isOGGSupported:()=>!!e.el.canPlayType&&e.el.canPlayType('audio/ogg; codecs="vorbis"'),isWAVSupported:()=>!!e.el.canPlayType&&e.el.canPlayType('audio/wav; codecs="1"'),isMP3Supported:()=>!!e.el.canPlayType&&e.el.canPlayType("audio/mpeg;"),isAACSupported:()=>!!e.el.canPlayType&&(e.el.canPlayType("audio/x-m4a;")||e.el.canPlayType("audio/aac;")),toTimer(t,e){let n,i,s;return n=Math.floor(t/3600),n=isNaN(n)?"--":n>=10?n:`0${n}`,i=e?Math.floor(t/60%60):Math.floor(t/60),i=isNaN(i)?"--":i>=10?i:`0${i}`,s=Math.floor(t%60),s=isNaN(s)?"--":s>=10?s:`0${s}`,e?`${n}:${i}:${s}`:`${i}:${s}`},fromTimer(t){const e=t.toString().split(":");return e&&3===e.length&&(t=3600*parseInt(e[0],10)+60*parseInt(e[1],10)+parseInt(e[2],10)),e&&2===e.length&&(t=60*parseInt(e[0],10)+parseInt(e[1],10)),t},toPercent(t,e,n){const i=Math.pow(10,n||0);return Math.round(100*t/e*i)/i},fromPercent(t,e,n){const i=Math.pow(10,n||0);return Math.round(e/100*t*i)/i}};return e}); //# sourceMappingURL=buzz.min.js.map