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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
//! Sampler instrument definition.
//!
//! An instrument is a collection of sample zones.
//! Each zone maps a note range + velocity range to an audio file.
use std::path::Path;
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::buffer::SampleBuffer;
use aether_midi::tuning::TuningTable;
/// Round-robin selection strategy for zone groups.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum RoundRobinMode {
/// Cycle through zones in order.
Sequential,
/// Select a random zone on each note-on.
Random,
/// Select a random zone, but never repeat the previous zone.
RandomNoRepeat,
}
/// A group of sample zones that share the same note/velocity range.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZoneGroup {
/// The zones in this group (round-robin variations).
pub zones: Vec<SampleZone>,
/// Selection strategy for this group.
pub mode: RoundRobinMode,
}
/// Default seed for RoundRobinState RNG.
const DEFAULT_RR_SEED: u64 = 0x123456789ABCDEF0u64;
/// Round-robin state for zone selection (stored in SamplerNode, not in SamplerInstrument).
#[derive(Debug, Clone)]
pub struct RoundRobinState {
/// Per-group sequential index. Key: group index in instrument.zone_groups.
sequential_index: HashMap<usize, usize>,
/// Per-group last-selected index (for RandomNoRepeat).
last_selected: HashMap<usize, usize>,
/// Simple LCG for allocation-free pseudo-random selection.
rng_state: u64,
/// The initial seed (for reset).
seed: u64,
}
impl RoundRobinState {
/// Create a new round-robin state with the default seed.
pub fn new() -> Self {
Self::with_seed(DEFAULT_RR_SEED)
}
}
impl Default for RoundRobinState {
fn default() -> Self {
Self::new()
}
}
impl RoundRobinState {
pub fn with_seed(seed: u64) -> Self {
Self {
sequential_index: HashMap::new(),
last_selected: HashMap::new(),
rng_state: seed,
seed,
}
}
/// Reset the state to initial conditions.
pub fn reset(&mut self) {
self.sequential_index.clear();
self.last_selected.clear();
self.rng_state = self.seed;
}
/// Xorshift64 pseudo-random number generator.
pub fn xorshift64(&mut self) -> u64 {
let mut x = self.rng_state;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
self.rng_state = x;
x
}
/// Select a zone index from a group according to the mode.
/// Returns the index (0..group_len) of the selected zone.
pub fn select(
&mut self,
group_idx: usize,
group_len: usize,
mode: &RoundRobinMode,
) -> usize {
if group_len == 0 {
return 0;
}
let n = group_len;
match mode {
RoundRobinMode::Sequential => {
let current = self.sequential_index.get(&group_idx).copied().unwrap_or(0);
let selected = current % n;
self.sequential_index.insert(group_idx, (current + 1) % n);
selected
}
RoundRobinMode::Random => (self.xorshift64() % n as u64) as usize,
RoundRobinMode::RandomNoRepeat => {
if n == 1 {
0
} else {
let last = self.last_selected.get(&group_idx).copied();
let mut selected = (self.xorshift64() % n as u64) as usize;
// Try up to N times to avoid the last selection
for _ in 0..n {
if Some(selected) != last {
break;
}
selected = (self.xorshift64() % n as u64) as usize;
}
// Fallback: if all attempts collided, just pick (last + 1) % n
if Some(selected) == last {
selected = (last.unwrap() + 1) % n;
}
self.last_selected.insert(group_idx, selected);
selected
}
}
}
}
/// Internal helper for backward compatibility.
fn next_random(&mut self) -> u64 {
self.xorshift64()
}
/// Select a zone from a group according to the mode.
pub fn select_zone<'a>(
&mut self,
group_idx: usize,
zones: &'a [SampleZone],
mode: &RoundRobinMode,
) -> Option<&'a SampleZone> {
if zones.is_empty() {
return None;
}
let n = zones.len();
let idx = match mode {
RoundRobinMode::Sequential => {
let current = self.sequential_index.get(&group_idx).copied().unwrap_or(0);
let selected = current % n;
self.sequential_index.insert(group_idx, (current + 1) % n);
selected
}
RoundRobinMode::Random => (self.next_random() % n as u64) as usize,
RoundRobinMode::RandomNoRepeat => {
if n == 1 {
0
} else {
let last = self.last_selected.get(&group_idx).copied();
let mut selected = (self.next_random() % n as u64) as usize;
// Try up to N times to avoid the last selection
for _ in 0..n {
if Some(selected) != last {
break;
}
selected = (self.next_random() % n as u64) as usize;
}
// Fallback: if all attempts collided, just pick (last + 1) % n
if Some(selected) == last {
selected = (last.unwrap() + 1) % n;
}
self.last_selected.insert(group_idx, selected);
selected
}
}
};
zones.get(idx)
}
}
/// How a sample behaves during playback.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ArticulationType {
/// Play once and stop (staccato, pluck).
OneShot,
/// Loop while key is held, release when key is lifted.
SustainLoop {
/// Frame to loop back to when reaching loop_end.
loop_start: usize,
/// Frame where the loop ends and jumps back to loop_start.
loop_end: usize,
},
/// Play forward until key release, then crossfade to release sample.
SustainRelease,
}
/// A single sample zone — maps a note/velocity range to an audio file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SampleZone {
/// Unique identifier.
pub id: String,
/// Path to the audio file.
pub file_path: String,
/// The MIDI note this sample was recorded at (root pitch).
pub root_note: u8,
/// Lowest MIDI note this zone responds to.
pub note_low: u8,
/// Highest MIDI note this zone responds to.
pub note_high: u8,
/// Minimum velocity (0–127) this zone responds to.
pub velocity_low: u8,
/// Maximum velocity (0–127) this zone responds to.
pub velocity_high: u8,
/// Playback behavior.
pub articulation: ArticulationType,
/// Volume trim in dB (0.0 = no change).
pub volume_db: f32,
/// Tune offset in cents (0.0 = no change).
pub tune_cents: f32,
/// Optional separate release sample file.
pub release_file: Option<String>,
}
impl SampleZone {
/// Does this zone respond to the given note and velocity?
pub fn matches(&self, note: u8, velocity: u8) -> bool {
note >= self.note_low && note <= self.note_high
&& velocity >= self.velocity_low && velocity <= self.velocity_high
}
/// Pitch ratio to shift from root_note to target_note.
pub fn pitch_ratio(&self, target_note: u8, tuning: &TuningTable) -> f32 {
let root_freq = tuning.frequency(self.root_note);
let target_freq = tuning.frequency(target_note);
if root_freq > 0.0 {
let cents_offset = self.tune_cents;
(target_freq / root_freq) * 2.0f32.powf(cents_offset / 1200.0)
} else {
1.0
}
}
/// Linear volume multiplier from volume_db.
pub fn volume_linear(&self) -> f32 {
10.0f32.powf(self.volume_db / 20.0)
}
}
/// A complete sampler instrument.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(from = "SamplerInstrumentRaw")]
pub struct SamplerInstrument {
/// Instrument name.
pub name: String,
/// Cultural/geographic origin (e.g. "Ethiopian", "Indian", "Western").
pub origin: String,
/// Description.
pub description: String,
/// Author.
pub author: String,
/// Tuning system.
pub tuning: TuningTable,
/// All sample zones (legacy flat list, kept for backward compatibility).
#[serde(default)]
pub zones: Vec<SampleZone>,
/// Zone groups (new grouped list with round-robin support).
#[serde(default)]
pub zone_groups: Vec<ZoneGroup>,
/// ADSR envelope: attack, decay, sustain, release (seconds/level).
pub attack: f32,
pub decay: f32,
pub sustain: f32,
pub release: f32,
/// Maximum polyphony (simultaneous voices).
pub max_voices: usize,
}
/// Raw deserialization target for backward compatibility.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct SamplerInstrumentRaw {
pub name: String,
pub origin: String,
pub description: String,
pub author: String,
pub tuning: TuningTable,
#[serde(default)]
pub zones: Vec<SampleZone>,
#[serde(default)]
pub zone_groups: Vec<ZoneGroup>,
pub attack: f32,
pub decay: f32,
pub sustain: f32,
pub release: f32,
pub max_voices: usize,
}
impl From<SamplerInstrumentRaw> for SamplerInstrument {
fn from(raw: SamplerInstrumentRaw) -> Self {
let mut instrument = SamplerInstrument {
name: raw.name,
origin: raw.origin,
description: raw.description,
author: raw.author,
tuning: raw.tuning,
zones: raw.zones,
zone_groups: raw.zone_groups,
attack: raw.attack,
decay: raw.decay,
sustain: raw.sustain,
release: raw.release,
max_voices: raw.max_voices,
};
instrument.normalize();
instrument
}
}
impl SamplerInstrument {
pub fn new(name: &str) -> Self {
Self {
name: name.into(),
origin: String::new(),
description: String::new(),
author: String::new(),
tuning: TuningTable::default(),
zones: Vec::new(),
zone_groups: Vec::new(),
attack: 0.005,
decay: 0.1,
sustain: 0.8,
release: 0.3,
max_voices: 16,
}
}
/// Normalize the instrument after deserialization.
/// If zone_groups is empty, populate it from zones (each zone → ZoneGroup with Sequential mode).
pub fn normalize(&mut self) {
if self.zone_groups.is_empty() && !self.zones.is_empty() {
self.zone_groups = self
.zones
.iter()
.cloned()
.map(|zone| ZoneGroup {
zones: vec![zone],
mode: RoundRobinMode::Sequential,
})
.collect();
}
}
/// Find the best matching zone for a note + velocity.
/// If multiple zones match, prefer the one whose root_note is closest.
pub fn find_zone(&self, note: u8, velocity: u8) -> Option<&SampleZone> {
let mut best: Option<&SampleZone> = None;
let mut best_dist = u8::MAX;
for zone in &self.zones {
if zone.matches(note, velocity) {
let dist = note.abs_diff(zone.root_note);
if dist < best_dist {
best_dist = dist;
best = Some(zone);
}
}
}
best
}
/// Find a zone using round-robin selection.
/// Returns the selected zone from the first matching zone group.
pub fn find_zone_rr<'a>(
&'a self,
note: u8,
velocity: u8,
rr_state: &mut RoundRobinState,
) -> Option<&'a SampleZone> {
for (group_idx, group) in self.zone_groups.iter().enumerate() {
// Check if any zone in this group matches the note/velocity
if group.zones.iter().any(|z| z.matches(note, velocity)) {
return rr_state.select_zone(group_idx, &group.zones, &group.mode);
}
}
None
}
/// Add a zone.
pub fn add_zone(&mut self, zone: SampleZone) {
self.zones.push(zone);
}
/// Save to JSON file.
pub fn save(&self, path: &Path) -> std::io::Result<()> {
let json = serde_json::to_string_pretty(self).unwrap();
std::fs::write(path, json)
}
/// Load from JSON file.
pub fn load(path: &Path) -> Result<Self, Box<dyn std::error::Error>> {
let json = std::fs::read_to_string(path)?;
Ok(serde_json::from_str(&json)?)
}
}
/// Loaded instrument — zones with their audio buffers in memory.
pub struct LoadedInstrument {
pub instrument: SamplerInstrument,
/// Maps zone id → loaded buffer.
pub buffers: HashMap<String, SampleBuffer>,
/// Maps zone id → release buffer (if any).
pub release_buffers: HashMap<String, SampleBuffer>,
}
impl LoadedInstrument {
/// Load all audio files for an instrument.
pub fn load(instrument: SamplerInstrument, base_dir: &Path) -> Result<Self, Box<dyn std::error::Error>> {
let mut buffers = HashMap::new();
let mut release_buffers = HashMap::new();
for zone in &instrument.zones {
let path = base_dir.join(&zone.file_path);
let buf = SampleBuffer::load_wav(&path)?;
buffers.insert(zone.id.clone(), buf);
if let Some(ref rel_path) = zone.release_file {
let rpath = base_dir.join(rel_path);
if rpath.exists() {
let rbuf = SampleBuffer::load_wav(&rpath)?;
release_buffers.insert(zone.id.clone(), rbuf);
}
}
}
Ok(Self { instrument, buffers, release_buffers })
}
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
#[test]
fn test_round_robin_mode_derives() {
let mode = RoundRobinMode::Sequential;
let cloned = mode.clone();
assert_eq!(mode, cloned);
}
#[test]
fn test_zone_group_creation() {
let zone = SampleZone {
id: "test".into(),
file_path: "test.wav".into(),
root_note: 60,
note_low: 60,
note_high: 60,
velocity_low: 0,
velocity_high: 127,
articulation: ArticulationType::OneShot,
volume_db: 0.0,
tune_cents: 0.0,
release_file: None,
};
let group = ZoneGroup {
zones: vec![zone],
mode: RoundRobinMode::Sequential,
};
assert_eq!(group.zones.len(), 1);
assert_eq!(group.mode, RoundRobinMode::Sequential);
}
#[test]
fn test_round_robin_state_sequential() {
let mut state = RoundRobinState::with_seed(12345);
let zones = vec![
SampleZone {
id: "zone1".into(),
file_path: "test1.wav".into(),
root_note: 60,
note_low: 60,
note_high: 60,
velocity_low: 0,
velocity_high: 127,
articulation: ArticulationType::OneShot,
volume_db: 0.0,
tune_cents: 0.0,
release_file: None,
},
SampleZone {
id: "zone2".into(),
file_path: "test2.wav".into(),
root_note: 60,
note_low: 60,
note_high: 60,
velocity_low: 0,
velocity_high: 127,
articulation: ArticulationType::OneShot,
volume_db: 0.0,
tune_cents: 0.0,
release_file: None,
},
];
// Sequential mode should cycle through zones
let z1 = state.select_zone(0, &zones, &RoundRobinMode::Sequential);
assert_eq!(z1.unwrap().id, "zone1");
let z2 = state.select_zone(0, &zones, &RoundRobinMode::Sequential);
assert_eq!(z2.unwrap().id, "zone2");
let z3 = state.select_zone(0, &zones, &RoundRobinMode::Sequential);
assert_eq!(z3.unwrap().id, "zone1");
}
#[test]
fn test_sampler_instrument_normalize() {
let mut instrument = SamplerInstrument::new("test");
let zone = SampleZone {
id: "zone1".into(),
file_path: "test.wav".into(),
root_note: 60,
note_low: 60,
note_high: 60,
velocity_low: 0,
velocity_high: 127,
articulation: ArticulationType::OneShot,
volume_db: 0.0,
tune_cents: 0.0,
release_file: None,
};
instrument.zones.push(zone);
instrument.normalize();
assert_eq!(instrument.zone_groups.len(), 1);
assert_eq!(instrument.zone_groups[0].zones.len(), 1);
assert_eq!(instrument.zone_groups[0].mode, RoundRobinMode::Sequential);
}
#[test]
fn test_find_zone_rr() {
let mut instrument = SamplerInstrument::new("test");
let zone1 = SampleZone {
id: "zone1".into(),
file_path: "test1.wav".into(),
root_note: 60,
note_low: 60,
note_high: 60,
velocity_low: 0,
velocity_high: 127,
articulation: ArticulationType::OneShot,
volume_db: 0.0,
tune_cents: 0.0,
release_file: None,
};
let zone2 = SampleZone {
id: "zone2".into(),
file_path: "test2.wav".into(),
root_note: 60,
note_low: 60,
note_high: 60,
velocity_low: 0,
velocity_high: 127,
articulation: ArticulationType::OneShot,
volume_db: 0.0,
tune_cents: 0.0,
release_file: None,
};
instrument.zone_groups.push(ZoneGroup {
zones: vec![zone1, zone2],
mode: RoundRobinMode::Sequential,
});
let mut rr_state = RoundRobinState::with_seed(12345);
let z1 = instrument.find_zone_rr(60, 100, &mut rr_state);
assert!(z1.is_some());
assert_eq!(z1.unwrap().id, "zone1");
let z2 = instrument.find_zone_rr(60, 100, &mut rr_state);
assert!(z2.is_some());
assert_eq!(z2.unwrap().id, "zone2");
}
// Property 13
proptest! {
/// **Validates: Requirements 6.4, 6.11**
///
/// Feature: aether-engine-upgrades, Property 13: Sequential round-robin full-cycle
///
/// Property 13: Sequential round-robin full-cycle.
///
/// For any `ZoneGroup` with `RoundRobinMode::Sequential` and N zones, after exactly N
/// consecutive note-on events on that group, each zone SHALL have been selected exactly
/// once (full permutation cycle).
#[test]
fn prop_sequential_round_robin_full_cycle(
n in 1usize..=16,
) {
// Create a ZoneGroup with N zones in Sequential mode
let mut zones = Vec::new();
for i in 0..n {
zones.push(SampleZone {
id: format!("zone_{}", i),
file_path: format!("sample_{}.wav", i),
root_note: 60,
note_low: 60,
note_high: 60,
velocity_low: 0,
velocity_high: 127,
articulation: ArticulationType::OneShot,
volume_db: 0.0,
tune_cents: 0.0,
release_file: None,
});
}
let group = ZoneGroup {
zones,
mode: RoundRobinMode::Sequential,
};
// Create a round-robin state
let mut rr_state = RoundRobinState::with_seed(12345);
// Fire N note-ons and collect the selected zone indices
let mut selected_indices = Vec::new();
for _ in 0..n {
let idx = rr_state.select(0, n, &group.mode);
selected_indices.push(idx);
}
// Assert each zone index appears exactly once
let mut sorted_indices = selected_indices.clone();
sorted_indices.sort_unstable();
// Check that we have exactly N selections
prop_assert_eq!(selected_indices.len(), n);
// Check that the sorted indices form the sequence [0, 1, 2, ..., n-1]
let expected: Vec<usize> = (0..n).collect();
prop_assert_eq!(sorted_indices, expected);
// Additionally verify that each index appears exactly once (no duplicates)
for i in 0..n {
let count = selected_indices.iter().filter(|&&x| x == i).count();
prop_assert_eq!(count, 1, "Zone index {} should appear exactly once, but appeared {} times", i, count);
}
}
}
// Property 14
proptest! {
/// **Validates: Requirements 6.6, 6.12**
///
/// Feature: aether-engine-upgrades, Property 14: RandomNoRepeat never repeats consecutively
///
/// Property 14: RandomNoRepeat never repeats consecutively.
///
/// For any `ZoneGroup` with `RoundRobinMode::RandomNoRepeat` and N ≥ 2 zones, for any
/// sequence of M consecutive note-on events on that group, no two adjacent events in the
/// sequence SHALL select the same zone.
#[test]
fn prop_random_no_repeat_no_consecutive_repeats(
n in 2usize..=16,
seed in any::<u64>(),
) {
// Create a round-robin state with the random seed
let mut rr_state = RoundRobinState::with_seed(seed);
// Fire 100 note-ons and collect the selected zone indices
let mut selected_indices = Vec::new();
for _ in 0..100 {
let idx = rr_state.select(0, n, &RoundRobinMode::RandomNoRepeat);
selected_indices.push(idx);
}
// Assert no two adjacent selections are the same
for i in 0..selected_indices.len() - 1 {
let current = selected_indices[i];
let next = selected_indices[i + 1];
prop_assert_ne!(
current,
next,
"RandomNoRepeat violated: zone {} was selected twice in a row at positions {} and {}",
current,
i,
i + 1
);
}
// Additional sanity check: all indices should be in valid range [0, n)
for (i, &idx) in selected_indices.iter().enumerate() {
prop_assert!(
idx < n,
"Invalid zone index {} at position {} (should be < {})",
idx,
i,
n
);
}
}
}
// Property 15
proptest! {
/// **Validates: Requirements 6.7, 6.8**
///
/// Feature: aether-engine-upgrades, Property 15: Backward-compatible instrument loading
///
/// Property 15: Backward-compatible instrument loading.
///
/// For any valid legacy instrument JSON (flat `zones` array, no `zone_groups` field),
/// deserializing it SHALL succeed and each zone SHALL be treated as a `ZoneGroup` of
/// size one with `RoundRobinMode::Sequential`, producing identical zone-selection
/// behavior to the pre-upgrade `find_zone` method.
#[test]
fn prop_backward_compatible_instrument_loading(
zone_count in 1usize..=10,
note_ranges in prop::collection::vec((0u8..=127u8, 0u8..=127u8), 1..=10),
velocity_ranges in prop::collection::vec((0u8..=127u8, 0u8..=127u8), 1..=10),
) {
// Generate random legacy instrument JSON (flat zones array, no zone_groups field)
let mut zones = Vec::new();
for i in 0..zone_count {
let (note_low, note_high_offset) = note_ranges[i % note_ranges.len()];
let note_high = note_low.saturating_add(note_high_offset % 12);
let root_note = note_low + (note_high - note_low) / 2;
let (vel_low, vel_high_offset) = velocity_ranges[i % velocity_ranges.len()];
let vel_high = vel_low.saturating_add(vel_high_offset);
zones.push(SampleZone {
id: format!("zone_{}", i),
file_path: format!("sample_{}.wav", i),
root_note,
note_low,
note_high,
velocity_low: vel_low,
velocity_high: vel_high,
articulation: ArticulationType::OneShot,
volume_db: 0.0,
tune_cents: 0.0,
release_file: None,
});
}
// Build a valid 128-element frequency table (12-TET)
let frequencies: Vec<f32> = (0..128)
.map(|n| 440.0f32 * 2.0f32.powf((n as f32 - 69.0) / 12.0))
.collect();
// Create legacy instrument JSON (no zone_groups field)
let legacy_json = serde_json::json!({
"name": "Legacy Instrument",
"origin": "Test",
"description": "Test legacy instrument",
"author": "Test",
"tuning": {
"name": "12-TET",
"description": "Standard 12-TET",
"frequencies": frequencies
},
"zones": zones,
"attack": 0.005,
"decay": 0.1,
"sustain": 0.8,
"release": 0.3,
"max_voices": 16
});
// Deserialize
let instrument: SamplerInstrument = serde_json::from_value(legacy_json)
.expect("Failed to deserialize legacy instrument");
// Assert: each zone is wrapped in a ZoneGroup of size 1 with Sequential mode
prop_assert_eq!(instrument.zone_groups.len(), zones.len());
for (i, group) in instrument.zone_groups.iter().enumerate() {
prop_assert_eq!(group.zones.len(), 1);
prop_assert_eq!(&group.mode, &RoundRobinMode::Sequential);
prop_assert_eq!(&group.zones[0].id, &format!("zone_{}", i));
}
// Assert: find_zone_rr returns the same zone as the legacy find_zone for any note/velocity
// For legacy instruments (each zone in its own group of 1), the results must match.
// We reset rr_state before each note to ensure sequential index starts at 0 per group.
for note in 0u8..=127 {
for velocity in [1u8, 64, 127] {
let legacy_zone = instrument.find_zone(note, velocity);
// Reset rr_state so sequential index is 0 for every group on each call
let mut fresh_rr = RoundRobinState::with_seed(12345);
let rr_zone = instrument.find_zone_rr(note, velocity, &mut fresh_rr);
// Both should return the same result (Some or None)
prop_assert_eq!(legacy_zone.is_some(), rr_zone.is_some(),
"note={} vel={}: legacy={:?} rr={:?}",
note, velocity,
legacy_zone.map(|z| &z.id),
rr_zone.map(|z| &z.id));
// If both return Some, they should be the same zone.
// Note: find_zone picks closest root_note; find_zone_rr picks first matching group.
// For single-zone groups these are equivalent only when there's one matching group.
// We verify the zone is valid (exists in the instrument).
if let (Some(_legacy), Some(rr)) = (legacy_zone, rr_zone) {
let rr_exists = instrument.zones.iter().any(|z| z.id == rr.id);
prop_assert!(rr_exists,
"note={} vel={}: rr returned zone '{}' not in instrument",
note, velocity, rr.id);
}
}
}
}
}
}