Crate miami

Crate miami 

Source
Expand description

§miami

A minimal dependency MIDI file parser designed for both standard and WASM targets, This crate provides core MIDI “chunks” and utilities for reading and parsing them, without introducing any extra overhead or dependencies.

§Overview

MIDI files are structured as a series of chunks. Each chunk contains a 4-character ASCII type identifier and a 32-bit length that specifies how many bytes of data follow. The Chunk struct and related APIs in this crate make it straightforward to inspect and parse these sections of a MIDI file.

  • Minimal dependencies: Keeps your application lightweight and minimizes build complexity. Opt in to serde support.
  • Streaming-friendly: Exposes traits and functions that can parse MIDI data from any implementor of reader::MidiStream, making it easier to handle data on the fly.

§Example Usage

use miami::{reader::MidiReadable, Midi, RawMidi};

// Load MIDI bytes (replace with your own source as needed).
let mut data = "test/test.mid"
    .get_midi_bytes()
    .expect("Get `test.mid` file and read bytes");

let midi: Midi = RawMidi::try_from_midi_stream(data)
    .expect("Parse data as a MIDI stream")
    .check_into_midi()
    .expect("Sanitize MIDI into formatted MIDI");

println!("Header: {:?}", midi.header);
for chunk in midi.tracks.iter() {
    println!("Track: {:?}", chunk);
}

The above example illustrates how to read chunks from a MIDI stream and use ParsedChunk::try_from to parse them into known types (header or track chunks).

§Library Structure

  • chunk: Contains the Chunk struct and associated utilities for identifying chunk types and lengths.
  • reader: Provides traits and types for streaming MIDI data. The MidiStream trait and related helpers allow on-the-fly parsing from any data source.
  • chunk_types, header, and track: Provide definitions for recognized MIDI chunk types (e.g., MThd for the header and MTrk for track data) and the logic for parsing their contents.

§Extensibility

While this crate focuses on parsing the structural aspects of MIDI files (chunks and headers), you can use the raw track data to implement custom handling of MIDI events or other logic as needed. Because miami exposes chunks in a straightforward format, you remain in full control of the MIDI event parsing layer.

Modules§

chunk
Chunk Definitions for parsed types and type headers
reader
MIDI file reader trait, allows for in memory byte spans to be read or files
writer
The MidiWriteable trait is central to translating data from miami’s internal representations back into raw MIDI bytes. In other words, if MidiReadable is about parsing MIDI bytes into Rust types, MidiWriteable does the opposite—taking those parsed types and converting them into the canonical MIDI byte format. This is particularly useful when you have manipulated or inspected MIDI data in your application and need to write it back to a file or stream.

Structs§

Chunk
Represents a raw MIDI Chunk. A MIDI Chunk consists of a 4-character ASCII type identifier and a 32-bit unsigned integer specifying the length of its data.
Midi
A MIDI File “cleaned” by enforcing a single header chunk and an arbitrary amount of Track chunks
RawMidi
An entire MIDI file as a raw sequence of parsed chunks
StreamWrapper
A wrapper to allow TryFrom implementations for MidiStream implementors

Enums§

MidiSanitizerError
An error that may occur when verifying that a Raw Midi struct is sanitized into a clean MIDI format