MusicalTime

Struct MusicalTime 

Source
pub struct MusicalTime { /* private fields */ }
Expand description

Musical time in units of beats + super-beats.

Note this value is always positive.

Implementations§

Source§

impl MusicalTime

Source

pub fn new(beats: u32, super_beats: u32) -> Self

  • beats - The time in musical beats.
  • super_beats - The number of super-beats (after the time in self.beats) (Note this value will be constrained to the range [0, 508,032,000)).

A “super-beat” is a unit of time equal to 1 / 508,032,000 of a beat. This number was chosen because it is nicely divisible by a whole slew of factors including 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 24, 32, 64, 128, 256, 512, and 1920, as well as common sampling rates such as 22050, 24000, 44100, 48000, 88200, 96000, 176400, and 192000. This ensures that any recording of note data in this format will always be at-least sample-accurate.

Source

pub fn beats(&self) -> u32

The time in musical beats.

Source

pub fn super_beats(&self) -> u32

The number of super-beats (after the time in self.beats()).

A “super-beat” is a unit of time equal to 1 / 508,032,000 of a beat. This number was chosen because it is nicely divisible by a whole slew of factors including 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 24, 32, 64, 128, 256, 512, and 1920, as well as common sampling rates such as 22050, 24000, 44100, 48000, 88200, 96000, 176400, and 192000. This ensures that any recording of note data in this format will always be at-least sample-accurate.

This value will always be in the range [0, 508,032,000).

Source

pub fn from_beats(beats: u32) -> Self

  • beats - The time in musical beats.
Source

pub fn from_half_beats(beats: u32, half_beats: u32) -> Self

  • beats - The time in musical beats.
  • half_beats - The number of half-beats (after the time beats). This will be constrained to the range [0, 1].
Source

pub fn from_quarter_beats(beats: u32, quarter_beats: u32) -> Self

  • beats - The time in musical beats.
  • quarter_beats - The number of quarter-beats (after the time beats). This will be constrained to the range [0, 3].
Source

pub fn from_eighth_beats(beats: u32, eighth_beats: u32) -> Self

  • beats - The time in musical beats.
  • eighth_beats - The number of eighth-beats (after the time beats). This will be constrained to the range [0, 7].
Source

pub fn from_sixteenth_beats(beats: u32, sixteenth_beats: u32) -> Self

  • beats - The time in musical beats.
  • sixteenth_beats - The number of sixteenth-beats (after the time beats). This will be constrained to the range [0, 15].
Source

pub fn from_32nd_beats(beats: u32, _32nd_beats: u32) -> Self

  • beats - The time in musical beats.
  • _32nd_beats - The number of 32nd-beats (after the time beats). This will be constrained to the range [0, 31].
Source

pub fn from_64th_beats(beats: u32, _64th_beats: u32) -> Self

  • beats - The time in musical beats.
  • _64th_beats - The number of 64th-beats (after the time beats). This will be constrained to the range [0, 63].
Source

pub fn from_128th_beats(beats: u32, _128th_beats: u32) -> Self

  • beats - The time in musical beats.
  • _128th_beats - The number of 128th-beats (after the time beats). This will be constrained to the range [0, 127].
Source

pub fn from_third_beats(beats: u32, third_beats: u32) -> Self

  • beats - The time in musical beats.
  • third_beats - The number of third-beats (after the time beats). This will be constrained to the range [0, 2].
Source

pub fn from_fifth_beats(beats: u32, fifth_beats: u32) -> Self

  • beats - The time in musical beats.
  • fifth_beats - The number of fifth-beats (after the time beats). This will be constrained to the range [0, 4].
Source

pub fn from_sixth_beats(beats: u32, fifth_beats: u32) -> Self

  • beats - The time in musical beats.
  • sixth_beats - The number of sixth-beats (after the time beats). This will be constrained to the range [0, 5].
Source

pub fn from_seventh_beats(beats: u32, seventh_beats: u32) -> Self

  • beats - The time in musical beats.
  • seventh_beats - The number of seventh-beats (after the time beats). This will be constrained to the range [0, 6].
Source

