From f6086f86cbc23fc0db538feafdda9e020e1763c8 Mon Sep 17 00:00:00 2001 From: Grant Shangreaux Date: Sat, 4 Jan 2020 17:28:10 -0600 Subject: Add: initial working tape-deck with 2019 mix Side A --- tape-deck.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tape-deck.js (limited to 'tape-deck.js') diff --git a/tape-deck.js b/tape-deck.js new file mode 100644 index 0000000..7d75abc --- /dev/null +++ b/tape-deck.js @@ -0,0 +1,45 @@ +const urlParams = new URLSearchParams(window.location.search); +const currentSide = urlParams.get("side"); +const sfx = new Audio("audio/tape-start.mp3"); +const tape = new Audio(`audio/2019-mix-side-${currentSide}.mp3`); +let display = document.getElementById("notes"); +let currentTrackIndex = 0; + +fetch(`side${currentSide}.json`) + .then(response => response.json()) + .then((trackData) => { + const startTimes = trackData.map( t => t.start ); + + sfx.play(); + + sfx.onended = (event) => { + tape.controls = true; + tape.play(); + } + + function isLastTrack() { + return currentTrackIndex + 1 === startTimes.length; + } + + function getTrack(time) { + const nextIndex = currentTrackIndex + 1 + + if (isLastTrack()) { + return trackData[trackData.length - 1]; + } else { + if (time >= startTimes[nextIndex]) { + currentTrackIndex = nextIndex; + } + + return trackData[currentTrackIndex]; + } + } + + + tape.addEventListener("timeupdate", (event) => { + const time = tape.currentTime; + const currentTrack = getTrack(time, trackData); + display.textContent = currentTrack.title; + }); + }); + -- cgit v1.2.3