-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
282 lines (246 loc) · 9.05 KB
/
controller.js
File metadata and controls
282 lines (246 loc) · 9.05 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
'use strict'
const PLAY_ICON = '<i class="codicon codicon-play"></i>'
const PAUSE_ICON = '<i class="codicon codicon-debug-pause"></i>'
const REFRESH_ICON = '<i class="codicon codicon-refresh"></i>'
;(() => {
// eslint-disable-next-line no-undef
const vscode = acquireVsCodeApi()
const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('canvas'))
const canvasContext = canvas.getContext('2d')
canvas.height = 512
const susresBtn = /** @type {HTMLButtonElement} */ (document.getElementById('susresBtn'))
const backBtn = /** @type {HTMLButtonElement} */ (document.getElementById('backBtn'))
const forwardBtn = /** @type {HTMLButtonElement} */ (document.getElementById('forwardBtn'))
const durationText = document.getElementById('duration')
const fileLabel = document.getElementById('label')
const seekbar = /** @type {HTMLInputElement} */ (document.getElementById('seekbar'))
let currPlayer, id, durationId, rgbColor
// Receive data from vscode
window.addEventListener('message', event => {
if (currPlayer) {
currPlayer.close()
durationText.innerHTML = ''
cancelAnimationFrame(id)
clearTimeout(durationId)
}
rgbColor = event.data.rgbColor
currPlayer = player(event.data)
})
/**
* @param {{ path: string; name: string; }} file
*/
function player(file) {
canvas.width = window.innerWidth - 10
const WIDTH = canvas.width
togglePlaybackButtons('LOADING')
const audioCtx = new AudioContext()
const analyser = audioCtx.createAnalyser()
analyser.smoothingTimeConstant = 0.0
analyser.fftSize = 1024
const bufferLength = analyser.frequencyBinCount
const eightBufferLength = 8 * bufferLength
const dataArray = new Uint8Array(bufferLength)
const imageDataFrame = canvasContext.createImageData(2, canvas.height)
// Initialize the imageDataFrame with alternating black and white pixels
for (let i = 0; i < imageDataFrame.data.length; i += 8) {
// Set the first pixel to black (0, 0, 0, 255)
// This is the background color
imageDataFrame.data[i] = 0
imageDataFrame.data[i + 1] = 0
imageDataFrame.data[i + 2] = 0
imageDataFrame.data[i + 3] = 255
// Set the second pixel to white (255, 255, 255, 255)
// This is the color of the vertical moving line
imageDataFrame.data[i + 4] = 255
imageDataFrame.data[i + 5] = 255
imageDataFrame.data[i + 6] = 255
imageDataFrame.data[i + 7] = 255
}
const request = new XMLHttpRequest()
request.open('GET', file.path)
request.responseType = 'arraybuffer'
request.onload = () => audioCtx.decodeAudioData(request.response, audioCtxSetup, onBufferError)
request.send()
fileLabel.innerHTML = file.name
let source = audioCtx.createBufferSource()
let buffer, startAt, length, lengthMs, played = 0, isEnded = false
susresBtn.onclick = () => {
if (audioCtx.state === 'running' && !isEnded) {
audioCtx.suspend().then(() => {
susresBtn.innerHTML = PLAY_ICON
cancelAnimationFrame(id)
played += Date.now() - startAt
togglePlaybackButtons('PAUSED')
})
} else if (isEnded) {
isEnded = false
// Similar to start() + seek()
source.onended = null
source.disconnect(audioCtx.destination)
source.disconnect(analyser)
source = audioCtx.createBufferSource()
source.buffer = buffer
source.connect(audioCtx.destination)
source.connect(analyser)
source.onended = playEnd
source.start()
draw()
startAt = Date.now()
played = 0
durationWatch()
togglePlaybackButtons('PLAYING')
} else {
// Was suspended so resume it
audioCtx.resume().then(() => {
susresBtn.innerHTML = PAUSE_ICON
startAt = Date.now()
draw()
durationWatch()
togglePlaybackButtons('PLAYING')
})
}
}
backBtn.onclick = () => seek(-5000)
forwardBtn.onclick = () => seek(5000)
seekbar.oninput = () => seekTo(seekbar.value)
seekbar.onmousemove = (event) => showHoverDuration(event)
function audioCtxSetup(theBuffer) {
// This prevents clicking too fast - closed before starting
if (audioCtx.state === 'closed') return
if (audioCtx.state === 'suspended') {
// https://goo.gl/7K7WLu
vscode.postMessage({ type: 'INFO', message: 'Please click the play button - autoplay policy' })
}
isEnded = false
buffer = theBuffer
source.buffer = theBuffer
length = source.buffer.duration
lengthMs = length * 1000
source.connect(audioCtx.destination)
source.connect(analyser)
source.onended = playEnd
if (audioCtx.state === 'running') {
draw()
togglePlaybackButtons('PLAYING')
} else togglePlaybackButtons('READY')
source.start()
startAt = Date.now()
durationWatch()
seekbar.value = '0'
seekbar.max = lengthMs.toString()
}
function onBufferError(err) {
vscode.postMessage({ type: 'error', message: `Error with decoding audio data -> ${err}` })
}
function seek(ms) {
played += Date.now() - startAt
if (played === 0 && ms < 0) return
if (played === lengthMs && ms > 0) return
// Memory leaks if seeking too many since source wasn't properly free (AudioContext.close())?
source.onended = null
source.disconnect(audioCtx.destination)
source.disconnect(analyser)
source = audioCtx.createBufferSource()
source.buffer = buffer
source.connect(audioCtx.destination)
source.connect(analyser)
source.onended = playEnd
played += ms
if (played < 0) played = 0
if (played > lengthMs) played = lengthMs
startAt = Date.now()
source.start(0, played / 1000)
if (audioCtx.state === 'suspended') updateDurationText()
}
function seekTo(ms) {
played = parseInt(ms)
if (played < 0) played = 0
if (played > lengthMs) played = lengthMs
source.onended = null
source.disconnect(audioCtx.destination)
source.disconnect(analyser)
source = audioCtx.createBufferSource()
source.buffer = buffer
source.connect(audioCtx.destination)
source.connect(analyser)
source.onended = playEnd
startAt = Date.now()
source.start(0, played / 1000)
if (audioCtx.state === 'suspended') updateDurationText()
}
function playEnd() {
isEnded = true
clearTimeout(durationId)
togglePlaybackButtons('ENDED')
cancelAnimationFrame(id)
vscode.postMessage({ type: 'DONE', message: 'Playing ended' })
}
function togglePlaybackButtons(state) {
switch (state) {
case 'LOADING':
susresBtn.textContent = 'Loading...'
susresBtn.classList.add('disabled')
susresBtn.disabled = true
backBtn.style.display = 'none'
forwardBtn.style.display = 'none'
seekbar.style.display = 'none'
break
case 'READY':
susresBtn.innerHTML = PLAY_ICON
susresBtn.classList.remove('disabled')
susresBtn.disabled = false
backBtn.style.display = 'none'
forwardBtn.style.display = 'none'
seekbar.style.display = 'none'
break
case 'PAUSED':
case 'PLAYING':
susresBtn.innerHTML = state === 'PAUSED' ? PLAY_ICON : PAUSE_ICON
susresBtn.classList.remove('disabled')
susresBtn.disabled = false
backBtn.style.display = 'inline-block'
forwardBtn.style.display = 'inline-block'
seekbar.style.display = 'block'
break
case 'ENDED':
susresBtn.innerHTML = REFRESH_ICON
durationText.innerHTML = null
backBtn.style.display = 'none'
forwardBtn.style.display = 'none'
seekbar.style.display = 'none'
break
}
}
function durationWatch() {
if (audioCtx.state !== 'running') return
updateDurationText()
durationId = setTimeout(durationWatch, 1000)
}
function updateDurationText() {
const durationPlayed = Date.now() - startAt + played
durationText.innerHTML = `- ${fmtMSS(Math.trunc(durationPlayed / 1000))} | ${fmtMSS(Math.trunc(length))}`
seekbar.value = durationPlayed.toString()
}
function fmtMSS(s) {
return (s - (s %= 60)) / 60 + (9 < s ? ':' : ':0') + s
}
function showHoverDuration(event) {
const hoverTime = (event.offsetX / seekbar.clientWidth) * lengthMs
durationText.innerHTML = `- ${fmtMSS(Math.trunc(hoverTime / 1000))} | ${fmtMSS(Math.trunc(length))}`
}
let x = 0
function draw() {
id = requestAnimationFrame(draw)
analyser.getByteFrequencyData(dataArray)
for (let i = 0, y = eightBufferLength; i < bufferLength; i++, y -= 8) {
imageDataFrame.data[y] = rgbColor.r
imageDataFrame.data[y + 1] = rgbColor.g
imageDataFrame.data[y + 2] = rgbColor.b
imageDataFrame.data[y + 3] = dataArray[i]
}
canvasContext.putImageData(imageDataFrame, x, 0)
x < WIDTH ? x++ : (x = 0)
}
return audioCtx
}
})()