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
impl MusicTimerEngine
Sourcepub fn new(time_signature: TimeSignature, bpm: f32) -> Self
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);Sourcepub fn pulse<TimerState: MusicTimerState>(&mut self, state: &mut TimerState)
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 traitMusicTimerStateused 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}Sourcepub fn get_beat_interval_duration(&self) -> Duration
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}Sourcepub fn get_current_time(&self) -> &MusicTime
pub fn get_current_time(&self) -> &MusicTime
Gets the current music time of the performance.
Sourcepub fn set_music_timer(&mut self, time: MusicTime) -> &mut Self
pub fn set_music_timer(&mut self, time: MusicTime) -> &mut Self
Auto Trait Implementations§
impl Freeze for MusicTimerEngine
impl RefUnwindSafe for MusicTimerEngine
impl Send for MusicTimerEngine
impl Sync for MusicTimerEngine
impl Unpin for MusicTimerEngine
impl UnwindSafe for MusicTimerEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more