Skip to content

Commit 4bdd8a7

Browse files
committed
Basic playing
1 parent 3ebfb0c commit 4bdd8a7

File tree

5 files changed

+51
-70
lines changed

5 files changed

+51
-70
lines changed

src/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/controller.js

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,37 @@
66
const canvasContext = canvasElement.getContext('2d')
77
const WIDTH = (canvasElement.width = window.innerWidth)
88
const HEIGHT = (canvasElement.height = 512)
9-
// let currentCount = (oldState && oldState.count) || 0;
109

11-
// setInterval(() => {
12-
// counter.textContent = currentCount++;
10+
let playing, id
1311

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
2812
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)
3615
})
3716

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+
}
5132
}
52-
}
5333

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)
5736
request.responseType = 'arraybuffer'
5837

5938
request.onload = () => {
60-
var audioData = request.response
61-
62-
audioCtx.decodeAudioData(audioData, buffer => {
39+
audioCtx.decodeAudioData(request.response, buffer => {
6340
let offlineCtx = new OfflineAudioContext(2, buffer.length, 44100)
6441
let source = offlineCtx.createBufferSource()
6542
source.buffer = buffer
@@ -79,31 +56,31 @@
7956
// song.playbackRate.value = 2
8057
song.start()
8158
// }
82-
song.onended = function(e) {
59+
song.onended = event => {
60+
cancelAnimationFrame(id)
8361
console.log('finished')
8462
}
8563
analyser.getByteFrequencyData(dataArray)
8664
draw()
8765
})
8866
.catch(err => {
8967
console.log('Rendering failed: ' + err)
90-
// Note: The promise should reject when startRendering is called a second time on an OfflineAudioContext
9168
})
9269
})
9370
}
9471
request.send()
95-
}
9672

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)
10582
}
106-
canvasContext.putImageData(imageDataFrame, x, 0)
107-
x < WIDTH ? x++ : (x = 0)
83+
84+
return audioCtx
10885
}
10986
})()

src/extension.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const pug = require('pug')
55
const path = require('path')
66
const jquery_path = require.resolve('jquery')
77
const semjs_path = require.resolve('semantic-ui-css')
8-
const semcss_path = require.resolve('semantic-ui-css/semantic.min.css');
8+
const semcss_path = require.resolve('semantic-ui-css/semantic.min.css')
99

1010
/**
1111
* @param {vscode.ExtensionContext} context
@@ -21,25 +21,27 @@ function activate(context) {
2121

2222
const compiledFunction = pug.compileFile(`${__dirname}\\index.pug`)
2323

24-
const wv = vscode.window.createWebviewPanel('something','Cat Coding', vscode.ViewColumn.One, {
24+
const wv = vscode.window.createWebviewPanel('something', 'Cat Coding', vscode.ViewColumn.One, {
2525
// Enable javascript in the webview
26-
enableScripts: true,
26+
enableScripts: true
2727
// And restric the webview to only loading content from our extension's `media` directory.
2828
// localResourceRoots: [vscode.Uri.file(path.join(extensionPath, 'media'))]
2929
})
3030

3131
specTV.specViewer.onDidChangeSelection(file => {
32-
if(specTV.specViewer.selection.indexOf('.mp3') != -1)
33-
wv.webview.postMessage(specTV.specViewer.selection)
32+
const fullFilePath = file.selection[0].fullFilePath
33+
if (fullFilePath.indexOf('.mp3') != -1) {
34+
const song_path = vscode.Uri.file(fullFilePath).with({ scheme: 'vscode-resource' })
35+
wv.webview.postMessage(`${song_path}`)
36+
}
3437
})
3538

36-
37-
const song_path = (vscode.Uri.file(path.join(context.extensionPath, 'beat.mp3'))).with({scheme: 'vscode-resource'})
38-
const bundle_uri = (vscode.Uri.file(path.join(context.extensionPath, 'src', 'bundle.js'))).with({scheme: 'vscode-resource'})
39-
const ctmcss_uri = (vscode.Uri.file(path.join(context.extensionPath, 'src', 'custom.css'))).with({scheme: 'vscode-resource'})
40-
const semjs_uri = (vscode.Uri.file(semjs_path)).with({scheme: 'vscode-resource'})
41-
const semcss_uri = (vscode.Uri.file(semcss_path)).with({scheme: 'vscode-resource'})
42-
const jquery_uri = (vscode.Uri.file(jquery_path)).with({scheme: 'vscode-resource'})
39+
const song_path = vscode.Uri.file(path.join(context.extensionPath, 'beat.mp3')).with({ scheme: 'vscode-resource' })
40+
const bundle_uri = vscode.Uri.file(path.join(context.extensionPath, 'src', 'bundle.js')).with({ scheme: 'vscode-resource' })
41+
const ctmcss_uri = vscode.Uri.file(path.join(context.extensionPath, 'src', 'custom.css')).with({ scheme: 'vscode-resource' })
42+
const semjs_uri = vscode.Uri.file(semjs_path).with({ scheme: 'vscode-resource' })
43+
const semcss_uri = vscode.Uri.file(semcss_path).with({ scheme: 'vscode-resource' })
44+
const jquery_uri = vscode.Uri.file(jquery_path).with({ scheme: 'vscode-resource' })
4345

4446
wv.webview.html = compiledFunction({
4547
bundle_uri: bundle_uri,

src/treeview.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class fileItem extends vscode.TreeItem {
7979
this.label = label
8080
this.collapsibleState = collapsibleState
8181
this.filePath = filePath
82+
this.fullFilePath = `${this.filePath}\\${this.label}`
8283
this.command = command
8384
this.contextValue = 'dependency'
8485
this.descriptionText = descriptionText

webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path')
22
module.exports = {
3+
mode: 'production',
34
entry: "./src/controller.js",
45
output: {
56
filename: "bundle.js",

0 commit comments

Comments
 (0)