diff --git a/src/rhythm/beat_time.rs b/src/rhythm/beat_time.rs index 429b156..d9a4486 100644 --- a/src/rhythm/beat_time.rs +++ b/src/rhythm/beat_time.rs @@ -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) } } diff --git a/src/rhythm/generic.rs b/src/rhythm/generic.rs index ee133f0..414059c 100644 --- a/src/rhythm/generic.rs +++ b/src/rhythm/generic.rs @@ -181,11 +181,13 @@ impl GenericRhythm 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; @@ -194,6 +196,7 @@ impl GenericRhythm SampleTime { let step_time = self.current_steps_sample_duration(); let length = length.to_f64().unwrap_or(1.0); diff --git a/src/rhythm/second_time.rs b/src/rhythm/second_time.rs index 0f8094d..a4a83d7 100644 --- a/src/rhythm/second_time.rs +++ b/src/rhythm/second_time.rs @@ -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 } } diff --git a/src/time.rs b/src/time.rs index 9c7f0e0..3c7dc75 100644 --- a/src/time.rs +++ b/src/time.rs @@ -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 - } } diff --git a/src/time/beats.rs b/src/time/beats.rs index 3418d24..8a206ac 100644 --- a/src/time/beats.rs +++ b/src/time/beats.rs @@ -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 } @@ -33,6 +35,7 @@ impl From for SecondTimeBase { } impl TimeBase for BeatTimeBase { + #[inline] fn samples_per_second(&self) -> u32 { self.samples_per_sec } @@ -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) } } @@ -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, @@ -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) } diff --git a/src/time/seconds.rs b/src/time/seconds.rs index a12eee8..04fd40b 100644 --- a/src/time/seconds.rs +++ b/src/time/seconds.rs @@ -12,6 +12,7 @@ pub struct SecondTimeBase { } impl TimeBase for SecondTimeBase { + #[inline] fn samples_per_second(&self) -> u32 { self.samples_per_sec }