const urlParams = new URLSearchParams(window.location.search); const currentSide = urlParams.get("side"); const tape = new Audio(`audio/2019-mix-side-${currentSide}.ogg`); const sfx = new Audio("audio/tape-start.mp3"); sfx.onended = (event) => { tape.play(); } let display = document.getElementById("notes"); let currentTrackIndex = 0; let hasStarted = false; function play() { fetch(`side${currentSide}.json`) .then(response => response.json()) .then((trackData) => { if (!hasStarted) { sfx.play(); hasStarted = true; tape.addEventListener("timeupdate", (event) => { const time = tape.currentTime; const currentTrack = getTrack(time, currentTrackIndex, trackData); display.textContent = currentTrack.title; }); } else { tape.play(); } }); } function pause() { tape.pause(); } function isLastTrack(current, tracks) { return current + 1 === tracks.length; } function getTrack(time, currentTrackIndex, trackData) { 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]; } }