From 99acedb3431f2d1f0614c4ce4d3b4e4f68760943 Mon Sep 17 00:00:00 2001 From: Trek H Date: Thu, 18 Jul 2019 16:04:50 +0930 Subject: [PATCH] audio-player: playing pcm in browser --- cmd/audio-player/main.js | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/cmd/audio-player/main.js b/cmd/audio-player/main.js index 92795a5f..174b9059 100644 --- a/cmd/audio-player/main.js +++ b/cmd/audio-player/main.js @@ -1,25 +1,24 @@ window.onload = function () { - document.getElementById('input').addEventListener('change', getFile) + document.getElementById('input').addEventListener('change', playFile) } -function getFile(){ - const input = event.target - if ('files' in input && input.files.length > 0) { - placeFileContent(document.getElementById('content'), input.files[0]) - } +function playFile() { + const input = event.target.files[0] + + const reader = new FileReader() + reader.onload = event => playData(event.target.result) // desired file content + reader.onerror = error => reject(error) + reader.readAsArrayBuffer(input) // you could also read images and other binaries } -function placeFileContent(target, file) { - readFileContent(file).then(content => { - target.value = content - }).catch(error => console.log(error)) -} - -function readFileContent(file) { - const reader = new FileReader() - return new Promise((resolve, reject) => { - reader.onload = event => resolve(event.target.result) - reader.onerror = error => reject(error) - reader.readAsText(file) - }) -} +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) +} \ No newline at end of file