av/cmd/audio-player/main.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-07-18 06:41:52 +03:00
window.onload = function () {
initWasm()
2019-07-18 09:34:50 +03:00
document.getElementById('input').addEventListener('change', playFile)
2019-07-18 06:41:52 +03:00
}
2019-07-18 09:34:50 +03:00
function playFile() {
console.log(decode(48000))
2019-07-18 09:34:50 +03:00
const input = event.target.files[0]
2019-07-18 06:41:52 +03:00
2019-07-18 09:34:50 +03:00
const reader = new FileReader()
2019-07-18 09:42:31 +03:00
reader.onload = event => playData(event.target.result)
2019-07-18 09:34:50 +03:00
reader.onerror = error => reject(error)
2019-07-18 09:42:31 +03:00
reader.readAsArrayBuffer(input)
2019-07-18 06:41:52 +03:00
}
function playData(array) {
2019-07-18 09:34:50 +03:00
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
}
2019-07-18 09:34:50 +03:00
}