audio_processor_traits/midi/mod.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
24/// `rust-vst` compatibility for the MidiMessageLike trait
25#[cfg(feature = "vst")]
26pub mod vst;
27
28/// Represents an "Event" type for audio processors. Due to how events are forwarded to processors,
29/// the list of events received might contain non-MIDI events.
30pub trait MidiMessageLike {
31 fn is_midi(&self) -> bool;
32 fn bytes(&self) -> Option<&[u8]>;
33}
34
35/// A MIDI event processor
36pub trait MidiEventHandler {
37 /// MIDI messages. May contain invalid events (of a different type) which should be skipped.
38 fn process_midi_events<Message: MidiMessageLike>(&mut self, midi_messages: &[Message]);
39}
40
41/// An instance of MidiEventHandler that doesn't do anything with its events.
42pub struct NoopMidiEventHandler {}
43
44impl Default for NoopMidiEventHandler {
45 fn default() -> Self {
46 Self::new()
47 }
48}
49
50impl NoopMidiEventHandler {
51 pub fn new() -> Self {
52 Self {}
53 }
54}
55
56impl MidiEventHandler for NoopMidiEventHandler {
57 fn process_midi_events<Message: MidiMessageLike>(&mut self, _midi_messages: &[Message]) {}
58}
59
60#[cfg(test)]
61mod test {
62 use super::*;
63
64 struct Message {}
65 impl MidiMessageLike for Message {
66 fn is_midi(&self) -> bool {
67 false
68 }
69
70 fn bytes(&self) -> Option<&[u8]> {
71 None
72 }
73 }
74
75 #[test]
76 fn test_sanity_message() {
77 let message = Message {};
78 assert_eq!(message.is_midi(), false);
79 assert_eq!(message.bytes(), None);
80 }
81
82 #[test]
83 fn test_noop_midi_event_handler() {
84 let mut handler = super::NoopMidiEventHandler::default();
85 handler.process_midi_events::<Message>(&[]);
86 }
87}