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
//! Rhythm game synchronization tool

use std::time::{Duration, Instant};

#[derive(Debug)]
pub struct AudioTracker {
    prior_ftime: Instant,
    prior_phead: u32,
    song_time: Duration,
    visual_lag: i32,
    audio_lag: i32,
}

impl AudioTracker {
    pub fn new(vlag: i32, alag: i32) -> Self {
        Self {
            prior_ftime: Instant::now(),
            prior_phead: 0,
            song_time: Duration::new(0, 0),
            audio_lag: alag,
            visual_lag: vlag,
        }
    }

    /// Only use when receiving a fresh playhead report
    pub fn ease(&mut self, playhead: u32) {
        let now = Instant::now();

        self.song_time = now.duration_since(self.prior_ftime);
        self.prior_ftime = now;

        if self.prior_phead != playhead {
            self.song_time = self.song_time + Duration::new(0, playhead);
            self.prior_phead = playhead;
        }
    }
}