Struct Rocket

Source
pub struct Rocket<P: AsRef<Path>> { /* private fields */ }
Expand description

Provides sync values.

§Usage

See module documentation.

Implementations§

Source§

impl<P: AsRef<Path>> Rocket<P>

Source

pub fn new(path: P, bpm: f32) -> Result<Self, DecodeError>

Initializes rocket.

§Without player feature

Attempts to connect to a rocket tracker.

§With player feature

Loads tracks from file specified by path using bincode.

§Errors

Any errors that occur are first printed to stderr, then returned to the caller.

An error is returned If the file specified by path cannot be read or its contents cannot be decoded.

The return value can be handled by calling unwrap if you want to panic, or ok if you want to ignore the error and continue without using rocket.

Examples found in repository?
examples/simple.rs (line 47)
46fn main() {
47    let mut rocket = Rocket::new("tracks.bin", 60.).unwrap();
48    let mut time_source = TimeSource::new();
49    let mut previous_print_time = Duration::ZERO;
50
51    'main: loop {
52        // <Handle other event sources such as SDL or winit here>
53
54        // Handle events from the rocket tracker
55        while let Some(event) = rocket.poll_events().ok().flatten() {
56            match event {
57                Event::Seek(to) => time_source.seek(to),
58                Event::Pause(state) => time_source.pause(state),
59                Event::NotConnected =>
60                /* Alternatively: break the loop here and keep rendering frames */
61                {
62                    std::thread::sleep(Duration::from_millis(10));
63                    continue 'main;
64                }
65            }
66        }
67
68        // Get current frame's time and keep the tracker updated
69        let time = time_source.get_time();
70        rocket.set_time(&time);
71
72        // <In a full demo you would render a frame here>
73
74        // Filter redundant output
75        if time != previous_print_time {
76            println!("{:?}: test = {}", time, rocket.get_value("test"));
77        }
78        previous_print_time = time;
79        thread::sleep(Duration::from_millis(10));
80    }
81}
Source

pub fn get_value(&mut self, track: &str) -> f32

Get value based on previous call to set_time, by track name.

§Panics

With player feature: if the file specified in call to new doesn’t contain track with name, the function handles the error by printing to stderr and panicking.

Examples found in repository?
examples/simple.rs (line 76)
46fn main() {
47    let mut rocket = Rocket::new("tracks.bin", 60.).unwrap();
48    let mut time_source = TimeSource::new();
49    let mut previous_print_time = Duration::ZERO;
50
51    'main: loop {
52        // <Handle other event sources such as SDL or winit here>
53
54        // Handle events from the rocket tracker
55        while let Some(event) = rocket.poll_events().ok().flatten() {
56            match event {
57                Event::Seek(to) => time_source.seek(to),
58                Event::Pause(state) => time_source.pause(state),
59                Event::NotConnected =>
60                /* Alternatively: break the loop here and keep rendering frames */
61                {
62                    std::thread::sleep(Duration::from_millis(10));
63                    continue 'main;
64                }
65            }
66        }
67
68        // Get current frame's time and keep the tracker updated
69        let time = time_source.get_time();
70        rocket.set_time(&time);
71
72        // <In a full demo you would render a frame here>
73
74        // Filter redundant output
75        if time != previous_print_time {
76            println!("{:?}: test = {}", time, rocket.get_value("test"));
77        }
78        previous_print_time = time;
79        thread::sleep(Duration::from_millis(10));
80    }
81}
Source

pub fn set_time(&mut self, time: &Duration)

Update rocket with the current time from your time source, e.g. music player.

Examples found in repository?
examples/simple.rs (line 70)
46fn main() {
47    let mut rocket = Rocket::new("tracks.bin", 60.).unwrap();
48    let mut time_source = TimeSource::new();
49    let mut previous_print_time = Duration::ZERO;
50
51    'main: loop {
52        // <Handle other event sources such as SDL or winit here>
53
54        // Handle events from the rocket tracker
55        while let Some(event) = rocket.poll_events().ok().flatten() {
56            match event {
57                Event::Seek(to) => time_source.seek(to),
58                Event::Pause(state) => time_source.pause(state),
59                Event::NotConnected =>
60                /* Alternatively: break the loop here and keep rendering frames */
61                {
62                    std::thread::sleep(Duration::from_millis(10));
63                    continue 'main;
64                }
65            }
66        }
67
68        // Get current frame's time and keep the tracker updated
69        let time = time_source.get_time();
70        rocket.set_time(&time);
71
72        // <In a full demo you would render a frame here>
73
74        // Filter redundant output
75        if time != previous_print_time {
76            println!("{:?}: test = {}", time, rocket.get_value("test"));
77        }
78        previous_print_time = time;
79        thread::sleep(Duration::from_millis(10));
80    }
81}
Source

