1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 12:50:36 +01:00

Additional audio tests

This commit is contained in:
kkapsner 2020-05-01 01:06:50 +02:00
parent 49f3c166a2
commit 64adb49b81
2 changed files with 75 additions and 33 deletions

View File

@ -21,7 +21,7 @@
<h1>Audio test</h1> <h1>Audio test</h1>
<h2>Expected result</h2> <h2>Expected result</h2>
<ul> <ul>
<li>the hashes in each set should be the same</li> <li>the hashes in each set should be the same (i.e. &quot;all tests equal&quot; should be displayed</li>
<li>the hashes should vary between the sets</li> <li>the hashes should vary between the sets</li>
<li>the &quot;empty&quot; hash should be 95476ffd46d1cd34fe8de83585c325df2b06019c852a5f6445200f57bc954a1b</li> <li>the &quot;empty&quot; hash should be 95476ffd46d1cd34fe8de83585c325df2b06019c852a5f6445200f57bc954a1b</li>
<li>hitting the refresh button should not change the values</li> <li>hitting the refresh button should not change the values</li>
@ -30,7 +30,8 @@
<h2>Tests</h2> <h2>Tests</h2>
<div id="test"> <div id="test">
Hashes: <table class="hashes"></table> Hashes: <table class="hashes"></table>
Sum: <span class="sum"></span> <br>
Sums: <table class="sum"></table>
<button>refresh</button> <button>refresh</button>
</div> </div>
<script src="audioTest.js"></script> <script src="audioTest.js"></script>

View File

@ -25,44 +25,73 @@
nameRow.appendChild(nameContainer); nameRow.appendChild(nameContainer);
nameContainer.colSpan = 2; nameContainer.colSpan = 2;
nameContainer.textContent = set; nameContainer.textContent = set;
hashSets[set] = setContainer; hashSets[set] = {firstHash: false, failed: false, hashes: [], setContainer};
} }
} }
async function displayData(data, set, title){ async function displayData(data, set, title){
createSet(set); createSet(set);
const container = document.createElement("tr"); const storage = hashSets[set];
const titleNode = document.createElement("td"); function addLine(title, hash){
titleNode.textContent = title; const container = document.createElement("tr");
container.appendChild(titleNode);
const titleNode = document.createElement("td");
titleNode.textContent = title;
container.appendChild(titleNode);
const hashNode = document.createElement("td");
hashNode.textContent = hash;
container.appendChild(hashNode);
storage.setContainer.appendChild(container);
return container;
}
const hash = byteArrayToHex(await crypto.subtle.digest("SHA-256", data));
storage.hashes.push({title, hash});
const hashNode = document.createElement("td"); if (!storage.firstHash){
hashNode.textContent = "calculating hash"; storage.firstHash = hash;
container.appendChild(hashNode); storage.allLine = addLine("all tests equal", hash);
}
hashSets[set].appendChild(container); else {
if (storage.failed){
const hash = await crypto.subtle.digest("SHA-256", data); addLine(title, hash);
hashNode.textContent = byteArrayToHex(hash); }
else if (hash !== storage.firstHash){
storage.setContainer.removeChild(storage.allLine);
storage.hashes.forEach(function(hash){
addLine(hash.title, hash.hash);
});
storage.failed = true;
}
}
} }
function getAudioContext(frequency = 1e4){ function getAudioContext(frequency = 1e4, type = "triangle", useDelayedValues = false){
function set(obj, value){
if (useDelayedValues){
obj.setTargetAtTime(value, 0, 0.01);
}
else {
obj.value = value;
}
}
const context = new window.OfflineAudioContext(2, 44100, 44100); const context = new window.OfflineAudioContext(2, 44100, 44100);
// Create oscillator // Create oscillator
const pxi_oscillator = context.createOscillator(); const pxi_oscillator = context.createOscillator();
pxi_oscillator.type = "triangle"; pxi_oscillator.type = type;
pxi_oscillator.frequency.value = frequency; set(pxi_oscillator.frequency, frequency);
// Create and configure compressor // Create and configure compressor
const pxi_compressor = context.createDynamicsCompressor(); const pxi_compressor = context.createDynamicsCompressor();
pxi_compressor.threshold && (pxi_compressor.threshold.value = -50); pxi_compressor.threshold && set(pxi_compressor.threshold, -50);
pxi_compressor.knee && (pxi_compressor.knee.value = 40); pxi_compressor.knee && set(pxi_compressor.knee, 40);
pxi_compressor.ratio && (pxi_compressor.ratio.value = 12); pxi_compressor.ratio && set(pxi_compressor.ratio, 12);
pxi_compressor.reduction && (pxi_compressor.reduction.value = -20); pxi_compressor.reduction && set(pxi_compressor.reduction, -20);
pxi_compressor.attack && (pxi_compressor.attack.value = 0); pxi_compressor.attack && set(pxi_compressor.attack, 0);
pxi_compressor.release && (pxi_compressor.release.value = .25); pxi_compressor.release && set(pxi_compressor.release, .25);
// Connect nodes // Connect nodes
pxi_oscillator.connect(pxi_compressor); pxi_oscillator.connect(pxi_compressor);
@ -102,13 +131,21 @@
return iframeWindow; return iframeWindow;
} }
function createHashData(frequency = 1e4){ function createHashData(frequency = 1e4, type = "triangle", useDelayedValues = false){
const context = getAudioContext(frequency); const context = getAudioContext(frequency, type, useDelayedValues);
const setName = " (" + frequency + " Hz)"; const setName = type + " (" + frequency + " Hz)" + (useDelayedValues? " delayed": "");
createSet(setName); createSet(setName);
const sumRow = document.createElement("tr");
const nameCell = document.createElement("td");
nameCell.textContent = setName;
sumRow.appendChild(nameCell);
const sumCell = document.createElement("td");
sumRow.appendChild(sumCell);
container.querySelector(".sum").appendChild(sumRow);
// Start audio processing // Start audio processing
context.startRendering(); context.startRendering();
context.oncomplete = function(event){ context.oncomplete = function(event){
@ -138,13 +175,11 @@
event.renderedBuffer.copyFromChannel(copyTest2, 0); event.renderedBuffer.copyFromChannel(copyTest2, 0);
displayData(copyTest2, setName, "copyFromChannel - second"); displayData(copyTest2, setName, "copyFromChannel - second");
if (frequency === 1e4){ let sum = 0;
let sum = 0; for (let i = 4500; i < 5000; i += 1) {
for (let i = 4500; i < 5000; i += 1) { sum += Math.abs(getTest[i]);
sum += Math.abs(getTest[i]);
}
container.querySelector(".sum").textContent = sum;
} }
sumCell.textContent = sum;
}; };
} }
@ -154,6 +189,12 @@
createEmptyData(); createEmptyData();
createHashData(1e4); createHashData(1e4);
createHashData(2e4); createHashData(2e4);
createHashData(1e4, "sine");
createHashData(2e4, "sine");
createHashData(1e4, undefined, true);
createHashData(2e4, undefined, true);
createHashData(1e4, "sine", true);
createHashData(2e4, "sine", true);
} }
createAllHashData(); createAllHashData();
container.querySelector("button").addEventListener("click", createAllHashData); container.querySelector("button").addEventListener("click", createAllHashData);