augmented_midi/lib.rs
1// Augmented Audio: Audio libraries and applications
2// Copyright (c) 2022 Pedro Tacla Yamada
3//
4// The MIT License (MIT)
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22// THE SOFTWARE.
23//! Implements a MIDI (file) parser/serializer using `nom` and `cookie-factory` combinators.
24//!
25//! Thanks to the combinators this library requires no allocation for
26//! serialization/de-serialization into/from the MIDI types provided.
27//!
28//! _(This is part of [augmented-audio](https://github.com/yamadapc/augmented-audio/))_
29//!
30//! # Specification
31//! Based on MIDI 1.0 specification. [MIDI 1.0](https://www.midi.org/specifications/midi1-specifications/m1-v4-2-1-midi-1-0-detailed-specification-96-1-4.)
32//!
33//! # License notes
34//! There's a Bach MIDI file used from `piano-midi.de` linked here. This file is licensed as described
35//! in <http://www.piano-midi.de/copy.htm>. Name: Bernd Krueger
36//! The distribution or public playback of the files is only allowed under identical license conditions.
37//! The scores are open source.
38//!
39//! # Parsing a single message
40//! We can parse a single MIDI message as follows.
41//!
42//! ```rust
43//! use augmented_midi::{MIDIMessage, MIDIMessageNote, MIDIParseResult, parse_midi_event, ParserState};
44//!
45//! // Initialize parser state. This is here to support rolling status on MIDI files
46//! let mut state = ParserState::default();
47//!
48//! // We'll parse this &[u8] buffer. This could be a vec
49//! let input_buffer = [0x9_8, 0x3C, 0x44];
50//!
51//! // We parse a message borrowing from the input buffer. We could use `MIDIMessage<Vec<u8>>` to
52//! // allocate owned messages.
53//! //
54//! // This is only relevant for variable size messages like SysEx.
55//! //
56//! // Parsing is otherwise only using the stack.
57//! let parse_result: MIDIParseResult<MIDIMessage<&[u8]>> =
58//! parse_midi_event(&input_buffer, &mut state);
59//! let (_remaining_input, midi_message) = parse_result.unwrap();
60//!
61//! assert_eq!(midi_message, MIDIMessage::NoteOn(MIDIMessageNote { channel: 8, note: 60, velocity: 68 }));
62//! ```
63//!
64//! # Serializing messages
65//!
66//! ```
67//! use augmented_midi::{serialize_message, MIDIMessage};
68//!
69//! let mut writer = [0_u8;3]; // This could be a vec.
70//! let message: MIDIMessage<Vec<u8>> = MIDIMessage::control_change(0, 55, 127); // CC#55 127 - channel 0
71//! let _ = serialize_message(message, &mut writer[..]).unwrap();
72//!
73//! assert_eq!(writer, [0xB0, 0x37, 0x7F]);
74//! ```
75pub use cookie_factory;
76pub use nom;
77pub use parser::*;
78pub use serializer::serialize_message;
79pub use types::*;
80
81mod parser;
82mod serializer;
83mod types;