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
|
import { DeckButton } from './deck-button.js';
const urlParams = new URLSearchParams(window.location.search);
const currentSide = urlParams.get("side");
const Cassette = new Audio(`audio/2019-mix-side-${currentSide}.mp3`);
const ffSound = new Audio("audio/ff.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" },
initSound: new Audio("audio/tape-start.mp3"),
playButton: DeckButton("play"),
ffButton: DeckButton("ff"),
rewindButton: DeckButton("rewind"),
go: function () {
this.elem.src = this.images.go;
this.isPlaying = true;
},
start: function () {
this.hasStarted = true;
this.initSound.onended = (event) => { Cassette.play(); };
this.initSound.play();
this.go();
},
pause: function () {
this.playButton.toggle();
Cassette.pause();
this.elem.src = this.images.stop;
this.isPlaying = false;
},
resume: function () {
this.go();
Cassette.play();
},
};
ffSound.onended = (event) => {
if (TapeDeck.ffButton.state() === "on") {
TapeDeck.ffButton.toggle();
} else {
TapeDeck.rewindButton.toggle();
}
TapeDeck.playButton.toggle({sound: false});
TapeDeck.resume();
};
// button onclick points here
window.play = function () {
if (TapeDeck.isPlaying) { TapeDeck.pause(); return; }
// figure out better way of handling play button
TapeDeck.playButton.toggle();
TapeDeck.hasStarted ? TapeDeck.resume() : TapeDeck.start();
}
window.ff = function () {
if (TapeDeck.isPlaying) TapeDeck.pause();
TapeDeck.ffButton.toggle();
Cassette.currentTime = Cassette.currentTime + 20;
ffSound.play();
}
window.rewind = function () {
if (TapeDeck.isPlaying) TapeDeck.pause();
TapeDeck.rewindButton.toggle();
Cassette.currentTime = Cassette.currentTime - 20;
ffSound.play();
}
window.reset = function () {
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);
});
|