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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
use super::super::types::{
    MidiParameterOutput, MidiPortFunction, MidiPortsOutputChannel, MidiTransportLayer,
    ParameterDestination,
};
use crate::{
    error::{ConversionError, ParameterError, RytmError},
    object::global::types::MidiChannel,
};
use derivative::Derivative;
use rytm_rs_macro::parameter_range;
use rytm_sys::ar_global_t;
use serde::{Deserialize, Serialize};

/// Represents the `Midi Config` menu.
#[derive(
    Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub struct MidiConfig {
    sync: Sync,
    port_config: PortConfig,
    channels: Channels,
}

impl TryFrom<&ar_global_t> for MidiConfig {
    type Error = ConversionError;
    fn try_from(raw_global: &ar_global_t) -> Result<Self, Self::Error> {
        Ok(Self {
            sync: raw_global.try_into()?,
            port_config: raw_global.try_into()?,
            channels: raw_global.into(),
        })
    }
}

impl MidiConfig {
    pub(crate) fn apply_to_raw_global(&self, raw_global: &mut ar_global_t) {
        self.sync.apply_to_raw_global(raw_global);
        self.port_config.apply_to_raw_global(raw_global);
        self.channels.apply_to_raw_global(raw_global);
    }

    /// Returns the `Sync` menu.
    pub const fn sync(&self) -> &Sync {
        &self.sync
    }

    /// Returns the `Port Config` menu.
    pub const fn port_config(&self) -> &PortConfig {
        &self.port_config
    }

    /// Returns the `Channels` menu.
    pub const fn channels(&self) -> &Channels {
        &self.channels
    }

    /// Returns the `Sync` menu as mutable.
    pub fn sync_mut(&mut self) -> &mut Sync {
        &mut self.sync
    }

    /// Returns the `Port Config` menu as mutable.
    pub fn port_config_mut(&mut self) -> &mut PortConfig {
        &mut self.port_config
    }

    /// Returns the `Channels` menu as mutable.
    pub fn channels_mut(&mut self) -> &mut Channels {
        &mut self.channels
    }
}

// It is better to do it a struct because of consistency.
#[allow(clippy::struct_excessive_bools)]
/// Represents the `Sync` menu in `Midi Config` menu.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Sync {
    clock_receive: bool,
    clock_send: bool,
    transport_receive: bool,
    transport_send: bool,
    program_change_receive: bool,
    program_change_send: bool,
}

impl Default for Sync {
    fn default() -> Self {
        Self {
            clock_receive: true,
            clock_send: false,
            transport_receive: true,
            transport_send: false,
            program_change_receive: false,
            program_change_send: false,
        }
    }
}

impl TryFrom<&ar_global_t> for Sync {
    type Error = ConversionError;
    fn try_from(raw_global: &ar_global_t) -> Result<Self, Self::Error> {
        Ok(Self {
            clock_receive: raw_global.clock_receive != 0,
            clock_send: raw_global.clock_send != 0,
            transport_receive: raw_global.transport_receive != 0,
            transport_send: raw_global.transport_send != 0,
            program_change_receive: raw_global.program_change_receive != 0,
            program_change_send: raw_global.program_change_send != 0,
        })
    }
}

impl Sync {
    pub(crate) fn apply_to_raw_global(self, raw_global: &mut ar_global_t) {
        raw_global.clock_receive = self.clock_receive as u8;
        raw_global.clock_send = self.clock_send as u8;
        raw_global.transport_receive = self.transport_receive as u8;
        raw_global.transport_send = self.transport_send as u8;
        raw_global.program_change_receive = self.program_change_receive as u8;
        raw_global.program_change_send = self.program_change_send as u8;
    }

    /// Turns clock receive on or off.
    pub fn set_clock_receive(&mut self, clock_receive: bool) {
        self.clock_receive = clock_receive;
    }

    /// Turns clock send on or off.
    pub fn set_clock_send(&mut self, clock_send: bool) {
        self.clock_send = clock_send;
    }

