1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-11 07:38:58 +01:00
CanvasBlocker/test/audioTest.js

160 lines
5.2 KiB
JavaScript
Raw Normal View History

2018-06-30 23:15:47 +02:00
(function(){
"use strict";
function byteArrayToHex(arrayBuffer){
2019-12-16 19:27:28 +01:00
const chunks = [];
2018-06-30 23:15:47 +02:00
(new Uint32Array(arrayBuffer)).forEach(function(num){
chunks.push(num.toString(16));
});
return chunks.map(function(chunk){
return "0".repeat(8 - chunk.length) + chunk;
}).join("");
}
2019-12-16 19:27:28 +01:00
const container = document.getElementById("test");
const hashContainer = container.querySelector(".hashes");
let 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]){
2019-12-16 19:27:28 +01:00
const setContainer = document.createElement("tbody");
2019-02-10 02:50:21 +01:00
hashContainer.appendChild(setContainer);
2019-12-16 19:27:28 +01:00
const nameRow = document.createElement("tr");
2019-02-10 02:50:21 +01:00
setContainer.appendChild(nameRow);
2019-12-16 19:27:28 +01:00
const nameContainer = document.createElement("th");
2019-02-10 02:50:21 +01:00
nameRow.appendChild(nameContainer);
nameContainer.colSpan = 2;
nameContainer.textContent = set;
hashSets[set] = setContainer;
}
}
2019-12-16 19:27:28 +01:00
async function displayData(data, set, title){
2019-02-10 02:50:21 +01:00
createSet(set);
2019-12-16 19:27:28 +01:00
const container = document.createElement("tr");
2019-02-10 02:50:21 +01:00
2019-12-16 19:27:28 +01:00
const titleNode = document.createElement("td");
2019-02-10 02:50:21 +01:00
titleNode.textContent = title;
container.appendChild(titleNode);
2019-12-16 19:27:28 +01:00
const hashNode = document.createElement("td");
2019-02-10 02:50:21 +01:00
hashNode.textContent = "calculating hash";
container.appendChild(hashNode);
hashSets[set].appendChild(container);
2019-12-16 19:27:28 +01:00
const hash = await crypto.subtle.digest("SHA-256", data);
hashNode.textContent = byteArrayToHex(hash);
2019-02-10 02:50:21 +01:00
}
function getAudioContext(frequency = 1e4){
2019-12-16 19:27:28 +01:00
const 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
2019-12-16 19:27:28 +01:00
const pxi_oscillator = context.createOscillator();
2018-06-30 23:15:47 +02:00
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
2019-12-16 19:27:28 +01:00
const pxi_compressor = context.createDynamicsCompressor();
2018-06-30 23:15:47 +02:00
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(){
2019-12-16 19:27:28 +01:00
const emptyArray = new Float32Array(44100);
2019-02-10 02:50:21 +01:00
displayData(emptyArray, "empty buffer", "no API involved");
2019-12-16 19:27:28 +01:00
const emptyContext = new OfflineAudioContext(1, 44100, 44100);
const emptyBuffer = emptyContext.createBuffer(1, 44100, 44100);
2019-02-10 02:50:21 +01:00
2019-12-16 19:27:28 +01:00
const emptyCopy = new Float32Array(44100);
2019-02-10 02:50:21 +01:00
emptyBuffer.copyFromChannel(emptyCopy, 0);
displayData(emptyCopy, "empty buffer", "copyFromChannel - first");
2019-12-16 19:27:28 +01:00
const emptyData = emptyBuffer.getChannelData(0);
2019-02-10 02:50:21 +01:00
displayData(emptyData, "empty buffer", "getChannelData - first");
displayData(emptyBuffer.getChannelData(0), "empty buffer", "getChannelData - second");
2019-12-16 19:27:28 +01:00
const emptyCopy2 = new Float32Array(44100);
2019-02-10 02:50:21 +01:00
emptyBuffer.copyFromChannel(emptyCopy2, 0);
displayData(emptyCopy2, "empty buffer", "copyFromChannel - second");
}
2019-05-22 23:55:57 +02:00
function getIframeWindow(){
2019-12-16 19:27:28 +01:00
const l = window.length;
const iframe = document.createElement("iframe");
2019-05-22 23:55:57 +02:00
document.body.appendChild(iframe);
const iframeWindow = window[l];
document.body.removeChild(iframe);
return iframeWindow;
}
2019-02-10 02:50:21 +01:00
function createHashData(frequency = 1e4){
2019-12-16 19:27:28 +01:00
const context = getAudioContext(frequency);
2019-02-10 02:50:21 +01:00
2019-12-16 19:27:28 +01:00
const setName = " (" + frequency + " Hz)";
2019-02-10 02:50:21 +01:00
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){
2019-12-16 19:27:28 +01:00
const copyTestIframe = new (getIframeWindow().Float32Array)(44100);
2019-05-22 23:55:57 +02:00
getIframeWindow().AudioBuffer.prototype.copyFromChannel.call(event.renderedBuffer, copyTestIframe, 0);
displayData(copyTestIframe, setName, "copyFromChannel - iframe");
2019-12-16 19:27:28 +01:00
const chunkTest = new Float32Array(44100);
const number = new Float32Array(100);
for (let chunkI = 0; chunkI < 44100; chunkI += number.length){
2019-03-12 22:30:37 +01:00
event.renderedBuffer.copyFromChannel(number, 0, chunkI);
chunkTest.set(number, chunkI);
2019-02-10 02:50:21 +01:00
}
displayData(chunkTest, setName, "copyFromChannel - chunks");
2019-12-16 19:27:28 +01:00
const 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");
2019-12-16 19:27:28 +01:00
const 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");
2019-12-16 19:27:28 +01:00
const copyTest2 = new Float32Array(44100);
2019-02-10 02:50:21 +01:00
event.renderedBuffer.copyFromChannel(copyTest2, 0);
displayData(copyTest2, setName, "copyFromChannel - second");
if (frequency === 1e4){
2019-12-16 19:27:28 +01:00
let sum = 0;
for (let i = 4500; i < 5000; i += 1) {
2019-02-10 02:50:21 +01:00
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
}());