Skip to content

Commit

Permalink
inline sample -> beat/second time conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
emuell committed Jul 6, 2024
1 parent f57b44b commit ff2fdd1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/rhythm/beat_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ use crate::{
// -------------------------------------------------------------------------------------------------

impl GenericRhythmTimeStep for BeatTimeStep {
#[inline]
fn default_offset() -> Self {
Self::Beats(0.0)
}

#[inline]
fn default_step() -> Self {
Self::Beats(1.0)
}

fn to_samples(&self, time_base: &crate::BeatTimeBase) -> f64 {
self.to_samples(time_base)
#[inline]
fn to_samples(&self, time_base: &BeatTimeBase) -> f64 {
BeatTimeStep::to_samples(self, time_base)
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/rhythm/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,13 @@ impl<Step: GenericRhythmTimeStep, Offset: GenericRhythmTimeStep> GenericRhythm<S
}

/// Return current pulse duration in samples
#[inline]
pub fn current_steps_sample_duration(&self) -> f64 {
self.step.to_samples(&self.time_base) * self.event_iter_pulse_item.step_time
}

/// Return start sample time of the given event iter item
#[inline]
fn event_iter_item_start_time(&self, start: &Fraction) -> SampleTime {
let step_time = self.current_steps_sample_duration();
let event_iter_time = self.sample_offset as f64 + self.event_iter_next_sample_time;
Expand All @@ -194,6 +196,7 @@ impl<Step: GenericRhythmTimeStep, Offset: GenericRhythmTimeStep> GenericRhythm<S
}

/// Return duration in sample time of the given event iter item
#[inline]
fn event_iter_item_duration(&self, length: &Fraction) -> SampleTime {
let step_time = self.current_steps_sample_duration();
let length = length.to_f64().unwrap_or(1.0);
Expand Down
5 changes: 4 additions & 1 deletion src/rhythm/second_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ use crate::{
// -------------------------------------------------------------------------------------------------

impl GenericRhythmTimeStep for SecondTimeStep {
#[inline]
fn default_offset() -> Self {
0.0
}

#[inline]
fn default_step() -> Self {
1.0
}

#[inline]
fn to_samples(&self, time_base: &BeatTimeBase) -> f64 {
time_base.seconds_to_samples_exact(*self)
*self * time_base.samples_per_second() as f64
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,4 @@ pub trait TimeBase: Debug {
fn seconds_to_samples(&self, seconds: f64) -> SampleTime {
(seconds * self.samples_per_second() as f64).trunc() as SampleTime
}
fn seconds_to_samples_exact(&self, seconds: f64) -> f64 {
seconds * self.samples_per_second() as f64
}
}
9 changes: 6 additions & 3 deletions src/time/beats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ pub struct BeatTimeBase {

impl BeatTimeBase {
/// Time base's samples per beat, in order to convert beat to sample time and vice versa.
#[inline]
pub fn samples_per_beat(&self) -> f64 {
self.samples_per_sec as f64 * 60.0 / self.beats_per_min as f64
}
/// Time base's samples per bar, in order to convert bar to sample time and vice versa.
#[inline]
pub fn samples_per_bar(&self) -> f64 {
self.samples_per_sec as f64 * 60.0 / self.beats_per_min as f64 * self.beats_per_bar as f64
}
Expand All @@ -33,6 +35,7 @@ impl From<BeatTimeBase> for SecondTimeBase {
}

impl TimeBase for BeatTimeBase {
#[inline]
fn samples_per_second(&self) -> u32 {
self.samples_per_sec
}
Expand All @@ -43,10 +46,10 @@ impl SampleTimeDisplay for BeatTimeBase {
fn display(&self, sample_time: SampleTime) -> String {
let total_beats = sample_time / self.samples_per_beat() as u64;
let total_beats_f = sample_time as f64 / self.samples_per_beat();
let beat_frations = total_beats_f - total_beats as f64;
let beat_fractions = total_beats_f - total_beats as f64;
let bars = total_beats / self.beats_per_bar as u64;
let beats = total_beats - self.beats_per_bar as u64 * bars;
let ppq = (beat_frations * 960.0 + 0.5) as u64;
let ppq = (beat_fractions * 960.0 + 0.5) as u64;
format!("{}.{}.{:03}", bars + 1, beats + 1, ppq)
}
}
Expand All @@ -69,7 +72,6 @@ pub enum BeatTimeStep {
impl BeatTimeStep {
/// Get number of steps in the current time resolution.
pub fn steps(&self) -> f32 {
#[allow(clippy::match_same_arms)]
match *self {
BeatTimeStep::SixtyFourth(amount) => amount,
BeatTimeStep::ThirtySecond(amount) => amount,
Expand Down Expand Up @@ -109,6 +111,7 @@ impl BeatTimeStep {
}
}
/// Convert a beat or bar step to samples for the given beat time base.
#[inline]
pub fn to_samples(&self, time_base: &BeatTimeBase) -> f64 {
self.steps() as f64 * self.samples_per_step(time_base)
}
Expand Down
1 change: 1 addition & 0 deletions src/time/seconds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct SecondTimeBase {
}

impl TimeBase for SecondTimeBase {
#[inline]
fn samples_per_second(&self) -> u32 {
self.samples_per_sec
}
Expand Down

0 comments on commit ff2fdd1

Please sign in to comment.