deepmind-midi 26.5.2

Sans-IO protocol library for Behringer DeepMind synthesizers: SysEx, NRPN, programs and device state
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! The universal device inquiry, and the firmware versions it answers with.
//!
//! ```text
//! request:  F0 7E <dev|7F> 06 01 F7
//! response: F0 7E <dev> 06 02 00 20 32 20 00 01 00 <jn> 00 <ii> <nn> F7
//! ```
//!
//! This is not a `DeepMind` message and does not use the `DeepMind` framing: it
//! is the MIDI non-realtime identity request, which is the only way to find a
//! unit whose device ID is unknown and the only documented way to read firmware
//! versions. The firmware version is not the comms protocol version and the two
//! move independently; see [`crate::ids::ProtocolVersion`].

use core::fmt;

use crate::error::{Error, Result};
use crate::ids::{DeviceId, MANUFACTURER_ID, Model};
use crate::wire::{SYSEX_END, SYSEX_START};

/// Universal non-realtime `SysEx` ID.
pub const NON_REALTIME: u8 = 0x7E;

/// General information sub-ID.
pub const GENERAL_INFORMATION: u8 = 0x06;

/// Identity request sub-ID.
pub const IDENTITY_REQUEST: u8 = 0x01;

/// Identity reply sub-ID.
pub const IDENTITY_REPLY: u8 = 0x02;

/// Device family the `DeepMind` reports, little end first.
pub const FAMILY: [u8; 2] = [0x20, 0x00];

/// Family member the `DeepMind` reports, little end first.
pub const MEMBER: [u8; 2] = [0x01, 0x00];

/// Length of an identity request.
pub const REQUEST_LEN: usize = 6;

/// Length of an identity reply.
pub const REPLY_LEN: usize = 17;

/// A software version as the identity reply reports it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Version {
    /// Major version.
    pub major: u8,
    /// Minor version.
    pub minor: u8,
}

impl Version {
    /// Reads the main software revision out of its packed byte.
    ///
    /// The manual writes that byte as `jn`, with `j` the minor revision and `n`
    /// the major: firmware 1.1 is `0x11` and firmware 1.0 is `0x01`.
    #[must_use]
    pub const fn from_packed(byte: u8) -> Self {
        Self {
            major: byte & 0x0F,
            minor: byte >> 4,
        }
    }

    /// Returns whether this version is `major.minor` or later.
    ///
    /// Value tables in the parameter layer declare the firmware they describe as
    /// a version and later, so selecting one is this comparison.
    #[must_use]
    pub const fn at_least(self, major: u8, minor: u8) -> bool {
        self.major > major || (self.major == major && self.minor >= minor)
    }

    /// Returns whether this version is exactly `major.minor`.
    #[must_use]
    pub const fn is(self, major: u8, minor: u8) -> bool {
        self.major == major && self.minor == minor
    }

    /// Returns the packed byte for this version.
    #[must_use]
    pub const fn to_packed(self) -> u8 {
        ((self.minor & 0x07) << 4) | (self.major & 0x0F)
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}", self.major, self.minor)
    }
}

/// What a unit answers a device inquiry with.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct Identity {
    /// Device ID the unit answered on, which is also its global MIDI channel.
    pub device: DeviceId,
    /// Main software revision, the one `spec/firmware.toml` describes.
    pub firmware: Version,
    /// Voice software revision, which the manual reports separately.
    pub voice: Version,
}

/// Builds an identity request for `device`.
///
/// [`DeviceId::Broadcast`] asks every unit on the port, which is how a host
/// finds a synthesizer whose device ID it does not know.
///
/// # Errors
///
/// Returns [`Error::BufferTooSmall`] when `out` is shorter than
/// [`REQUEST_LEN`].
///
/// ```
/// use deepmind_midi::ids::DeviceId;
/// use deepmind_midi::sysex::inquiry;
///
/// let mut bytes = [0u8; inquiry::REQUEST_LEN];
/// inquiry::request_into(DeviceId::Broadcast, &mut bytes).expect("the buffer fits");
/// assert_eq!(bytes, [0xF0, 0x7E, 0x7F, 0x06, 0x01, 0xF7]);
/// ```
pub fn request_into(device: DeviceId, out: &mut [u8]) -> Result<usize> {
    let available = out.len();
    let out = out.get_mut(..REQUEST_LEN).ok_or(Error::BufferTooSmall {
        needed: REQUEST_LEN,
        available,
    })?;
    let bytes = [
        SYSEX_START,
        NON_REALTIME,
        device.to_byte(),
        GENERAL_INFORMATION,
        IDENTITY_REQUEST,
        SYSEX_END,
    ];
    for (slot, byte) in out.iter_mut().zip(bytes) {
        *slot = byte;
    }
    Ok(REQUEST_LEN)
}

