Decode audio files with AudioContext
This commit is contained in:
parent
a4f975b5e5
commit
4f005e86c2
99
js/ngfile.js
99
js/ngfile.js
@ -128,45 +128,74 @@ app.controller('mainController', function ($scope, $http, wave) {
|
||||
}
|
||||
|
||||
|
||||
reader.onload = function (e) {
|
||||
var x = reader.result.split(/[\s,;\t\r\n]+/);
|
||||
function parseTextData(inputText) {
|
||||
var x = inputText.split(/[\s,;\t\r\n]+/);
|
||||
|
||||
var j = 0;
|
||||
var j = 0;
|
||||
|
||||
for (var i = 0; i < x.length; i++) {
|
||||
if (!isNaN(parseFloat(x[i]))) {
|
||||
j = j + 1;
|
||||
}
|
||||
}
|
||||
|
||||
wave.sigLength = j;
|
||||
wave.sigData = new Float64Array(j);
|
||||
|
||||
j = 0;
|
||||
|
||||
for (var i = 0; i < x.length; i++) {
|
||||
temp = parseFloat(x[i]);
|
||||
if (!isNaN(temp)) {
|
||||
wave.sigData[j] = temp;
|
||||
j = j + 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
var rm = x.length - j;
|
||||
if (rm > 0) {
|
||||
wave.sigData.splice(j, rm);
|
||||
}
|
||||
*/
|
||||
if (wave.transform == 1) {
|
||||
location.href = '#/display';
|
||||
} else if (wave.transform == 2) {
|
||||
location.href = '#/cdisplay';
|
||||
} else if (wave.transform == 4) {
|
||||
location.href = '#/denoise';
|
||||
}
|
||||
for (var i = 0; i < x.length; i++) {
|
||||
if (!isNaN(parseFloat(x[i]))) {
|
||||
j = j + 1;
|
||||
}
|
||||
}
|
||||
|
||||
wave.sigLength = j;
|
||||
wave.sigData = new Float64Array(j);
|
||||
|
||||
j = 0;
|
||||
|
||||
for (var i = 0; i < x.length; i++) {
|
||||
temp = parseFloat(x[i]);
|
||||
if (!isNaN(temp)) {
|
||||
wave.sigData[j] = temp;
|
||||
j = j + 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
var rm = x.length - j;
|
||||
if (rm > 0) {
|
||||
wave.sigData.splice(j, rm);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
async function parseAudioData(inputBuffer) {
|
||||
console.log("Converting a", inputBuffer.byteLength,
|
||||
"bytes audio buffer to linear PCM");
|
||||
|
||||
var audioCtx = new AudioContext({ sampleRate: 44100 });
|
||||
var pcmData = await audioCtx.decodeAudioData(inputBuffer);
|
||||
var pcmSamples = pcmData.getChannelData(0); // Float32Array
|
||||
|
||||
wave.sigData = new Float64Array(pcmSamples);
|
||||
wave.sigLength = pcmSamples.length;
|
||||
}
|
||||
|
||||
reader.onload = async function (e) {
|
||||
var input = reader.result;
|
||||
|
||||
if (input instanceof ArrayBuffer)
|
||||
await parseAudioData(input);
|
||||
else
|
||||
parseTextData(input);
|
||||
|
||||
console.log("Input wave:", wave.sigLength, "samples");
|
||||
|
||||
if (wave.transform == 1) {
|
||||
location.href = '#/display';
|
||||
} else if (wave.transform == 2) {
|
||||
location.href = '#/cdisplay';
|
||||
} else if (wave.transform == 4) {
|
||||
location.href = '#/denoise';
|
||||
} else {
|
||||
location.href = '#/cdisplay';
|
||||
}
|
||||
}
|
||||
|
||||
if (finp1.type == "audio/mpeg")
|
||||
reader.readAsArrayBuffer(finp1);
|
||||
else
|
||||
reader.readAsText(finp1);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user