av/cmd/audio-player/main.js

145 lines
4.0 KiB
JavaScript

/*
NAME
main.js
AUTHOR
Trek Hopton <trek@ausocean.org>
Alan Noble <alan@ausocean.org>
LICENSE
This file is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see [GNU licenses](http://www.gnu.org/licenses).
*/
window.onload = function () {
document.getElementById('fileinput').addEventListener('change', processData);
}
function processData() {
const input = event.target.files[0]
const reader = new FileReader()
reader.onload = event => {
bytes = new Uint8Array(event.target.result)
// decode adpcm to pcm
var decoded = decode(Array.from(bytes))
// convert raw pcm to wav
var wav = pcmToWav(decoded, 48000, 1, 16);
// play wav data in player
const blob = new Blob([Uint8Array.from(wav)], {
type: 'audio/wav'
});
const url = URL.createObjectURL(blob);
const audio = document.getElementById('audio');
const source = document.getElementById('source');
source.src = url;
audio.load();
audio.play();
}
reader.onerror = error => reject(error)
reader.readAsArrayBuffer(input)
}
function sync(dateID, tsID, dateChanged) {
if (dateChanged) { // Go from date to Unix timestamp.
var s = document.getElementById(dateID).value;
if (s == "") {
document.getElementById(tsID).value = "";
} else {
if (s.length == 16) {
s += ':00'; // Append seconds.
}
s += 'Z';
var dt = new Date(s)
document.getElementById(tsID).value = (dt.getTime() / 1000).toString();
}
} else { // Go from Unix timestamp to local date.
var s = document.getElementById(dateID).value;
if (s == "") {
document.getElementById(tsID).value = "";
} else {
var ts = parseInt(document.getElementById(tsID).value) * 1000;
var dt = new Date(ts).toISOString().slice(0, -1);
document.getElementById(dateID).value = dt;
}
}
}
function presubmit() {
if (document.getElementById('st').value == "" && document.getElementById('sd').value != "") {
sync('sd', 'st', true);
}
if (document.getElementById('ft').value == "" && document.getElementById('fd').value != "") {
sync('fd', 'ft', true);
}
if (document.getElementById('ft').value == "") {
document.getElementById('ts').value = document.getElementById('st').value;
} else {
document.getElementById('ts').value = document.getElementById('st').value + '-' + document
.getElementById('ft').value;
}
}
// encodeMAC encodes a MAC address into a 48-bit integer.
function encodeMAC(mac) {
if (mac.length != 17) {
return 0;
}
var enc = BigInt(0);
for (i = 15, shift = 0; i >= 0; i -= 3, shift += 8) {
var n = BigInt(parseInt(mac.substring(i, i + 2), 16));
enc += (n << BigInt(shift));
}
return enc;
}
// encodePin encodes a pin into a 4-bit integer.
function encodePin(pin) {
var enc = Number(0);
if (pin.charAt(0) == 'S') {
enc = 8;
}
var pn = Number(pin.charAt(1) - '0');
if (pin.length > 2) {
pn = pn * 10 + Number(pin.CharAt(2) - '0');
}
enc |= pn;
return enc;
}
// toMID returns a Media ID as a BigInt given a MAC address and a pin.
function toMID(mac, pin) {
if (mac == "" || pin == "") {
return 0;
}
return (encodeMAC(mac) << BigInt(4)) | BigInt(encodePin(pin))
}
// updateMID updates a Media ID element using values from MAC and pin elements.
function updateMID(mac, pin, mid) {
var macElem = document.getElementById(mac);
var pinElem = document.getElementById(pin);
var midElem = document.getElementById(mid);
if (!macElem || !pinElem || !midElem) {
return;
}
midElem.value = toMID(macElem.value, pinElem.value).toString();
}