Skip to content

Commit 44d5e90

Browse files
committed
Remove p5 use Web Audio API
1 parent 3abf298 commit 44d5e90

File tree

7 files changed

+135
-61
lines changed

7 files changed

+135
-61
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.vscode-test/
22
*.mp3
3+
*.ogg
34
*.vsix
45
media
56
node_modules

.vscodeignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
.gitignore
66
.vscode-test/**
77
.vscode/**
8+
*.mp3
9+
*.ogg
810
package-lock
911
test/**
1012
vsc-extension-quickstart.md

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
},
4747
"dependencies": {
4848
"jquery": "^3.3.1",
49-
"p5": "^0.7.3",
5049
"pug": "^2.0.3",
5150
"semantic-ui-css": "^2.4.1",
5251
"vue": "^2.6.2",

src/bundle.js

Lines changed: 3 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/extension.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ function activate(context) {
3232
// localResourceRoots: [vscode.Uri.file(path.join(extensionPath, 'media'))]
3333
})
3434
const vue_template_uri = (vscode.Uri.file(path.join(context.extensionPath, 'src', 'vue_template.js'))).with({scheme: 'vscode-resource'})
35+
const song_path = (vscode.Uri.file(path.join(context.extensionPath, 'angel.mp3'))).with({scheme: 'vscode-resource'})
3536
const sk_uri = (vscode.Uri.file(path.join(context.extensionPath, 'src', 'bundle.js'))).with({scheme: 'vscode-resource'})
3637
const semjs_uri = (vscode.Uri.file(semjs_path)).with({scheme: 'vscode-resource'})
3738
const semcss_uri = (vscode.Uri.file(semcss_path)).with({scheme: 'vscode-resource'})
3839
const jquery_uri = (vscode.Uri.file(jquery_path)).with({scheme: 'vscode-resource'})
40+
3941
wv.webview.html = compiledFunction({
4042
sk_uri: sk_uri,
4143
semjs_uri: semjs_uri,
4244
semcss_uri: semcss_uri,
4345
vue_uri: vue_template_uri,
44-
jquery_uri: jquery_uri,
46+
jquery_uri: jquery_uri,
47+
song_path: song_path,
4548
nonce: '123'
4649
})
4750
}

src/index.pug

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,18 @@
11
html
22
head
3-
title VueJs Instance
3+
title Audio
44
meta(charset='UTF-8')
5-
meta(http-equiv='Content-Security-Policy', content=`default-src 'none'; img-src vscode-resource: https:; script-src 'nonce-${nonce}'; style-src 'nonce-${nonce}';`)
5+
meta(http-equiv='Content-Security-Policy',
6+
content=`default-src 'none' file:; img-src vscode-resource: https:; script-src 'nonce-${nonce}'; style-src 'nonce-${nonce}' https:; font-src data: vscode-resource: https:; worker-src blob:; connect-src vscode-resource:`)
67
meta(name='viewport', content='width=device-width, initial-scale=1.0')
8+
script(nonce=`${nonce}`).
9+
rootPath = '#{song_path}'
710
script(nonce=`${nonce}`, src=`${jquery_uri}`)
811
script(nonce=`${nonce}`, src=`${semjs_uri}`)
12+
script(nonce=`${nonce}`, src=`${vue_uri}`)
913
link(nonce=`${nonce}`, rel='stylesheet', type="text/css", href=`${semcss_uri}`)
10-
1114
body
12-
h1 Firstname : #{firstname}
13-
h1 Lastname : #{lastname}
14-
.sketch
15-
h3 #{semcss_uri}
16-
17-
18-
19-
button.ui.secondary.button Okay
20-
button.ui.button Cancel
21-
.ui.inverted.segment
22-
button.ui.inverted.button Standard
23-
button.ui.inverted.primary.button Primary
24-
button.ui.inverted.secondary.button Secondary
25-
button.ui.inverted.red.button Red
26-
button.ui.inverted.orange.button Orange
27-
button.ui.inverted.yellow.button Yellow
28-
button.ui.inverted.olive.button Olive
29-
button.ui.inverted.green.button Green
30-
button.ui.inverted.teal.button Teal
31-
button.ui.inverted.blue.button Blue
32-
button.ui.inverted.violet.button Violet
33-
button.ui.inverted.purple.button Purple
34-
button.ui.inverted.pink.button Pink
35-
button.ui.inverted.brown.button Brown
36-
button.ui.inverted.grey.button Grey
37-
button.ui.inverted.black.button Black
38-
39-
40-
41-
script(nonce=`${nonce}`, src=`${vue_uri}`)
42-
//- script(nonce=`${nonce}`, src=`${p5_uri}`)
43-
script(nonce=`${nonce}`, src=`${sk_uri}`)
44-
15+
p hello world
16+
canvas#canvas(height='512', width='800')
17+
script(nonce=`${nonce}`, src=`${sk_uri}`)
4518

46-
script(nonce=`${nonce}`).
47-
console.log('#{p5_uri}')

src/sketch.js

Lines changed: 116 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,121 @@
1+
import { request } from 'http'
12

2-
const p5 = require('p5')
3+
let canvasElement = document.getElementById('canvas')
4+
let canvasContext = canvasElement.getContext('2d')
5+
let WIDTH = canvasElement.width
6+
let flag = true
37

4-
let sketch = function(p) {
5-
p.setup = function() {
6-
p.createCanvas(100, 100)
7-
p.background(255)
8+
// define online and offline audio context
9+
let audioCtx = new AudioContext()
10+
11+
let analyser = audioCtx.createAnalyser()
12+
analyser.smoothingTimeConstant = 0.0
13+
14+
analyser.fftSize = 1024
15+
let bufferLength = analyser.frequencyBinCount
16+
let eightBufferLength = 8 * bufferLength + 1
17+
let dataArray = new Uint8Array(bufferLength)
18+
19+
// setup the imageDataFrame early
20+
let imageDataFrame = canvasContext.createImageData(2, canvasElement.height)
21+
22+
for (var index = 0; index < imageDataFrame.data.length * 4; index += 8) {
23+
imageDataFrame.data[index] = imageDataFrame.data[index + 6] = 0
24+
imageDataFrame.data[index + 3] = imageDataFrame.data[
25+
index + 4
26+
] = imageDataFrame.data[index + 5] = imageDataFrame.data[index + 7] = 255
27+
}
28+
29+
30+
31+
// use XHR to load an audio track, and
32+
// decodeAudioData to decode it and OfflineAudioContext to render it
33+
34+
function getData() {
35+
// var reader = new FileReader()
36+
// reader.onload = function(ev) {}
37+
38+
let request = new XMLHttpRequest()
39+
let printLog = (a, b) => console.log(JSON.stringify(a), b)
40+
request.open('GET', rootPath, true)
41+
request.addEventListener('progress', printLog)
42+
request.addEventListener('load', printLog)
43+
request.addEventListener('error', printLog)
44+
request.addEventListener('abort', printLog)
45+
request.responseType = 'arraybuffer'
46+
47+
request.onload = () => {
48+
var audioData = request.response
49+
50+
audioCtx.decodeAudioData(audioData, buffer => {
51+
console.log(buffer.length)
52+
let myBuffer = buffer
53+
54+
let offlineCtx = new OfflineAudioContext(2, buffer.length, 44100)
55+
let source = offlineCtx.createBufferSource()
56+
source.buffer = myBuffer
57+
source.connect(offlineCtx.destination)
58+
source.start()
59+
//source.loop = true;
60+
61+
offlineCtx
62+
.startRendering()
63+
.then(renderedBuffer => {
64+
console.log('Rendering completed successfully')
65+
66+
var song = audioCtx.createBufferSource()
67+
song.buffer = renderedBuffer
68+
console.log(JSON.stringify(renderedBuffer))
69+
70+
song.connect(audioCtx.destination)
71+
song.connect(analyser)
72+
// // play.onclick = function() {
73+
song.playbackRate.value = 5
74+
song.start()
75+
// }
76+
song.onended = function(e) {
77+
flag = false
78+
console.log('finished')
79+
}
80+
81+
analyser.getByteFrequencyData(dataArray)
82+
console.log(Array.apply([], dataArray).join(","));
83+
draw()
84+
})
85+
.catch(err => {
86+
console.log('Rendering failed: ' + err)
87+
// Note: The promise should reject when startRendering is called a second time on an OfflineAudioContext
88+
})
89+
})
890
}
91+
request.send()
992
}
1093

11-
new p5(sketch, 'sketch')
12-
console.log('sketch here')
94+
// Run getData to start the process off
95+
96+
getData()
97+
98+
let x= 0
99+
function draw() {
100+
if(flag) requestAnimationFrame(draw)
101+
analyser.getByteFrequencyData(dataArray)
102+
103+
104+
// for(var index = bufferLength, y = 0; index > 0; --index, y += 8)
105+
for (
106+
var index = 0, y = eightBufferLength;
107+
index < bufferLength;
108+
++index, y -= 8
109+
) {
110+
imageDataFrame.data[y] = imageDataFrame.data[y + 1] = dataArray[index]
111+
// increment by 8, as to not interfere with the playhead (second coloumn of pixels at x=1)
112+
}
113+
114+
canvasContext.putImageData(imageDataFrame, x, 0)
115+
116+
if (x < WIDTH) {
117+
x++
118+
} else {
119+
x = 0
120+
}
121+
}

0 commit comments

Comments
 (0)