audio-player: decode function completed

This commit is contained in:
Trek H 2019-07-24 17:55:02 +09:30
parent 8a3eeec59d
commit 2c436b7edf
3 changed files with 22 additions and 10 deletions

View File

@ -30,6 +30,10 @@ let est, // Estimation of sample based on quantised ADPCM nibble.
idx, // Index to step used for estimation. idx, // Index to step used for estimation.
step; step;
function decodeSample(nibble) {
return new Uint8Array([1, 2])
}
function decode(b) { function decode(b) {
// b should be a Uint8Array // b should be a Uint8Array
if (!(b instanceof Uint8Array)) { if (!(b instanceof Uint8Array)) {
@ -49,15 +53,15 @@ function decode(b) {
var nib2 = twoNibs >> 4; var nib2 = twoNibs >> 4;
var nib1 = (nib2 << 4) ^ twoNibs var nib1 = (nib2 << 4) ^ twoNibs
var firstBytes = decodeSample(nib1) var firstBytes = decodeSample(nib1)
concat(result, firstBytes) result = concat(result, firstBytes)
var secondBytes = decodeSample(nib2) var secondBytes = decodeSample(nib2)
concat(result, secondBytes) result = concat(result, secondBytes)
} }
if (b[3] == 1) { if (b[3] == 1) {
var padNib = b[b.length - 1] var padNib = b[b.length - 1]
var samp = decodeSample(padNib) var samp = decodeSample(padNib)
concat(result, samp) result = concat(result, samp)
} }
return result; return result;

View File

@ -5,6 +5,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<title>Audio Player</title> <title>Audio Player</title>
<script type="text/javascript" src="pcm-player.min.js"></script> <script type="text/javascript" src="pcm-player.min.js"></script>
<script type="text/javascript" src="adpcm.js"></script>
<script type="text/javascript" src="main.js"></script> <script type="text/javascript" src="main.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

View File

@ -7,14 +7,21 @@ window.onload = function () {
reader.onload = event => { reader.onload = event => {
bytes = new Uint8Array(event.target.result) bytes = new Uint8Array(event.target.result)
console.log(bytes.slice(0, 16))
var decoded = decode(bytes)
console.log("playing file") console.log("playing file")
var player = new PCMPlayer({
encoding: '16bitInt', console.log(decoded.slice(0, 16))
channels: 1,
sampleRate: 48000, // var player = new PCMPlayer({
flushingTime: 2000 // encoding: '16bitInt',
}); // channels: 1,
player.feed(bytes) // sampleRate: 48000,
// flushingTime: 2000
// });
// player.feed(bytes)
} }
reader.onerror = error => reject(error) reader.onerror = error => reject(error)
reader.readAsArrayBuffer(input) reader.readAsArrayBuffer(input)