Struct MusicTimerEngine

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

The engine uses all of this crate’s utilities to allow to use of a music performance state system that triggers callbacks. Its aims are to allow for an easy interface for changes in music time.

Implementations§

Source§

impl MusicTimerEngine

Source

pub fn new(time_signature: TimeSignature, bpm: f32) -> Self

Create a new MusicTimerEngine with a TimeSignature and bpm.

§Arguments
  • time_signature - The time signature for the performance.
  • bpm - The beats per minute used for the performance.
§Example
use music_timer::{music_timer_engine::MusicTimerEngine, time_signature::TimeSignature};
let mut performer = MusicTimerEngine::new(TimeSignature::new(3, 4), 155.0);
Source

pub fn pulse<TimerState: MusicTimerState>(&mut self, state: &mut TimerState)

Pulse the engine. The time since the last pulse is used to evaluate if there is a change in music time. It is suggested to call this from a loop.

§Arguments
  • state - The trait MusicTimerState used for changes in music time callbacks.TimeSignature
§Example
use music_timer::{music_timer_engine::{MusicTimerEngine, MusicTimerState}, music_time::MusicTime};
struct PerformanceState;
impl MusicTimerState for PerformanceState {
    fn on_beat_interval(&mut self, current_time: &MusicTime) {
      // Do something on the beat interval
    }
    fn on_beat(&mut self, current_time: &MusicTime) {
        // Do something on the beat
    }
    fn on_bar(&mut self, current_time: &MusicTime) {
        // Do something on the bar
    }
}
let mut performer_state = PerformanceState{};
let mut performer = music_timer::create_performance_engine(3, 4, 155.0);
performer.pulse(&mut performer_state);
Examples found in repository?
examples/event_performance.rs (line 68)
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_beat_interval_duration(&self) -> Duration

Gets the duration of time between beat intervals. Handy for sleeping threads.

§Example
let mut performer = music_timer::create_performance_engine(3, 4, 155.0);

// We can set the delay to be half the trigger target. This will give
// us a reasonable cycle speed with enough buffer to keep an accurate time.
// This of course is not needed if the application is managing thread sleeping.
// The shorter the sleep duration of the thread, the more accurate the
// time triggering will be. In most cases setting the sleep to 60fps is recommended for
// < 180bpm @ 4/4.
let sleep_duration = performer.get_beat_interval_duration() / 2;
println!("SLEEP_DURATION: {:?}", sleep_duration);
std::thread::sleep(sleep_duration);
Examples found in repository?
examples/event_performance.rs (line 63)
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_current_time(&self) -> &MusicTime

Gets the current music time of the performance.

Source

pub fn set_music_timer(&mut self, time: MusicTime) -> &mut Self

Sets the current music time.

§Arguments
  • time - The new music time to set.

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> 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, 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.