/// Reads an identity request, returning the device it addresses.
///
/// The counterpart of [`request_into`], here for the same reason
/// [`reply_into`] is: something answering an inquiry has to notice one first. A
/// host has no reason to read one.
///
/// The request is a universal message and says nothing about a `DeepMind`, so
/// what comes back is the address it carries and nothing more.
/// [`DeviceId::Broadcast`] addresses every unit on the port.
///
/// # Errors
///
/// Returns [`Error::Unframed`] when the `F0` or the `F7` is missing, and
/// [`Error::NotAnInquiryRequest`] when the frame is some other universal
/// message. A device ID byte of `0x10` through `0x7E` addresses no unit and is
/// [`Error::InvalidDeviceId`].
///
/// ```
/// use deepmind_midi::ids::DeviceId;
/// use deepmind_midi::sysex::inquiry;
///
/// let asked = inquiry::parse_request(&[0xF0, 0x7E, 0x7F, 0x06, 0x01, 0xF7])?;
/// assert_eq!(asked, DeviceId::Broadcast);
/// # Ok::<(), deepmind_midi::Error>(())
/// ```
pub fn parse_request(bytes: &[u8]) -> Result<DeviceId> {
    let (first, rest) = bytes.split_first().ok_or(Error::Unframed)?;
    let (last, body) = rest.split_last().ok_or(Error::Unframed)?;
    if *first != SYSEX_START || *last != SYSEX_END {
        return Err(Error::Unframed);
    }
    let &[NON_REALTIME, device, GENERAL_INFORMATION, IDENTITY_REQUEST] = body else {
        return Err(Error::NotAnInquiryRequest);
    };
    DeviceId::from_byte(device)
}

/// Reads an identity reply.
///
/// # Errors
///
/// Returns [`Error::Unframed`] when the `F0` or the `F7` is missing, and
/// [`Error::NotAnInquiryResponse`] when the frame is some other universal
/// message, or an identity reply from something that is not a `DeepMind`.
pub fn parse_reply(bytes: &[u8]) -> Result<Identity> {
    let (first, rest) = bytes.split_first().ok_or(Error::Unframed)?;
    let (last, body) = rest.split_last().ok_or(Error::Unframed)?;
    if *first != SYSEX_START || *last != SYSEX_END {
        return Err(Error::Unframed);
    }
    let &[
        NON_REALTIME,
        device,
        GENERAL_INFORMATION,
        IDENTITY_REPLY,
        manufacturer_0,
        manufacturer_1,
        manufacturer_2,
        family_0,
        family_1,
        member_0,
        member_1,
        firmware,
        _reserved,
        voice_major,
        voice_minor,
    ] = body
    else {
        return Err(Error::NotAnInquiryResponse);
    };
    if [manufacturer_0, manufacturer_1, manufacturer_2] != MANUFACTURER_ID
        || [family_0, family_1] != FAMILY
        || [member_0, member_1] != MEMBER
    {
        return Err(Error::NotAnInquiryResponse);
    }
    Ok(Identity {
        device: DeviceId::from_byte(device)?,
        firmware: Version::from_packed(firmware),
        voice: Version {
            major: voice_major,
            minor: voice_minor,
        },
    })
}

/// Writes the identity reply a unit would send.
///
/// Here so a test harness or a fake synthesizer can answer an inquiry; a host
/// has no reason to send one.
///
/// # Errors
///
/// Returns [`Error::BufferTooSmall`] when `out` is shorter than [`REPLY_LEN`].
pub fn reply_into(identity: &Identity, out: &mut [u8]) -> Result<usize> {
    let available = out.len();
    let out = out.get_mut(..REPLY_LEN).ok_or(Error::BufferTooSmall {
        needed: REPLY_LEN,
        available,
    })?;
    let bytes = [
        SYSEX_START,
        NON_REALTIME,
        identity.device.to_byte(),
        GENERAL_INFORMATION,
        IDENTITY_REPLY,
        MANUFACTURER_ID[0],
        MANUFACTURER_ID[1],
        MANUFACTURER_ID[2],
        FAMILY[0],
        FAMILY[1],
        MEMBER[0],
        MEMBER[1],
        identity.firmware.to_packed(),
        0x00,
        identity.voice.major,
        identity.voice.minor,
        SYSEX_END,
    ];
    for (slot, byte) in out.iter_mut().zip(bytes) {
        *slot = byte;
    }
    Ok(REPLY_LEN)
}

/// Returns the model every `DeepMind` reports, which is every variant at once.
///
/// The identity reply carries the shared model ID and nothing that separates a
/// `DeepMind` 6 from a 12XD, so there is nothing to return but the list. A host
/// that needs to know asks the user.
#[must_use]
pub const fn models() -> &'static [Model] {
    Model::ALL
}

