-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement library functions for dsp graph building and realtime and o…
…ffline audio buffer comparison Co-authored-by: kizjkre <kizjkre@users.noreply.github.com>
- Loading branch information
1 parent
0c9e322
commit 055cd61
Showing
6 changed files
with
118 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<button id="play">Play</button> | ||
<script type="module"> | ||
document.getElementById("play").addEventListener("click", async () => { | ||
import ('./util/audioComparer.js') | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { bufferinator } from './bufferinator.js'; | ||
import { createGraphCache } from './graph.js'; | ||
|
||
const SAMPLE_RATE = 48000; | ||
const NUM_CHANNELS = 1; | ||
|
||
// CREATE DSP GRAPH HERE!!! | ||
const createGraph = ctx => { | ||
// My Graph | ||
const osc = new OscillatorNode(ctx); | ||
osc.type = 'sawtooth'; | ||
const gain = new GainNode(ctx); | ||
gain.gain.value = 2.2; | ||
const biq = new BiquadFilterNode(ctx); | ||
biq.type = 'bandpass'; | ||
biq.frequency.value = 1000; | ||
biq.Q.value = 10; | ||
osc.connect(gain).connect(biq).connect(ctx.destination); | ||
|
||
// My render time | ||
const length = 1; | ||
|
||
osc.start(); | ||
osc.stop(ctx.currentTime + length); | ||
} | ||
|
||
/** | ||
* Compares two audio buffers for equality. | ||
* @param {AudioBuffer} myBuf | ||
* @param {AudioBuffer} refBuf | ||
* @returns {number} returns the percentage of samples that are equal | ||
*/ | ||
function bufferComparer(myBuf, refBuf) { | ||
let numCorrect = 0; | ||
const numChannels = myBuf.numberOfChannels; | ||
for (let c = 0; c < numChannels; c++) { | ||
const myChannel = myBuf.getChannelData(c); | ||
const refChannel = refBuf.getChannelData(c); | ||
console.log(myChannel.length, refChannel.length) | ||
for (let i = 0; i < myChannel.length; i++) { | ||
if (myChannel[i] === refChannel[i]) { | ||
numCorrect++; | ||
} | ||
} | ||
} | ||
return numCorrect / (numChannels * myBuf.length); | ||
} | ||
|
||
/** | ||
* Takes in an dsp graph to build for realtime and offline context + length of buffer to render. | ||
* Records output of both contexts as audio buffers. | ||
* Compares the real time buffer and the offline buffer for equality. | ||
* @param {(ctx: AudioContext) => void} ctxGraph dsp graph to build in context | ||
* @param {number} length length of buffer to render in seconds | ||
* @returns {boolean} true if buffers are equal | ||
*/ | ||
async function evaluateGraph(ctxGraph, length = 1) { | ||
const audioContext = new AudioContext({ sampleRate: SAMPLE_RATE }); | ||
audioContext.suspend(); | ||
const offlineAudioContext = new OfflineAudioContext({ | ||
numberOfChannels: NUM_CHANNELS, | ||
length: length * SAMPLE_RATE, | ||
sampleRate: SAMPLE_RATE | ||
}); | ||
|
||
// create ctxGraph cache | ||
const cache = createGraphCache(); | ||
|
||
// create realtime and offline graphs | ||
ctxGraph(audioContext); | ||
ctxGraph(offlineAudioContext); | ||
|
||
// render graphs to audio buffer | ||
const realtimeBuffer = await bufferinator(audioContext, length, cache) | ||
const offlineBuffer = await bufferinator(offlineAudioContext) | ||
|
||
const score = bufferComparer(realtimeBuffer, offlineBuffer); | ||
|
||
return score; | ||
} | ||
|
||
console.log(await evaluateGraph(createGraph, 1)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
import record from "../processors/recorder/recorder-main"; | ||
import record from "../processors/recorder/recorder-main.js"; | ||
|
||
export const bufferinator = async (ctx, length, graph) => { | ||
if (ctx instanceof AudioContext) { | ||
const {recorder, buffer} = await record(ctx, length); | ||
recorder.connect(ctx.destination); | ||
graph.get(ctx)?.forEach(([from, to]) => { | ||
if (to instanceof AudioDestinationNode) { | ||
from.disconnect(to); | ||
from.connect(recorder); | ||
from._WAS_disconnect(to); | ||
from._WAS_connect(recorder); | ||
} | ||
}); | ||
|
||
recorder.connect(ctx.destination); | ||
ctx.resume(); | ||
|
||
// for realtime | ||
return await buffer; | ||
} | ||
|
||
return ctx.startRendering(); | ||
// for offline | ||
return ctx.startRendering(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters