summaryrefslogtreecommitdiff
path: root/tape-deck.js
blob: 7362feb312d273f013b1dd22f968db5e783f0b21 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const urlParams = new URLSearchParams(window.location.search);
const currentSide = urlParams.get("side");
const Cassette = new Audio(`audio/2019-mix-side-${currentSide}.mp3`);

let TapeDeck = {
    elem: document.getElementById("tape-deck"),
    notes: document.getElementById("notes"),
    trackData: [],
    currentTrackIndex: 0,
    hasStarted: false,
    isPlaying: false,
    images: { stop: "images/cool-deck.png", go: "images/cool-deck.gif" },
    go: function () {
        this.elem.src = this.images.go;
        this.isPlaying = true;
    },
    stop: function () {
        this.elem.src = this.images.stop;
        this.isPlaying = false;
    },
    playButton: {
        elem: document.getElementById("play-button"),
        state: "off",
        up: "images/play-button-up.png",
        down: "images/play-button-down.png",
        initSound: new Audio("audio/tape-start.mp3"),
        pressSound: new Audio("audio/button-press.mp3"),
        toggle: function () {
            if (this.state === "off") {
                this.elem.src = this.down;
                this.state = "on";
            } else {
                this.elem.src = this.up;
                this.state = "off";
            }
        }
    }
};

function start() {
    const btn = TapeDeck.playButton;

    TapeDeck.hasStarted = true;
    btn.toggle();
    btn.initSound.onended = (event) => { Cassette.play(); };
    btn.initSound.play();
}

function pause() {
    const btn = TapeDeck.playButton;

    btn.toggle();
    btn.pressSound.play();
    Cassette.pause();
    TapeDeck.stop();
}

function resume() {
    const btn = TapeDeck.playButton;

    btn.pressSound.play();
    btn.toggle();
    Cassette.play();
}

// button onclick points here
function play() {
    if (TapeDeck.isPlaying) { pause(); return; }

    TapeDeck.hasStarted ? resume() : start();
    TapeDeck.go();
}

function reset() {
    Cassette.currentTime = 0;
}

function isLastTrack(current, tracks) {
    return current + 1 === tracks.length;
}

function getTrack(time) {
    const { currentTrackIndex, trackData } = TapeDeck;
    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]) {
            TapeDeck.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;
}

function loadTrackData() {
    fetch(`side${currentSide}.json`)
        .then(response => response.json())
        .then((data) => {
            TapeDeck.trackData = data;
        });
}

function resumeMaybe() {
    if (localStorage.getItem(currentSide)) {
        Cassette.currentTime = localStorage.getItem(currentSide);
    }
}

loadTrackData();

resumeMaybe();

Cassette.addEventListener("timeupdate", (event) => {
    const time = Cassette.currentTime;
    const currentTrack = getTrack(time);

    localStorage.setItem(currentSide, time);

    if (!currentTrack) return;

    TapeDeck.notes.textContent = formatTrackData(currentTrack);
});