redact_composer_midi/
lib.rs

1#![deny(missing_docs, missing_debug_implementations)]
2//! MIDI-related types, [`Element`]s and [`Composition`] output converter.
3
4#[allow(unused_imports)] // Imports used in doc comments only
5use redact_composer_core::{elements::Part, Composition, Element};
6
7/// Midi converter for [`Composition`] output.
8pub mod convert;
9
10/// General Midi Level 1 types and elements.
11pub mod gm;
12
13use redact_composer_core::derive::Element;
14use redact_composer_core::render::{AdhocRenderer, RenderEngine, Renderer};
15use redact_composer_core::IntoSegment;
16
17#[cfg(feature = "serde")]
18use serde::{Deserialize, Serialize};
19
20/// Elements implementing [`Element`].
21pub mod elements {
22    pub use super::{DrumKit, Program};
23}
24
25/// The renderers for [`Element`]s of this module.
26pub fn renderers() -> RenderEngine {
27    RenderEngine::new() + DrumKit::renderer() + gm::renderers()
28}
29
30/// A program number (instrument) that should play during a [`Part`].
31#[derive(Element, Debug)]
32#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
33pub struct Program(pub u8);
34
35/// A semantic wrapper for program number indicating a drum instrument.
36#[derive(Element, Debug, Copy, Clone)]
37#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
38pub struct DrumKit(pub u8);
39
40impl DrumKit {
41    /// Default [`DrumKit`] renderer. Simply converts it into a [`Program`].
42    pub fn renderer() -> impl Renderer<Element = Self> {
43        AdhocRenderer::<Self>::new(|segment, _| {
44            Ok(vec![Program::from(segment.element).over(segment.timing)])
45        })
46    }
47}
48
49impl From<DrumKit> for Program {
50    fn from(value: DrumKit) -> Self {
51        Program(value.0)
52    }
53}
54
55impl From<&DrumKit> for Program {
56    fn from(value: &DrumKit) -> Self {
57        Program(value.0)
58    }
59}
60
61impl From<u8> for DrumKit {
62    fn from(value: u8) -> Self {
63        DrumKit(value)
64    }
65}
66
67impl From<&u8> for DrumKit {
68    fn from(value: &u8) -> Self {
69        DrumKit(*value)
70    }
71}