Demos/BeatPad/NoteRepeatClock.cs
namespace Sandbox.BeatPad;

// Fixed-interval repeat clock for NOTE mode. Accumulates dt and reports how many
// repeats elapsed this tick. The owning pad already fired on the original press,
// so the first repeat lands one full interval later.
public struct NoteRepeatClock
{
    public const float DefaultIntervalSeconds = 0.24f; // 8th notes at 120 BPM

    readonly float _interval;
    float _accumulator;

    public NoteRepeatClock(float intervalSeconds)
    {
        _interval = intervalSeconds <= 0f ? DefaultIntervalSeconds : intervalSeconds;
        _accumulator = 0f;
    }

    public void Reset() => _accumulator = 0f;

    public int Tick(float dt)
    {
        if (dt <= 0f) return 0;
        _accumulator += dt;
        int fires = 0;
        while (_accumulator >= _interval)
        {
            _accumulator -= _interval;
            fires++;
        }
        return fires;
    }
}