const urlParams = new URLSearchParams(window.location.search); const currentSide = urlParams.get("side"); const tape = new Audio(`audio/2019-mix-side-${currentSide}.mp3`); const pressPlaySound = new Audio("audio/tape-start.mp3"); const buttonPressSound = new Audio("audio/button-press.mp3"); pressPlaySound.onended = (event) => { tape.play(); }; buttonPressSound.onended = (event) => { if (isPlaying()) { playing = false; } else { tape.play(); } }; const stopImage = "images/cool-deck.png"; const playImage = "images/cool-deck.gif"; let playButton = document.getElementById("play-button"); const playButtonUp = "images/play-button-up.png"; const playButtonDown = "images/play-button-down.png"; let deck = document.getElementById("tape-deck"); let notes = document.getElementById("notes"); let trackData = []; let currentTrackIndex = 0; let hasStarted = false; let playing = false; function isPlaying() { return playing; } function loadTrackData() { fetch(`side${currentSide}.json`) .then(response => response.json()) .then((data) => { trackData = data; }); } function play() { if (isPlaying()) { playButton.src = playButtonUp; buttonPressSound.play(); tape.pause(); deck.src = stopImage; return; } if (!hasStarted) { pressPlaySound.play(); hasStarted = true; } else { buttonPressSound.play(); } playing = true; playButton.src = playButtonDown; deck.src = playImage; } function isLastTrack(current, tracks) { return current + 1 === tracks.length; } function getTrack(time) { const nextIndex = currentTrackIndex + 1; const startTimes = trackData.map( t => t.start ); if (isLastTrack(currentTrackIndex, startTimes)) { return trackData[trackData.length - 1]; } else { if (time >= startTimes[nextIndex]) { currentTrackIndex = nextIndex; } return trackData[currentTrackIndex]; } } function formatTrackData(track) { let string = `${track.title} - ${track.artist}`; if (track.album) { string = string.concat(` - ${track.album}`); } if (track.link) { string = string.concat(` - ${track.link}`) } return string; } loadTrackData(); tape.addEventListener("timeupdate", (event) => { const time = tape.currentTime; const currentTrack = getTrack(time); notes.textContent = formatTrackData(currentTrack); });