Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 9x 7x 2x 2x 2x 5x 5x 5x 16x 14x 14x 5x 6x 21x 6x 21x 17x 6x | import type { BeMusicJudgmentTiming } from './types.ts';
/** A histogram of judgement timings (see {@link binJudgmentTimings}). */
export interface BeMusicJudgmentTimingHistogram {
/** Width of each bin (ms). */
binMs: number;
/** Lower edge (ms) of `counts[0]`; bin `i` covers `originMs + i × binMs` up to (not including) the next edge. */
originMs: number;
/** Judgements per bin, earliest (most negative) first. */
counts: readonly number[];
}
/**
* Bins judgement timings by their signed offset for a timing histogram (negative = early / FAST, positive = late /
* SLOW). Bin edges sit on multiples of `binMs`, so the bin starting at 0 holds the dead-on and slightly late presses.
*
* Without `rangeMs` the bins cover the earliest to the latest timing (no bins when `timings` is empty). With it they
* cover `-rangeMs..rangeMs` whatever the timings, so histograms of different plays line up, and timings outside that
* range are left out.
*
* @throws RangeError when `binMs` is not a positive finite number.
*/
export function binJudgmentTimings(
timings: readonly Pick<BeMusicJudgmentTiming, 'deltaMs'>[],
binMs = 1,
rangeMs?: number,
): BeMusicJudgmentTimingHistogram {
if (!(Number.isFinite(binMs) && binMs > 0)) throw new RangeError(`binMs must be a positive number, got ${binMs}`);
let min: number;
let max: number;
if (rangeMs !== undefined) {
const range = Math.abs(rangeMs);
min = -range;
max = range;
} else {
min = Number.POSITIVE_INFINITY;
max = Number.NEGATIVE_INFINITY;
for (const { deltaMs } of timings) {
if (!Number.isFinite(deltaMs)) continue;
if (deltaMs < min) min = deltaMs;
if (deltaMs > max) max = deltaMs;
}
if (min > max) return { binMs, originMs: 0, counts: [] };
}
const firstBin = Math.floor(min / binMs);
const counts = Array.from({ length: Math.floor(max / binMs) - firstBin + 1 }, () => 0);
for (const { deltaMs } of timings) {
if (!Number.isFinite(deltaMs) || deltaMs < min || deltaMs > max) continue;
counts[Math.floor(deltaMs / binMs) - firstBin]! += 1;
}
// `+ 0` turns a -0 origin (an all-late set starting in bin 0) into 0.
return { binMs, originMs: firstBin * binMs + 0, counts };
}
|