2024-05-03 18:31:14 +00:00
|
|
|
import { Compare, TestAudioBuffer } from "./compare/index.js";
|
|
|
|
import { ToneAudioBuffer } from "../../Tone/core/context/ToneAudioBuffer.js";
|
|
|
|
import { Offline } from "../../Tone/core/context/Offline.js";
|
|
|
|
import { Context } from "../../Tone/core/context/Context.js";
|
2020-04-29 19:45:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a file for comparison
|
|
|
|
*/
|
|
|
|
async function getBuffersToCompare(
|
|
|
|
callback: (context: Context) => Promise<void> | void,
|
|
|
|
filename: string,
|
|
|
|
duration = 0.5,
|
|
|
|
channels = 1,
|
|
|
|
sampleRate = 11025,
|
|
|
|
forceRender = false
|
|
|
|
): Promise<{ bufferA: TestAudioBuffer; bufferB: TestAudioBuffer } | void> {
|
|
|
|
if (forceRender) {
|
|
|
|
const buffer = await Offline(callback, duration, channels, sampleRate);
|
|
|
|
new TestAudioBuffer(buffer).downloadWav(filename);
|
|
|
|
} else {
|
|
|
|
const loadedBuffer = await ToneAudioBuffer.fromUrl(filename);
|
|
|
|
const bufferB = new TestAudioBuffer(loadedBuffer);
|
2024-05-03 18:31:14 +00:00
|
|
|
const renderedBuffer = await Offline(
|
|
|
|
callback,
|
|
|
|
bufferB.duration,
|
|
|
|
bufferB.numberOfChannels,
|
|
|
|
bufferB.sampleRate
|
|
|
|
);
|
2020-04-29 19:45:11 +00:00
|
|
|
const bufferA = new TestAudioBuffer(renderedBuffer);
|
|
|
|
return {
|
2024-05-03 18:31:14 +00:00
|
|
|
bufferA,
|
|
|
|
bufferB,
|
2020-04-29 19:45:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 18:03:52 +00:00
|
|
|
|
2024-05-03 18:31:14 +00:00
|
|
|
/**
|
|
|
|
* Compare the output of the callback to a pre-rendered file
|
|
|
|
*/
|
2019-06-17 18:03:52 +00:00
|
|
|
export async function CompareToFile(
|
2024-05-03 18:31:14 +00:00
|
|
|
callback,
|
|
|
|
url: string,
|
2020-04-29 19:45:11 +00:00
|
|
|
threshold = 0.001,
|
|
|
|
RENDER_NEW = false,
|
2024-05-03 18:31:14 +00:00
|
|
|
duration = 0.1,
|
|
|
|
channels = 1
|
2019-06-17 18:03:52 +00:00
|
|
|
): Promise<void> {
|
2024-05-03 18:31:14 +00:00
|
|
|
url = "test/audio/compare/" + url;
|
|
|
|
const response = await getBuffersToCompare(
|
|
|
|
callback,
|
|
|
|
url,
|
|
|
|
duration,
|
|
|
|
channels,
|
|
|
|
44100,
|
|
|
|
RENDER_NEW
|
|
|
|
);
|
2020-04-29 19:45:11 +00:00
|
|
|
if (response) {
|
|
|
|
const { bufferA, bufferB } = response;
|
|
|
|
const error = Compare.compareSpectra(bufferA, bufferB);
|
|
|
|
if (error > threshold) {
|
2024-05-03 18:31:14 +00:00
|
|
|
throw new Error(
|
|
|
|
`Error ${error} greater than threshold ${threshold}`
|
|
|
|
);
|
2020-04-29 19:45:11 +00:00
|
|
|
}
|
2019-06-17 18:03:52 +00:00
|
|
|
}
|
|
|
|
}
|