    /// Turns transport receive on or off.
    pub fn set_transport_receive(&mut self, transport_receive: bool) {
        self.transport_receive = transport_receive;
    }

    /// Turns transport send on or off.
    pub fn set_transport_send(&mut self, transport_send: bool) {
        self.transport_send = transport_send;
    }

    /// Turns program change receive on or off.
    pub fn set_program_change_receive(&mut self, program_change_receive: bool) {
        self.program_change_receive = program_change_receive;
    }

    /// Turns program change send on or off.
    pub fn set_program_change_send(&mut self, program_change_send: bool) {
        self.program_change_send = program_change_send;
    }

    /// Returns `true` if clock receive is on.
    pub const fn clock_receive(&self) -> bool {
        self.clock_receive
    }

    /// Returns `true` if clock send is on.
    pub const fn clock_send(&self) -> bool {
        self.clock_send
    }

    /// Returns `true` if transport receive is on.
    pub const fn transport_receive(&self) -> bool {
        self.transport_receive
    }

    /// Returns `true` if transport send is on.
    pub const fn transport_send(&self) -> bool {
        self.transport_send
    }

    /// Returns `true` if program change receive is on.
    pub const fn program_change_receive(&self) -> bool {
        self.program_change_receive
    }

    /// Returns `true` if program change send is on.
    pub const fn program_change_send(&self) -> bool {
        self.program_change_send
    }
}

/// Represents the `Port Config` menu in `Midi Config` menu.
#[derive(Derivative, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[derivative(Debug)]
pub struct PortConfig {
    out_port_func: MidiPortFunction,
    thru_port_func: MidiPortFunction,
    input_from: MidiTransportLayer,
    output_to: MidiTransportLayer,
    param_output: MidiParameterOutput,
    receive_notes: bool,
    receive_cc_nrpn: bool,
    pad_dest: ParameterDestination,
    pressure_dest: ParameterDestination,
    encoder_dest: ParameterDestination,
    mute_dest: ParameterDestination,
    ports_output_channel: MidiPortsOutputChannel,

    // Probably `TURBO SPEED` but I can not confirm this.
    #[derivative(Debug = "ignore")]
    pub(crate) __unknown0x29: u8,
}

impl Default for PortConfig {
    fn default() -> Self {
        Self {
            out_port_func: MidiPortFunction::default(),
            thru_port_func: MidiPortFunction::default(),
            input_from: MidiTransportLayer::default(),
            output_to: MidiTransportLayer::default(),
            param_output: MidiParameterOutput::default(),
            receive_notes: true,
            receive_cc_nrpn: true,
            pad_dest: ParameterDestination::default(),
            pressure_dest: ParameterDestination::default(),
            encoder_dest: ParameterDestination::Internal,
            mute_dest: ParameterDestination::default(),
            ports_output_channel: MidiPortsOutputChannel::default(),
            __unknown0x29: 0,
        }
    }
}

impl TryFrom<&ar_global_t> for PortConfig {
    type Error = ConversionError;
    fn try_from(raw_global: &ar_global_t) -> Result<Self, Self::Error> {
        if raw_global.receive_notes > 1 || raw_global.receive_cc_nrpn > 1 {
            return Err(ConversionError::Range {
                value: raw_global.receive_notes.to_string(),
                type_name: "PortConfig".into(),
            });
        }

        Ok(Self {
            out_port_func: raw_global.out_port_func.try_into()?,
            thru_port_func: raw_global.thru_port_func.try_into()?,
            input_from: raw_global.input_from.try_into()?,
            output_to: raw_global.output_to.try_into()?,
            param_output: raw_global.param_output.try_into()?,
            receive_notes: raw_global.receive_notes != 0,
            receive_cc_nrpn: raw_global.receive_cc_nrpn != 0,
            pad_dest: raw_global.pad_dest.try_into()?,
            pressure_dest: raw_global.pressure_dest.try_into()?,
            encoder_dest: raw_global.encoder_dest.try_into()?,
            mute_dest: raw_global.mute_dest.try_into()?,
            ports_output_channel: raw_global.ports_output_channel.try_into()?,
            __unknown0x29: raw_global.__unknown0x29,
        })
    }
}

impl PortConfig {
    pub(crate) fn apply_to_raw_global(&self, raw_global: &mut ar_global_t) {
        raw_global.out_port_func = self.out_port_func.into();
        raw_global.thru_port_func = self.thru_port_func.into();
        raw_global.input_from = self.input_from.into();
        raw_global.output_to = self.output_to.into();
        raw_global.param_output = self.param_output.into();
        raw_global.receive_notes = self.receive_notes as u8;
        raw_global.receive_cc_nrpn = self.receive_cc_nrpn as u8;
        raw_global.pad_dest = self.pad_dest.into();
        raw_global.pressure_dest = self.pressure_dest.into();
        raw_global.encoder_dest = self.encoder_dest.into();
        raw_global.mute_dest = self.mute_dest.into();
        raw_global.ports_output_channel = self.ports_output_channel.into();
    }

    /// Sets the function of the MIDI out port.
    pub fn set_output_port_function(&mut self, output_port_function: MidiPortFunction) {
        self.out_port_func = output_port_function;
    }

    /// Sets the function of the MIDI thru port.
    pub fn set_thru_port_function(&mut self, thru_port_function: MidiPortFunction) {
        self.thru_port_func = thru_port_function;
    }

    /// Sets the transport layer to receive MIDI from.
    pub fn set_input_transport(&mut self, input_from: MidiTransportLayer) {
        self.input_from = input_from;
    }

    /// Sets the transport layer to send MIDI to.
    pub fn set_output_transport(&mut self, output_to: MidiTransportLayer) {
        self.output_to = output_to;
    }

    /// Sets the MIDI parameter output type.
    pub fn set_parameter_output_type(&mut self, param_output: MidiParameterOutput) {
        self.param_output = param_output;
    }

    /// Turns note messages receive on or off.
    pub fn set_receive_notes(&mut self, receive_notes: bool) {
        self.receive_notes = receive_notes;
    }

    /// Turns CC and NRPN messages receive on or off.
    pub fn set_receive_cc_nrpn(&mut self, receive_cc_nrpn: bool) {
        self.receive_cc_nrpn = receive_cc_nrpn;
    }

    /// Sets the destination of parameters produced by pressing pads.
    pub fn set_pad_parameter_destination(
        &mut self,
        pad_parameter_destination: ParameterDestination,
    ) {
        self.pad_dest = pad_parameter_destination;
    }

    /// Sets the destination of parameters produced by pressure amount when pressing pads.
    pub fn set_pressure_parameter_destination(
        &mut self,
        pressure_parameter_destination: ParameterDestination,
    ) {
        self.pressure_dest = pressure_parameter_destination;
    }

    /// Sets the destination of parameters produced by turning encoders.
    pub fn set_encoder_parameter_destination(
        &mut self,
        encoder_parameter_destination: ParameterDestination,
    ) {
        self.encoder_dest = encoder_parameter_destination;
    }

    /// Sets the destination of parameters produced by muting tracks.
    pub fn set_mute_parameter_destination(
        &mut self,
        mute_parameter_destination: ParameterDestination,
    ) {
        self.mute_dest = mute_parameter_destination;
    }

    /// Sets the channel of the MIDI ports output.
    ///
    /// `AUTO CH` means the channel is determined by the set auto channel.
    /// `TRK_CH` means the channel is determined by the track channel.
    pub fn set_ports_output_channel(&mut self, ports_output_channel: MidiPortsOutputChannel) {
        self.ports_output_channel = ports_output_channel;
    }

    /// Returns the function of the MIDI out port.
    pub const fn output_port_function(&self) -> MidiPortFunction {
        self.out_port_func
    }

    /// Returns the function of the MIDI thru port.
    pub const fn thru_port_function(&self) -> MidiPortFunction {
        self.thru_port_func
    }

    /// Returns the transport layer to receive MIDI from.
    pub const fn input_transport(&self) -> MidiTransportLayer {
        self.input_from
    }

