record into sequencer from midi in!

This commit is contained in:
🪞👃🪞 2024-06-16 20:41:13 +03:00
parent 2e7252f8b9
commit 2118615aea
2 changed files with 94 additions and 53 deletions

View file

@ -5,7 +5,7 @@ pub struct Hz(pub u32);
pub struct Frame(pub u32);
/// One microsecond.
pub struct Usec(pub u32);
pub struct Usec(pub u64);
/// Beats per minute as `120000` = 120.000BPM
pub struct Tempo(pub u32);
@ -24,29 +24,29 @@ pub enum NoteDuration {
impl Frame {
#[inline]
pub fn to_usec (&self, rate: &Hz) -> Usec {
Usec((self.0 * 1000) / rate.0)
Usec((self.0 as u64 * 1000000) / rate.0 as u64)
}
}
impl Usec {
#[inline]
pub fn to_frame (&self, rate: &Hz) -> Frame {
Frame((self.0 * rate.0) / 1000)
Frame(((self.0 * rate.0 as u64) / 1000) as u32)
}
}
impl Tempo {
#[inline]
pub fn to_usec_per_beat (&self) -> Usec {
Usec((60_000_000_000 / self.0 as usize) as u32)
pub fn usec_per_beat (&self) -> Usec {
Usec(60_000_000_000 / self.0 as u64)
}
#[inline]
pub fn to_usec_per_quarter (&self) -> Usec {
Usec(self.to_usec_per_quarter().0 / 4)
pub fn usec_per_quarter (&self) -> Usec {
Usec(self.usec_per_beat().0 / 4)
}
#[inline]
pub fn to_usec_per_tick (&self, ppq: u32) -> Usec {
Usec(self.to_usec_per_quarter().0 / ppq)
pub fn usec_per_tick (&self, ppq: u32) -> Usec {
Usec(self.usec_per_quarter().0 / ppq as u64)
}
#[inline]
pub fn quantize (&self, step: &NoteDuration, time: Usec) -> Usec {
@ -69,11 +69,11 @@ impl NoteDuration {
fn to_usec (&self, bpm: &Tempo) -> Usec {
Usec(match self {
Self::Nth(time, flies) =>
bpm.to_usec_per_beat().0 * *time as u32 / *flies as u32,
bpm.usec_per_beat().0 * *time as u64 / *flies as u64,
Self::Dotted(duration) =>
duration.to_usec(bpm).0 * 3 / 2,
Self::Tuplet(n, duration) =>
duration.to_usec(bpm).0 * 2 / *n as u32,
duration.to_usec(bpm).0 * 2 / *n as u64,
})
}
fn to_frame (&self, bpm: &Tempo, rate: &Hz) -> Frame {