blob: 55aa4de10f7482f6e78991acfd5f39648764908d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
const urlParams = new URLSearchParams(window.location.search);
const currentSide = urlParams.get("side");
const tape = new Audio(`audio/2019-mix-side-${currentSide}.ogg`);
const pressPlaySound = new Audio("audio/tape-start.mp3");
pressPlaySound.onended = (event) => { tape.play(); };
const stopImage = "images/cool-deck.png";
const playImage = "images/cool-deck.gif";
let deck = document.getElementById("tape-deck");
let notes = document.getElementById("notes");
let trackData = [];
let currentTrackIndex = 0;
let hasStarted = false;
function loadTrackData() {
fetch(`side${currentSide}.json`)
.then(response => response.json())
.then((data) => {
trackData = data;
});
}
function play() {
if (!hasStarted) {
pressPlaySound.play();
hasStarted = true;
} else {
tape.play();
}
deck.src = playImage;
}
function pause() {
tape.pause();
deck.src = stopImage;
}
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];
}
}
function formatTrackData(track) {
let string = `${track.title} - ${track.artist}`;
if (track.album) {
string.concat(` - ${track.album}`)
}
if (track.link) {
string.concat(` - ${track.link}`)
}
return string;
}
loadTrackData();
tape.addEventListener("timeupdate", (event) => {
const time = tape.currentTime;
const currentTrack = getTrack(time, currentTrackIndex, trackData);
notes.textContent = formatTrackData(currentTrack);
});
|