ksynth 0.18.0

Patch manipulation for Kawai digital synths
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
//! Data models for controllers and macros.
//!

use std::convert::TryFrom;
use std::fmt;

use num_enum::TryFromPrimitive;
use bit::BitIndex;
use strum_macros;

use crate::{
    SystemExclusiveData,
    ParseError
};
use crate::k5000::{
    MacroParameterDepth,
    Pan,
    ControlDepth
};

/// Velocity switch settings.
#[derive(Debug, Eq, PartialEq, Copy, Clone, TryFromPrimitive, Default, strum_macros::Display)]
#[repr(u8)]
pub enum VelocitySwitch {
    #[default]
    Off,

    Loud,
    Soft,
    Unknown,
}

/// Velocity switch settings.
#[derive(Default, Debug)]
pub struct VelocitySwitchSettings {
    pub switch_type: VelocitySwitch,
    pub threshold: u8,
}

impl VelocitySwitchSettings {
    fn threshold_from(value: usize) -> u8 {
        let table: [u8; 32] = [
            4, 8, 12, 16, 20, 24, 28, 32,
            36, 40, 44, 48, 52, 56, 60, 64,
            68, 72, 76, 80, 84, 88, 92, 96,
            100, 104, 108, 112, 116, 120, 124, 127
        ];
        table[value]
    }

    fn from_threshold(threshold: u8) -> usize {
        let table: [u8; 32] = [
            4, 8, 12, 16, 20, 24, 28, 32,
            36, 40, 44, 48, 52, 56, 60, 64,
            68, 72, 76, 80, 84, 88, 92, 96,
            100, 104, 108, 112, 116, 120, 124, 127
        ];

        table.to_vec().iter().position(|x| *x == threshold).unwrap_or_default()
    }
}

impl fmt::Display for VelocitySwitchSettings {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Type={} Threshold={}",
            self.switch_type, self.threshold
        )
    }
}

impl SystemExclusiveData for VelocitySwitchSettings {
    fn from_bytes(data: &[u8]) -> Result<Self, ParseError> {
        let vs = data[0].bit_range(5..7) & 0b11;  // bits 5-6
        let t = data[0].bit_range(0..5); // bits 0-4
        eprintln!("VelocitySwitchSettings: vs = 0b{:b} ({}), t = 0b{:b} ({})", vs, vs, t, t);
        Ok(VelocitySwitchSettings {
            switch_type: VelocitySwitch::try_from(vs).unwrap(),
            threshold: VelocitySwitchSettings::threshold_from(t as usize),
        })
    }

    fn to_bytes(&self) -> Vec<u8> {
        let t = VelocitySwitchSettings::from_threshold(self.threshold) as u8;
        let value = t | ((self.switch_type as u8) << 5);
        vec![value]
    }

    fn data_size() -> usize { 1 }
}

/// Control source.
#[derive(
    Debug,
    Eq, PartialEq,
    Copy, Clone,
    TryFromPrimitive,
    Default,
    strum_macros::Display
)]
#[repr(u8)]
pub enum ControlSource {
    #[default]
    #[strum(to_string = "Bender")]
    Bender,

    #[strum(to_string = "Channel pressure")]
    ChannelPressure,

    #[strum(to_string = "Wheel")]
    Wheel,

    #[strum(to_string = "Expression")]
    Expression,

    #[strum(to_string = "MIDI volume")]
    MidiVolume,

    #[strum(to_string = "Pan pot")]
    PanPot,

    #[strum(to_string = "General controller 1")]
    GeneralController1,

    #[strum(to_string = "General controller 2")]
    GeneralController2,

    #[strum(to_string = "General controller 3")]
    GeneralController3,

    #[strum(to_string = "General controller 4")]
    GeneralController4,

    #[strum(to_string = "General controller 5")]
    GeneralController5,

    #[strum(to_string = "General controller 6")]
    GeneralController6,

    #[strum(to_string = "General controller 7")]
    GeneralController7,

    #[strum(to_string = "General controller 8")]
    GeneralController8,
}

/// Control destination.
#[derive(
    Debug, Eq, PartialEq,
    Copy, Clone,
    TryFromPrimitive,
    Default,
    strum_macros::Display
)]
#[repr(u8)]
pub enum ControlDestination {
    #[default]
    #[strum(to_string = "Pitch offset")]
    PitchOffset,

    #[strum(to_string = "Cutoff offset")]
    CutoffOffset,

