Expand description
Implements a MIDI (file) parser/serializer using nom
and cookie-factory
combinators.
Thanks to the combinators this library requires no allocation for serialization/de-serialization into/from the MIDI types provided.
(This is part of augmented-audio)
§Specification
Based on MIDI 1.0 specification. MIDI 1.0
§License notes
There’s a Bach MIDI file used from piano-midi.de
linked here. This file is licensed as described
in http://www.piano-midi.de/copy.htm. Name: Bernd Krueger
The distribution or public playback of the files is only allowed under identical license conditions.
The scores are open source.
§Parsing a single message
We can parse a single MIDI message as follows.
use augmented_midi::{MIDIMessage, MIDIMessageNote, MIDIParseResult, parse_midi_event, ParserState};
// Initialize parser state. This is here to support rolling status on MIDI files
let mut state = ParserState::default();
// We'll parse this &[u8] buffer. This could be a vec
let input_buffer = [0x9_8, 0x3C, 0x44];
// We parse a message borrowing from the input buffer. We could use `MIDIMessage<Vec<u8>>` to
// allocate owned messages.
//
// This is only relevant for variable size messages like SysEx.
//
// Parsing is otherwise only using the stack.
let parse_result: MIDIParseResult<MIDIMessage<&[u8]>> =
parse_midi_event(&input_buffer, &mut state);
let (_remaining_input, midi_message) = parse_result.unwrap();
assert_eq!(midi_message, MIDIMessage::NoteOn(MIDIMessageNote { channel: 8, note: 60, velocity: 68 }));
§Serializing messages
use augmented_midi::{serialize_message, MIDIMessage};
let mut writer = [0_u8;3]; // This could be a vec.
let message: MIDIMessage<Vec<u8>> = MIDIMessage::control_change(0, 55, 127); // CC#55 127 - channel 0
let _ = serialize_message(message, &mut writer[..]).unwrap();
assert_eq!(writer, [0xB0, 0x37, 0x7F]);
Re-exports§
pub use nom;
Structs§
- MIDI
File - MIDI
File Header - The header chunk’s contents
- MIDI
Message Note - Type of node-on or note-off contents
- MIDI
Meta Event - MIDI
SysEx Event - MIDI
Track Event - Parser
State
Enums§
- MIDI
File Chunk - MIDI
File Division - MIDI
File Format - Describes how the file is organized
- MIDI
Message - Represents a MIDI message
- MIDI
Track Inner
Constants§
- ACTIVE_
SENSING_ MASK - CHANNEL_
PRESSURE_ MASK - CONTINUE_
MASK - CONTROL_
CHANGE_ MASK - NOTE_
OFF_ MASK - NOTE_
ON_ MASK - PITCH_
WHEEL_ CHANGE_ MASK - POLYPHONIC_
KEY_ PRESSURE_ MASK - PROGRAM_
CHANGE_ MASK - RESET_
MASK - SONG_
POSITION_ POINTER_ MASK - SONG_
SELECT_ MASK - START_
MASK - STATUS_
BYTE_ MASK - These are bit-masks bit-mask to match the status byte
- STOP_
MASK - SYSEX_
MESSAGE_ END_ MASK - SYSEX_
MESSAGE_ MASK - TIMING_
CLOCK_ MASK - TUNE_
REQUEST_ MASK
Functions§
- parse_
chunk - parse_
header_ body - Parses 3 16bit words. In order:
- parse_
meta_ event - parse_
midi - parse_
midi_ event - parse_
midi_ file - parse_
track_ event - parse_
variable_ length_ num - serialize_
message