nord-format 0.6.0

Read and write Nord keyboard files from Rust, byte for byte
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
//! The Electro 5 program as its panel is divided.
//!
//! The body keeps every organ model's registration and both parts' settings at once, so
//! most of it is state rather than controls: what the instrument is *using* is decided by
//! the part selectors and the organ model, and that is what the conditions here say.
//!
//! Reading order is the panel's, left to right: the keyboard section leads because its
//! part pickers decide which engine sections mean anything, and a picker that brings a
//! section back must never be inside the section it brings back.

use crate::panel::{Group, Match, Panel, Relevance, Selection};

/// One of a model's two stored presets, chosen by its `…_preset2_selected` flag.
macro_rules! preset {
    ($field:expr, $value:expr) => {
        Some(Selection {
            field: $field,
            value: $value,
        })
    };
}

/// Either part playing `instrument` — the condition an engine section hangs on.
macro_rules! part_plays {
    ($instrument:expr) => {
        Some(Relevance {
            any_of: &[
                Match {
                    field: "center_panel.lower_part",
                    is: &[$instrument],
                },
                Match {
                    field: "center_panel.upper_part",
                    is: &[$instrument],
                },
            ],
        })
    };
}

/// The organ model the program has selected.
macro_rules! organ_is {
    ($($model:expr),+) => {
        Some(Relevance {
            any_of: &[Match {
                field: "center_panel.organ_type",
                is: &[$($model),+],
            }],
        })
    };
}

/// One effect routed to a part. `Unknown` is how older firmware spelled *off* and
/// presents as off, so it is not one of these.
/// Confirmed on hardware.
macro_rules! routed {
    ($field:expr) => {
        Some(Relevance {
            any_of: &[Match {
                field: $field,
                is: &["Lower", "Upper"],
            }],
        })
    };
}

macro_rules! switched_on {
    ($field:expr) => {
        Some(Relevance {
            any_of: &[Match {
                field: $field,
                is: &["true"],
            }],
        })
    };
}

