From b4a850714008ab84db4b59a8d3994502bc139354 Mon Sep 17 00:00:00 2001 From: kkapsner Date: Sat, 30 Jun 2018 23:15:47 +0200 Subject: [PATCH] Added test for audio faking --- test/audioTest.html | 16 +++++++++++ test/audioTest.js | 66 +++++++++++++++++++++++++++++++++++++++++++++ test/index.html | 1 + 3 files changed, 83 insertions(+) create mode 100644 test/audioTest.html create mode 100644 test/audioTest.js diff --git a/test/audioTest.html b/test/audioTest.html new file mode 100644 index 0000000..e056759 --- /dev/null +++ b/test/audioTest.html @@ -0,0 +1,16 @@ + + + + Audio test + + + +

Audio test

+
+ Hash:
+ Sum: + +
+ + + \ No newline at end of file diff --git a/test/audioTest.js b/test/audioTest.js new file mode 100644 index 0000000..68734bb --- /dev/null +++ b/test/audioTest.js @@ -0,0 +1,66 @@ +(function(){ + "use strict"; + + function byteArrayToHex(arrayBuffer){ + var chunks = []; + (new Uint32Array(arrayBuffer)).forEach(function(num){ + chunks.push(num.toString(16)); + }); + return chunks.map(function(chunk){ + return "0".repeat(8 - chunk.length) + chunk; + }).join(""); + } + + var container = document.getElementById("test"); + + var pxi_output; + var pxi_full_buffer; + function run_pxi_fp(){ + var context = new window.OfflineAudioContext(1, 44100, 44100); + + // Create oscillator + var pxi_oscillator = context.createOscillator(); + pxi_oscillator.type = "triangle"; + pxi_oscillator.frequency.value = 1e4; + + // Create and configure compressor + var pxi_compressor = context.createDynamicsCompressor(); + pxi_compressor.threshold && (pxi_compressor.threshold.value = -50); + pxi_compressor.knee && (pxi_compressor.knee.value = 40); + pxi_compressor.ratio && (pxi_compressor.ratio.value = 12); + pxi_compressor.reduction && (pxi_compressor.reduction.value = -20); + pxi_compressor.attack && (pxi_compressor.attack.value = 0); + pxi_compressor.release && (pxi_compressor.release.value = .25); + + // Connect nodes + pxi_oscillator.connect(pxi_compressor); + pxi_compressor.connect(context.destination); + + // Start audio processing + pxi_oscillator.start(0); + context.startRendering(); + context.oncomplete = function(evnt) { + var str = ""; + var copyTest = new Float32Array(44100); + evnt.renderedBuffer.copyFromChannel(copyTest, 0); + var getTest = evnt.renderedBuffer.getChannelData(0); + Promise.all([ + crypto.subtle.digest("SHA-256", getTest), + crypto.subtle.digest("SHA-256", copyTest), + ]).then(function(hashes){ + container.querySelector(".hash").textContent = + byteArrayToHex(hashes[0]) + + " / " + + byteArrayToHex(hashes[1]); + }); + var sum = 0; + for (var i = 4500; i < 5000; i += 1) { + sum += Math.abs(getTest[i]); + } + container.querySelector(".sum").textContent = sum; + pxi_compressor.disconnect(); + }; + } + run_pxi_fp(); + container.querySelector("button").addEventListener("click", run_pxi_fp); +}()); \ No newline at end of file diff --git a/test/index.html b/test/index.html index 7f6d37b..4f7fcad 100644 --- a/test/index.html +++ b/test/index.html @@ -8,6 +8,7 @@