use crate::*; /// Temporal resolutions: sample rate, tempo, MIDI pulses per quaver (beat) #[derive(Debug, Clone)] pub struct Timebase { /// Audio samples per second pub sr: SampleRate, /// MIDI beats per minute pub bpm: BeatsPerMinute, /// MIDI ticks per beat pub ppq: PulsesPerQuaver, } impl Timebase { /// Specify sample rate, BPM and PPQ pub fn new ( s: impl Into, b: impl Into, p: impl Into ) -> Self { Self { sr: s.into(), bpm: b.into(), ppq: p.into() } } /// Iterate over ticks between start and end. #[inline] pub fn pulses_between_samples (&self, start: usize, end: usize) -> TicksIterator { TicksIterator { spp: self.samples_per_pulse(), sample: start, start, end } } /// Return the duration fo a beat in microseconds #[inline] pub fn usec_per_beat (&self) -> f64 { 60_000_000f64 / self.bpm.get() } /// Return the number of beats in a second #[inline] pub fn beat_per_second (&self) -> f64 { self.bpm.get() / 60f64 } /// Return the number of microseconds corresponding to a note of the given duration #[inline] pub fn note_to_usec (&self, (num, den): (f64, f64)) -> f64 { 4.0 * self.usec_per_beat() * num / den } /// Return duration of a pulse in microseconds (BPM-dependent) #[inline] pub fn pulse_per_usec (&self) -> f64 { self.ppq.get() / self.usec_per_beat() } /// Return duration of a pulse in microseconds (BPM-dependent) #[inline] pub fn usec_per_pulse (&self) -> f64 { self.usec_per_beat() / self.ppq.get() } /// Return number of pulses to which a number of microseconds corresponds (BPM-dependent) #[inline] pub fn usecs_to_pulse (&self, usec: f64) -> f64 { usec * self.pulse_per_usec() } /// Convert a number of pulses to a sample number (SR- and BPM-dependent) #[inline] pub fn pulses_to_usec (&self, pulse: f64) -> f64 { pulse / self.usec_per_pulse() } /// Return number of pulses in a second (BPM-dependent) #[inline] pub fn pulses_per_second (&self) -> f64 { self.beat_per_second() * self.ppq.get() } /// Return fraction of a pulse to which a sample corresponds (SR- and BPM-dependent) #[inline] pub fn pulses_per_sample (&self) -> f64 { self.usec_per_pulse() / self.sr.usec_per_sample() } /// Return number of samples in a pulse (SR- and BPM-dependent) #[inline] pub fn samples_per_pulse (&self) -> f64 { self.sr.get() / self.pulses_per_second() } /// Convert a number of pulses to a sample number (SR- and BPM-dependent) #[inline] pub fn pulses_to_sample (&self, p: f64) -> f64 { self.pulses_per_sample() * p } /// Convert a number of samples to a pulse number (SR- and BPM-dependent) #[inline] pub fn samples_to_pulse (&self, s: f64) -> f64 { s / self.pulses_per_sample() } /// Return the number of samples corresponding to a note of the given duration #[inline] pub fn note_to_samples (&self, note: (f64, f64)) -> f64 { self.usec_to_sample(self.note_to_usec(note)) } /// Return the number of samples corresponding to the given number of microseconds #[inline] pub fn usec_to_sample (&self, usec: f64) -> f64 { usec * self.sr.get() / 1000f64 } /// Return the quantized position of a moment in time given a step #[inline] pub fn quantize (&self, step: (f64, f64), time: f64) -> (f64, f64) { let step = self.note_to_usec(step); (time / step, time % step) } /// Quantize a collection of events #[inline] pub fn quantize_into + Sized, T> ( &self, step: (f64, f64), events: E ) -> Vec<(f64, f64)> { events.map(|(time, event)|(self.quantize(step, time).0, event)).collect() } /// Format a number of pulses into Beat.Bar.Pulse starting from 0 #[inline] pub fn format_beats_0 (&self, pulse: f64) -> Arc { let pulse = pulse as usize; let ppq = self.ppq.get() as usize; let (beats, pulses) = if ppq > 0 { (pulse / ppq, pulse % ppq) } else { (0, 0) }; format!("{}.{}.{pulses:02}", beats / 4, beats % 4).into() } /// Format a number of pulses into Beat.Bar starting from 0 #[inline] pub fn format_beats_0_short (&self, pulse: f64) -> Arc { let pulse = pulse as usize; let ppq = self.ppq.get() as usize; let beats = if ppq > 0 { pulse / ppq } else { 0 }; format!("{}.{}", beats / 4, beats % 4).into() } /// Format a number of pulses into Beat.Bar.Pulse starting from 1 #[inline] pub fn format_beats_1 (&self, pulse: f64) -> Arc { let mut string = String::with_capacity(16); self.format_beats_1_to(&mut string, pulse).expect("failed to format {pulse} into beat"); string.into() } /// Format a number of pulses into Beat.Bar.Pulse starting from 1 #[inline] pub fn format_beats_1_to (&self, w: &mut impl std::fmt::Write, pulse: f64) -> Result<(), std::fmt::Error> { let pulse = pulse as usize; let ppq = self.ppq.get() as usize; let (beats, pulses) = if ppq > 0 { (pulse / ppq, pulse % ppq) } else { (0, 0) }; write!(w, "{}.{}.{pulses:02}", beats / 4 + 1, beats % 4 + 1) } /// Format a number of pulses into Beat.Bar.Pulse starting from 1 #[inline] pub fn format_beats_1_short (&self, pulse: f64) -> Arc { let pulse = pulse as usize; let ppq = self.ppq.get() as usize; let beats = if ppq > 0 { pulse / ppq } else { 0 }; format!("{}.{}", beats / 4 + 1, beats % 4 + 1).into() } } impl Default for Timebase { fn default () -> Self { Self::new(48000f64, 150f64, DEFAULT_PPQ) } }