pub const PANEL: Panel = Panel {
    exhaustive: false,
    groups: &[
        Group {
            title: "Keyboard & split",
            selected_by: None,
            when: None,
            groups: &[],
            members: &[
                "center_panel.lower_part",
                "center_panel.lower_octave_shift",
                "center_panel.lower_sustain",
                "center_panel.lower_control",
                "center_panel.upper_part",
                "center_panel.upper_octave_shift",
                "center_panel.upper_sustain",
                "center_panel.upper_control",
                "center_panel.split",
                "center_panel.split_point",
                "center_panel.part_mix",
                // ⚠️ Two fields, one control: the enable is sticky and an untouched
                // program stores +1 rather than 0, so neither reads on its own. They are
                // adjacent because a caller drawing them as one needs them together.
                "center_panel.transpose_enabled",
                "center_panel.transpose",
                "center_panel.gain",
            ],
        },
        Group {
            title: "Organ",
            selected_by: None,
            when: part_plays!("Organ"),
            // The model selector stays here rather than inside a model's cluster: it is
            // what a reader changes to make another cluster relevant.
            members: &["center_panel.organ_type", "center_panel.drawbar_live"],
            groups: &[
                Group {
                    title: "B3",
                    selected_by: None,
                    when: organ_is!("B3", "B3Bass"),
                    members: &[
                        "organ_panel.b3_vib",
                        "organ_panel.b3_perc_third",
                        "organ_panel.b3_perc_speed",
                        "organ_panel.b3_preset2_selected",
                    ],
                    groups: &[
                        Group {
                            title: "Preset 1",
                            selected_by: preset!("organ_panel.b3_preset2_selected", "false"),
                            // b3+bass replaces this registration with the bass manual
                            // below, and the nine nibbles it would draw hold stale
                            // leftovers there — showing them asserts a registration that
                            // plays nothing.
                            when: organ_is!("B3"),
                            members: &[
                                "organ_panel.b3_preset1_drawbars",
                                "organ_panel.b3_preset1_vib",
                                "organ_panel.b3_preset1_perc",
                            ],
                            groups: &[],
                        },
                        Group {
                            title: "Preset 1, bass manual",
                            selected_by: preset!("organ_panel.b3_preset2_selected", "false"),
                            // ⚠️ Two live bars, outside the nine-nibble block. The vib
                            // and percussion flags of preset 1 are in the group above,
                            // so they read as not-relevant here; whether the bass manual
                            // answers them is not established.
                            when: organ_is!("B3Bass"),
                            members: &["organ_panel.b3_bass_bar1", "organ_panel.b3_bass_bar2"],
                            groups: &[],
                        },
                        Group {
                            title: "Preset 2",
                            selected_by: preset!("organ_panel.b3_preset2_selected", "true"),
                            when: None,
                            members: &[
                                "organ_panel.b3_preset2_drawbars",
                                "organ_panel.b3_preset2_vib",
                                "organ_panel.b3_preset2_perc",
                            ],
                            groups: &[],
                        },
                    ],
                },
                Group {
                    title: "Vox",
                    selected_by: None,
                    when: organ_is!("Vox"),
                    members: &["organ_panel.vox_vib", "organ_panel.vox_preset2_selected"],
                    groups: &[
                        Group {
                            title: "Preset 1",
                            selected_by: preset!("organ_panel.vox_preset2_selected", "false"),
                            when: None,
                            members: &[
                                "organ_panel.vox_preset1_drawbars",
                                "organ_panel.vox_preset1_vib",
                            ],
                            groups: &[],
                        },
                        Group {
                            title: "Preset 2",
                            selected_by: preset!("organ_panel.vox_preset2_selected", "true"),
                            when: None,
                            members: &[
                                "organ_panel.vox_preset2_drawbars",
                                "organ_panel.vox_preset2_vib",
                            ],
                            groups: &[],
                        },
                    ],
                },
                Group {
                    title: "Farfisa",
                    selected_by: None,
                    // The registers are stored as drawbar positions and read by the
                    // instrument as on/off tabs, at a threshold of 5.
                    when: organ_is!("Farfisa"),
                    members: &[
                        "organ_panel.farfisa_vib",
                        "organ_panel.farfisa_preset2_selected",
                    ],
                    groups: &[
                        Group {
                            title: "Preset 1",
                            selected_by: preset!("organ_panel.farfisa_preset2_selected", "false"),
                            when: None,
                            members: &[
                                "organ_panel.farfisa_preset1_drawbars",
                                "organ_panel.farfisa_preset1_vib",
                            ],
                            groups: &[],
                        },
                        Group {
                            title: "Preset 2",
                            selected_by: preset!("organ_panel.farfisa_preset2_selected", "true"),
                            when: None,
                            members: &[
                                "organ_panel.farfisa_preset2_drawbars",
                                "organ_panel.farfisa_preset2_vib",
                            ],
                            groups: &[],
                        },
                    ],
                },
                Group {
                    title: "Pipe",
                    selected_by: None,
                    // No vibrato and no percussion the panel can reach: the bit the other
                    // models use for preset-1 vib is set in nearly every real program,
                    // and the vib button does not respond while pipe is selected.
                    // Confirmed on hardware.
                    when: organ_is!("Pipe"),
                    members: &["organ_panel.pipe_preset2_selected"],
                    groups: &[
                        Group {
                            title: "Preset 1",
                            selected_by: preset!("organ_panel.pipe_preset2_selected", "false"),
                            when: None,
                            members: &["organ_panel.pipe_preset1_drawbars"],
                            groups: &[],
                        },
                        Group {
                            title: "Preset 2",
                            selected_by: preset!("organ_panel.pipe_preset2_selected", "true"),
                            when: None,
                            members: &["organ_panel.pipe_preset2_drawbars"],
                            groups: &[],
                        },
                    ],
                },
            ],
        },
        Group {
            title: "Piano",
            selected_by: None,
            when: part_plays!("Piano"),
            members: &[
                "piano_panel.category",
                "piano_panel.piano_model",
                "piano_panel.acoustics",
                "piano_panel.touch",
                "piano_panel.mono",
            ],
            groups: &[Group {
                title: "Clavinet",
                selected_by: None,
                // Inferred from specimens; not confirmed on hardware.
                when: Some(Relevance {
                    any_of: &[Match {
                        field: "piano_panel.category",
                        is: &["Clavinet"],
                    }],
                }),
                members: &["piano_panel.clav_model"],
                groups: &[],
            }],
        },
        Group {
            title: "Sample",
            selected_by: None,
            when: part_plays!("Sample"),
            members: &[
                "sample_panel.number",
                "sample_panel.attack",
                "sample_panel.decay_release",
                "sample_panel.dynamics",
                "sample_panel.filter",
            ],
            groups: &[],
        },
        Group {
            title: "Effects",
            selected_by: None,
            when: None,
            // The five routing switches, which are what turns each effect on. They stay
            // out of the clusters they govern for the same reason the organ model does.
            members: &[
                "effects_panel.fx1",
                "effects_panel.fx2",
                "effects_panel.fx3",
                "effects_panel.fx4",
                "effects_panel.fx5",
            ],
            groups: &[
                Group {
                    title: "Effect 1",
                    selected_by: None,
                    when: routed!("effects_panel.fx1"),
                    members: &[
                        "effects_panel.fx1_type",
                        "effects_panel.fx1_rate",
                        "effects_panel.fx1_control",
                    ],
                    groups: &[],
                },
                Group {
                    title: "Effect 2",
                    selected_by: None,
                    when: routed!("effects_panel.fx2"),
                    members: &[
                        "effects_panel.fx2_type",
                        "effects_panel.fx2_rate",
                        "effects_panel.fx2_deep",
                    ],
                    groups: &[],
                },
                Group {
                    title: "Amp / compressor",
                    selected_by: None,
                    when: routed!("effects_panel.fx3"),
                    members: &["effects_panel.fx3_type", "effects_panel.fx3_compression"],
                    groups: &[Group {
                        title: "Rotary speaker",
                        selected_by: None,
                        // Nested, so it needs both: the amp block routed to a part *and*
                        // the rotary chosen as its model.
                        when: Some(Relevance {
                            any_of: &[Match {
                                field: "effects_panel.fx3_type",
                                is: &["Rotary"],
                            }],
                        }),
                        members: &["effects_panel.rotary_speed", "effects_panel.rotary_stop"],
                        groups: &[],
                    }],
                },
                Group {
                    title: "Delay",
                    selected_by: None,
                    when: routed!("effects_panel.fx4"),
                    members: &[
                        "effects_panel.fx4_tempo",
                        "effects_panel.fx4_feedback",
                        "effects_panel.fx4_moisture",
                        "effects_panel.fx4_ping_pong",
                    ],
                    groups: &[],
                },
                Group {
                    title: "Reverb",
                    selected_by: None,
                    when: switched_on!("effects_panel.fx5"),
                    members: &["effects_panel.fx5_type", "effects_panel.fx5_moisture"],
                    groups: &[],
                },
            ],
        },
        Group {
            title: "EQ",
            selected_by: None,
            when: None,
            members: &["effects_panel.equalizer_on"],
            groups: &[Group {
                title: "Bands",
                selected_by: None,
                when: switched_on!("effects_panel.equalizer_on"),
                members: &[
                    "effects_panel.equalizer_part",
                    "effects_panel.equalizer_bass",
                    "effects_panel.equalizer_freq",
                    "effects_panel.equalizer_freq_gain",
                    "effects_panel.equalizer_treble",
                ],
                groups: &[],
            }],
        },
    ],
};

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fields::Field;
    use crate::formats::ne5::Program;

    /// A program with `sets` applied, as the registry reads it back.
    fn program(sets: &[(&str, &str)]) -> Vec<Field> {
        let mut body = Program::default();
        for (path, value) in sets {
            body.set_field(path, value).expect(path);
        }
        body.fields()
    }

    fn group(title: &str) -> &'static Group {
        PANEL
            .walk()
            .into_iter()
            .find(|group| group.title == title)
            .unwrap_or_else(|| panic!("no group {title}"))
    }

    fn relevant(title: &str, fields: &[Field]) -> bool {
        group(title).is_relevant(fields)
    }

    /// An engine section is relevant while a part is playing it, and the pickers that
    /// bring one back are in a section that always is.
    #[test]
    fn a_section_follows_the_parts() {
        let fresh = program(&[]);
        assert!(relevant("Organ", &fresh), "a fresh program plays organ");
        assert!(!relevant("Piano", &fresh));
        assert!(!relevant("Sample", &fresh));
        for always in ["Keyboard & split", "Effects", "EQ"] {
            assert!(relevant(always, &fresh), "{always}");
        }

        let split = program(&[("center_panel.upper_part", "Piano")]);
        assert!(relevant("Piano", &split));
        assert!(relevant("Organ", &split), "the lower part still plays it");

        let neither = program(&[
            ("center_panel.lower_part", "Sample"),
            ("center_panel.upper_part", "Piano"),
        ]);
        assert!(!relevant("Organ", &neither));
        // The part pickers are in the section that never goes away.
        let keyboard = group("Keyboard & split").members;
        assert!(keyboard.contains(&"center_panel.lower_part"));
        assert!(keyboard.contains(&"center_panel.upper_part"));
    }

    /// One model's registration means anything at a time, and the model selector is not
    /// inside the cluster it selects.
    #[test]
    fn only_the_selected_organ_is_relevant() {
        let b3 = program(&[("center_panel.organ_type", "B3")]);
        assert!(relevant("B3", &b3));
        for other in ["Vox", "Farfisa", "Pipe"] {
            assert!(!relevant(other, &b3), "{other}");
        }
        assert!(group("Organ").members.contains(&"center_panel.organ_type"));

        let vox = program(&[("center_panel.organ_type", "Vox")]);
        assert!(relevant("Vox", &vox));
        assert!(!relevant("B3", &vox));

        // A selection the library cannot name matches nothing, so no model speaks for
        // the state and every registration reads as what it is.
        let unknown = program(&[("center_panel.organ_type", "unknown (6)")]);
        for model in ["B3", "Vox", "Farfisa", "Pipe"] {
            assert!(!relevant(model, &unknown), "{model}");
        }
    }

    /// b3+bass: preset 1 is the bass manual's two bars, and the nine nibbles they shadow
    /// hold stale leftovers, so that registration is not the one to draw.
    #[test]
    fn b3_bass_replaces_the_first_registration() {
        let b3 = program(&[("center_panel.organ_type", "B3")]);
        assert!(relevant("Preset 1", &b3));
        assert!(!relevant("Preset 1, bass manual", &b3));

        let bass = program(&[("center_panel.organ_type", "B3Bass")]);
        assert!(!relevant("Preset 1", &bass));
        assert!(relevant("Preset 1, bass manual", &bass));
        // Preset 2 is an ordinary B3 either way.
        assert!(relevant("Preset 2", &bass));
        assert!(
            relevant("B3", &bass),
            "the shared registration is still B3's"
        );
    }

    /// The rotary knobs need both the amp block routed to a part and the rotary chosen
    /// as its model — a conjunction, which is what nesting is for.
    #[test]
    fn a_nested_group_needs_its_parent_too() {
        let rotary = program(&[
            ("effects_panel.fx3", "Upper"),
            ("effects_panel.fx3_type", "Rotary"),
        ]);
        assert!(relevant("Amp / compressor", &rotary));
        assert!(relevant("Rotary speaker", &rotary));

        // The model alone: the nested group's own condition still holds, and the parent's
        // does not — which is the caller's cue to stop descending.
        let unrouted = program(&[("effects_panel.fx3_type", "Rotary")]);
        assert!(!relevant("Amp / compressor", &unrouted));
        assert!(relevant("Rotary speaker", &unrouted));

        let amp = program(&[
            ("effects_panel.fx3", "Upper"),
            ("effects_panel.fx3_type", "Twin"),
        ]);
        assert!(relevant("Amp / compressor", &amp));
        assert!(!relevant("Rotary speaker", &amp));
    }

    /// ⚠️ `Unknown` is how older firmware spelled *off* and presents as off, so it does
    /// not make an effect relevant.
    #[test]
    fn the_older_spelling_of_off_reads_as_off() {
        let off = program(&[("effects_panel.fx1", "Unknown")]);
        assert!(!relevant("Effect 1", &off));
        let on = program(&[("effects_panel.fx1", "Lower")]);
        assert!(relevant("Effect 1", &on));
    }
}