Last active
November 10, 2023 21:41
-
-
Save johnstcn/fcf9cbc1fe714f88a3ad4f63f8039d57 to your computer and use it in GitHub Desktop.
Foundry VTT Macro: Cross-fade Playlists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function({fadeIn="", fadeDuration=5000}={}) { | |
function fadeInPlaylist(playlist, fadeDuration) { | |
playlist.playAll().then(function(p) { | |
let globalVol = game.settings.get("core", "globalPlaylistVolume"); | |
p.sounds.filter(s => s.playing).find(_ => true) | |
.sound | |
.fade(globalVol, { duration: fadeDuration, from: 0}); | |
}); | |
} | |
function fadeOutPlaylist(playlist, fadeDuration) { | |
if (!playlist.playing) return; | |
let playingSound = playlist.sounds.filter(s => s.playing).find(_ => true).sound; | |
if (!!!playingSound) return; // should not happen | |
let currVol = playingSound.volume; | |
let globalVol = game.settings.get("core", "globalPlaylistVolume"); | |
if (currVol == 0) return; | |
playingSound.fade(0, { duration: fadeDuration, from: currVol}) | |
setTimeout(() => playlist.stopAll(), fadeDuration); | |
return; | |
} | |
let playlistIn = game.playlists.filter(p => p.name == fadeIn).find(_ => true); | |
let playlistOut = game.playlists.filter(p => p.playing).find(_ => true); | |
if (!!!playlistIn) { | |
ui.notifications.error(`Playlist not found: "${fadeIn}"`); | |
return; | |
} | |
let alreadyPlaying = game.playlists.filter(p => p.playing) | |
.filter(p => p.id == playlistIn.id) | |
.find(_ => true); | |
if (!!alreadyPlaying) { | |
ui.notifications.info(`Already playing ${fadeIn}`); | |
return; | |
} | |
game.playlists.filter(p => p.playing).forEach(p => fadeOutPlaylist(p, fadeDuration)); | |
fadeInPlaylist(playlistIn, fadeDuration); | |
})({fadeIn: "playlist name", fadeDuration: 5000}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment