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
impl MusicTime
Sourcepub fn new(bar: u16, beat: u8, beat_interval: u8) -> MusicTime
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}Sourcepub fn get_bar(&self) -> u16
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 }Sourcepub fn get_beat(&self) -> u8
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 }Sourcepub fn get_beat_interval(&self) -> u8
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 }Sourcepub fn advance_beat(&mut self, time_signature: &TimeSignature)
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);Sourcepub fn advance_beat_interval(&mut self, time_signature: &TimeSignature)
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 Ord for MusicTime
impl Ord for MusicTime
Source§impl PartialOrd for MusicTime
impl PartialOrd for MusicTime
impl Copy for MusicTime
impl Eq for MusicTime
Auto Trait Implementations§
impl Freeze for MusicTime
impl RefUnwindSafe for MusicTime
impl Send for MusicTime
impl Sync for MusicTime
impl Unpin for MusicTime
impl UnwindSafe for MusicTime
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