mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
tabula rasa
This commit is contained in:
commit
11a9f3ba50
33 changed files with 1937 additions and 0 deletions
46
crates/timebase/src/lib.rs
Normal file
46
crates/timebase/src/lib.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
pub struct Timebase {
|
||||
/// Sample rate
|
||||
rate: u64,
|
||||
/// Current frame
|
||||
frame: u64,
|
||||
/// Beats per bar
|
||||
signature: (u64, u64),
|
||||
/// Beats per minute
|
||||
bpm: f64,
|
||||
}
|
||||
|
||||
impl Timebase {
|
||||
pub fn new (rate: u64, signature: (u64, u64), bpm: f64) -> Self {
|
||||
Self {
|
||||
rate,
|
||||
frame: 0,
|
||||
signature,
|
||||
bpm,
|
||||
}
|
||||
}
|
||||
pub fn seconds (&self) -> f64 {
|
||||
self.frame as f64 / self.rate as f64
|
||||
}
|
||||
pub fn minutes (&self) -> f64 {
|
||||
self.seconds / 60.0
|
||||
}
|
||||
pub fn beats (&self) -> f64 {
|
||||
self.minutes / self.bpm
|
||||
}
|
||||
pub fn bar (&self) -> u64 {
|
||||
((self.beats() * self.signature.0 as f64) / self.signature.1 as f64) as u64
|
||||
}
|
||||
pub fn beat (&self) -> u64 {
|
||||
((self.beats() * self.signature.0 as f64) % self.signature.1 as f64) as u64
|
||||
}
|
||||
pub fn time_string (&self) -> String {
|
||||
let minutes = self.minutes() as u64;
|
||||
let seconds = self.seconds() as u64;
|
||||
let milliseconds = self.seconds() - seconds;
|
||||
format!("{minutes}:{seconds}.{milliseconds:03}")
|
||||
}
|
||||
pub fn beat_string (&self) -> String {
|
||||
let beats = self.beats() as u64;
|
||||
format!("{beats} {}/{}", minutes, self.beat(), self.signature.1)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue