rytm_rs/query/
song.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 song object from rytm.
10///
11/// Currently the song object is not supported by rytm-rs.
12pub struct SongQuery {
13    /// Song index or track index if targeting work buffer
14    object_number: usize,
15    sysex_type: SysexType,
16    device_id: u8,
17}
18
19impl SongQuery {
20    /// Creates a new song query.
21    ///
22    /// Accepts a song index in the range of `0..=15`.
23    #[parameter_range(range = "song_index:0..=15")]
24    pub fn new(song_index: usize) -> Result<Self, RytmError> {
25        Ok(Self {
26            object_number: song_index,
27            sysex_type: SysexType::Song,
28            device_id: 0,
29        })
30    }
31
32    /// Creates a new song query.
33    ///
34    /// Accepts a song index in the range of `0..=15`.
35    ///
36    /// Accepts a device id in the range of `0..=255`.
37    #[parameter_range(range = "song_index:0..=15")]
38    pub fn new_with_device_id(song_index: usize, device_id: u8) -> Result<Self, RytmError> {
39        Ok(Self {
40            object_number: song_index,
41            sysex_type: SysexType::Song,
42            device_id,
43        })
44    }
45
46    /// Creates a new song query for the song in the work buffer.
47    pub const fn new_targeting_work_buffer() -> Self {
48        Self {
49            object_number: 0b1000_0000,
50            sysex_type: SysexType::Song,
51            device_id: 0,
52        }
53    }
54
55    /// Creates a new song query for the song in the work buffer.
56    ///
57    /// Accepts a device id in the range of `0..=255`.
58    pub const fn new_targeting_work_buffer_with_device_id(device_id: u8) -> Self {
59        Self {
60            object_number: 0b1000_0000,
61            sysex_type: SysexType::Song,
62            device_id,
63        }
64    }
65}
66
67impl ObjectQuery for SongQuery {
68    fn sysex_type(&self) -> AnySysexType {
69        self.sysex_type.into()
70    }
71
72    fn device_id(&self) -> u8 {
73        self.device_id
74    }
75
76    fn obj_nr(&self) -> u16 {
77        self.object_number as u16
78    }
79}