-
Notifications
You must be signed in to change notification settings - Fork 1
/
wasmbeat.js
44 lines (44 loc) · 1.42 KB
/
wasmbeat.js
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
import { buildWabt } from './src/build-wabt.js'
import { BytebeatNote } from './src/bytebeat-note.js'
const userInput = document.querySelector('textarea[name="code"]')
const output = document.querySelector('output[name="result"]')
const buildLog = document.getElementById('build-log')
const audio = new (window.AudioContext || window.webkitAudioContext)()
const gain = new GainNode(audio, { gain: 0.4 })
audio.audioWorklet.addModule('./src/bytebeat-processor.js')
gain.connect(audio.destination)
let currentModule
let currentNote
let currentInstance
document.querySelector('button[name="build"]').onclick = async () => {
try {
const { instance, module, log } = await buildWabt(userInput.value, true)
output.innerText = '✔'
buildLog.innerText = log
currentModule = module
currentInstance = instance
} catch (error) {
output.innerText = '❌'
}
}
document.querySelector('button[name="play"]').onclick = async () => {
if (currentModule) {
if (currentNote) currentNote.stop()
currentNote = new BytebeatNote(gain, { attack: 0.01, sustain: 0.5}, [
{
module: currentModule,
tempo: 120,
frequency: 8000,
},
])
}
}
document.querySelector('button[name="stop"]').onclick = () => {
if (currentNote) {
currentNote.stopNote(0)
currentNote = undefined
}
}
document.querySelector('input[name="gain"]').addEventListener('change', e => {
gain.gain.value = e.target.value
})