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
//! Query structures for the rytm.
//!
//! The structures in this module are used to send sysex queries to the rytm.
//!
//! The all implement the `ObjectQuery` trait and there is a blanket implementation for all types which implement `ObjectQuery` to be `SysexCompatible`.
//!
//! # Example
//!
//! ```
//! use rytm::prelude::*;
//!
//! let query = GlobalQuery::new_targeting_work_buffer();
//! let sysex = query.as_sysex().unwrap();
//!
//! ```

// All casts in this file are intended or safe within the context of this library.
//
// One can change `allow` to `warn` to review them if necessary.
#![allow(
    clippy::cast_lossless,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss
)]

/// Holds the global query structure.
mod global;
/// Holds the kit query structure.
mod kit;
/// Holds the pattern query structure.
mod pattern;
/// Holds the raw query structure.
mod raw;
/// Holds the settings query structure.
mod settings;
/// Holds the song query structure.
mod song;
/// Holds the sound query structure.
mod sound;

pub use global::GlobalQuery;
pub use kit::KitQuery;
pub use pattern::PatternQuery;
pub use raw::RawQuery;
pub use settings::SettingsQuery;
pub use song::SongQuery;
pub use sound::SoundQuery;

/// The size of the rytm sysex query in bytes.
///
/// `0xF0 0x00 0x20 0x3c 0x07 0x00 <id> 0x01 0x01 <nr> 0x00 0x00 0x00 0x05 0xF7`
const RYTM_SYSEX_QUERY_SIZE: usize = rytm_sys::AR_SYSEX_REQUEST_MSG_SZ as usize;

use super::error::{RytmError, SysexConversionError};
use crate::{
    sysex::{AnySysexType, SysexMeta},
    SysexCompatible,
};

/// A trait which is implemented by all structures which can be converted to rytm sysex query messages.
pub trait ObjectQuery: SysexCompatible {
    /// Returns the sysex type of the object.
    fn sysex_type(&self) -> AnySysexType;

    /// Returns the device id of the object.
    fn device_id(&self) -> u8;

    /// Returns the object number (index) of the object.
    fn obj_nr(&self) -> u16;

    /// Returns the sysex meta data for the object creating it.
    fn as_sysex_meta(&self) -> SysexMeta {
        SysexMeta {
            container_version: 0x0101,
            dev_id: self.device_id(),
            obj_type: ObjectQuery::sysex_type(self).into(),
            obj_nr: self.obj_nr(),
            // Calculated in libanalogrytm, they're dummy values here in this state.
            chksum: 0,
            data_size: 0,
        }
    }

    /// Returns the information if this query is targeting the work buffer.
    fn is_targeting_work_buffer(&self) -> bool {
        self.obj_nr() >= 128
    }
}

impl<T: ObjectQuery> SysexCompatible for T {
    fn sysex_type(&self) -> AnySysexType {
        ObjectQuery::sysex_type(self)
    }

    fn as_sysex(&self) -> Result<Vec<u8>, RytmError> {
        let mut buffer = vec![0; RYTM_SYSEX_QUERY_SIZE];
        let destination_buffer = buffer.as_mut_slice();
        let meta: rytm_sys::ar_sysex_meta_t = self.as_sysex_meta().into();

        // The count of return error codes from `rytm-sys` is far below 255.
        #[allow(clippy::cast_possible_truncation)]
        unsafe {
            let return_code = rytm_sys::ar_sysex_request(
                destination_buffer.as_mut_ptr(),
                std::ptr::addr_of!(meta),
            ) as u8;

            if return_code != 0 {
                return Err(SysexConversionError::from(return_code).into());
            }
        }

        Ok(buffer)
    }
}