using System.Collections.Generic; // 延遲校正的計算(純函式,方便測試) public static class CalibrationMath { // 點擊時間對最近一拍的偏差;超出半拍或屬於暖身拍則回傳 false public static bool TryBeatDelta(double tapTime, double firstBeat, double interval, int beatCount, int warmupBeats, out double delta) { delta = 0; int i = (int)System.Math.Round((tapTime - firstBeat) / interval); if (i < warmupBeats || i >= beatCount) return false; delta = tapTime - (firstBeat + i * interval); return System.Math.Abs(delta) < interval / 2; } // 中位數(比平均值不受誤觸影響) public static double Median(List values) { if (values == null || values.Count == 0) return 0; var sorted = new List(values); sorted.Sort(); int n = sorted.Count; return n % 2 == 1 ? sorted[n / 2] : (sorted[n / 2 - 1] + sorted[n / 2]) / 2; } }