Initial Commit
This commit is contained in:
61
packages/ui/react/react-audio-fingerprint/tests/fixtures.ts
Normal file
61
packages/ui/react/react-audio-fingerprint/tests/fixtures.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { AudioDocument } from "@suaveplan/audio-core/document/document";
|
||||
|
||||
const PAGE_SIZE = 1024 * 1024;
|
||||
|
||||
/** Build an `AudioDocument` from a flat mono `Float32Array`. */
|
||||
export function makeDoc(
|
||||
samples: Float32Array,
|
||||
sampleRate = 48_000
|
||||
): AudioDocument {
|
||||
const pages: { channelData: Float32Array[]; startSample: number }[] = [];
|
||||
let offset = 0;
|
||||
while (offset < samples.length) {
|
||||
const end = Math.min(offset + PAGE_SIZE, samples.length);
|
||||
pages.push({
|
||||
channelData: [samples.slice(offset, end)],
|
||||
startSample: offset,
|
||||
});
|
||||
offset = end;
|
||||
}
|
||||
if (pages.length === 0) {
|
||||
pages.push({ channelData: [new Float32Array(0)], startSample: 0 });
|
||||
}
|
||||
return new AudioDocument({
|
||||
sampleRate,
|
||||
channels: 1,
|
||||
sourceLength: samples.length,
|
||||
pages,
|
||||
});
|
||||
}
|
||||
|
||||
/** Generate a sine-wave Float32Array. */
|
||||
export function sineWave(
|
||||
durationSec: number,
|
||||
freqHz: number,
|
||||
sampleRate = 48_000
|
||||
): Float32Array {
|
||||
const n = Math.floor(durationSec * sampleRate);
|
||||
const out = new Float32Array(n);
|
||||
for (let i = 0; i < n; i++) {
|
||||
out[i] = Math.sin((2 * Math.PI * freqHz * i) / sampleRate);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Sum of two sines (lets the peak detector find more than one frequency). */
|
||||
export function twoTone(
|
||||
durationSec: number,
|
||||
f1: number,
|
||||
f2: number,
|
||||
sampleRate = 48_000
|
||||
): Float32Array {
|
||||
const n = Math.floor(durationSec * sampleRate);
|
||||
const out = new Float32Array(n);
|
||||
for (let i = 0; i < n; i++) {
|
||||
const t = i / sampleRate;
|
||||
out[i] =
|
||||
0.5 * Math.sin(2 * Math.PI * f1 * t) +
|
||||
0.5 * Math.sin(2 * Math.PI * f2 * t);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Reference in New Issue
Block a user