1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-23 05:10:27 +01:00
CanvasBlocker/test/audioTest.js

149 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2018-06-30 23:15:47 +02:00
(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");
2019-02-10 02:50:21 +01:00
var hashContainer = container.querySelector(".hashes");
var hashSets = Object.create(null);
2018-06-30 23:15:47 +02:00
2019-02-10 02:50:21 +01:00
function createSet(set){
if (!hashSets[set]){
var setContainer = document.createElement("tbody");
hashContainer.appendChild(setContainer);
var nameRow = document.createElement("tr");
setContainer.appendChild(nameRow);
var nameContainer = document.createElement("th");
nameRow.appendChild(nameContainer);
nameContainer.colSpan = 2;
nameContainer.textContent = set;
hashSets[set] = setContainer;
}
}
function displayData(data, set, title){
createSet(set);
var container = document.createElement("tr");
var titleNode = document.createElement("td");
titleNode.textContent = title;
container.appendChild(titleNode);
var hashNode = document.createElement("td");
hashNode.textContent = "calculating hash";
container.appendChild(hashNode);
crypto.subtle.digest("SHA-256", data).then(function(hash){
hashNode.textContent = byteArrayToHex(hash);
2019-02-10 13:00:18 +01:00
}, function(error){
hashNode.textContent = error;
2019-02-10 02:50:21 +01:00
});
hashSets[set].appendChild(container);
}
function getAudioContext(frequency = 1e4){
var context = new window.OfflineAudioContext(2, 44100, 44100);
2019-02-10 02:50:21 +01:00
2018-06-30 23:15:47 +02:00
// Create oscillator
var pxi_oscillator = context.createOscillator();
pxi_oscillator.type = "triangle";
2019-02-10 02:50:21 +01:00
pxi_oscillator.frequency.value = frequency;
2018-06-30 23:15:47 +02:00
// 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);
2019-02-10 02:50:21 +01:00
2018-06-30 23:15:47 +02:00
pxi_oscillator.start(0);
2019-02-10 02:50:21 +01:00
return context;
}
function createEmptyData(){
var 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);
var emptyCopy = new Float32Array(44100);
emptyBuffer.copyFromChannel(emptyCopy, 0);
displayData(emptyCopy, "empty buffer", "copyFromChannel - first");
var emptyData = emptyBuffer.getChannelData(0);
displayData(emptyData, "empty buffer", "getChannelData - first");
displayData(emptyBuffer.getChannelData(0), "empty buffer", "getChannelData - second");
var emptyCopy2 = new Float32Array(44100);
emptyBuffer.copyFromChannel(emptyCopy2, 0);
displayData(emptyCopy2, "empty buffer", "copyFromChannel - second");
}
function createHashData(frequency = 1e4){
var context = getAudioContext(frequency);
var setName = " (" + frequency + " Hz)";
createSet(setName);
// Start audio processing
2018-06-30 23:15:47 +02:00
context.startRendering();
2019-02-10 02:50:21 +01:00
context.oncomplete = function(event){
var chunkTest = new Float32Array(44100);
var number = new Float32Array(100);
for (var i = 0; i < 44100; i += number.length){
event.renderedBuffer.copyFromChannel(number, 0, i);
chunkTest.set(number, i);
}
displayData(chunkTest, setName, "copyFromChannel - chunks");
2018-06-30 23:15:47 +02:00
var copyTest = new Float32Array(44100);
2018-08-22 22:16:49 +02:00
event.renderedBuffer.copyFromChannel(copyTest, 0);
2019-02-10 02:50:21 +01:00
displayData(copyTest, setName, "copyFromChannel - first");
2018-08-22 22:16:49 +02:00
var getTest = event.renderedBuffer.getChannelData(0);
2019-02-10 02:50:21 +01:00
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);
event.renderedBuffer.copyFromChannel(copyTest2, 0);
displayData(copyTest2, setName, "copyFromChannel - second");
if (frequency === 1e4){
var sum = 0;
for (var i = 4500; i < 5000; i += 1) {
sum += Math.abs(getTest[i]);
}
container.querySelector(".sum").textContent = sum;
2018-06-30 23:15:47 +02:00
}
};
}
2019-02-10 02:50:21 +01:00
function createAllHashData(){
hashContainer.innerHTML = "";
hashSets = Object.create(null);
createEmptyData();
createHashData(1e4);
createHashData(2e4);
}
createAllHashData();
container.querySelector("button").addEventListener("click", createAllHashData);
2018-06-30 23:15:47 +02:00
}());