    #[strum(to_string = "Level")]
    Level,

    #[strum(to_string = "Vibrato depth offset")]
    VibratoDepthOffset,

    #[strum(to_string = "Growl depth offset")]
    GrowlDepthOffset,

    #[strum(to_string = "Tremolo depth offset")]
    TremoloDepthOffset,

    #[strum(to_string = "LFO speed offset")]
    LfoSpeedOffset,

    #[strum(to_string = "Attack time offset")]
    AttackTimeOffset,

    #[strum(to_string = "Decay 1 time offset")]
    Decay1TimeOffset,

    #[strum(to_string = "Release time offset")]
    ReleaseTimeOffset,

    #[strum(to_string = "Velocity offset")]
    VelocityOffset,

    #[strum(to_string = "Resonance offset")]
    ResonanceOffset,

    #[strum(to_string = "Pan pot offset")]
    PanPotOffset,

    #[strum(to_string = "Formant filter bias offset")]
    FormantFilterBiasOffset,

    #[strum(to_string = "Formant filter envelope LFO depth offset")]
    FormantFilterEnvelopeLfoDepthOffset,

    #[strum(to_string = "Formant filter envelope LFO speed offset")]
    FormantFilterEnvelopeLfoSpeedOffset,

    #[strum(to_string = "Harmonic low offset")]
    HarmonicLowOffset,

    #[strum(to_string = "Harmonic high offset")]
    HarmonicHighOffset,

    #[strum(to_string = "Harmonic even offset")]
    HarmonicEvenOffset,

    #[strum(to_string = "Harmonic odd offset")]
    HarmonicOddOffset,
}

/// Macro controller.
#[derive(Debug)]
pub struct MacroController {
    pub destination1: ControlDestination,
    pub depth1: MacroParameterDepth,
    pub destination2: ControlDestination,
    pub depth2: MacroParameterDepth,
}

impl Default for MacroController {
    fn default() -> Self {
        MacroController {
            destination1: Default::default(),
            depth1: MacroParameterDepth::new(0),
            destination2: Default::default(),
            depth2: MacroParameterDepth::new(0),
        }
    }
}

impl fmt::Display for MacroController {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Dest1={} Depth={}\nDest2={} Depth={}",
            self.destination1, self.depth1, self.destination2, self.depth2
        )
    }
}

impl SystemExclusiveData for MacroController {
    fn from_bytes(data: &[u8]) -> Result<Self, ParseError> {
        eprintln!("MacroController from bytes {:?}", data);

        Ok(MacroController {
            destination1: ControlDestination::try_from(data[0]).unwrap(),
            depth1: MacroParameterDepth::from(data[1]),
            destination2: ControlDestination::try_from(data[2]).unwrap(),
            depth2: MacroParameterDepth::from(data[3]),
        })
    }

    fn to_bytes(&self) -> Vec<u8> {
        vec![
            self.destination1 as u8,
            self.depth1.into(),
            self.destination2 as u8,
            self.depth2.into()
        ]
    }

    fn data_size() -> usize { 4 }
}

/// Assignable controller.
#[derive(Default, Debug)]
pub struct AssignableController {
    pub source: ControlSource,
    pub destination: ControlDestination,
    pub depth: ControlDepth,
}

impl SystemExclusiveData for AssignableController {
    fn from_bytes(data: &[u8]) -> Result<Self, ParseError> {
        Ok(AssignableController {
            source: ControlSource::try_from(data[0]).unwrap(),
            destination: ControlDestination::try_from(data[1]).unwrap(),
            depth: ControlDepth::from(data[2]),
        })
    }

    fn to_bytes(&self) -> Vec<u8> {
        vec![self.source as u8, self.destination as u8, self.depth.into()]
    }

    fn data_size() -> usize { 3 }
}

/// Modulation settings.
#[derive(Default, Debug)]
pub struct ModulationSettings {
    pub pressure: MacroController,
    pub wheel: MacroController,
    pub expression: MacroController,
    pub assignable1: AssignableController,
    pub assignable2: AssignableController,
}

impl SystemExclusiveData for ModulationSettings {
    fn from_bytes(data: &[u8]) -> Result<Self, ParseError> {
        Ok(ModulationSettings {
            pressure: MacroController::from_bytes(&data[..4])?,
            wheel: MacroController::from_bytes(&data[4..8])?,
            expression: MacroController::from_bytes(&data[8..12])?,
            assignable1: AssignableController::from_bytes(&data[12..15])?,  // NOTE: only three bytes
            assignable2: AssignableController::from_bytes(&data[15..18])?,  // not four like macros
        })
    }