    /// Returns the transport layer to send MIDI to.
    pub const fn output_transport(&self) -> MidiTransportLayer {
        self.output_to
    }

    /// Returns the MIDI parameter output type.
    pub const fn parameter_output_type(&self) -> MidiParameterOutput {
        self.param_output
    }

    /// Returns `true` if note messages receive is on.
    pub const fn receive_notes(&self) -> bool {
        self.receive_notes
    }

    /// Returns `true` if CC and NRPN messages receive is on.
    pub const fn receive_cc_nrpn(&self) -> bool {
        self.receive_cc_nrpn
    }

    /// Returns the destination of parameters produced by pressing pads.
    pub const fn pad_parameter_destination(&self) -> ParameterDestination {
        self.pad_dest
    }

    /// Returns the destination of parameters produced by pressure amount when pressing pads.
    pub const fn pressure_parameter_destination(&self) -> ParameterDestination {
        self.pressure_dest
    }

    /// Returns the destination of parameters produced by turning encoders.
    pub const fn encoder_parameter_destination(&self) -> ParameterDestination {
        self.encoder_dest
    }

    /// Returns the destination of parameters produced by muting tracks.
    pub const fn mute_parameter_destination(&self) -> ParameterDestination {
        self.mute_dest
    }

    /// Returns the channel of the MIDI ports output.
    pub const fn ports_output_channel(&self) -> MidiPortsOutputChannel {
        self.ports_output_channel
    }

    /// Returns `true` if turbo speed is on.
    ///
    /// # Note
    ///
    /// I believe this parameter is `TURBO SPEED` since it is the only one left in the menu when reverse engineering this type.
    /// But since I can not enable it without connecting a turbo speed capable MIDI interface I can not be sure.
    ///
    /// If you have a turbo speed capable MIDI interface and can confirm this please open an issue on the GitHub repository.
    pub const fn turbo_speed(&self) -> bool {
        self.__unknown0x29 != 0
    }
}

/// Represents the `Channels` menu in `Midi Config` menu.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Channels {
    auto_channel: MidiChannel,
    track_channels: [MidiChannel; 12],
    track_fx_channel: MidiChannel,
    program_change_in_channel: MidiChannel,
    program_change_out_channel: MidiChannel,
    performance_channel: MidiChannel,
}

impl Default for Channels {
    fn default() -> Self {
        Self {
            auto_channel: MidiChannel::Channel(13),
            track_channels: [
                MidiChannel::Channel(0),
                MidiChannel::Channel(1),
                MidiChannel::Channel(2),
                MidiChannel::Channel(3),
                MidiChannel::Channel(4),
                MidiChannel::Channel(5),
                MidiChannel::Channel(6),
                MidiChannel::Channel(7),
                MidiChannel::Channel(8),
                MidiChannel::Channel(9),
                MidiChannel::Channel(10),
                MidiChannel::Channel(11),
            ],
            track_fx_channel: MidiChannel::Channel(12),
            program_change_in_channel: MidiChannel::Auto,
            program_change_out_channel: MidiChannel::Auto,
            performance_channel: MidiChannel::Channel(15),
        }
    }
}

impl From<&ar_global_t> for Channels {
    fn from(raw_global: &ar_global_t) -> Self {
        let mut track_channels = [MidiChannel::default(); 12];
        for (i, channel) in raw_global.track_channels.iter().enumerate() {
            track_channels[i] = if *channel == 0xFF {
                MidiChannel::Off
            } else {
                MidiChannel::Channel(*channel as usize)
            };
        }

        Self {
            auto_channel: if raw_global.auto_channel == 0xFF {
                MidiChannel::Off
            } else {
                MidiChannel::Channel(raw_global.auto_channel as usize)
            },
            track_channels,
            track_fx_channel: if raw_global.track_fx_channel == 0xFF {
                MidiChannel::Off
            } else {
                MidiChannel::Channel(raw_global.track_fx_channel as usize)
            },
            program_change_in_channel: if raw_global.prog_ch_in_channel == 0xFF {
                MidiChannel::Auto
            } else {
                MidiChannel::Channel(raw_global.prog_ch_in_channel as usize)
            },
            program_change_out_channel: if raw_global.prog_ch_out_channel == 0xFF {
                MidiChannel::Auto
            } else {
                MidiChannel::Channel(raw_global.prog_ch_out_channel as usize)
            },
            performance_channel: if raw_global.perf_channel == 0xFF {
                MidiChannel::Off
            } else {
                MidiChannel::Channel(raw_global.perf_channel as usize)
            },
        }
    }
}

impl Channels {
    pub(crate) fn apply_to_raw_global(&self, raw_global: &mut ar_global_t) {
        const ERROR_MSG: &str = "We shouldn't reach here. There are many checks before this function is called. But if we did that means either the sysex format has changed or corrupted.";

        raw_global.auto_channel = self.auto_channel.try_into().expect(ERROR_MSG);
        raw_global.track_channels[0] = self.track_channels[0].try_into().expect(ERROR_MSG);
        raw_global.track_channels[1] = self.track_channels[1].try_into().expect(ERROR_MSG);
        raw_global.track_channels[2] = self.track_channels[2].try_into().expect(ERROR_MSG);
        raw_global.track_channels[3] = self.track_channels[3].try_into().expect(ERROR_MSG);
        raw_global.track_channels[4] = self.track_channels[4].try_into().expect(ERROR_MSG);
        raw_global.track_channels[5] = self.track_channels[5].try_into().expect(ERROR_MSG);
        raw_global.track_channels[6] = self.track_channels[6].try_into().expect(ERROR_MSG);
        raw_global.track_channels[7] = self.track_channels[7].try_into().expect(ERROR_MSG);
        raw_global.track_channels[8] = self.track_channels[8].try_into().expect(ERROR_MSG);
        raw_global.track_channels[9] = self.track_channels[9].try_into().expect(ERROR_MSG);
        raw_global.track_channels[10] = self.track_channels[10].try_into().expect(ERROR_MSG);
        raw_global.track_channels[11] = self.track_channels[11].try_into().expect(ERROR_MSG);
        raw_global.track_fx_channel = self.track_fx_channel.try_into().expect(ERROR_MSG);
        raw_global.prog_ch_in_channel = self.program_change_in_channel.try_into().expect(ERROR_MSG);
        raw_global.prog_ch_out_channel =
            self.program_change_out_channel.try_into().expect(ERROR_MSG);
        raw_global.perf_channel = self.performance_channel.try_into().expect(ERROR_MSG);
    }

    fn validate_midi_channel(channel: &MidiChannel, auto: bool) -> Result<(), ParameterError> {
        // Either auto or off could be used.
        match channel {
            MidiChannel::Off => {
                if auto {
                    Err(ParameterError::Compatibility {
                        value: "MidiChannel::Off".to_string(),
                        parameter_name: "channel".into(),
                        reason: Some("Only MidiChannel::Auto or MidiChannel::Channel(usize) variants are allowed for this function.".into()),
                    })
                } else {
                    Ok(())
                }
            }
            MidiChannel::Auto => {
                if auto {
                    Ok(())
                } else {
                    Err(ParameterError::Compatibility {
                        value: "MidiChannel::Off".to_string(),
                        parameter_name: "channel".into(),
                        reason: Some("Only MidiChannel::Off or MidiChannel::Channel(usize) variants are allowed for this function.".into()),
                    })
                }
            }
            MidiChannel::Channel(channel) => {
                if *channel > 15 {
                    Err(ParameterError::Range {
                        value: channel.to_string(),
                        parameter_name: "MidiChannel".into(),
                    })
                } else {
                    Ok(())
                }
            }
        }
    }

    /// Sets the auto channel.
    ///
    /// Only [`MidiChannel::Off`] or [`MidiChannel::Channel(usize)`] variants can be used.
    ///
    /// Range of [`MidiChannel::Channel(usize)`] is: `0..=15`.
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is not compatible with the function.
    pub fn set_auto_channel(&mut self, auto_channel: MidiChannel) -> Result<(), RytmError> {
        Self::validate_midi_channel(&auto_channel, false)?;
        self.auto_channel = auto_channel;
        Ok(())
    }

    /// Sets the track channels.
    ///
    /// Only [`MidiChannel::Off`] or [`MidiChannel::Channel(usize)`] variants can be used.
    ///
    /// Range of [`MidiChannel::Channel(usize)`] is: `0..=15`.
    #[parameter_range(range = "track:0..=11")]
    pub fn set_track_channel(
        &mut self,
        track: usize,
        track_channel: MidiChannel,
    ) -> Result<(), RytmError> {
        Self::validate_midi_channel(&track_channel, false)?;
        self.track_channels[track] = track_channel;
        Ok(())
    }

    /// Sets the track FX channel.
    ///
    /// Only [`MidiChannel::Off`] or [`MidiChannel::Channel(usize)`] variants can be used.
    ///
    /// Range of [`MidiChannel::Channel(usize)`] is: `0..=15`.
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is not compatible with the function.
    pub fn set_track_fx_channel(&mut self, track_fx_channel: MidiChannel) -> Result<(), RytmError> {
        Self::validate_midi_channel(&track_fx_channel, false)?;
        self.track_fx_channel = track_fx_channel;
        Ok(())
    }

    /// Sets the program change in channel.
    ///
    /// Only [`MidiChannel::Auto`] or [`MidiChannel::Channel(usize)`] variants can be used.
    ///
    /// Range of [`MidiChannel::Channel(usize)`] is: `0..=15`.
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is not compatible with the function.
    pub fn set_program_change_in_channel(
        &mut self,
        program_change_in_channel: MidiChannel,
    ) -> Result<(), RytmError> {
        Self::validate_midi_channel(&program_change_in_channel, true)?;
        self.program_change_in_channel = program_change_in_channel;
        Ok(())
    }

    /// Sets the program change out channel.
    ///
    /// Only [`MidiChannel::Auto`] or [`MidiChannel::Channel(usize)`] variants can be used.
    ///
    /// Range of [`MidiChannel::Channel(usize)`] is: `0..=15`.
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is not compatible with the function.
    pub fn set_program_change_out_channel(
        &mut self,
        program_change_out_channel: MidiChannel,
    ) -> Result<(), RytmError> {
        Self::validate_midi_channel(&program_change_out_channel, true)?;
        self.program_change_out_channel = program_change_out_channel;
        Ok(())
    }

    /// Sets the performance channel.
    ///
    /// Only [`MidiChannel::Off`] or [`MidiChannel::Channel(usize)`] variants can be used.
    ///
    /// Range of [`MidiChannel::Channel(usize)`] is: `0..=15`.
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is not compatible with the function.
    pub fn set_performance_channel(
        &mut self,
        performance_channel: MidiChannel,
    ) -> Result<(), RytmError> {
        Self::validate_midi_channel(&performance_channel, false)?;
        self.performance_channel = performance_channel;
        Ok(())
    }

    /// Returns the auto channel.
    pub const fn auto_channel(&self) -> MidiChannel {
        self.auto_channel
    }

    /// Returns the track channels.
    pub const fn track_channels(&self) -> &[MidiChannel; 12] {
        &self.track_channels
    }

    /// Returns the channel of a track.
    ///
    /// Range: `0..=11`.
    #[parameter_range(range = "track_index:0..=11")]
    pub fn track_channel(&self, track_index: usize) -> Result<MidiChannel, RytmError> {
        Ok(self.track_channels[track_index])
    }

    /// Returns the track FX channel.
    pub const fn track_fx_channel(&self) -> MidiChannel {
        self.track_fx_channel
    }

    /// Returns the program change in channel.
    pub const fn program_change_in_channel(&self) -> MidiChannel {
        self.program_change_in_channel
    }

    /// Returns the program change out channel.
    pub const fn program_change_out_channel(&self) -> MidiChannel {
        self.program_change_out_channel
    }

    /// Returns the performance channel.
    pub const fn performance_channel(&self) -> MidiChannel {
        self.performance_channel
    }
}