-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
373 lines (315 loc) · 13.8 KB
/
test.html
File metadata and controls
373 lines (315 loc) · 13.8 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<!DOCTYPE html lang="en-US">
<head>
<title>
My player
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/fontawesome.min.css" integrity="sha384-wESLQ85D6gbsF459vf1CiZ2+rr+CsxRY0RpiF1tLlQpDnAgg6rwdsUF1+Ics2bni" crossorigin="anonymous">
<script src="https://kit.fontawesome.com/4b97760acf.js" crossorigin="anonymous"></script>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Antonio:wght@400&display=swap" rel="stylesheet">
</head>
<style>
#mySongList {
margin-top: 50px;
font-family: 'Antonio', sans-serif;
width: 420px;
text-align: center;
border: solid;
border-top-left-radius: 20px;
border-top-right-radius: 20px;
padding-bottom: 20px;
padding-top: 20px;
background-color: #AED6F1;
}
#currentplay {
width: 420px;
padding-top: 10x;
padding-left: 20px;
padding-right: 20px;
border-style: solid;
border-bottom-left-radius: 20px;
border-bottom-right-radius: 20px;
padding-bottom: 20px;
background-color: #AED6F1;
}
#currentTitle {
font-family: 'Antonio', sans-serif;
margin-top: 20px;
width: 375px;
border-style: solid;
border-radius: 20px;
padding-top: 20px;
padding-left: 15px;
padding-bottom: 20px;
background-color: #DAF7A6;
margin-bottom: 20px;
}
#myvolume {
margin-left: 10px;
margin-right: 10px;
}
#songHeader {
background-color: #51AEE4;
width: 420px;
margin-top: 100px;
text-align:center;
}
#music_board {
border: solid;
width: 420;
background-color: #AED6F1;
}
</style>
<body>
<div class="container-sm" id="mySongList">
<h3>My Song Title</h3>
<ol class="list-group"></ol>
</div>
<div class="container-sm" id="music_board">
<div class="container-sm" id="my_display" style="padding-right: 10px;">
<canvas
id="my_music_drawing" width="368" height="100"
style="display: inline;"
></canvas>
</div>
<div class="container-sm">
<div id="switch">
<button id="display_switch">Click for Display</button>
</div>
</div>
</div>
<div class="container-sm">
<div class="container-sm" id="currentplay">
<div id="currentTitle">***************** No Song To Play ******************</div>
<div id="controller">
<audio id="myplayer" preload="metadata"></audio>
<button class="btn btn-warning" onclick="myAudioPlayer.playPrevious()">
<i class="fas fa-step-backward"></i>
</button>
<button type="button" class="btn btn-primary" id="playButton" onclick="myAudioPlayer.playSong()">
<i class="far fa-play-circle"></i>
</button>
<button class="btn btn-success" onclick="myAudioPlayer.playNext()">
<i class="fas fa-step-forward"></i>
</button>
<button class="btn btn-danger" onclick="myAudioPlayer.muteSound()">
<i class="fas fa-volume-mute"></i>
</button>
<!-- #scrubber input type="range" min="0" max="100" value="0" id="time_slider" onchange="displayTime()"-->
<i id="myvolume" class="fas fa-volume-up"></i><input type="range" min="0" max="100" value="50" onchange="myAudioPlayer.changeVolume(this.value)">
</div>
</div>
</div>
</body>
<script>
class AudioPlayer {
duration_sec = 0
current_play_sec = 0
trackIndex = 0
myTrack = null
audioElement = null
songStarted = false
songList = null
space_grap = " *"
plotted = false
constructor(myTrack) {
this.myTrack = myTrack
this.audioElement = document.getElementById("myplayer")
this.loadSongMenu()
this.trackAction()
this.changeColor(this.trackIndex)
}
loadSongMenu() {
this.songList = document.getElementById("mySongList").children[1]
for (let i = 0; i < this.myTrack.length; i++) {
this.songList.innerHTML += `<li class=\"list-group-item\"><a id=${i}>${myTrack[i].split(".")[0]}</a></li>`
}
}
changeVolume(value) {
this.audioElement.volume = value/100
if (this.audioElement.volume == 0) {
document.getElementById("myvolume").setAttribute("class", "fas fa-volume-mute")
} else {
document.getElementById("myvolume").setAttribute("class", "fas fa-volume-up")
}
}
displayTime() {
document.getElementById("time_slider").value = parseInt((this.audioElement.currentTime/this.audioElement.duration) * 100)
}
loadSong() {
if (!((this.trackIndex >= this.myTrack.length))) {
this.songStarted = true
this.audioElement.src = "http://127.0.0.1:8088/static/" + this.myTrack[this.trackIndex]
this.audioElement.volume = 0.5
this.audioElement.play()
this.changeColor(this.trackIndex)
this.audioElement.onloadedmetadata = (event) => {
this.duration_sec = this.audioElement.duration
this.duration_sec = parseInt(this.duration_sec / 60) + ":" + parseInt(this.duration_sec % 60)
}
this.audioElement.ontimeupdate = (event) => {
this.current_play_sec = this.audioElement.currentTime
if (parseInt(this.current_play_sec) % 45 == 0) {
if (!this.plotted) {
this.space_grap += "*"
this.plotted = !this.plotted
}
} else {
if(this.plotted) {
this.plotted = false
}
}
//the display can hold up to 52 characters
document.getElementById("currentTitle").innerHTML = this.myTrack[this.trackIndex].split(".")[0].slice(0, 35) +
this.space_grap + " " +
parseInt(this.current_play_sec /60) +
":" +
String(parseInt(this.current_play_sec % 60)).padStart(2, "0") +
" / " + this.duration_sec
}
this.audioElement.onended = (event) => {
if (this.trackIndex < this.myTrack.length) {
//this.songStarted = false
this.space_grap = " *"
this.playNext()
}
}
} else if (this.trackIndex < -1) {
this.resetJuke()
this.loadSong()
}
}
changeTrack(trackNumber) {
this.songStarted = false
this.trackIndex = trackNumber
}
resetJuke() {
this.trackIndex = 0
this.space_grap = " *"
this.songStarted = false
}
playSong() {
if (this.songStarted) {
this.pauseSong()
} else {
this.loadSong()
}
}
muteSound() {
this.audioElement.muted = !this.audioElement.muted
}
playNext() {
if (this.trackIndex >= this.myTrack.length - 1) {
document.getElementById("currentTitle").innerHTML = "You are at Last Track"
//this.trackIndex = 0
} else {
this.trackIndex++
this.songStarted= false
this.playSong()
}
//this.songStarted= false
this.space_grap = " *"
}
playPrevious() {
if (this.trackIndex <= 0) {
document.getElementById("currentTitle").innerHTML = "You are at the First Track"
//this.trackIndex = 0
} else {
this.trackIndex--
this.songStarted = false
this.playSong()
}
this.space_grap = " *"
}
pauseSong() {
if (!this.audioElement.paused) {
document.getElementById("playButton").children[0].setAttribute("class", "far fa-pause-circle")
this.audioElement.pause()
} else {
document.getElementById("playButton").children[0].setAttribute("class", "far fa-play-circle")
this.audioElement.play()
}
}
changeColor(index) {
for (let i = 0; i < this.songList.children.length; i++) {
this.songList.children[i].style.backgroundColor = "\#FCF3CF"
this.songList.children[i].style.color = "black"
}
if (index < this.songList.children.length) {
this.songList.children[index].style.backgroundColor = "\#D35400"
this.songList.children[index].style.color = "white"
}
}
trackAction() {
let songMenu = document.getElementsByTagName("li")
for (let i = 0; i < songMenu.length; i++) {
songMenu[i].onclick = (event) => {
this.changeTrack(i)
this.playSong()
this.space_grap = " *"
}
}
}
}
let myTrack = [
"time.flac",
"one_last_kiss.flac",
"桜流し (2021 Remastered).flac",
"Merry Christmas Mr Lawrence - FYI.flac",
"Come Back To Me.flac",
"Prisoner Of Love.flac",
"In My Room.flac"
]
let myAudioPlayer = new AudioPlayer(myTrack)
//code adapted from https://engineering.carsguide.com.au/javascripts-web-audio-api-3ad0e624945
let buttonClicked = false
let audioCtx = new (window.AudioContext || window.webkitAudioContext)();
let myAudio = document.querySelector('audio');
myAudio.crossOrigin = "anonymous";
let mySource = audioCtx.createMediaElementSource(myAudio);
let myAnalyzer = audioCtx.createAnalyser();
mySource.connect(myAnalyzer);
myAnalyzer.connect(audioCtx.destination);
myAnalyzer.fftSize = 4096;
let bufferLength = myAnalyzer.frequencyBinCount;
let dataArray = new Uint8Array(bufferLength);
let canvas = document.getElementById("my_music_drawing")
let ctx = canvas.getContext("2d")
let WIDTH = canvas.width;
let HEIGHT = canvas.height;
let barWidth = (WIDTH / bufferLength) * 2.5
let barHeight
function renderFrame() {
requestAnimationFrame(renderFrame);
let x = 0;
myAnalyzer.getByteFrequencyData(dataArray);
ctx.fillStyle = "white";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
for (let i = 0; i < bufferLength; i++) {
barHeight = dataArray[i] * 0.3;
let r = barHeight + (25 * (i/bufferLength));
let g = 200 * (i/bufferLength);
let b = 100;
ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);
x += barWidth;
}
}
renderFrame()
document.getElementById("display_switch").addEventListener('click', function() {
buttonClicked = !buttonClicked
if (buttonClicked) {
audioCtx.resume().then(() => {
console.log('Playback resumed successfully');
});
} else {
audioCtx.suspend().then(() => {
console.log('Playback stopped successfully');
});
}
});
</script>
</html>