    fn to_bytes(&self) -> Vec<u8> {
        let mut result: Vec<u8> = Vec::new();

        result.extend(self.pressure.to_bytes());
        result.extend(self.wheel.to_bytes());
        result.extend(self.expression.to_bytes());
        result.extend(self.assignable1.to_bytes());
        result.extend(self.assignable2.to_bytes());

        result
    }

    fn data_size() -> usize {
        3 * MacroController::data_size()
        + 2 * AssignableController::data_size()
    }
}

/// Pan type.
#[derive(
    Debug,
    Eq, PartialEq,
    Copy, Clone,
    TryFromPrimitive,
    Default,
    strum_macros::Display
)]
#[repr(u8)]
pub enum PanKind {
    #[default]
    Normal,

    Random,

    #[strum(to_string = "Key scale")]
    KeyScale,

    #[strum(to_string = "Negative key scale")]
    NegativeKeyScale,
}

/// Pan settings.
#[derive(Debug)]
pub struct PanSettings {
    pub pan_type: PanKind,
    pub pan_value: Pan,
}

impl Default for PanSettings {
    fn default() -> Self {
        PanSettings {
            pan_type: Default::default(),
            pan_value: Pan::new(0),
        }
    }
}

impl SystemExclusiveData for PanSettings {
    fn from_bytes(data: &[u8]) -> Result<Self, ParseError> {
        Ok(PanSettings {
            pan_type: PanKind::try_from(data[0]).unwrap(),
            pan_value: Pan::from(data[1]),
        })
    }

    fn to_bytes(&self) -> Vec<u8> {
        vec![self.pan_type as u8, self.pan_value.into()]
    }

    fn data_size() -> usize { 2 }
}

/// Switch kind.
#[derive(Debug, Eq, PartialEq, Copy, Clone, TryFromPrimitive, Default)]
#[repr(u8)]
pub enum Switch {
    #[default]
    Off,

    HarmMax,
    HarmBright,
    HarmDark,
    HarmSaw,
    SelectLoud,
    AddLoud,
    AddFifth,
    AddOdd,
    AddEven,
    He1,
    He2,
    HarmonicEnvelopeLoop,
    FfMax,
    FfComb,
    FfHiCut,
    FfComb2,
}

impl fmt::Display for Switch {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", match self {
            Switch::Off => String::from("Off"),
            Switch::HarmMax => String::from("Max harmonics"),
            Switch::HarmBright => String::from("Bright harmonics"),
            Switch::HarmDark => String::from("Dark harmonics"),
            Switch::HarmSaw => String::from("Saw harmonics"),
            Switch::SelectLoud => String::from("Select loud"),
            Switch::AddLoud => String::from("Add loud"),
            Switch::AddFifth => String::from("Add fifth"),
            Switch::AddOdd => String::from("Add odd"),
            Switch::AddEven => String::from("Add even"),
            Switch::He1 => String::from("Harmonic Env 1"),
            Switch::He2 => String::from("Harmonic Env 2"),
            Switch::HarmonicEnvelopeLoop => String::from("Harmonic envelope loop"),
            Switch::FfMax => String::from("Formant filter max"),
            Switch::FfComb => String::from("Formant filter comb"),
            Switch::FfHiCut => String::from("Formant filter high cut"),
            Switch::FfComb2 => String::from("Formant filter comb 2"),
        })
    }
}

/// Switch control settings.
#[derive(Debug, Default)]
pub struct SwitchControl {
    pub switch1: Switch,
    pub switch2: Switch,
    pub footswitch1: Switch,
    pub footswitch2: Switch,
}

impl SystemExclusiveData for SwitchControl {
    fn from_bytes(data: &[u8]) -> Result<Self, ParseError> {
        Ok(SwitchControl {
            switch1: Switch::try_from(data[0]).unwrap(),
            switch2: Switch::try_from(data[1]).unwrap(),
            footswitch1: Switch::try_from(data[2]).unwrap(),
            footswitch2: Switch::try_from(data[3]).unwrap(),
        })
    }

    fn to_bytes(&self) -> Vec<u8> {
        vec![self.switch1 as u8, self.switch2 as u8, self.footswitch1 as u8, self.footswitch2 as u8]
    }

