summaryrefslogtreecommitdiff
path: root/tape-deck.js
blob: f7306007952915f6e52b36170339d8de80395d50 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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(); };

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 isPlaying = false;

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;
        isPlaying = false;
        return;
    }

    if (!hasStarted) {
        pressPlaySound.play();
        hasStarted = true;
    } else {
        buttonPressSound.play();
        tape.play();
    }

    isPlaying = 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);
});