/* * NatCorder * Copyright (c) 2020 Yusuf Olokoba. */ namespace NatSuite.Recorders.Clocks { using System.Runtime.CompilerServices; /// /// Clock that produces timestamps spaced at a fixed interval. /// This clock is useful for enforcing a fixed framerate in a recording. /// public sealed class FixedIntervalClock : IClock { /// /// Interval between consecutive timestamps generated by the clock in seconds. /// public double interval { get; set; } /// /// Current timestamp in nanoseconds. /// The very first value reported by this property will always be zero. /// public long timestamp { [MethodImpl(MethodImplOptions.Synchronized)] get => (long)((autoTick ? ticks++ : ticks) * interval * 1e+9); } /// /// Create a fixed interval clock for a given framerate. /// /// Desired framerate for clock's timestamps. /// Optional. If true, the clock will tick when its `Timestamp` is accessed. public FixedIntervalClock (int framerate, bool autoTick = true) : this(1.0 / framerate, autoTick) { } /// /// Create a fixed interval clock with a given timestamp interval. /// /// Interval between consecutive timestamps in seconds. /// Optional. If true, the clock will tick when its `Timestamp` is accessed. public FixedIntervalClock (double interval, bool autoTick = true) { this.interval = interval; this.ticks = 0; this.autoTick = autoTick; } /// /// Advance the clock by its time interval. /// [MethodImpl(MethodImplOptions.Synchronized)] public void Tick () => ticks++; private readonly bool autoTick; private int ticks; } }