pub fn from_ninth_beats(beats: u32, ninth_beats: u32) -> Self

  • beats - The time in musical beats.
  • ninth_beats - The number of ninth-beats (after the time beats). This will be constrained to the range [0, 8].
Source

pub fn from_tenth_beats(beats: u32, tenth_beats: u32) -> Self

  • beats - The time in musical beats.
  • tenth_beats - The number of tenth-beats (after the time beats). This will be constrained to the range [0, 9].
Source

pub fn from_twelth_beats(beats: u32, twelth_beats: u32) -> Self

  • beats - The time in musical beats.
  • twelth_beats - The number of twelth-beats (after the time beats). This will be constrained to the range [0, 11].
Source

pub fn from_24th_beats(beats: u32, _24th_beats: u32) -> Self

  • beats - The time in musical beats.
  • _24th_beats - The number of 24th-beats (after the time beats). This will be constrained to the range [0, 23].
Source

pub fn from_beats_f64(beats: f64) -> Self

Get the corresponding musical time from the number of beats (as an f64).

Note that this conversion is NOT lossless.

If beats is negative, then a MusicalTime of 0 will be returned instead.

Source

pub fn as_beats_f64(&self) -> f64

Convert the corresponding musical time in units of beats (as an f64 value).

Note that this conversion is NOT lossless.

This is useful for displaying notes in UI.

Source

pub fn snap_to_nearest_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_whole_beats(&self, beats: u32) -> MusicalTime

Snap to the nearest multiple of whole beats.

Source

pub fn snap_to_nearest_fractional_beat<const DIVISOR: u32>(&self) -> MusicalTime

Source

pub fn snap_to_nearest_half_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_quarter_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_eigth_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_sixteenth_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_32nd_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_64th_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_128th_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_third_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_fifth_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_sixth_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_seventh_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_ninth_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_tenth_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_twelth_beat(&self) -> MusicalTime

Source

pub fn snap_to_nearest_24th_beat(&self) -> MusicalTime

Source

pub fn num_fractional_beats<const DIVISOR: u32>(&self) -> u32

The number of fractional-beats after self.beats() (floored to the nearest fractional-beat).

This will always be in the range [0, DIVISOR - 1].

Source

pub fn num_half_beats(&self) -> u32

The number of half-beats after self.beats() (floored to the nearest half-beat).

This will always be in the range [0, 1].

Source

pub fn num_quarter_beats(&self) -> u32

The number of quarter-beats after self.beats() (floored to the nearest quarter-beat).

This will always be in the range [0, 3].

Source

pub fn num_eigth_beats(&self) -> u32

The number of eigth-beats after self.beats() (floored to the nearest eigth-beat).

This will always be in the range [0, 7].

Source

pub fn num_sixteenth_beats(&self) -> u32

The number of sixteenth-beats after self.beats() (floored to the nearest sixteenth-beat).

This will always be in the range [0, 15].

Source

pub fn num_32nd_beats(&self) -> u32

The number of 32nd-beats after self.beats() (floored to the nearest 32nd-beat).

This will always be in the range [0, 31].

Source

pub fn num_64th_beats(&self) -> u32

The number of 128th-beats after self.beats() (floored to the nearest 64th-beat).

This will always be in the range [0, 63].

Source

pub fn num_128th_beats(&self) -> u32

The number of 64th-beats after self.beats() (floored to the nearest 128th-beat).

This will always be in the range [0, 127].

Source

pub fn num_third_beats(&self) -> u32

The number of third-beats after self.beats() (floored to the nearest third-beat).

This will always be in the range [0, 2].

Source

pub fn num_fifth_beats(&self) -> u32

The number of fifth-beats after self.beats() (floored to the nearest fifth-beat).

This will always be in the range [0, 4].

Source

pub fn num_sixth_beats(&self) -> u32

The number of sixth-beats after self.beats() (floored to the nearest sixth-beat).

This will always be in the range [0, 5].

Source

pub fn num_seventh_beats(&self) -> u32

The number of seventh-beats after self.beats() (floored to the nearest seventh-beat).

This will always be in the range [0, 6].

Source

pub fn num_ninth_beats(&self) -> u32

The number of ninth-beats after self.beats() (floored to the nearest ninth-beat).

