-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaudioOverlap.min.js
1 lines (1 loc) · 2.46 KB
/
audioOverlap.min.js
1
let music,musicTracksSize,pMusic=!0,pSFX=!0,musicTracks=[],sfx=[],lastRand=0,trackIndex=0,init=!0,firstTrackSet=!1,shuffle=!0,loopThroughMusic=!0;function addMusic(u,e){musicTracks.push({key:[u],audio:new Audio(e)})}function addSFX(u,e,s){for(let i=0;i<s;i++)sfx.push({key:[u],audio:new Audio(e)})}function playSFX(u){if(pSFX){let e=sfx.length;for(let s=0;s<e;s++)if(sfx[s].key==u&&sfx[s].audio.paused){let u=sfx[s].audio.play();void 0!==u&&u.then(u=>{sfx[s].audio.play()}).catch(u=>{console.log("audio not yet loaded")});break}}}function playMusic(){if(init)firstTrackSet||(music=musicTracks[0],firstTrackSet=!0),music.audio.play(),init=!1;else if(music.audio.paused&&shuffle){let e=Math.floor(Math.random()*Math.floor(u()));lastRand===e?playMusic():((music=musicTracks[e]).audio.play(),lastRand=e)}else music.audio.paused&&(music=musicTracks[trackIndex],++trackIndex>u()-1&&(trackIndex=0));function u(){return musicTracksSize=musicTracks.length}loopThroughMusic&&music.audio.addEventListener("ended",playMusic)}function setFirstTrack(u){for(let s=0;s<e();s++)musicTracks[s].key==u&&(music=musicTracks[s],firstTrackSet=!0);function e(){return musicTracksSize=musicTracks.length}}function shuffleOn(){shuffle=!0}function shuffleOff(){shuffle=!1}function setMusicVolume(u,e){if("number"==typeof u)2==arguments.length?function(u,e){for(let i=0;i<s();i++)musicTracks[i].key==e&&(musicTracks[i].audio.volume=u)}(u,e):function(u){for(let e=0;e<s();e++)musicTracks[e].audio.volume=u}(u);else{let e=typeof u;console.log("You've entered a(n): "+e+" as the volume parameter. setMusicVolume() only accepts numbers for the volume parameter.")}function s(){return musicTracksSize=musicTracks.length}}function setSFXVolume(u,e){if("number"==typeof u)2==arguments.length?function(u,e){console.log("1");for(let s=0;s<sfx.length;s++)sfx[s].key==e&&(sfx[s].audio.volume=u)}(u,e):function(u){console.log("2");for(let e=0;e<sfx.length;e++)sfx[e].audio.volume=u}(u);else{let e=typeof u;console.log("You've entered a(n): "+e+" as the volume parameter. setSFXVolume() only accepts numbers for the volume parameter.")}}function toggleSFX(){pSFX?SFXOff():SFXOn()}function SFXOn(){pSFX=!0}function SFXOff(){pSFX=!1}function toggleMusic(){music.audio.paused?(pMusic=!0,music.audio.play()):(pMusic=!1,music.audio.pause())}function musicOn(){music.audio.paused&&(pMusic=!0,music.audio.play())}function musicOff(){music.audio.paused||(pMusic=!1,music.audio.pause())}function loopMusicOn(){loopThroughMusic=!0}function loopMusicOff(){loopThroughMusic=!1}