| Crates.io | miami |
| lib.rs | miami |
| version | 0.1.3 |
| created_at | 2025-01-23 21:32:48.014924+00 |
| updated_at | 2025-01-26 18:07:35.833896+00 |
| description | Minimal dependency MIDI file format parser and writer |
| homepage | |
| repository | https://github.com/BradenEverson/miami |
| max_upload_size | |
| id | 1528395 |
| size | 381,540 |
parsing midi files is not as scary as it sounds. 🐔
A lightweight, minimal dependency MIDI file parser and binary writer designed for resource-constrained environments, making it great for WebAssembly applications.
Add the following to your Cargo.toml:
[dependencies]
miami = "{whatever version you want}"
For serde support include the serde feature flag ;)
The following example demonstrates how to read and process MIDI chunks from a file:
let mut data = "path/to/midi/file.mid"
.get_midi_bytes()
.expect("Failed to load MIDI file");
let midi = RawMidi::try_from_midi_stream(data).expect("Parse data as a MIDI stream");
for chunk in midi.chunks.iter() {
println!("{chunk:?}");
}
A RawMidi can also be sanitized and upgraded into a Midi struct that contains a single header and a subsequent list of tracks:
let sanitized: Midi = midi.check_into_midi().expect("Upgrade to Midi");
Writing a RawMidi or Midi to a file:
let mut output = File::create("output.mid").unwrap();
// Works for `Midi` and `RawMidi` types!
output.write_all(&midi.to_midi_bytes()).unwrap()
A raw MIDI chunk consists of a 4-character ASCII type identifier and a 32-bit unsigned integer specifying the length of its data:
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Chunk {
pub chunk_type: [char; 4],
pub length: u32,
}
Parsed chunks are categorized into meaningful types such as HeaderChunk and TrackChunk:
#[derive(Debug, Clone, PartialEq)]
pub enum ParsedChunk {
Header(HeaderChunk),
Track(TrackChunk),
}
The HeaderChunk struct stores essential MIDI metadata:
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HeaderChunk {
format: Format,
ntrks: u16,
division: Division,
}
Contributions are welcome! If you find a bug or have a feature request, feel free to open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.