rytm_rs/query/
sound.rs

1use super::ObjectQuery;
2use crate::{
3    error::{ParameterError, RytmError},
4    sysex::{AnySysexType, SysexType},
5};
6use rytm_rs_macro::parameter_range;
7
8#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
9/// A query to retrieve a [`Sound`](crate::object::Sound) object from rytm.
10pub struct SoundQuery {
11    /// Sound index or track index if targeting work buffer
12    object_number: usize,
13    sysex_type: SysexType,
14    device_id: u8,
15}
16
17impl SoundQuery {
18    /// Creates a new sound query for a pool sound.
19    ///
20    /// Accepts a sound index in the range of `0..=127`.
21    #[parameter_range(range = "sound_index:0..=127")]
22    pub fn new(sound_index: usize) -> Result<Self, RytmError> {
23        Ok(Self {
24            object_number: sound_index,
25            sysex_type: SysexType::Sound,
26            device_id: 0,
27        })
28    }
29
30    /// Creates a new sound query for a pool sound.
31    ///
32    /// Accepts a sound index in the range of `0..=127`.
33    ///
34    /// Accepts a device id in the range of `0..=255`.
35    #[parameter_range(range = "sound_index:0..=127")]
36    pub fn new_with_device_id(sound_index: usize, device_id: u8) -> Result<Self, RytmError> {
37        Ok(Self {
38            object_number: sound_index,
39            sysex_type: SysexType::Sound,
40            device_id,
41        })
42    }
43
44    /// Creates a new sound query for a sound in the work buffer.
45    ///
46    /// Accepts a track index in the range of `0..=11`.
47    #[parameter_range(range = "track_index:0..=11")]
48    pub fn new_targeting_work_buffer(track_index: usize) -> Result<Self, RytmError> {
49        Ok(Self {
50            object_number: track_index | 0b1000_0000,
51            sysex_type: SysexType::Sound,
52            device_id: 0,
53        })
54    }
55
56    /// Creates a new sound query for a sound in the work buffer.
57    ///
58    /// Accepts a track index in the range of `0..=11`.
59    ///
60    /// Accepts a device id in the range of `0..=255`.
61    #[parameter_range(range = "track_index:0..=11")]
62    pub fn new_targeting_work_buffer_with_device_id(
63        track_index: usize,
64        device_id: u8,
65    ) -> Result<Self, RytmError> {
66        Ok(Self {
67            object_number: track_index | 0b1000_0000,
68            sysex_type: SysexType::Sound,
69            device_id,
70        })
71    }
72}
73
74impl ObjectQuery for SoundQuery {
75    fn sysex_type(&self) -> AnySysexType {
76        self.sysex_type.into()
77    }
78
79    fn device_id(&self) -> u8 {
80        self.device_id
81    }
82
83    fn obj_nr(&self) -> u16 {
84        self.object_number as u16
85    }
86}