Skip to main content

oscyl_midi/
logue.rs

1//! The logue family's own operations (user units, global and tuning
2//! data), which the driver contract does not model, for every logue
3//! driver: the minilogue xd and the prologue. A [`Device`](crate::Device)
4//! runs them between its driver session's ops, pumped like them.
5
6use std::time::Instant;
7
8use oscyl_driver::Driver;
9use oscyl_driver::engine::{Engine, OpError};
10use oscyl_logue::Model;
11use oscyl_logue::session::{Event, Op, Proto};
12
13pub use oscyl_logue::msg::Module;
14pub use oscyl_logue::unit::{Platform, unit_name};
15
16/// A logue operation on user slots and settings.
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum UnitOp {
19    /// The status of every user slot.
20    ReadSlots,
21    ReadSlotData {
22        module: Module,
23        slot: u8,
24    },
25    /// Load TABLE 7 slot data (`u32 size, u32 checksum, blob`).
26    UploadUnit {
27        module: Module,
28        slot: u8,
29        data: Vec<u8>,
30    },
31    ClearSlot {
32        module: Module,
33        slot: u8,
34    },
35    ReadGlobal,
36    /// The 6 user scales and 6 user octaves.
37    ReadTuning,
38}
39
40/// What [`UnitOp`]s report, in order; each op ends with `Done` or
41/// `Failed`.
42#[derive(Debug, Clone, PartialEq, Eq)]
43pub enum UnitEvent {
44    Slot {
45        module: Module,
46        slot: u8,
47        header: Option<Vec<u8>>,
48    },
49    SlotData {
50        module: Module,
51        slot: u8,
52        data: Vec<u8>,
53    },
54    Global(Vec<u8>),
55    Scale(u8, Vec<u8>),
56    Octave(u8, Vec<u8>),
57    Done,
58    Failed(OpError),
59}
60
61/// A sans-IO session speaking [`UnitOp`].
62pub trait UnitSession: Send {
63    fn submit(&mut self, op: UnitOp);
64    fn is_idle(&self) -> bool;
65    fn poll_transmit(&mut self, now: Instant) -> Option<Vec<u8>>;
66    fn next_deadline(&self) -> Option<Instant>;
67    fn handle_timeout(&mut self, now: Instant);
68    fn handle_input(&mut self, bytes: &[u8]);
69    fn poll_event(&mut self) -> Option<UnitEvent>;
70}
71
72struct Logue<M: Model>(Engine<Proto<M>>);
73
74impl<M: Model> UnitSession for Logue<M> {
75    fn submit(&mut self, op: UnitOp) {
76        self.0.submit(match op {
77            UnitOp::ReadSlots => Op::ReadSlots,
78            UnitOp::ReadSlotData { module, slot } => Op::ReadSlotData { module, slot },
79            UnitOp::UploadUnit { module, slot, data } => Op::UploadUnit { module, slot, data },
80            UnitOp::ClearSlot { module, slot } => Op::ClearSlot { module, slot },
81            UnitOp::ReadGlobal => Op::ReadGlobal,
82            UnitOp::ReadTuning => Op::ReadTuning,
83        })
84    }
85
86    fn is_idle(&self) -> bool {
87        self.0.is_idle()
88    }
89
90    fn poll_transmit(&mut self, now: Instant) -> Option<Vec<u8>> {
91        self.0.poll_transmit(now)
92    }
93
94    fn next_deadline(&self) -> Option<Instant> {
95        self.0.next_deadline()
96    }
97
98    fn handle_timeout(&mut self, now: Instant) {
99        self.0.handle_timeout(now)
100    }
101
102    fn handle_input(&mut self, bytes: &[u8]) {
103        self.0.handle_input(bytes)
104    }
105
106    fn poll_event(&mut self) -> Option<UnitEvent> {
107        loop {
108            return Some(match self.0.poll_event()? {
109                Event::Slot {
110                    module,
111                    slot,
112                    header,
113                } => UnitEvent::Slot {
114                    module,
115                    slot,
116                    header,
117                },
118                Event::SlotData { module, slot, data } => {
119                    UnitEvent::SlotData { module, slot, data }
120                }
121                Event::Global(g) => UnitEvent::Global(g),
122                Event::Scale(n, v) => UnitEvent::Scale(n, v),
123                Event::Octave(n, v) => UnitEvent::Octave(n, v),
124                Event::OpDone => UnitEvent::Done,
125                Event::OpFailed(e) => UnitEvent::Failed(e),
126                // Program events come only from program ops, never
127                // submitted here.
128                _ => continue,
129            });
130        }
131    }
132}
133
134type MakeSession = fn(u8) -> Box<dyn UnitSession>;
135
136fn session<M: Model>(channel: u8) -> Box<dyn UnitSession> {
137    Box::new(Logue::<M>(Engine::new(Proto::new(channel))))
138}
139
140/// `d`'s logue model: its unit session maker, SDK platform and unit file
141/// extension. `None` for a synth outside the logue family.
142fn model(d: &dyn Driver) -> Option<(MakeSession, Platform, &'static str)> {
143    use oscyl_korg_minilogue_xd::model::XdModel;
144    use oscyl_korg_prologue::model::PrologueModel;
145    let id = d.info().id;
146    if id == oscyl_korg_minilogue_xd::DRIVER.info().id {
147        Some((session::<XdModel>, XdModel::PLATFORM, "mnlgxdunit"))
148    } else if id == oscyl_korg_prologue::DRIVER.info().id {
149        Some((
150            session::<PrologueModel>,
151            PrologueModel::PLATFORM,
152            "prlgunit",
153        ))
154    } else {
155        None
156    }
157}
158
159/// A session for `d`'s logue operations on `channel`, if it is a logue
160/// synth.
161pub fn unit_session(d: &dyn Driver, channel: u8) -> Option<Box<dyn UnitSession>> {
162    model(d).map(|(make, ..)| make(channel))
163}
164
165/// The logue SDK platform `d`'s units are built for, if it is a logue
166/// synth.
167pub fn platform(d: &dyn Driver) -> Option<Platform> {
168    model(d).map(|(_, p, _)| p)
169}
170
171/// The extension of `d`'s logue SDK unit files (`.mnlgxdunit`,
172/// `.prlgunit`), if it is a logue synth.
173pub fn unit_ext(d: &dyn Driver) -> Option<&'static str> {
174    model(d).map(|(.., e)| e)
175}