|
6 | 6 | const canvasContext = canvasElement.getContext('2d') |
7 | 7 | const WIDTH = (canvasElement.width = window.innerWidth) |
8 | 8 | const HEIGHT = (canvasElement.height = 512) |
9 | | - // let currentCount = (oldState && oldState.count) || 0; |
10 | 9 |
|
11 | | - // setInterval(() => { |
12 | | - // counter.textContent = currentCount++; |
| 10 | + let playing, id |
13 | 11 |
|
14 | | - // // Update state |
15 | | - // vscode.setState({ count: currentCount }); |
16 | | - |
17 | | - // // Alert the extension when the cat introduces a bug |
18 | | - // if (Math.random() < Math.min(0.001 * currentCount, 0.05)) { |
19 | | - // // Send a message back to the extension |
20 | | - // vscode.postMessage({ |
21 | | - // command: 'alert', |
22 | | - // text: '🐛 on line ' + currentCount |
23 | | - // }); |
24 | | - // } |
25 | | - // }, 100); |
26 | | - |
27 | | - // Handle messages sent from the extension to the webview |
28 | 12 | window.addEventListener('message', event => { |
29 | | - const message = event.data // The json data that the extension sent |
30 | | - switch (message.command) { |
31 | | - case 'refactor': |
32 | | - currentCount = Math.ceil(currentCount * 0.5) |
33 | | - counter.textContent = currentCount |
34 | | - break |
35 | | - } |
| 13 | + if (playing) playing.close().then(cancelAnimationFrame(id)) |
| 14 | + playing = player(event.data) |
36 | 15 | }) |
37 | 16 |
|
38 | | - let audioCtx = new AudioContext() |
39 | | - let analyser = audioCtx.createAnalyser() |
40 | | - analyser.smoothingTimeConstant = 0.0 |
41 | | - analyser.fftSize = 1024 |
42 | | - |
43 | | - let bufferLength = analyser.frequencyBinCount |
44 | | - let eightBufferLength = 8 * bufferLength |
45 | | - let dataArray = new Uint8Array(bufferLength) |
46 | | - |
47 | | - let imageDataFrame = canvasContext.createImageData(2, canvasElement.height) |
48 | | - for (let i = 0; i < imageDataFrame.data.length * 4; i += 8) { |
49 | | - for (let j = 3; j <= 7; j++) { |
50 | | - imageDataFrame.data[i + j] = 255 // = 0,0,0,255|255,255,255,255 |
| 17 | + function player(path) { |
| 18 | + const audioCtx = new AudioContext() |
| 19 | + const analyser = audioCtx.createAnalyser() |
| 20 | + analyser.smoothingTimeConstant = 0.0 |
| 21 | + analyser.fftSize = 1024 |
| 22 | + |
| 23 | + let bufferLength = analyser.frequencyBinCount |
| 24 | + let eightBufferLength = 8 * bufferLength |
| 25 | + let dataArray = new Uint8Array(bufferLength) |
| 26 | + |
| 27 | + let imageDataFrame = canvasContext.createImageData(2, canvasElement.height) |
| 28 | + for (let i = 0; i < imageDataFrame.data.length * 4; i += 8) { |
| 29 | + for (let j = 3; j <= 7; j++) { |
| 30 | + imageDataFrame.data[i + j] = 255 // = 0,0,0,255|255,255,255,255 |
| 31 | + } |
51 | 32 | } |
52 | | - } |
53 | 33 |
|
54 | | - function getData() { |
55 | | - let request = new XMLHttpRequest() |
56 | | - request.open('GET', rootPath, true) |
| 34 | + const request = new XMLHttpRequest() |
| 35 | + request.open('GET', path, true) |
57 | 36 | request.responseType = 'arraybuffer' |
58 | 37 |
|
59 | 38 | request.onload = () => { |
60 | | - var audioData = request.response |
61 | | - |
62 | | - audioCtx.decodeAudioData(audioData, buffer => { |
| 39 | + audioCtx.decodeAudioData(request.response, buffer => { |
63 | 40 | let offlineCtx = new OfflineAudioContext(2, buffer.length, 44100) |
64 | 41 | let source = offlineCtx.createBufferSource() |
65 | 42 | source.buffer = buffer |
|
79 | 56 | // song.playbackRate.value = 2 |
80 | 57 | song.start() |
81 | 58 | // } |
82 | | - song.onended = function(e) { |
| 59 | + song.onended = event => { |
| 60 | + cancelAnimationFrame(id) |
83 | 61 | console.log('finished') |
84 | 62 | } |
85 | 63 | analyser.getByteFrequencyData(dataArray) |
86 | 64 | draw() |
87 | 65 | }) |
88 | 66 | .catch(err => { |
89 | 67 | console.log('Rendering failed: ' + err) |
90 | | - // Note: The promise should reject when startRendering is called a second time on an OfflineAudioContext |
91 | 68 | }) |
92 | 69 | }) |
93 | 70 | } |
94 | 71 | request.send() |
95 | | - } |
96 | 72 |
|
97 | | - getData() |
98 | | - |
99 | | - let x = 0 |
100 | | - function draw() { |
101 | | - requestAnimationFrame(draw) |
102 | | - analyser.getByteFrequencyData(dataArray) |
103 | | - for (let i = 0, y = eightBufferLength; i < bufferLength; i++, y -= 8) { |
104 | | - imageDataFrame.data[y] = dataArray[i] |
| 73 | + let x = 0 |
| 74 | + function draw() { |
| 75 | + id = requestAnimationFrame(draw) |
| 76 | + analyser.getByteFrequencyData(dataArray) |
| 77 | + for (let i = 0, y = eightBufferLength; i < bufferLength; i++, y -= 8) { |
| 78 | + imageDataFrame.data[y] = dataArray[i] |
| 79 | + } |
| 80 | + canvasContext.putImageData(imageDataFrame, x, 0) |
| 81 | + x < WIDTH ? x++ : (x = 0) |
105 | 82 | } |
106 | | - canvasContext.putImageData(imageDataFrame, x, 0) |
107 | | - x < WIDTH ? x++ : (x = 0) |
| 83 | + |
| 84 | + return audioCtx |
108 | 85 | } |
109 | 86 | })() |
0 commit comments