mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-01-03 10:31:54 +01:00
Improved audio API test
This commit is contained in:
parent
ea30fb3370
commit
ee02c8b999
@ -2,12 +2,31 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Audio test</title>
|
<title>Audio test</title>
|
||||||
<style></style>
|
<style>
|
||||||
|
table {
|
||||||
|
border-collapse: collapse;
|
||||||
|
width: 100%;
|
||||||
|
table-layout: auto;
|
||||||
|
}
|
||||||
|
table td {
|
||||||
|
border: 1px solid lightgray;
|
||||||
|
padding: 1px 5px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Audio test</h1>
|
<h1>Audio test</h1>
|
||||||
|
Expected result:
|
||||||
|
<ul>
|
||||||
|
<li>the hashes in each set should be the same</li>
|
||||||
|
<li>the hashes should vary between the sets</li>
|
||||||
|
<li>the "empty" hash should be 95476ffd46d1cd34fe8de83585c325df2b06019c852a5f6445200f57bc954a1b</li>
|
||||||
|
<li>hitting the refresh button should not change the values</li>
|
||||||
|
<li>reloading the page should change the values (depending on CanvasBlocker settings)</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
<div id="test">
|
<div id="test">
|
||||||
Hashes: <ul class="hash"></ul>
|
Hashes: <table class="hashes"></table>
|
||||||
Sum: <span class="sum"></span>
|
Sum: <span class="sum"></span>
|
||||||
<button>refresh</button>
|
<button>refresh</button>
|
||||||
</div>
|
</div>
|
||||||
|
@ -12,16 +12,48 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
var container = document.getElementById("test");
|
var container = document.getElementById("test");
|
||||||
|
var hashContainer = container.querySelector(".hashes");
|
||||||
|
var hashSets = Object.create(null);
|
||||||
|
|
||||||
var pxi_output;
|
function createSet(set){
|
||||||
var pxi_full_buffer;
|
if (!hashSets[set]){
|
||||||
function run_pxi_fp(){
|
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);
|
||||||
|
});
|
||||||
|
hashSets[set].appendChild(container);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAudioContext(frequency = 1e4){
|
||||||
var context = new window.OfflineAudioContext(2, 44100, 44100);
|
var context = new window.OfflineAudioContext(2, 44100, 44100);
|
||||||
|
|
||||||
// Create oscillator
|
// Create oscillator
|
||||||
var pxi_oscillator = context.createOscillator();
|
var pxi_oscillator = context.createOscillator();
|
||||||
pxi_oscillator.type = "triangle";
|
pxi_oscillator.type = "triangle";
|
||||||
pxi_oscillator.frequency.value = 1e4;
|
pxi_oscillator.frequency.value = frequency;
|
||||||
|
|
||||||
// Create and configure compressor
|
// Create and configure compressor
|
||||||
var pxi_compressor = context.createDynamicsCompressor();
|
var pxi_compressor = context.createDynamicsCompressor();
|
||||||
@ -35,32 +67,81 @@
|
|||||||
// Connect nodes
|
// Connect nodes
|
||||||
pxi_oscillator.connect(pxi_compressor);
|
pxi_oscillator.connect(pxi_compressor);
|
||||||
pxi_compressor.connect(context.destination);
|
pxi_compressor.connect(context.destination);
|
||||||
|
|
||||||
// Start audio processing
|
|
||||||
pxi_oscillator.start(0);
|
pxi_oscillator.start(0);
|
||||||
|
|
||||||
|
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
|
||||||
context.startRendering();
|
context.startRendering();
|
||||||
context.oncomplete = function(event) {
|
context.oncomplete = function(event){
|
||||||
var str = "";
|
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");
|
||||||
|
|
||||||
var copyTest = new Float32Array(44100);
|
var copyTest = new Float32Array(44100);
|
||||||
event.renderedBuffer.copyFromChannel(copyTest, 0);
|
event.renderedBuffer.copyFromChannel(copyTest, 0);
|
||||||
|
displayData(copyTest, setName, "copyFromChannel - first");
|
||||||
|
|
||||||
|
|
||||||
var getTest = event.renderedBuffer.getChannelData(0);
|
var getTest = event.renderedBuffer.getChannelData(0);
|
||||||
var getTest2 = event.renderedBuffer.getChannelData(0);
|
displayData(getTest, setName, "getChannelData - first");
|
||||||
var getTest3 = event.renderedBuffer.getChannelData(1);
|
displayData(event.renderedBuffer.getChannelData(0), setName, "getChannelData - second readout");
|
||||||
Promise.all([getTest, getTest2, getTest3, copyTest].map(function(array){
|
displayData(event.renderedBuffer.getChannelData(1), setName, "getChannelData - second channel");
|
||||||
return crypto.subtle.digest("SHA-256", array);
|
|
||||||
})).then(function(hashes){
|
var copyTest2 = new Float32Array(44100);
|
||||||
container.querySelector(".hash").innerHTML = hashes.map(byteArrayToHex).map(function(hash){
|
event.renderedBuffer.copyFromChannel(copyTest2, 0);
|
||||||
return "<li>" + hash + "</li>";
|
displayData(copyTest2, setName, "copyFromChannel - second");
|
||||||
}).join("");
|
|
||||||
});
|
if (frequency === 1e4){
|
||||||
var sum = 0;
|
var sum = 0;
|
||||||
for (var i = 4500; i < 5000; i += 1) {
|
for (var i = 4500; i < 5000; i += 1) {
|
||||||
sum += Math.abs(getTest[i]);
|
sum += Math.abs(getTest[i]);
|
||||||
|
}
|
||||||
|
container.querySelector(".sum").textContent = sum;
|
||||||
}
|
}
|
||||||
container.querySelector(".sum").textContent = sum;
|
|
||||||
pxi_compressor.disconnect();
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
run_pxi_fp();
|
|
||||||
container.querySelector("button").addEventListener("click", run_pxi_fp);
|
function createAllHashData(){
|
||||||
|
hashContainer.innerHTML = "";
|
||||||
|
hashSets = Object.create(null);
|
||||||
|
createEmptyData();
|
||||||
|
createHashData(1e4);
|
||||||
|
createHashData(2e4);
|
||||||
|
}
|
||||||
|
createAllHashData();
|
||||||
|
container.querySelector("button").addEventListener("click", createAllHashData);
|
||||||
}());
|
}());
|
Loading…
x
Reference in New Issue
Block a user