|
1 | | -const canvasElement = document.getElementById('canvas') |
2 | | -const canvasContext = canvasElement.getContext('2d') |
3 | | -const WIDTH = canvasElement.width = window.innerWidth |
4 | | -const HEIGHT = canvasElement.height = 512 |
5 | | - |
6 | | -let audioCtx = new AudioContext() |
7 | | -let analyser = audioCtx.createAnalyser() |
8 | | -analyser.smoothingTimeConstant = 0.0 |
9 | | -analyser.fftSize = 1024 |
10 | | - |
11 | | -let bufferLength = analyser.frequencyBinCount |
12 | | -let eightBufferLength = 8 * bufferLength |
13 | | -let dataArray = new Uint8Array(bufferLength) |
14 | | - |
15 | | -let imageDataFrame = canvasContext.createImageData(2, canvasElement.height) |
16 | | -for (let i = 0; i < imageDataFrame.data.length * 4; i += 8) { |
17 | | - for(let j = 3; j <= 7; j++) { |
18 | | - imageDataFrame.data[i + j] = 255 // = 0,0,0,255|255,255,255,255 |
| 1 | +;(() => { |
| 2 | + const vscode = acquireVsCodeApi() |
| 3 | + const oldState = vscode.getState() |
| 4 | + |
| 5 | + const canvasElement = document.getElementById('canvas') |
| 6 | + const canvasContext = canvasElement.getContext('2d') |
| 7 | + const WIDTH = (canvasElement.width = window.innerWidth) |
| 8 | + const HEIGHT = (canvasElement.height = 512) |
| 9 | + // let currentCount = (oldState && oldState.count) || 0; |
| 10 | + |
| 11 | + // setInterval(() => { |
| 12 | + // counter.textContent = currentCount++; |
| 13 | + |
| 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 | + 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 | + } |
| 36 | + }) |
| 37 | + |
| 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 |
| 51 | + } |
19 | 52 | } |
20 | | -} |
21 | | - |
22 | | -function getData() { |
23 | | - let request = new XMLHttpRequest() |
24 | | - request.open('GET', rootPath, true) |
25 | | - request.responseType = 'arraybuffer' |
26 | | - |
27 | | - request.onload = () => { |
28 | | - var audioData = request.response |
29 | | - |
30 | | - audioCtx.decodeAudioData(audioData, buffer => { |
31 | | - |
32 | | - let offlineCtx = new OfflineAudioContext(2, buffer.length, 44100) |
33 | | - let source = offlineCtx.createBufferSource() |
34 | | - source.buffer = buffer |
35 | | - |
36 | | - source.connect(offlineCtx.destination) |
37 | | - source.start() |
38 | | - |
39 | | - offlineCtx |
40 | | - .startRendering() |
41 | | - .then(renderedBuffer => { |
42 | | - const song = audioCtx.createBufferSource() |
43 | | - song.buffer = renderedBuffer |
44 | | - |
45 | | - song.connect(audioCtx.destination) |
46 | | - song.connect(analyser) |
47 | | - // // play.onclick = function() { |
48 | | - // song.playbackRate.value = 2 |
49 | | - song.start() |
50 | | - // } |
51 | | - song.onended = function(e) { |
52 | | - console.log('finished') |
53 | | - } |
54 | | - analyser.getByteFrequencyData(dataArray) |
55 | | - draw() |
56 | | - }) |
57 | | - .catch(err => { |
58 | | - console.log('Rendering failed: ' + err) |
59 | | - // Note: The promise should reject when startRendering is called a second time on an OfflineAudioContext |
60 | | - }) |
61 | | - }) |
| 53 | + |
| 54 | + function getData() { |
| 55 | + let request = new XMLHttpRequest() |
| 56 | + request.open('GET', rootPath, true) |
| 57 | + request.responseType = 'arraybuffer' |
| 58 | + |
| 59 | + request.onload = () => { |
| 60 | + var audioData = request.response |
| 61 | + |
| 62 | + audioCtx.decodeAudioData(audioData, buffer => { |
| 63 | + let offlineCtx = new OfflineAudioContext(2, buffer.length, 44100) |
| 64 | + let source = offlineCtx.createBufferSource() |
| 65 | + source.buffer = buffer |
| 66 | + |
| 67 | + source.connect(offlineCtx.destination) |
| 68 | + source.start() |
| 69 | + |
| 70 | + offlineCtx |
| 71 | + .startRendering() |
| 72 | + .then(renderedBuffer => { |
| 73 | + const song = audioCtx.createBufferSource() |
| 74 | + song.buffer = renderedBuffer |
| 75 | + |
| 76 | + song.connect(audioCtx.destination) |
| 77 | + song.connect(analyser) |
| 78 | + // // play.onclick = function() { |
| 79 | + // song.playbackRate.value = 2 |
| 80 | + song.start() |
| 81 | + // } |
| 82 | + song.onended = function(e) { |
| 83 | + console.log('finished') |
| 84 | + } |
| 85 | + analyser.getByteFrequencyData(dataArray) |
| 86 | + draw() |
| 87 | + }) |
| 88 | + .catch(err => { |
| 89 | + console.log('Rendering failed: ' + err) |
| 90 | + // Note: The promise should reject when startRendering is called a second time on an OfflineAudioContext |
| 91 | + }) |
| 92 | + }) |
| 93 | + } |
| 94 | + request.send() |
62 | 95 | } |
63 | | - request.send() |
64 | | -} |
65 | 96 |
|
66 | | -getData() |
| 97 | + getData() |
67 | 98 |
|
68 | | -let x= 0 |
69 | | -function draw() { |
70 | | - requestAnimationFrame(draw) |
71 | | - analyser.getByteFrequencyData(dataArray) |
72 | | - for (let i = 0, y = eightBufferLength; i < bufferLength; i++, y -= 8) { |
73 | | - imageDataFrame.data[y] = dataArray[i] |
| 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] |
| 105 | + } |
| 106 | + canvasContext.putImageData(imageDataFrame, x, 0) |
| 107 | + x < WIDTH ? x++ : (x = 0) |
74 | 108 | } |
75 | | - canvasContext.putImageData(imageDataFrame, x, 0) |
76 | | - x < WIDTH ? x++ : x = 0 |
77 | | -} |
| 109 | +})() |
0 commit comments