rytm-rs 0.1.3

More than safe rust abstractions over rytm-sys, an unofficial SDK for Analog Rytm MKII running firmware 1.70
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// 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,
    clippy::cast_possible_wrap
)]

use super::types::{MachineType, SoundModTarget, SoundSettingsChromaticMode};
use crate::error::{ConversionError, ParameterError, RytmError};
use rytm_rs_macro::parameter_range;
use rytm_sys::ar_sound_t;
use serde::{Deserialize, Serialize};

/// A sound's settings.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]

pub struct SoundSettings {
    pub(crate) machine_type: MachineType,
    chromatic_mode: SoundSettingsChromaticMode,
    env_reset_filter: bool,
    velocity_to_volume: bool,
    legacy_fx_send: bool,

    velocity_modulation_amt_1: i8,
    velocity_modulation_target_1: SoundModTarget,
    velocity_modulation_amt_2: i8,
    velocity_modulation_target_2: SoundModTarget,
    velocity_modulation_amt_3: i8,
    velocity_modulation_target_3: SoundModTarget,
    velocity_modulation_amt_4: i8,
    velocity_modulation_target_4: SoundModTarget,

    after_touch_modulation_amt_1: i8,
    after_touch_modulation_target_1: SoundModTarget,
    after_touch_modulation_amt_2: i8,
    after_touch_modulation_target_2: SoundModTarget,
    after_touch_modulation_amt_3: i8,
    after_touch_modulation_target_3: SoundModTarget,
    after_touch_modulation_amt_4: i8,
    after_touch_modulation_target_4: SoundModTarget,
}

impl TryFrom<&ar_sound_t> for SoundSettings {
    type Error = ConversionError;
    fn try_from(raw_sound: &ar_sound_t) -> Result<Self, Self::Error> {
        // bit 0  : ?
        // bit 1  : env reset filter switch
        // bit 2  : legacy fx send switch
        // bit 3  : ?
        // bit 4+5: chromatic mode  0=OFF, 1=SYNTH, 2=SAMPLE, 3=SYN+SMP
        // bit 6  : velocity to vol switch
        // bit 7  : ?
        let raw_mode_flags = raw_sound.mode_flags;
        let chromatic_mode_number = (raw_mode_flags & 0b0011_0000) >> 4;

        Ok(Self {
            machine_type: raw_sound.machine_type.try_into()?,
            chromatic_mode: chromatic_mode_number.try_into()?,
            env_reset_filter: raw_mode_flags & 0b0000_0010 != 0,
            velocity_to_volume: raw_mode_flags & 0b0100_0000 != 0,
            legacy_fx_send: raw_mode_flags & 0b0000_0100 != 0,

            velocity_modulation_amt_1: raw_sound.vel_amt_1 as i8,
            velocity_modulation_target_1: raw_sound.vel_target_1.try_into()?,
            velocity_modulation_amt_2: raw_sound.vel_amt_2 as i8,
            velocity_modulation_target_2: raw_sound.vel_target_2.try_into()?,
            velocity_modulation_amt_3: raw_sound.vel_amt_3 as i8,
            velocity_modulation_target_3: raw_sound.vel_target_3.try_into()?,
            velocity_modulation_amt_4: raw_sound.vel_amt_4 as i8,
            velocity_modulation_target_4: raw_sound.vel_target_4.try_into()?,

            after_touch_modulation_amt_1: raw_sound.at_amt_1 as i8,
            after_touch_modulation_target_1: raw_sound.at_target_1.try_into()?,
            after_touch_modulation_amt_2: raw_sound.at_amt_2 as i8,
            after_touch_modulation_target_2: raw_sound.at_target_2.try_into()?,
            after_touch_modulation_amt_3: raw_sound.at_amt_3 as i8,
            after_touch_modulation_target_3: raw_sound.at_target_3.try_into()?,
            after_touch_modulation_amt_4: raw_sound.at_amt_4 as i8,
            after_touch_modulation_target_4: raw_sound.at_target_4.try_into()?,
        })
    }
}

impl SoundSettings {
    pub(crate) fn apply_to_raw_sound(&self, raw_sound: &mut ar_sound_t) {
        raw_sound.machine_type = self.machine_type.into();

        let chromatic_mode_number: u8 = self.chromatic_mode.into();
        raw_sound.mode_flags = chromatic_mode_number << 4;
        raw_sound.mode_flags |= (self.env_reset_filter as u8) << 1;
        raw_sound.mode_flags |= (self.legacy_fx_send as u8) << 2;
        raw_sound.mode_flags |= (self.velocity_to_volume as u8) << 6;

        raw_sound.vel_amt_1 = self.velocity_modulation_amt_1 as u8;
        raw_sound.vel_target_1 = self.velocity_modulation_target_1.into();
        raw_sound.vel_amt_2 = self.velocity_modulation_amt_2 as u8;
        raw_sound.vel_target_2 = self.velocity_modulation_target_2.into();
        raw_sound.vel_amt_3 = self.velocity_modulation_amt_3 as u8;
        raw_sound.vel_target_3 = self.velocity_modulation_target_3.into();
        raw_sound.vel_amt_4 = self.velocity_modulation_amt_4 as u8;
        raw_sound.vel_target_4 = self.velocity_modulation_target_4.into();

        raw_sound.at_amt_1 = self.after_touch_modulation_amt_1 as u8;
        raw_sound.at_target_1 = self.after_touch_modulation_target_1.into();
        raw_sound.at_amt_2 = self.after_touch_modulation_amt_2 as u8;
        raw_sound.at_target_2 = self.after_touch_modulation_target_2.into();
        raw_sound.at_amt_3 = self.after_touch_modulation_amt_3 as u8;
        raw_sound.at_target_3 = self.after_touch_modulation_target_3.into();
        raw_sound.at_amt_4 = self.after_touch_modulation_amt_4 as u8;
        raw_sound.at_target_4 = self.after_touch_modulation_target_4.into();
    }

    /// Sets the chromatic mode of the sound.
    pub fn set_chromatic_mode(&mut self, chromatic_mode: SoundSettingsChromaticMode) {
        self.chromatic_mode = chromatic_mode;
    }

    /// Sets the env reset filter switch of the sound.
    pub fn set_env_reset_filter(&mut self, env_reset_filter: bool) {
        self.env_reset_filter = env_reset_filter;
    }

    /// Sets the velocity to volume switch of the sound.
    pub fn set_velocity_to_volume(&mut self, velocity_to_volume: bool) {
        self.velocity_to_volume = velocity_to_volume;
    }

    /// Sets the legacy fx send switch of the sound.
    pub fn set_legacy_fx_send(&mut self, legacy_fx_send: bool) {
        self.legacy_fx_send = legacy_fx_send;
    }

    /// Sets the velocity modulation amount 1 of the sound.
    ///
    /// Range: `-127..=128`
    #[parameter_range(range = "velocity_modulation_amt_1:-127..=128")]
    pub fn set_velocity_modulation_amt_1(
        &mut self,
        velocity_modulation_amt_1: isize,
    ) -> Result<(), RytmError> {
        self.velocity_modulation_amt_1 = velocity_modulation_amt_1 as i8;
        Ok(())
    }

    /// Sets the velocity modulation target 1 of the sound.
    pub fn set_velocity_modulation_target_1(
        &mut self,
        velocity_modulation_target_1: SoundModTarget,
    ) {
        self.velocity_modulation_target_1 = velocity_modulation_target_1;
    }

    /// Sets the velocity modulation amount 2 of the sound.
    ///
    /// Range: `-127..=128`
    #[parameter_range(range = "velocity_modulation_amt_2:-127..=128")]
    pub fn set_velocity_modulation_amt_2(
        &mut self,
        velocity_modulation_amt_2: isize,
    ) -> Result<(), RytmError> {
        self.velocity_modulation_amt_2 = velocity_modulation_amt_2 as i8;
        Ok(())
    }

    /// Sets the velocity modulation target 2 of the sound.
    pub fn set_velocity_modulation_target_2(
        &mut self,
        velocity_modulation_target_2: SoundModTarget,
    ) {
        self.velocity_modulation_target_2 = velocity_modulation_target_2;
    }

    /// Sets the velocity modulation amount 3 of the sound.
    ///
    /// Range: `-127..=128`
    #[parameter_range(range = "velocity_modulation_amt_3:-127..=128")]
    pub fn set_velocity_modulation_amt_3(
        &mut self,
        velocity_modulation_amt_3: isize,
    ) -> Result<(), RytmError> {
        self.velocity_modulation_amt_3 = velocity_modulation_amt_3 as i8;
        Ok(())
    }

    /// Sets the velocity modulation target 3 of the sound.
    pub fn set_velocity_modulation_target_3(
        &mut self,
        velocity_modulation_target_3: SoundModTarget,
    ) {
        self.velocity_modulation_target_3 = velocity_modulation_target_3;
    }

    /// Sets the velocity modulation amount 4 of the sound.
    ///
    /// Range: `-127..=128`
    #[parameter_range(range = "velocity_modulation_amt_4:-127..=128")]
    pub fn set_velocity_modulation_amt_4(
        &mut self,
        velocity_modulation_amt_4: isize,
    ) -> Result<(), RytmError> {
        self.velocity_modulation_amt_4 = velocity_modulation_amt_4 as i8;
        Ok(())
    }

    /// Sets the velocity modulation target 4 of the sound.
    pub fn set_velocity_modulation_target_4(
        &mut self,
        velocity_modulation_target_4: SoundModTarget,
    ) {
        self.velocity_modulation_target_4 = velocity_modulation_target_4;
    }

    /// Sets the after touch modulation amount 1 of the sound.
    ///
    /// Range: `-127..=128`
    #[parameter_range(range = "after_touch_modulation_amt_1:-127..=128")]
    pub fn set_after_touch_modulation_amt_1(
        &mut self,
        after_touch_modulation_amt_1: isize,
    ) -> Result<(), RytmError> {
        self.after_touch_modulation_amt_1 = after_touch_modulation_amt_1 as i8;
        Ok(())
    }

    /// Sets the after touch modulation target 1 of the sound.
    pub fn set_after_touch_modulation_target_1(
        &mut self,
        after_touch_modulation_target_1: SoundModTarget,
    ) {
        self.after_touch_modulation_target_1 = after_touch_modulation_target_1;
    }

    /// Sets the after touch modulation amount 2 of the sound.
    ///
    /// Range: `-127..=128`
    #[parameter_range(range = "after_touch_modulation_amt_2:-127..=128")]
    pub fn set_after_touch_modulation_amt_2(
        &mut self,
        after_touch_modulation_amt_2: isize,
    ) -> Result<(), RytmError> {
        self.after_touch_modulation_amt_2 = after_touch_modulation_amt_2 as i8;
        Ok(())
    }

    /// Sets the after touch modulation target 2 of the sound.
    ///
    /// Range: `-127..=128`
    pub fn set_after_touch_modulation_target_2(
        &mut self,
        after_touch_modulation_target_2: SoundModTarget,
    ) {
        self.after_touch_modulation_target_2 = after_touch_modulation_target_2;
    }

    /// Sets the after touch modulation amount 3 of the sound.
    ///
    /// Range: `-127..=128`
    #[parameter_range(range = "after_touch_modulation_amt_3:-127..=128")]
    pub fn set_after_touch_modulation_amt_3(
        &mut self,
        after_touch_modulation_amt_3: isize,
    ) -> Result<(), RytmError> {
        self.after_touch_modulation_amt_3 = after_touch_modulation_amt_3 as i8;
        Ok(())
    }

    /// Sets the after touch modulation target 3 of the sound.
    pub fn set_after_touch_modulation_target_3(
        &mut self,
        after_touch_modulation_target_3: SoundModTarget,
    ) {
        self.after_touch_modulation_target_3 = after_touch_modulation_target_3;
    }

    /// Sets the after touch modulation amount 4 of the sound.
    ///
    /// Range: `-127..=128`
    #[parameter_range(range = "after_touch_modulation_amt_4:-127..=128")]
    pub fn set_after_touch_modulation_amt_4(
        &mut self,
        after_touch_modulation_amt_4: isize,
    ) -> Result<(), RytmError> {
        self.after_touch_modulation_amt_4 = after_touch_modulation_amt_4 as i8;
        Ok(())
    }

    /// Sets the after touch modulation target 4 of the sound.
    pub fn set_after_touch_modulation_target_4(
        &mut self,
        after_touch_modulation_target_4: SoundModTarget,
    ) {
        self.after_touch_modulation_target_4 = after_touch_modulation_target_4;
    }

    /// Returns the machine type of the sound.
    pub const fn machine(&self) -> MachineType {
        self.machine_type
    }

    /// Returns the chromatic mode of the sound.
    pub const fn chromatic_mode(&self) -> SoundSettingsChromaticMode {
        self.chromatic_mode
    }

    /// Returns the env reset filter switch of the sound.
    pub const fn env_reset_filter(&self) -> bool {
        self.env_reset_filter
    }

    /// Returns the velocity to volume switch of the sound.
    pub const fn velocity_to_volume(&self) -> bool {
        self.velocity_to_volume
    }

    /// Returns the legacy fx send switch of the sound.
    pub const fn legacy_fx_send(&self) -> bool {
        self.legacy_fx_send
    }

    /// Returns the velocity modulation amount 1 of the sound.
    ///
    /// Range: `-127..=128`
    pub const fn velocity_modulation_amt_1(&self) -> isize {
        self.velocity_modulation_amt_1 as isize
    }

    /// Returns the velocity modulation target 1 of the sound.
    pub const fn velocity_modulation_target_1(&self) -> SoundModTarget {
        self.velocity_modulation_target_1
    }

    /// Returns the velocity modulation amount 2 of the sound.
    ///    
    /// Range: `-127..=128`
    pub const fn velocity_modulation_amt_2(&self) -> isize {
        self.velocity_modulation_amt_2 as isize
    }

    /// Returns the velocity modulation target 2 of the sound.
    pub const fn velocity_modulation_target_2(&self) -> SoundModTarget {
        self.velocity_modulation_target_2
    }

    /// Returns the velocity modulation amount 3 of the sound.
    ///
    /// Range: `-127..=128`
    pub const fn velocity_modulation_amt_3(&self) -> isize {
        self.velocity_modulation_amt_3 as isize
    }

    /// Returns the velocity modulation target 3 of the sound.
    pub const fn velocity_modulation_target_3(&self) -> SoundModTarget {
        self.velocity_modulation_target_3
    }

    /// Returns the velocity modulation amount 4 of the sound
    ///
    /// Range: `-127..=128`
    pub const fn velocity_modulation_amt_4(&self) -> isize {
        self.velocity_modulation_amt_4 as isize
    }

    /// Returns the velocity modulation target 4 of the sound.
    pub const fn velocity_modulation_target_4(&self) -> SoundModTarget {
        self.velocity_modulation_target_4
    }

    /// Returns the after touch modulation amount 1 of the sound.
    ///
    /// Range: `-127..=128`
    pub const fn after_touch_modulation_amt_1(&self) -> isize {
        self.after_touch_modulation_amt_1 as isize
    }

    /// Returns the after touch modulation target 1 of the sound.
    pub const fn after_touch_modulation_target_1(&self) -> SoundModTarget {
        self.after_touch_modulation_target_1
    }

    /// Returns the after touch modulation amount 2 of the sound.
    ///
    /// Range: `-127..=128`
    pub const fn after_touch_modulation_amt_2(&self) -> isize {
        self.after_touch_modulation_amt_2 as isize
    }

    /// Returns the after touch modulation target 2 of the sound.
    pub const fn after_touch_modulation_target_2(&self) -> SoundModTarget {
        self.after_touch_modulation_target_2
    }

    /// Returns the after touch modulation amount 3 of the sound.
    ///
    /// Range: `-127..=128`
    pub const fn after_touch_modulation_amt_3(&self) -> isize {
        self.after_touch_modulation_amt_3 as isize
    }

    /// Returns the after touch modulation target 3 of the sound.
    pub const fn after_touch_modulation_target_3(&self) -> SoundModTarget {
        self.after_touch_modulation_target_3
    }

    /// Returns the after touch modulation amount 4 of the sound.
    ///
    /// Range: `-127..=128`
    pub const fn after_touch_modulation_amt_4(&self) -> isize {
        self.after_touch_modulation_amt_4 as isize
    }

    /// Returns the after touch modulation target 4 of the sound.
    pub const fn after_touch_modulation_target_4(&self) -> SoundModTarget {
        self.after_touch_modulation_target_4
    }

    #[parameter_range(range = "track_index:0..=11")]
    pub fn try_default_for_track(track_index: usize) -> Result<Self, RytmError> {
        Ok(Self {
            machine_type: match track_index {
                0 => MachineType::BdHard,
                1 => MachineType::SdHard,
                2 => MachineType::RsHard,
                3 => MachineType::CpClassic,
                4 => MachineType::BtClassic,
                5..=7 => MachineType::XtClassic,
                8 => MachineType::ChClassic,
                9 => MachineType::OhClassic,
                10 => MachineType::CyClassic,
                11 => MachineType::CbClassic,
                _ => unreachable!(),
            },
            chromatic_mode: SoundSettingsChromaticMode::default(),
            env_reset_filter: true,
            velocity_to_volume: true,
            legacy_fx_send: false,

            velocity_modulation_amt_1: 0,
            velocity_modulation_target_1: SoundModTarget::default(),
            velocity_modulation_amt_2: 0,
            velocity_modulation_target_2: SoundModTarget::FilterFrequency,
            velocity_modulation_amt_3: 0,
            velocity_modulation_target_3: SoundModTarget::FilterResonance,
            velocity_modulation_amt_4: 0,
            velocity_modulation_target_4: SoundModTarget::default(),

            after_touch_modulation_amt_1: 16,
            after_touch_modulation_target_1: SoundModTarget::FilterFrequency,
            after_touch_modulation_amt_2: 0,
            after_touch_modulation_target_2: SoundModTarget::default(),
            after_touch_modulation_amt_3: 0,
            after_touch_modulation_target_3: SoundModTarget::default(),
            after_touch_modulation_amt_4: 0,
            after_touch_modulation_target_4: SoundModTarget::default(),
        })
    }
}

impl Default for SoundSettings {
    fn default() -> Self {
        Self {
            machine_type: MachineType::default(),
            chromatic_mode: SoundSettingsChromaticMode::default(),
            env_reset_filter: true,
            velocity_to_volume: true,
            legacy_fx_send: false,

            velocity_modulation_amt_1: 0,
            velocity_modulation_target_1: SoundModTarget::default(),
            velocity_modulation_amt_2: 0,
            velocity_modulation_target_2: SoundModTarget::FilterFrequency,
            velocity_modulation_amt_3: 0,
            velocity_modulation_target_3: SoundModTarget::FilterResonance,
            velocity_modulation_amt_4: 0,
            velocity_modulation_target_4: SoundModTarget::default(),

            after_touch_modulation_amt_1: 16,
            after_touch_modulation_target_1: SoundModTarget::FilterFrequency,
            after_touch_modulation_amt_2: 0,
            after_touch_modulation_target_2: SoundModTarget::default(),
            after_touch_modulation_amt_3: 0,
            after_touch_modulation_target_3: SoundModTarget::default(),
            after_touch_modulation_amt_4: 0,
            after_touch_modulation_target_4: SoundModTarget::default(),
        }
    }
}