25 lines
1.2 KiB
JavaScript
25 lines
1.2 KiB
JavaScript
// Every ordered pair in the repeating marker sequence occurs once. This lets
|
|
// a receiver joining mid-cycle identify position without synchronized clocks.
|
|
export const ANCHOR_HZ = 900;
|
|
export const MARKERS_HZ = [1400, 1800, 2200, 2600];
|
|
export const ORDER = [0,1,0,2,0,3,1,2,1,3,2,3];
|
|
export const SYMBOL_SECONDS = 0.1;
|
|
export const STAGE_MINUTES = Object.freeze([1, 5, 10, 15, 20, 30]);
|
|
export const TRIAL_SECONDS = STAGE_MINUTES.at(-1) * 60;
|
|
export const SOURCE_LEAD_SECONDS = 15;
|
|
|
|
export function referenceLoop(rate) {
|
|
const data = new Float32Array(Math.round(rate * SYMBOL_SECONDS * ORDER.length));
|
|
for (let i = 0; i < data.length; i++) {
|
|
const t = i / rate;
|
|
const slot = Math.min(ORDER.length - 1, Math.floor(i * ORDER.length / data.length));
|
|
const tone = MARKERS_HZ[ORDER[slot]];
|
|
const oldTone = MARKERS_HZ[ORDER[(slot + ORDER.length - 1) % ORDER.length]];
|
|
const fade = Math.min(1, Math.max(0, (t - slot * SYMBOL_SECONDS) / 0.004));
|
|
const phase = 2 * Math.PI * t;
|
|
data[i] = 0.15 * Math.sin(phase * ANCHOR_HZ) +
|
|
0.15 * (fade * Math.sin(phase * tone) + (1 - fade) * Math.sin(phase * oldTone));
|
|
}
|
|
return data;
|
|
}
|