1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-04 12:36:37 +02:00

test updates

This commit is contained in:
kkapsner 2019-12-16 19:27:28 +01:00
parent 717e1d3e3a
commit 0d0e3e30ec
16 changed files with 455 additions and 497 deletions

View file

@ -2,7 +2,7 @@
"use strict";
function byteArrayToHex(arrayBuffer){
var chunks = [];
const chunks = [];
(new Uint32Array(arrayBuffer)).forEach(function(num){
chunks.push(num.toString(16));
});
@ -11,17 +11,17 @@
}).join("");
}
var container = document.getElementById("test");
var hashContainer = container.querySelector(".hashes");
var hashSets = Object.create(null);
const container = document.getElementById("test");
const hashContainer = container.querySelector(".hashes");
let hashSets = Object.create(null);
function createSet(set){
if (!hashSets[set]){
var setContainer = document.createElement("tbody");
const setContainer = document.createElement("tbody");
hashContainer.appendChild(setContainer);
var nameRow = document.createElement("tr");
const nameRow = document.createElement("tr");
setContainer.appendChild(nameRow);
var nameContainer = document.createElement("th");
const nameContainer = document.createElement("th");
nameRow.appendChild(nameContainer);
nameContainer.colSpan = 2;
nameContainer.textContent = set;
@ -29,37 +29,34 @@
}
}
function displayData(data, set, title){
async function displayData(data, set, title){
createSet(set);
var container = document.createElement("tr");
const container = document.createElement("tr");
var titleNode = document.createElement("td");
const titleNode = document.createElement("td");
titleNode.textContent = title;
container.appendChild(titleNode);
var hashNode = document.createElement("td");
const hashNode = document.createElement("td");
hashNode.textContent = "calculating hash";
container.appendChild(hashNode);
crypto.subtle.digest("SHA-256", data).then(function(hash){
hashNode.textContent = byteArrayToHex(hash);
return;
}).catch(function(error){
hashNode.textContent = error;
});
hashSets[set].appendChild(container);
const hash = await crypto.subtle.digest("SHA-256", data);
hashNode.textContent = byteArrayToHex(hash);
}
function getAudioContext(frequency = 1e4){
var context = new window.OfflineAudioContext(2, 44100, 44100);
const context = new window.OfflineAudioContext(2, 44100, 44100);
// Create oscillator
var pxi_oscillator = context.createOscillator();
const pxi_oscillator = context.createOscillator();
pxi_oscillator.type = "triangle";
pxi_oscillator.frequency.value = frequency;
// Create and configure compressor
var pxi_compressor = context.createDynamicsCompressor();
const 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);
@ -77,28 +74,28 @@
}
function createEmptyData(){
var emptyArray = new Float32Array(44100);
const emptyArray = new Float32Array(44100);
displayData(emptyArray, "empty buffer", "no API involved");
var emptyContext = new OfflineAudioContext(1, 44100, 44100);
var emptyBuffer = emptyContext.createBuffer(1, 44100, 44100);
const emptyContext = new OfflineAudioContext(1, 44100, 44100);
const emptyBuffer = emptyContext.createBuffer(1, 44100, 44100);
var emptyCopy = new Float32Array(44100);
const emptyCopy = new Float32Array(44100);
emptyBuffer.copyFromChannel(emptyCopy, 0);
displayData(emptyCopy, "empty buffer", "copyFromChannel - first");
var emptyData = emptyBuffer.getChannelData(0);
const emptyData = emptyBuffer.getChannelData(0);
displayData(emptyData, "empty buffer", "getChannelData - first");
displayData(emptyBuffer.getChannelData(0), "empty buffer", "getChannelData - second");
var emptyCopy2 = new Float32Array(44100);
const emptyCopy2 = new Float32Array(44100);
emptyBuffer.copyFromChannel(emptyCopy2, 0);
displayData(emptyCopy2, "empty buffer", "copyFromChannel - second");
}
function getIframeWindow(){
var l = window.length;
var iframe = document.createElement("iframe");
const l = window.length;
const iframe = document.createElement("iframe");
document.body.appendChild(iframe);
const iframeWindow = window[l];
document.body.removeChild(iframe);
@ -107,43 +104,43 @@
function createHashData(frequency = 1e4){
var context = getAudioContext(frequency);
const context = getAudioContext(frequency);
var setName = " (" + frequency + " Hz)";
const setName = " (" + frequency + " Hz)";
createSet(setName);
// Start audio processing
context.startRendering();
context.oncomplete = function(event){
var copyTestIframe = new (getIframeWindow().Float32Array)(44100);
const copyTestIframe = new (getIframeWindow().Float32Array)(44100);
getIframeWindow().AudioBuffer.prototype.copyFromChannel.call(event.renderedBuffer, copyTestIframe, 0);
displayData(copyTestIframe, setName, "copyFromChannel - iframe");
var chunkTest = new Float32Array(44100);
var number = new Float32Array(100);
for (var chunkI = 0; chunkI < 44100; chunkI += number.length){
const chunkTest = new Float32Array(44100);
const number = new Float32Array(100);
for (let chunkI = 0; chunkI < 44100; chunkI += number.length){
event.renderedBuffer.copyFromChannel(number, 0, chunkI);
chunkTest.set(number, chunkI);
}
displayData(chunkTest, setName, "copyFromChannel - chunks");
var copyTest = new Float32Array(44100);
const copyTest = new Float32Array(44100);
event.renderedBuffer.copyFromChannel(copyTest, 0);
displayData(copyTest, setName, "copyFromChannel - first");
var getTest = event.renderedBuffer.getChannelData(0);
const getTest = event.renderedBuffer.getChannelData(0);
displayData(getTest, setName, "getChannelData - first");
displayData(event.renderedBuffer.getChannelData(0), setName, "getChannelData - second readout");
displayData(event.renderedBuffer.getChannelData(1), setName, "getChannelData - second channel");
var copyTest2 = new Float32Array(44100);
const copyTest2 = new Float32Array(44100);
event.renderedBuffer.copyFromChannel(copyTest2, 0);
displayData(copyTest2, setName, "copyFromChannel - second");
if (frequency === 1e4){
var sum = 0;
for (var i = 4500; i < 5000; i += 1) {
let sum = 0;
for (let i = 4500; i < 5000; i += 1) {
sum += Math.abs(getTest[i]);
}
container.querySelector(".sum").textContent = sum;