This will always be in the range [0, 8].

Source

pub fn num_tenth_beats(&self) -> u32

The number of tenth-beats after self.beats() (floored to the nearest tenth-beat).

This will always be in the range [0, 9].

Source

pub fn num_twelth_beats(&self) -> u32

The number of twelth-beats after self.beats() (floored to the nearest twelth-beat).

This will always be in the range [0, 11].

Source

pub fn num_24th_beats(&self) -> u32

The number of 24th-beats after self.beats() (floored to the nearest 24th-beat).

This will always be in the range [0, 23].

Source

pub fn to_seconds(&self, bpm: f64) -> Seconds

Convert to the corresponding time in Seconds.

Note that this conversion is NOT lossless.

Source

pub fn to_nearest_frame_round( &self, bpm: f64, sample_rate: SampleRate, ) -> Frames

Convert to the corresponding discrete Frames. This will be rounded to the nearest sample.

Note that this conversion is NOT lossless.

Note that this must be re-calculated after recieving a new SampleRate.

Source

pub fn to_nearest_frame_floor( &self, bpm: f64, sample_rate: SampleRate, ) -> Frames

Convert to the corresponding discrete Frames. This will be floored to the nearest sample.

Note that this conversion is NOT lossless.

Note that this must be re-calculated after recieving a new SampleRate.

Source

pub fn to_nearest_frame_ceil(&self, bpm: f64, sample_rate: SampleRate) -> Frames

Convert to the corresponding discrete Frames. This will be ceil-ed to the nearest sample.

Note that this conversion is NOT lossless.

Note that this must be re-calculated after recieving a new SampleRate.

Source

pub fn to_sub_frames(&self, bpm: f64, sample_rate: SampleRate) -> (Frames, f64)

Convert to the corresponding discrete Frames floored to the nearest sample, while also returning the fractional sub-sample part.

Note that this conversion is NOT lossless.

Note that this must be re-calculated after recieving a new SampleRate.

Source

pub fn to_nearest_super_frame_round(&self, bpm: f64) -> SuperFrames

Convert to the corresponding discrete SuperFrames. This will be rounded to the nearest super-frame.

Note that this conversion is NOT lossless.

Source

pub fn to_nearest_super_frame_floor(&self, bpm: f64) -> SuperFrames

Convert to the corresponding discrete SuperFrames. This will be floored to the nearest super-frame.

Note that this conversion is NOT lossless.

Source

pub fn to_nearest_super_frame_ceil(&self, bpm: f64) -> SuperFrames

Convert to the corresponding discrete SuperFrames. This will be ceil-ed to the nearest super-frame.

Note that this conversion is NOT lossless.

Source

pub fn to_sub_super_frames(&self, bpm: f64) -> (SuperFrames, f64)

Convert to the corresponding discrete SuperFrames floored to the nearest super-frame, while also returning the fractional sub-super-frame part.

Note that this conversion is NOT lossless.

Source

pub fn checked_sub(self, rhs: MusicalTime) -> Option<MusicalTime>

Try subtracting rhs from self. This will return None if the resulting value is negative due to rhs being larger than self (overflow).

Trait Implementations§

Source§

impl Add for MusicalTime

Source§

type Output = MusicalTime

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign for MusicalTime

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl Clone for MusicalTime

Source§

fn clone(&self) -> MusicalTime

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MusicalTime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MusicalTime

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<AtomicMusicalTime> for MusicalTime

Source§

fn from(value: AtomicMusicalTime) -> Self

Converts to this type from the input type.
Source§

impl From<MusicalTime> for AtomicMusicalTime

Source§

fn from(value: MusicalTime) -> Self

Converts to this type from the input type.
Source§

impl Hash for MusicalTime

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul<u32> for MusicalTime

Source§

type Output = MusicalTime

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u32) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<u32> for MusicalTime

Source§

fn mul_assign(&mut self, other: u32)

Performs the *= operation. Read more
Source§

impl PartialEq for MusicalTime

Source§

fn eq(&self, other: &MusicalTime) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for MusicalTime

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for MusicalTime

Source§

impl Eq for MusicalTime

Source§

impl StructuralPartialEq for MusicalTime

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.