Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder<const N: usize = MAX_SYSEX_LEN> { /* private fields */ }
Expand description

Reassembles MIDI messages from bytes arriving in any chunking.

The decoder handles the three things that make a MIDI stream awkward:

  • Running status. A status byte stays in force until another one arrives, so a stream of note-ons may carry one status byte and nothing but data afterwards. System common messages clear it; real-time messages do not.
  • Real-time interleaving. F8 through FF may appear between any two bytes, including between the F0 and F7 of a SysEx frame. They are reported where they arrive and disturb nothing.
  • Chunking. A USB packet or a serial read boundary can fall anywhere. Feeding one byte at a time and feeding a whole dump at once produce the same events.

N is the SysEx buffer size and defaults to MAX_SYSEX_LEN. A frame longer than the buffer is dropped, with Error::SysExTooLong reported once.

use deepmind_midi::wire::{Decoder, Event};

// A firmware-sized buffer is wasted on a host that only sends NRPN edits.
let mut decoder: Decoder<64> = Decoder::new();
let mut frames = 0;
decoder.feed(&[0xF0, 0x00, 0x20, 0x32, 0x20, 0x00, 0x03, 0xF7], |event| {
    if matches!(event, Ok(Event::SysEx(_))) {
        frames += 1;
    }
});
assert_eq!(frames, 1);

Implementations§

Source§

impl<const N: usize> Decoder<N>

Source

pub const fn new() -> Self

Builds a decoder with an empty buffer.

Source

pub const fn capacity(&self) -> usize

Returns the SysEx buffer size in bytes.

Source

pub const fn in_sysex(&self) -> bool

Returns true while a SysEx frame is still being reassembled.

Source

pub fn reset(&mut self)

Forgets any partial message and the running status.

Worth calling when a port is reopened, since the first bytes after a reconnect may be the middle of something.

Source

pub fn feed<F>(&mut self, bytes: &[u8], on_event: F)
where F: FnMut(Result<Event<'_>>),

Feeds bytes in, calling on_event once per message or rejection.

Chunking is irrelevant: a message split across two calls is reported when its last byte arrives.

Trait Implementations§

Source§

impl<const N: usize> Clone for Decoder<N>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Debug for Decoder<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Default for Decoder<N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<const N: usize> Forget for Decoder<N>

§

impl<const N: usize> Freeze for Decoder<N>
where [u8; N]: Freeze,

§

impl<const N: usize> Move for Decoder<N>

§

impl<const N: usize> RefUnwindSafe for Decoder<N>
where [u8; N]: RefUnwindSafe,

§

impl<const N: usize> Send for Decoder<N>
where [u8; N]: Send,

§

impl<const N: usize> Sync for Decoder<N>
where [u8; N]: Sync,

§

impl<const N: usize> Unpin for Decoder<N>
where [u8; N]: Unpin,

§

impl<const N: usize> UnsafeUnpin for Decoder<N>
where [u8; N]: UnsafeUnpin,

§

impl<const N: usize> UnwindSafe for Decoder<N>
where [u8; N]: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.