Skip to content

Commit

Permalink
fix: add missing timestamp.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
SolarLiner committed Jul 27, 2024
1 parent ac0121b commit c62405d
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/timestamp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::ops;
use std::ops::AddAssign;
use std::time::Duration;

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Timestamp {
pub counter: u64,
pub samplerate: f64,
}

impl AddAssign<Duration> for Timestamp {
fn add_assign(&mut self, rhs: Duration) {
let samples = rhs.as_secs_f64() * self.samplerate;
self.counter += samples as u64;
}
}

impl AddAssign<u64> for Timestamp {
fn add_assign(&mut self, rhs: u64) {
self.counter += rhs;
}
}

impl<T> ops::Add<T> for Timestamp
where
Self: AddAssign<T>,
{
type Output = Self;

fn add(mut self, rhs: T) -> Self {
self.add_assign(rhs);
self
}
}

impl Timestamp {
pub fn new(samplerate: f64) -> Self {
Self {
counter: 0,
samplerate,
}
}

pub fn from_count(samplerate: f64, counter: u64) -> Self {
Self {
samplerate,
counter,
}
}

pub fn from_duration(samplerate: f64, duration: Duration) -> Self {
Self::from_seconds(samplerate, duration.as_secs_f64())
}

pub fn from_seconds(samplerate: f64, seconds: f64) -> Self {
let samples = samplerate * seconds;
Self {
samplerate,
counter: samples as _,
}
}

pub fn as_duration(&self) -> Duration {
Duration::from_secs_f64(self.as_seconds())
}

pub fn as_seconds(&self) -> f64 {
self.counter as f64 / self.samplerate
}
}

0 comments on commit c62405d

Please sign in to comment.