#[cfg(test)]
#[expect(
    clippy::expect_used,
    reason = "a failed expectation is the test failure"
)]
mod tests {
    use super::*;
    use crate::testing::assert_display;

    fn identity() -> Identity {
        Identity {
            device: DeviceId::Unit(3),
            firmware: Version { major: 1, minor: 1 },
            voice: Version { major: 1, minor: 0 },
        }
    }

    #[test]
    fn a_request_round_trips() {
        for device in [DeviceId::Unit(0), DeviceId::Unit(15), DeviceId::Broadcast] {
            let mut bytes = [0u8; REQUEST_LEN];
            let len = request_into(device, &mut bytes).expect("the buffer fits");
            assert_eq!(len, REQUEST_LEN);
            assert_eq!(parse_request(&bytes), Ok(device));
        }
    }

    #[test]
    fn a_reply_is_not_a_request() {
        let mut bytes = [0u8; REPLY_LEN];
        reply_into(&identity(), &mut bytes).expect("the buffer fits");
        assert_eq!(parse_request(&bytes), Err(Error::NotAnInquiryRequest));
    }

    /// The `DeepMind` frame is not a universal one, so it fails on its second
    /// byte rather than its length.
    #[test]
    fn a_deepmind_frame_is_not_a_request() {
        let bytes = [0xF0, 0x00, 0x20, 0x32, 0x20, 0x00, 0x03, 0xF7];
        assert_eq!(parse_request(&bytes), Err(Error::NotAnInquiryRequest));
    }

    #[test]
    fn a_request_has_to_be_framed() {
        assert_eq!(parse_request(&[]), Err(Error::Unframed));
        assert_eq!(
            parse_request(&[0xF0, 0x7E, 0x7F, 0x06, 0x01]),
            Err(Error::Unframed)
        );
    }

    #[test]
    fn a_request_addressing_no_unit_is_refused() {
        assert_eq!(
            parse_request(&[0xF0, 0x7E, 0x10, 0x06, 0x01, 0xF7]),
            Err(Error::InvalidDeviceId(0x10))
        );
    }

    #[test]
    fn a_reply_round_trips() {
        let mut bytes = [0u8; REPLY_LEN];
        let len = reply_into(&identity(), &mut bytes).expect("the buffer fits");
        assert_eq!(len, REPLY_LEN);
        assert_eq!(parse_reply(&bytes), Ok(identity()));
    }

    #[test]
    fn the_manuals_own_byte_layout_parses() {
        // F0 7E dev 06 02 00 20 32 20 00 01 00 jn 00 ii nn F7, with jn = 0x11.
        let bytes = [
            0xF0, 0x7E, 0x00, 0x06, 0x02, 0x00, 0x20, 0x32, 0x20, 0x00, 0x01, 0x00, 0x11, 0x00,
            0x01, 0x00, 0xF7,
        ];
        let identity = parse_reply(&bytes).expect("the manual's own bytes");
        assert_eq!(identity.device, DeviceId::Unit(0));
        assert_eq!(identity.firmware.major, 1);
        assert_eq!(identity.firmware.minor, 1);
    }

    #[test]
    fn firmware_versions_pack_the_minor_revision_above_the_major() {
        assert_display(Version::from_packed(0x01), "1.0");
        assert_display(Version::from_packed(0x11), "1.1");
        for minor in 0..=7 {
            for major in 0..=15 {
                let version = Version { major, minor };
                assert_eq!(Version::from_packed(version.to_packed()), version);
            }
        }
    }

    #[test]
    fn another_manufacturers_identity_reply_is_not_ours() {
        let mut bytes = [0u8; REPLY_LEN];
        reply_into(&identity(), &mut bytes).expect("the buffer fits");
        let mut foreign = bytes;
        if let Some(slot) = foreign.get_mut(5) {
            *slot = 0x41;
        }
        assert_eq!(parse_reply(&foreign), Err(Error::NotAnInquiryResponse));

        // A reply one byte short is not one either.
        assert_eq!(
            parse_reply(bytes.get(..REPLY_LEN - 1).expect("a shorter reply")),
            Err(Error::Unframed)
        );
    }

    #[test]
    fn a_request_addresses_one_unit_or_all_of_them() {
        let mut bytes = [0u8; REQUEST_LEN];
        request_into(DeviceId::Unit(9), &mut bytes).expect("the buffer fits");
        assert_eq!(bytes, [0xF0, 0x7E, 0x09, 0x06, 0x01, 0xF7]);
        let mut small = [0u8; 5];
        assert_eq!(
            request_into(DeviceId::Broadcast, &mut small),
            Err(Error::BufferTooSmall {
                needed: 6,
                available: 5
            })
        );
    }
}