Player

Struct Player 

Source
pub struct Player<T: Timer, C: Connection> {
    pub con: C,
    /* private fields */
}
Expand description

A type that orchestrates playing of MIDI tracks.

Player is a struct with two requirements:

  • A Timer for properly timing a sleep.
  • A Connection to send the MIDI events when they are ready to play.

So, Player is the glue that binds timing and playback.

This type is more of a convenience struct; it cannot possibly satisfy all use cases.

§Implementation Details

In this section, the "track" refers to either a Sheet or a slice of Moments.

This type orchestrates playback of tracks. There are some things that are assumed:

  1. Every moment in the given sheet is offset by exactly 1 MIDI tick.
  2. The provided Timer is assumed to be aware of #1 above.

The implementation of Player::play is roughly as follows:

  1. Initialize a counter that increments by 1 every tick and resets to 0 wwhenever there is a non-empty Moment.
  2. Start iterating over the provided track, incrementing the counter every iteration (tick).
  3. Whenever the iterated value is a non-empty Moment, check to see if there are any tempo change events.
  4. If the event is a tempo change, call Timer::change_tempo, if it’s a MIDI event, call Connection::play.
  5. Repeat until the iteration is complete.

Fields§

§con: C

An active midi connection.

Implementations§

Source§

impl<T: Timer, C: Connection> Player<T, C>

Source

pub fn new(timer: T, con: C) -> Self

Creates a new Player with the given Timer and Connection.

Source

pub fn set_timer(&mut self, timer: T) -> T

Changes self.timer, returning the old one.

Source

pub fn play(&mut self, sheet: &[Moment]) -> bool

Plays the given Moment slice.

§Notes

The tempo change events are handled by self.timer and playing sound by self.con.

Stops playing if Connection::play returns false. Returns true if the track is played through the end, false otherwise.

Auto Trait Implementations§

§

impl<T, C> Freeze for Player<T, C>
where C: Freeze, T: Freeze,

§

impl<T, C> RefUnwindSafe for Player<T, C>

§

impl<T, C> Send for Player<T, C>
where C: Send, T: Send,

§

impl<T, C> Sync for Player<T, C>
where C: Sync, T: Sync,

§

impl<T, C> Unpin for Player<T, C>
where C: Unpin, T: Unpin,

§

impl<T, C> UnwindSafe for Player<T, C>
where C: UnwindSafe, T: 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.