Expand description
Overview
midly
is a full-featured MIDI parser and writer, focused on performance.
Parsing a .mid
file can be as simple as:
use midly::Smf;
let smf = Smf::parse(include_bytes!("../test-asset/Clementi.mid")).unwrap();
for (i, track) in smf.tracks.iter().enumerate() {
println!("track {} has {} events", i, track.len());
}
Parsing Standard Midi Files (.mid
files)
Parsing Standard Midi Files is usually done through the Smf
struct (or if
working in a no_std
environment without an allocator, through the parse
function).
Note that most types in this crate have a lifetime parameter, because they reference the bytes in the original file (in order to avoid allocations). For this reason, reading a file and parsing it must be done in two separate steps:
use std::fs;
use midly::Smf;
// Load bytes into a buffer
let bytes = fs::read("test-asset/Clementi.mid").unwrap();
// Parse bytes in a separate step
let smf = Smf::parse(&bytes).unwrap();
Writing Standard Midi Files
Saving .mid
files is as simple as using the Smf::save
method:
// Parse file
let bytes = fs::read("test-asset/Clementi.mid").unwrap();
let smf = Smf::parse(&bytes).unwrap();
// Rewrite file
smf.save("test-asset/ClementiRewritten.mid").unwrap();
SMF files can also be written to an arbitrary writer:
let mut in_memory = Vec::new();
smf.write(&mut in_memory).unwrap();
println!("midi file fits in {} bytes!", in_memory.len());
Parsing standalone MIDI messages
When using an OS API such as midir
,
LiveEvent
can be used to parse the raw MIDI bytes:
use midly::{live::LiveEvent, MidiMessage};
fn on_midi(event: &[u8]) {
let event = LiveEvent::parse(event).unwrap();
match event {
LiveEvent::Midi { channel, message } => match message {
MidiMessage::NoteOn { key, vel } => {
println!("hit note {} on channel {}", key, channel);
}
_ => {}
},
_ => {}
}
}
Writing standalone MIDI messages
Raw MIDI message bytes can be produced for consumption by OS APIs, such as
midir
, through the
LiveEvent::write
method:
use midly::{live::LiveEvent, MidiMessage};
fn note_on(channel: u8, key: u8) {
let ev = LiveEvent::Midi {
channel: channel.into(),
message: MidiMessage::NoteOn {
key: key.into(),
vel: 127.into(),
},
};
let mut buf = Vec::new();
ev.write(&mut buf).unwrap();
write_midi(&buf[..]);
}
About features
The following cargo features are available to enable or disable parts of the crate:
-
parallel
(enabled by default)This feature enables the use of multiple threads when parsing large midi files.
Disabling this feature will remove the dependency on
rayon
. -
std
(enabled by default)This feature enables integration with
std
, for example implementingstd::error::Error
formidly::Error
, support for writing tostd::io::Write
streams, among others.Disabling this feature will make the crate
no_std
. -
alloc
(enabled by default)This feature enables allocations both for ergonomics and performance.
Disabling both the
std
and thealloc
feature will make the crate fullyno_std
, but will reduce functionality to a minimum. For example, theSmf
type is unavailable without thealloc
feature. All types that are unavailable when a feature is disabled are marked as such in their documentation. -
strict
By default
midly
will attempt to plow through non-standard and even obviously corrupted files, throwing away any unreadable data, or even entire tracks in the worst scenario. By enabling thestrict
feature the parser will reject uncompliant data and do additional checking, throwing errors of the kindErrorKind::Malformed
when such a situation arises.
Modules
no_std
environments..mid
file.Macros
MidiStream
.Structs
EventIter::bytemapped
method..mid
Standard Midi File.
If you’re casually looking to parse a .mid
file, this is the type you’re looking for..mid
Standard Midi File, but keeps a mapping to the raw bytes that make up each event.Enums
Functions
Type Definitions
Vec
of events along with their originating bytes.