av/cmd/audio-player/main.js

59 lines
1.6 KiB
JavaScript

window.onload = function () {
initWasm()
document.getElementById('input').addEventListener('change', playFile)
}
function playFile() {
console.log(decode(48000))
const input = event.target.files[0]
const reader = new FileReader()
reader.onload = event => playData(event.target.result)
reader.onerror = error => reject(error)
reader.readAsArrayBuffer(input)
}
function playData(array) {
var data = new Uint8Array(array)
console.log("playing file")
var player = new PCMPlayer({
encoding: '16bitInt',
channels: 1,
sampleRate: 48000,
flushingTime: 2000
});
player.feed(data)
}
function initWasm() {
if (!WebAssembly.instantiateStreaming) {
// polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer()
return await WebAssembly.instantiate(source, importObject)
}
}
const go = new Go()
let mod, inst
// memoryBytes is an Uint8Array pointing to the webassembly linear memory.
let memoryBytes;
console.log("Initializing wasm...")
WebAssembly.instantiateStreaming(
fetch('lib.wasm'), go.importObject).then(
result => {
mod = result.module
inst = result.instance
memoryBytes = new Uint8Array(inst.exports.mem.buffer)
console.log("Initialization complete.")
run()
}
)
async function run() {
await go.run(inst)
inst = await WebAssembly.instantiate(mod, go.importObject) // reset instance
}
}