    fn data_size() -> usize { 4 }
}

/// Polyphony type.
#[derive(Debug, Eq, PartialEq, Copy, Clone, TryFromPrimitive)]
#[repr(u8)]
pub enum Polyphony {
    Poly,
    Solo1,
    Solo2,
}

impl fmt::Display for Polyphony {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", match self {
            Polyphony::Poly => "POLY",
            Polyphony::Solo1 => "SOLO1",
            Polyphony::Solo2 => "SOLO2",
        })
    }
}

/// Amplitude modulation kind.
#[derive(Debug, Eq, PartialEq, Copy, Clone, TryFromPrimitive, Default)]
#[repr(u8)]
pub enum AmplitudeModulation {
    #[default]
    Off,

    Source2,
    Source3,
    Source4,
    Source5,
    Source6,
}

impl fmt::Display for AmplitudeModulation {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", match self {
            AmplitudeModulation::Off => "OFF",
            AmplitudeModulation::Source2 => "1->2",
            AmplitudeModulation::Source3 => "2->3",
            AmplitudeModulation::Source4 => "3->4",
            AmplitudeModulation::Source5 => "4->5",
            AmplitudeModulation::Source6 => "5->6",
        })
    }
}

/// Velocity curve.
#[derive(Debug, Eq, PartialEq, Copy, Clone, TryFromPrimitive)]
#[repr(u8)]
pub enum VelocityCurve {
    Curve1,
    Curve2,
    Curve3,
    Curve4,
    Curve5,
    Curve6,
    Curve7,
    Curve8,
    Curve9,
    Curve10,
    Curve11,
    Curve12,
}

impl fmt::Display for VelocityCurve {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", match self {
            VelocityCurve::Curve1 => String::from("Curve 1"),
            VelocityCurve::Curve2 => String::from("Curve 2"),
            VelocityCurve::Curve3 => String::from("Curve 3"),
            VelocityCurve::Curve4 => String::from("Curve 4"),
            VelocityCurve::Curve5 => String::from("Curve 5"),
            VelocityCurve::Curve6 => String::from("Curve 6"),
            VelocityCurve::Curve7 => String::from("Curve 7"),
            VelocityCurve::Curve8 => String::from("Curve 8"),
            VelocityCurve::Curve9 => String::from("Curve 9"),
            VelocityCurve::Curve10 => String::from("Curve 10"),
            VelocityCurve::Curve11 => String::from("Curve 11"),
            VelocityCurve::Curve12 => String::from("Curve 12"),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::{*};

    #[test]
    fn test_macro_controller_from_bytes() {
        let data = vec![0x01, 0x4f, 0x03, 0x40];
        let mac = MacroController::from_bytes(&data);
        assert_eq!(mac.unwrap().destination1, ControlDestination::CutoffOffset);
    }

    #[test]
    fn test_velocity_switch_strum_display() {
        let vs = VelocitySwitch::Loud;
        assert_eq!(String::from("Loud"), format!("{}", vs));
    }

    #[test]
    fn test_control_destination_strum_display() {
        let cd = ControlDestination::VelocityOffset;
        assert_eq!(String::from("Velocity offset"), format!("{}", cd));
    }

    #[test]
    fn test_pan_kind_strum_display() {
        let p = PanKind::Normal;
        assert_eq!(String::from("Normal"), format!("{}", p));

    }

    /*
    #[test]
    fn test_modulation_settings_from_bytes() {
        let data = vec![
            0x01, 0x4f, 0x03, 0x40,  // press: destination 1, depth, destination 2, depth
            0x03, 0x59, 0x01, 0x40,  // wheel: destination 1, depth, destination 2, depth
            0x02, 0x5f, 0x00, 0x40,  // express: destination 1, depth, destination 2, depth
            0x02, 0x0d, 0x40,  // assignable: control source 1, destination, depth
            0x02, 0x09, 0x40,  // assignable: control source 2, destination, depth
        ];
        let modulation_settings = ModulationSettings::from_bytes(data);
        assert_eq!(modulation_settings.pressure,
            MacroController {
                destination1: ControlDestination::CutoffOffset,
                depth1: MacroParameterDepth::from(0x4f),
                destination2: ControlDestination::VibratoDepthOffset,
                depth2: MacroParameterDepth::from(0x40),
             }
        );
    }
    */
}