2019-09-14 16:05:37 +03:00
|
|
|
/*
|
|
|
|
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).
|
|
|
|
*/
|
|
|
|
|
2019-09-24 10:45:58 +03:00
|
|
|
// playFile will process and play the chosen target file.
|
|
|
|
function playFile() {
|
|
|
|
const input = event.target.files[0]
|
|
|
|
const reader = new FileReader()
|
2019-09-14 16:05:37 +03:00
|
|
|
|
2019-09-24 10:45:58 +03:00
|
|
|
reader.onload = event => {
|
2019-09-30 12:13:55 +03:00
|
|
|
bytes = new Uint8Array(event.target.result);
|
2019-09-14 16:05:37 +03:00
|
|
|
|
2019-09-27 09:06:25 +03:00
|
|
|
let dec = new Decoder();
|
|
|
|
|
2019-09-24 10:45:58 +03:00
|
|
|
// Decode adpcm to pcm.
|
2019-09-30 12:13:55 +03:00
|
|
|
let decoded = dec.decode(bytes);
|
2019-09-14 16:05:37 +03:00
|
|
|
|
2019-09-24 10:45:58 +03:00
|
|
|
// Convert raw pcm to wav TODO(Trek): make these configurable.
|
2019-09-30 12:13:55 +03:00
|
|
|
let wav = pcmToWav(new Uint8Array(decoded.buffer), 48000, 1, 16);
|
2019-09-14 16:05:37 +03:00
|
|
|
|
2019-09-24 10:45:58 +03:00
|
|
|
// Play wav data in player.
|
2019-09-30 12:13:55 +03:00
|
|
|
const blob = new Blob([wav], {
|
2019-09-24 10:45:58 +03:00
|
|
|
type: 'audio/wav'
|
|
|
|
});
|
|
|
|
const url = URL.createObjectURL(blob);
|
2019-09-14 16:05:37 +03:00
|
|
|
|
2019-09-24 10:45:58 +03:00
|
|
|
const audio = document.getElementById('audio');
|
|
|
|
const source = document.getElementById('source');
|
2019-09-14 16:05:37 +03:00
|
|
|
|
2019-09-24 10:45:58 +03:00
|
|
|
source.src = url;
|
|
|
|
audio.load();
|
|
|
|
audio.play();
|
|
|
|
}
|
|
|
|
reader.onerror = error => reject(error)
|
|
|
|
reader.readAsArrayBuffer(input)
|
2019-09-14 16:05:37 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// getQuery gets everything after the question mark in the URL.
|
|
|
|
function getQuery() {
|
2019-09-27 09:06:25 +03:00
|
|
|
let regex = new RegExp("\\?(.*)");
|
|
|
|
let match = regex.exec(window.location.href);
|
2019-09-24 10:45:58 +03:00
|
|
|
if (match == null) {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return decodeURIComponent(match[1].replace(/\+/g, " "));
|
|
|
|
}
|
2019-09-14 16:05:37 +03:00
|
|
|
}
|
|
|
|
|
2019-09-24 10:45:58 +03:00
|
|
|
// load gets the file from the given url and displays a link for download.
|
2019-09-14 16:05:37 +03:00
|
|
|
function load() {
|
2019-09-27 09:06:25 +03:00
|
|
|
let url = document.getElementById('url').value;
|
2019-09-24 10:45:58 +03:00
|
|
|
if (url == "") {
|
|
|
|
url = getQuery()
|
|
|
|
document.getElementById('url').value = url;
|
|
|
|
}
|
|
|
|
if (url[0] == '/') {
|
|
|
|
url = window.location.protocol + '//' + window.location.host + url;
|
|
|
|
}
|
|
|
|
if (url == "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-27 09:06:25 +03:00
|
|
|
let request = new XMLHttpRequest();
|
2019-09-24 10:45:58 +03:00
|
|
|
request.responseType = "blob";
|
|
|
|
request.onreadystatechange = function () {
|
|
|
|
if (request.readyState === XMLHttpRequest.DONE) {
|
|
|
|
if (request.status === 200) {
|
|
|
|
console.log("request received");
|
|
|
|
|
|
|
|
data = request.response;
|
|
|
|
|
|
|
|
dataURL = URL.createObjectURL(data);
|
|
|
|
|
2019-09-27 09:06:25 +03:00
|
|
|
let link = document.getElementById("link");
|
2019-09-24 10:45:58 +03:00
|
|
|
link.href = dataURL;
|
|
|
|
link.download = "media.ts";
|
|
|
|
link.innerHTML = "Download";
|
|
|
|
|
|
|
|
} else {
|
|
|
|
console.log('There was a problem with the request. Status: ' + request.status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
request.open("GET", url, true);
|
|
|
|
request.send();
|
2019-09-14 16:05:37 +03:00
|
|
|
}
|