pub fn poll_events(&mut self) -> Result<Option<Event>, EncodeError>

Poll for new events from rocket.

§Without player feature

This polls from events from the tracker. You should call this at least once per frame. It is recommended to keep calling this in a while loop until you receive Ok(None).

§Errors

Any errors that occur are first printed to stderr, then returned to the caller.

An error is returned if the file specified in call to new cannot be written to.

The return value can be handled by calling unwrap if you want to panic, or .ok().flatten() if you want to ignore the error and continue.

§Example
while let Some(event) = rocket.poll_events().ok().flatten() {
    match event {
        Event::Seek(to) => music.seek(to),
        Event::Pause(state) => music.pause(state),
        Event::NotConnected => break,
    }
}
§Tips

There are three sensible ways to handle the Event::NotConnected variant:

  1. break: End your event polling while let-loop and proceed to rendering the frame. All Rocket methods keep working, but without control from the tracker.
  2. continue 'main: Restart your main loop, don’t render the frame. This lets you keep calling other event polling functions from other libraries, e.g. SDL or winit.
  3. {}: Ignore it and let your event polling loop continue.

Options 2 and 3 result is a busy wait, e.g. waste a lot of CPU time. It’s better to combine them with std::thread::sleep for at least a few milliseconds in order to mitigate that.

§With player feature

The function is a no-op.

Examples found in repository?
examples/simple.rs (line 55)
46fn main() {
47    let mut rocket = Rocket::new("tracks.bin", 60.).unwrap();
48    let mut time_source = TimeSource::new();
49    let mut previous_print_time = Duration::ZERO;
50
51    'main: loop {
52        // <Handle other event sources such as SDL or winit here>
53
54        // Handle events from the rocket tracker
55        while let Some(event) = rocket.poll_events().ok().flatten() {
56            match event {
57                Event::Seek(to) => time_source.seek(to),
58                Event::Pause(state) => time_source.pause(state),
59                Event::NotConnected =>
60                /* Alternatively: break the loop here and keep rendering frames */
61                {
62                    std::thread::sleep(Duration::from_millis(10));
63                    continue 'main;
64                }
65            }
66        }
67
68        // Get current frame's time and keep the tracker updated
69        let time = time_source.get_time();
70        rocket.set_time(&time);
71
72        // <In a full demo you would render a frame here>
73
74        // Filter redundant output
75        if time != previous_print_time {
76            println!("{:?}: test = {}", time, rocket.get_value("test"));
77        }
78        previous_print_time = time;
79        thread::sleep(Duration::from_millis(10));
80    }
81}
Source

pub fn save_tracks(&self) -> Result<(), EncodeError>

Save a snapshot of the tracks in the session, overwriting the file specified in call to new.

§Errors

Any errors that occur are first printed to stderr, then returned to the caller.

An error is returned if the file specified in call to new cannot be written to.

The return value can be handled by calling unwrap if you want to panic, or ok if you want to ignore the error and continue.

§With player feature

The function is a no-op.

Source§

impl Rocket<&str>

Source

pub fn from_std_read<R: Read>( read: &mut R, bpm: f32, ) -> Result<Self, DecodeError>

An escape hatch constructor for advanced users who want to handle track loading via other means than File.

This function is only available when the player feature is enabled, so you should not default to using it.

§Usage

The function makes it possible to load from e.g. std::include_bytes! in release builds.

// const SYNC_DATA: &[u8] = include_bytes!("tracks.bin");

#[cfg(feature = "player")]
let rocket = Rocket::from_std_read(&mut SYNC_DATA, 120.).unwrap_or_else(|_| unsafe {
    std::hint::unreachable_unchecked()
});

Auto Trait Implementations§

§

impl<P> Freeze for Rocket<P>
where P: Freeze,

§

impl<P> RefUnwindSafe for Rocket<P>
where P: RefUnwindSafe,

§

impl<P> Send for Rocket<P>
where P: Send,

§

impl<P> Sync for Rocket<P>
where P: Sync,

§

impl<P> Unpin for Rocket<P>
where P: Unpin,

§

impl<P> UnwindSafe for Rocket<P>
where P: UnwindSafe,

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.