2019-07-18 06:41:52 +03:00
|
|
|
window.onload = function () {
|
2019-07-23 07:58:43 +03:00
|
|
|
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() {
|
2019-07-23 07:58:43 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-23 07:58:43 +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)
|
2019-07-23 07:58:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|