-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
160 lines (142 loc) · 3.92 KB
/
index.html
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>MEDUSA</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="/css/main.css">
</head>
<body>
<div class="container mb-2">
<h1>MEDUSA</h1>
<span id="track-info"></span>
<div class="spotify-container">
<img id="cover-art" src="" class="rounded mx-auto d-block main-image" alt="Cover Art">
<a id="play-button" class="play on-top" title="Start MEDUSA"></a>
</div>
</div>
<div class="container">
<div id="data">
<!-- Handled by plotly.js -->
</div>
</div>
</body>
<!-- Scripts -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://sdk.scdn.co/spotify-player.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var ready = false;
var player;
var img = document.getElementById("cover-art");
var icon = document.getElementById("play-button");
var info = document.getElementById("track-info");
img.crossOrigin = 'Anonymous';
var playing = false;
icon.onclick = function() {
icon.classList.toggle('active');
socket.emit("toggle");
playing = !playing;
return false;
};
// Plotly handler
var target = {
x: [0],
y: [0],
mode: "markers",
type: "scatter",
name: "Target",
marker: {size: 12}
};
var state = {
x: [0],
y: [0],
mode: "markers",
type: "scatter",
name: "You",
marker: {size: 12}
};
var layout = {
shapes: [null, {
type: "rect",
xref: "x",
yref: "y",
x0: 0.5,
y0: 0,
x1: 1,
y1: 0.5,
fillcolor: "#3cb371",
opacity: 0.2,
line: {
width: 0
}
}],
xaxis: {
range: [0, 1],
title: 'Valence'
},
yaxis: { range: [0, 1] },
title:'Valence vs. Arousal'
};
Plotly.newPlot("data", [target, state], layout);
socket.on("target", data => {
console.log("TARGET: ", data);
info.innerHTML = data.album + ": " + data.name + " (" + data.artist + ")";
target.x[0] = data.valence;
target.y[0] = data.arousal;
layout.shapes[0] = {
type: 'circle',
xref: 'x',
yref: 'y',
x0: data.valence - 0.05,
y0: data.arousal - 0.05,
x1: data.valence + 0.05,
y1: data.arousal + 0.05,
opacity: 1,
line: {
color: 'blue'
}
};
Plotly.update("data", [target, state], layout);
});
socket.on("bci", data => {
state.x[0] = data.valence;
state.y[0] = data.arousal;
Plotly.update("data", [target, state], layout);
});
// Connect to the player!
socket.on("token", token => {
if (ready) {
player = new Spotify.Player({
name: 'MEDUSA: BCI Easy Listening',
getOAuthToken: cb => { cb(token); }
});
// Error handling
player.addListener('initialization_error', ({ message }) => { console.error(message); });
player.addListener('authentication_error', ({ message }) => { console.error(message); });
player.addListener('account_error', ({ message }) => { console.error(message); });
player.addListener('playback_error', ({ message }) => { console.error(message); });
// Playback status updates
player.addListener('player_state_changed', state => {
if (!state) return;
console.log(state);
var _url = state.track_window.current_track.album.images[0].url;
if (img.src != _url) img.src = _url;
socket.emit("playing", !state.paused);
});
// Ready
player.addListener('ready', ({ device_id }) => {
console.log('Ready with Device ID', device_id);
socket.emit("id", device_id);
});
player.connect();
}
});
window.onSpotifyWebPlaybackSDKReady = () => {
ready = true;
};
</script>
</html>