"""Note-gap statistics of a chart: which inter-onset intervals it uses, and how consecutive gaps relate. gap histogram -- gaps between consecutive hit notes in beats, binned to the usual note values (1/8, 1/6, 1/4, 1/3, 1/2, 2/3, 3/4, 1), other gaps under one beat, and rests (> 1 beat) ratio histogram -- next gap / this gap for consecutive gaps both within one beat: equal, x2, /2, x3, /3, x1.5, /1.5, other. It describes the shape of the rhythm (long-short-short ...) independently of the note value. """ from __future__ import annotations import numpy as np GAPS = np.array([1 / 8, 1 / 6, 1 / 4, 1 / 3, 1 / 2, 2 / 3, 3 / 4, 1.0]) GAP_NAMES = ["1/8", "1/6", "1/4", "1/3", "1/2", "2/3", "3/4", "1", "other<1", "rest"] RATIOS = np.array([1.0, 2.0, 0.5, 3.0, 1 / 3, 1.5, 2 / 3]) RATIO_NAMES = ["1:1", "x2", "/2", "x3", "/3", "x1.5", "/1.5", "other"] N_GAP_BINS, N_RATIO_BINS = len(GAP_NAMES), len(RATIO_NAMES) def gap_bins(gaps_beats: np.ndarray) -> np.ndarray: """Bin index per gap (in beats).""" g = np.asarray(gaps_beats, float) rel = np.abs(g[:, None] - GAPS[None]) / GAPS[None] k = rel.argmin(1) out = np.where(rel[np.arange(len(g)), k] < 0.08, k, N_GAP_BINS - 2) return np.where(g > 1.05, N_GAP_BINS - 1, out) def ratio_bins(gaps_beats: np.ndarray) -> np.ndarray: """Bin index per consecutive gap pair, -1 where either gap is a rest.""" g = np.asarray(gaps_beats, float) if len(g) < 2: return np.zeros(0, np.int64) a, b = g[:-1], g[1:] ok = (a <= 1.05) & (b <= 1.05) & (a > 0) & (b > 0) r = np.log(np.where(ok, b, 1.0) / np.where(ok, a, 1.0)) d = np.abs(r[:, None] - np.log(RATIOS)[None]) k = d.argmin(1) out = np.where(d[np.arange(len(r)), k] < 0.08, k, N_RATIO_BINS - 1) return np.where(ok, out, -1) def hists(t_ms: np.ndarray, beat_ms: float) -> tuple[np.ndarray, np.ndarray]: """(gap histogram, ratio histogram) as counts, from hit-note times in ms.""" t = np.sort(np.asarray(t_ms, float)) g = np.diff(t) / beat_ms hg = np.bincount(gap_bins(g), minlength=N_GAP_BINS).astype(float) if len(g) else np.zeros(N_GAP_BINS) rb = ratio_bins(g) rb = rb[rb >= 0] hr = np.bincount(rb, minlength=N_RATIO_BINS).astype(float) if len(rb) else np.zeros(N_RATIO_BINS) return hg, hr def tv(a: np.ndarray, b: np.ndarray) -> float: """Total-variation distance between two count histograms (0 when either is empty).""" sa, sb = a.sum(), b.sum() if sa == 0 or sb == 0: return 0.0 return float(0.5 * np.abs(a / sa - b / sb).sum()) # Finer binning for evaluation: 1/16 and 1/12 get their own bins (generated charts use 1/16 doubles where # mappers use 1/6 runs, and the 10-bin version above merged both into "other"). The 10-bin version stays # the model's plan format. GAPS_FINE = np.array([1 / 16, 1 / 12, 1 / 8, 1 / 6, 1 / 4, 1 / 3, 1 / 2, 2 / 3, 3 / 4, 1.0]) GAP_NAMES_FINE = ["1/16", "1/12", "1/8", "1/6", "1/4", "1/3", "1/2", "2/3", "3/4", "1", "other<1", "rest"] N_GAP_BINS_FINE = len(GAP_NAMES_FINE) def gap_bins_fine(gaps_beats: np.ndarray) -> np.ndarray: g = np.asarray(gaps_beats, float) if len(g) == 0: return np.zeros(0, np.int64) rel = np.abs(g[:, None] - GAPS_FINE[None]) / GAPS_FINE[None] k = rel.argmin(1) out = np.where(rel[np.arange(len(g)), k] < 0.08, k, N_GAP_BINS_FINE - 2) return np.where(g > 1.05, N_GAP_BINS_FINE - 1, out) def beat_positions(t_ms: np.ndarray, sections: np.ndarray) -> np.ndarray: """Exact beat coordinate of each time from the chart's timing sections (columns: time, beat length, meter, beat index at the section start, ...); follows tempo changes.""" t = np.asarray(t_ms, float) idx = np.clip(np.searchsorted(sections[:, 0], t + 1, side="right") - 1, 0, len(sections) - 1) s = sections[idx] return s[:, 3] + (t - s[:, 0]) / s[:, 1] def bar_hists(t_ms: np.ndarray, sections: np.ndarray): """{measure index: (fine gap histogram, ratio histogram)} for hit-note times; a gap (and a ratio) belongs to the measure of its later note. Real measures of the timing sections, as in dataset.bar_plan.""" from .dataset import measure_at b = np.sort(beat_positions(t_ms, sections)) if len(b) < 2: return {} g = np.diff(b) gb = gap_bins_fine(g) rb = ratio_bins(g) bar_g = measure_at(sections, np.round(b[1:] * 48) / 48) # snapped to the 1/48 grid, like dataset.bar_plan bar_r = bar_g[1:] out = {} for k in np.unique(bar_g): hg = np.bincount(gb[bar_g == k], minlength=N_GAP_BINS_FINE).astype(float) r = rb[bar_r == k] r = r[r >= 0] hr = np.bincount(r, minlength=N_RATIO_BINS).astype(float) if len(r) else np.zeros(N_RATIO_BINS) out[int(k)] = (hg, hr) return out