Struct MusicTime

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

Data structure that holds music time and logic when advancing beats and beat intervals.

Implementations§

Source§

impl MusicTime

Source

pub fn new(bar: u16, beat: u8, beat_interval: u8) -> MusicTime

Create a new MusicTime.

§Arguments
  • bar - The musical bar.
  • beat - The musical beat.
  • beat - The musical beat interval, the subdivisions of a beat.
§Example
let time = music_timer::music_time::MusicTime::new(1, 1, 1);
Examples found in repository?
examples/event_performance.rs (line 45)
39fn main() {
40    use std::thread;
41
42    // Create the performer_state with bunch of events
43    let mut performer_state = PerformanceState {
44        is_playing: true,
45        performance_end: MusicTime::new(4, 3, 8),
46        events: vec![
47            MusicTime::new(1, 1, 1),
48            MusicTime::new(2, 2, 5),
49            MusicTime::new(4, 3, 8),
50        ],
51        event_head: 0,
52    };
53
54    // Run our main loop
55    let mut performer = music_timer::create_performance_engine(3, 4, 155.0);
56
57    // We can set the delay to be half the trigger target. This will give
58    // us a resonable cycle speed with enough buffer to keep an accurate time.
59    // This of course is not needed if the application is managing thread sleeping.
60    // The shorter the sleep duration of the thread, the more accurate the
61    // time triggering will be. In most cases setting the sleep to 60fps is recommended for
62    // < 180bpm @ 4/4.
63    let sleep_duration = performer.get_beat_interval_duration() / 2;
64    println!("SLEEP_DURATION: {:?}", sleep_duration);
65
66    while performer_state.is_playing {
67        // Pass in our performance state to trigger our on event callback functions
68        performer.pulse(&mut performer_state);
69        thread::sleep(sleep_duration);
70    }
71}
Source

pub fn get_bar(&self) -> u16

Get the bar number.

Examples found in repository?
examples/event_performance.rs (line 23)
11    fn on_beat_interval(&mut self, current_time: &MusicTime) {
12        let event_triggered =
13            self.event_head < self.events.len() && *current_time == self.events[self.event_head];
14
15        // Advance the event head
16        if event_triggered {
17            self.event_head += 1;
18        }
19
20        // Print out esoteric data
21        println!(
22            "{:02}.{}.{} = {}",
23            current_time.get_bar(),
24            current_time.get_beat(),
25            current_time.get_beat_interval(),
26            event_triggered
27        );
28
29        // Check to end the performance
30        self.is_playing = *current_time < self.performance_end;
31    }
Source

pub fn get_beat(&self) -> u8

Get the beat number.

Examples found in repository?
examples/event_performance.rs (line 24)
11    fn on_beat_interval(&mut self, current_time: &MusicTime) {
12        let event_triggered =
13            self.event_head < self.events.len() && *current_time == self.events[self.event_head];
14
15        // Advance the event head
16        if event_triggered {
17            self.event_head += 1;
18        }
19
20        // Print out esoteric data
21        println!(
22            "{:02}.{}.{} = {}",
23            current_time.get_bar(),
24            current_time.get_beat(),
25            current_time.get_beat_interval(),
26            event_triggered
27        );
28
29        // Check to end the performance
30        self.is_playing = *current_time < self.performance_end;
31    }
Source

pub fn get_beat_interval(&self) -> u8

Get the interval between the beat.

Examples found in repository?
examples/event_performance.rs (line 25)
11    fn on_beat_interval(&mut self, current_time: &MusicTime) {
12        let event_triggered =
13            self.event_head < self.events.len() && *current_time == self.events[self.event_head];
14
15        // Advance the event head
16        if event_triggered {
17            self.event_head += 1;
18        }
19
20        // Print out esoteric data
21        println!(
22            "{:02}.{}.{} = {}",
23            current_time.get_bar(),
24            current_time.get_beat(),
25            current_time.get_beat_interval(),
26            event_triggered
27        );
28
29        // Check to end the performance
30        self.is_playing = *current_time < self.performance_end;
31    }
Source

pub fn advance_beat(&mut self, time_signature: &TimeSignature)

Advance the beat by 1. The bar number will increase if the beat exceeds the TimeSignature numerator.

§Arguments
  • time_signature - The time signature to constrain the music time by.
§Example
use music_timer::{time_signature::TimeSignature, music_time::MusicTime};
let time_signature = TimeSignature::new(4, 4);
let mut a = MusicTime::default();
assert_eq!(a.get_bar() == 1 && a.get_beat() == 1, true);
a.advance_beat(&time_signature);
assert_eq!(a.get_bar() == 1 && a.get_beat() == 2, true);
a.advance_beat(&time_signature);
assert_eq!(a.get_bar() == 1 && a.get_beat() == 3, true);
a.advance_beat(&time_signature);
assert_eq!(a.get_bar() == 1 && a.get_beat() == 4, true);
a.advance_beat(&time_signature);
assert_eq!(a.get_bar() == 2 && a.get_beat() == 1, true);
Source

pub fn advance_beat_interval(&mut self, time_signature: &TimeSignature)

Advance the beat interval by 1. The beat number will increase if the beat interval exceeds the the interval resolution of 8. Then The bar number will increase if the beat exceeds the TimeSignature numerator.

§Arguments
  • time_signature - The time signature to constrain the music time by.
§Example
use music_timer::{time_signature::TimeSignature, music_time::MusicTime};
let time_signature = TimeSignature::new(4, 4);
let mut a = MusicTime::default();
assert_eq!(a, MusicTime::new(1, 1, 1));
a.advance_beat_interval(&time_signature);
assert_eq!(a, MusicTime::new(1, 1, 2));
a.advance_beat_interval(&time_signature);
a.advance_beat_interval(&time_signature);
a.advance_beat_interval(&time_signature);
a.advance_beat_interval(&time_signature);
a.advance_beat_interval(&time_signature);
a.advance_beat_interval(&time_signature);
assert_eq!(a, MusicTime::new(1, 1, 8));
a.advance_beat_interval(&time_signature);
assert_eq!(a, MusicTime::new(1, 2, 1));

Trait Implementations§

Source§

impl Clone for MusicTime

Source§

fn clone(&self) -> MusicTime

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 MusicTime

Source§

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

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

impl Default for MusicTime

Source§

fn default() -> MusicTime

Default is MusicTime::new(1,1,1)

Source§

impl Ord for MusicTime

Source§

fn cmp(&self, other: &MusicTime) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for MusicTime

Source§

fn eq(&self, other: &Self) -> 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 MusicTime

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 MusicTime

Source§

impl Eq for MusicTime

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.