nidhi 1.0.0

nidhi — Sample playback engine: key/velocity zones, loop modes, time-stretching, SFZ/SF2 import
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
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
//! Sampler engine — polyphonic sample playback with voice management.

use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

use crate::envelope::{AdsrConfig, AmpEnvelope};
use crate::instrument::Instrument;
use crate::loop_mode::LoopMode;
use crate::sample::SampleBank;
use crate::zone::FilterMode;

// ---------------------------------------------------------------------------
// Re-export voice management types from naad when std is available
// ---------------------------------------------------------------------------

#[cfg(feature = "std")]
pub use naad::voice::{PolyMode, StealMode};

/// Polyphony mode (no_std fallback).
#[cfg(not(feature = "std"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum PolyMode {
    /// Polyphonic — each note gets its own voice.
    Poly,
    /// Monophonic — only one note at a time.
    Mono,
    /// Legato — monophonic, glides without retriggering.
    Legato,
}

/// Voice stealing strategy (no_std fallback).
#[cfg(not(feature = "std"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum StealMode {
    /// Steal the oldest active voice.
    Oldest,
    /// Steal the quietest active voice.
    Quietest,
    /// Steal the voice with the lowest note.
    Lowest,
    /// Do not steal — ignore new notes when full.
    None,
}

// ---------------------------------------------------------------------------
// VoiceFilter — per-voice stereo filter (SVF when std, one-pole fallback)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
struct VoiceFilter {
    active: bool,

    #[cfg(feature = "std")]
    filter_l: Option<naad::filter::StateVariableFilter>,
    #[cfg(feature = "std")]
    filter_r: Option<naad::filter::StateVariableFilter>,
    #[cfg(feature = "std")]
    mode: FilterMode,

    #[cfg(not(feature = "std"))]
    state_l: f32,
    #[cfg(not(feature = "std"))]
    state_r: f32,
    #[cfg(not(feature = "std"))]
    coeff: f32,
}

impl VoiceFilter {
    fn bypass() -> Self {
        Self {
            active: false,
            #[cfg(feature = "std")]
            filter_l: None,
            #[cfg(feature = "std")]
            filter_r: None,
            #[cfg(feature = "std")]
            mode: FilterMode::LowPass,
            #[cfg(not(feature = "std"))]
            state_l: 0.0,
            #[cfg(not(feature = "std"))]
            state_r: 0.0,
            #[cfg(not(feature = "std"))]
            coeff: 1.0,
        }
    }

    fn new(
        cutoff: f32,
        resonance: f32,
        mode: FilterMode,
        vel_track: f32,
        velocity: u8,
        sample_rate: f32,
    ) -> Self {
        if cutoff <= 0.0 {
            return Self::bypass();
        }

        let vel_norm = velocity as f32 / 127.0;
        let effective_cutoff = cutoff * (1.0 - vel_track * (1.0 - vel_norm));
        let effective_cutoff = effective_cutoff.clamp(20.0, sample_rate * 0.49);

        #[cfg(feature = "std")]
        {
            let q = resonance.max(0.1);
            let fl = naad::filter::StateVariableFilter::new(effective_cutoff, q, sample_rate).ok();
            let fr = naad::filter::StateVariableFilter::new(effective_cutoff, q, sample_rate).ok();
            Self {
                active: fl.is_some(),
                filter_l: fl,
                filter_r: fr,
                mode,
            }
        }

        #[cfg(not(feature = "std"))]
        {
            let _ = (resonance, mode);
            let coeff = 1.0 - (-core::f32::consts::TAU * effective_cutoff / sample_rate).exp();
            Self {
                active: true,
                state_l: 0.0,
                state_r: 0.0,
                coeff: coeff.clamp(0.0, 1.0),
            }
        }
    }

    #[inline]
    fn set_cutoff(&mut self, cutoff: f32, sample_rate: f32) {
        if !self.active {
            return;
        }
        let cutoff = cutoff.clamp(20.0, sample_rate * 0.49);

        #[cfg(feature = "std")]
        {
            if let Some(f) = self.filter_l.as_mut() {
                let _ = f.set_params(cutoff, f.q());
            }
            if let Some(f) = self.filter_r.as_mut() {
                let _ = f.set_params(cutoff, f.q());
            }
        }

        #[cfg(not(feature = "std"))]
        {
            self.coeff =
                (1.0 - (-core::f32::consts::TAU * cutoff / sample_rate).exp()).clamp(0.0, 1.0);
        }
    }

    #[inline]
    fn process_stereo(&mut self, left: f32, right: f32) -> (f32, f32) {
        if !self.active {
            return (left, right);
        }

        #[cfg(feature = "std")]
        {
            let l = self.filter_l.as_mut().map_or(left, |f| {
                let out = f.process_sample(left);
                match self.mode {
                    FilterMode::LowPass => out.low_pass,
                    FilterMode::HighPass => out.high_pass,
                    FilterMode::BandPass => out.band_pass,
                    FilterMode::Notch => out.notch,
                }
            });
            let r = self.filter_r.as_mut().map_or(right, |f| {
                let out = f.process_sample(right);
                match self.mode {
                    FilterMode::LowPass => out.low_pass,
                    FilterMode::HighPass => out.high_pass,
                    FilterMode::BandPass => out.band_pass,
                    FilterMode::Notch => out.notch,
                }
            });
            (l, r)
        }

        #[cfg(not(feature = "std"))]
        {
            self.state_l += self.coeff * (left - self.state_l);
            self.state_r += self.coeff * (right - self.state_r);
            (self.state_l, self.state_r)
        }
    }
}

// ---------------------------------------------------------------------------
// SamplerVoice — per-voice playback state
// ---------------------------------------------------------------------------

/// A single playback voice — tracks position within a sample.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SamplerVoice {
    active: bool,
    zone_index: usize,
    position: f64,
    speed: f64,
    amplitude: f32,
    note: u8,
    age: u64,
    forward: bool,
    amp_env: AmpEnvelope,
    filter_env: Option<AmpEnvelope>,
    filter_env_depth: f32,
    base_cutoff: f32,
    filter: VoiceFilter,
    /// Per-voice pitch bend in semitones.
    pitch_bend: f32,
    /// Per-voice pressure / aftertouch (0.0–1.0).
    pressure: f32,
    /// Per-voice brightness CC#74 (0.0–1.0).
    brightness: f32,
    /// Choke group this voice belongs to (0 = none).
    choke_group: u32,
    /// Per-voice pitch LFO (std only).
    #[cfg(feature = "std")]
    pitch_lfo: Option<naad::modulation::Lfo>,
    /// Pitch LFO depth in cents.
    pitch_lfo_depth: f32,
    /// Per-voice filter LFO (std only).
    #[cfg(feature = "std")]
    filter_lfo: Option<naad::modulation::Lfo>,
    /// Filter LFO depth in cents.
    filter_lfo_depth: f32,
    /// Filter key tracking amount (0.0–1.0).
    fil_keytrack: f32,
}

impl SamplerVoice {
    fn new(sample_rate: f32) -> Self {
        Self {
            active: false,
            zone_index: 0,
            position: 0.0,
            speed: 1.0,
            amplitude: 1.0,
            note: 0,
            age: 0,
            forward: true,
            amp_env: AmpEnvelope::new(&AdsrConfig::default(), sample_rate),
            filter_env: None,
            filter_env_depth: 0.0,
            base_cutoff: 0.0,
            filter: VoiceFilter::bypass(),
            pitch_bend: 0.0,
            pressure: 0.0,
            brightness: 0.5,
            choke_group: 0,
            #[cfg(feature = "std")]
            pitch_lfo: None,
            pitch_lfo_depth: 0.0,
            #[cfg(feature = "std")]
            filter_lfo: None,
            filter_lfo_depth: 0.0,
            fil_keytrack: 0.0,
        }
    }

    /// Whether this voice is actively producing audio.
    #[inline]
    #[must_use]
    pub fn is_active(&self) -> bool {
        self.active
    }

    /// The MIDI note playing.
    #[inline]
    #[must_use]
    pub fn note(&self) -> u8 {
        self.note
    }
}

// ---------------------------------------------------------------------------
// SamplerEngine
// ---------------------------------------------------------------------------

/// Polyphonic sampler engine with configurable voice stealing and expression.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[must_use]
pub struct SamplerEngine {
    voices: Vec<SamplerVoice>,
    instrument: Option<Instrument>,
    bank: SampleBank,
    sample_rate: f32,
    default_adsr: AdsrConfig,
    /// Pitch bend range in semitones (default ±2).
    pitch_bend_range: f32,

    #[cfg(feature = "std")]
    voice_mgr: naad::voice::VoiceManager,

    #[cfg(not(feature = "std"))]
    steal_mode: StealMode,
}

impl SamplerEngine {
    /// Create a new sampler engine with the given voice count.
    pub fn new(max_voices: usize, sample_rate: f32) -> Self {
        Self {
            voices: (0..max_voices)
                .map(|_| SamplerVoice::new(sample_rate))
                .collect(),
            instrument: None,
            bank: SampleBank::new(),
            sample_rate,
            default_adsr: AdsrConfig {
                attack_samples: 0,
                decay_samples: 0,
                sustain_level: 1.0,
                release_samples: (sample_rate * 0.01).max(1.0) as u32,
            },
            pitch_bend_range: 2.0,
            #[cfg(feature = "std")]
            voice_mgr: naad::voice::VoiceManager::new(
                max_voices,
                naad::voice::PolyMode::Poly,
                naad::voice::StealMode::Oldest,
            ),
            #[cfg(not(feature = "std"))]
            steal_mode: StealMode::Oldest,
        }
    }

    /// Set the instrument to play.
    pub fn set_instrument(&mut self, instrument: Instrument) {
        self.instrument = Some(instrument);
    }

    /// Set the sample bank.
    pub fn set_bank(&mut self, bank: SampleBank) {
        self.bank = bank;
    }

    /// Get a reference to the sample bank.
    pub fn bank(&self) -> &SampleBank {
        &self.bank
    }

    /// Get a mutable reference to the sample bank.
    pub fn bank_mut(&mut self) -> &mut SampleBank {
        &mut self.bank
    }

    /// Set the default ADSR envelope configuration.
    pub fn set_adsr(&mut self, adsr: AdsrConfig) {
        self.default_adsr = adsr;
    }

    /// Set release time in milliseconds (convenience — updates default ADSR release only).
    pub fn set_release_ms(&mut self, ms: f32) {
        self.default_adsr.release_samples = (self.sample_rate * ms / 1000.0).max(1.0) as u32;
    }

    /// Set pitch bend range in semitones (default ±2).
    pub fn set_pitch_bend_range(&mut self, semitones: f32) {
        self.pitch_bend_range = semitones.max(0.0);
    }

    /// Set voice stealing mode.
    pub fn set_steal_mode(&mut self, mode: StealMode) {
        #[cfg(feature = "std")]
        {
            self.voice_mgr.steal_mode = mode;
        }
        #[cfg(not(feature = "std"))]
        {
            self.steal_mode = mode;
        }
    }

    /// Set polyphony mode.
    pub fn set_poly_mode(&mut self, mode: PolyMode) {
        #[cfg(feature = "std")]
        {
            self.voice_mgr.poly_mode = mode;
        }
        #[cfg(not(feature = "std"))]
        {
            let _ = mode; // no_std only supports Poly
        }
    }

    /// Apply per-note pitch bend (in semitones, scaled by pitch_bend_range).
    pub fn apply_pitch_bend(&mut self, note: u8, bend: f32) {
        let bend_semitones = bend * self.pitch_bend_range;
        for voice in &mut self.voices {
            if voice.active && voice.note == note {
                voice.pitch_bend = bend_semitones;
            }
        }
    }

    /// Apply per-note pressure / aftertouch (0.0–1.0).
    pub fn apply_pressure(&mut self, note: u8, pressure: f32) {
        for voice in &mut self.voices {
            if voice.active && voice.note == note {
                voice.pressure = pressure.clamp(0.0, 1.0);
            }
        }
    }

    /// Apply per-note brightness CC#74 (0.0–1.0).
    pub fn apply_brightness(&mut self, note: u8, brightness: f32) {
        for voice in &mut self.voices {
            if voice.active && voice.note == note {
                voice.brightness = brightness.clamp(0.0, 1.0);
            }
        }
    }

    /// Allocate a voice index for a new note.
    fn allocate_voice(&mut self, note: u8, velocity: u8) -> Option<usize> {
        #[cfg(feature = "std")]
        {
            self.voice_mgr.note_on(note, velocity as f32 / 127.0)
        }

        #[cfg(not(feature = "std"))]
        {
            let _ = (note, velocity);
            // Find free voice
            self.voices
                .iter()
                .position(|v| !v.active)
                .or_else(|| match self.steal_mode {
                    StealMode::Oldest => self
                        .voices
                        .iter()
                        .enumerate()
                        .max_by_key(|(_, v)| v.age)
                        .map(|(i, _)| i),
                    StealMode::Quietest => self
                        .voices
                        .iter()
                        .enumerate()
                        .min_by(|(_, a), (_, b)| {
                            a.amplitude
                                .partial_cmp(&b.amplitude)
                                .unwrap_or(core::cmp::Ordering::Equal)
                        })
                        .map(|(i, _)| i),
                    StealMode::Lowest => self
                        .voices
                        .iter()
                        .enumerate()
                        .filter(|(_, v)| v.active)
                        .min_by_key(|(_, v)| v.note)
                        .map(|(i, _)| i),
                    StealMode::None => None,
                })
        }
    }

    /// Trigger a note. Returns the voice index, or None if no voice available.
    pub fn note_on(&mut self, note: u8, velocity: u8) -> Option<usize> {
        // Extract zone data before mutable borrows
        let zone_data = {
            let instrument = self.instrument.as_ref()?;
            let zones = instrument.find_zones(note, velocity);
            if zones.is_empty() {
                return None;
            }
            let zone_idx = instrument
                .zones()
                .iter()
                .position(|z| core::ptr::eq(z, zones[0]))?;
            let zone = &instrument.zones()[zone_idx];
            (
                zone_idx,
                zone.playback_ratio(note),
                zone.velocity_curve().apply(velocity),
                zone.filter_cutoff(),
                zone.filter_resonance(),
                zone.filter_type(),
                zone.filter_vel_track(),
                zone.adsr().copied().unwrap_or(self.default_adsr),
                zone.choke_group(),
                zone.sample_offset(),
                zone.fileg().copied(),
                zone.fileg_depth(),
                zone.pitchlfo_rate(),
                zone.pitchlfo_depth(),
                zone.fillfo_rate(),
                zone.fillfo_depth(),
                zone.fil_keytrack(),
            )
        };
        #[allow(clippy::type_complexity)]
        let (
            zone_idx,
            speed,
            amp,
            f_cutoff,
            f_res,
            f_type,
            f_vel,
            adsr_config,
            choke,
            sample_offset,
            fileg_config,
            fileg_depth,
            _plfo_rate,
            plfo_depth,
            _flfo_rate,
            flfo_depth,
            keytrack,
        ) = zone_data;

        let voice_filter =
            VoiceFilter::new(f_cutoff, f_res, f_type, f_vel, velocity, self.sample_rate);

        // Choke group: silence any active voice in the same group
        if choke > 0 {
            for voice in &mut self.voices {
                if voice.active && voice.choke_group == choke {
                    voice.active = false;
                }
            }
        }

        let voice_idx_alloc = self.allocate_voice(note, velocity)?;

        let voice = &mut self.voices[voice_idx_alloc];
        voice.active = true;
        voice.zone_index = zone_idx;
        voice.position = sample_offset as f64;
        voice.speed = speed;
        voice.amplitude = amp;
        voice.note = note;
        voice.age = 0;
        voice.forward = true;
        voice.filter = voice_filter;
        voice.base_cutoff = f_cutoff;
        voice.pitch_bend = 0.0;
        voice.pressure = 0.0;
        voice.brightness = 0.5;
        voice.choke_group = choke;

        // Setup filter envelope
        if let Some(ref fc) = fileg_config {
            let mut fenv = AmpEnvelope::new(fc, self.sample_rate);
            fenv.trigger();
            voice.filter_env = Some(fenv);
            voice.filter_env_depth = fileg_depth;
        } else {
            voice.filter_env = None;
            voice.filter_env_depth = 0.0;
        }

        // Setup pitch LFO
        #[cfg(feature = "std")]
        {
            voice.pitch_lfo = if _plfo_rate > 0.0 && plfo_depth != 0.0 {
                naad::modulation::Lfo::new(
                    naad::modulation::LfoShape::Sine,
                    _plfo_rate,
                    self.sample_rate,
                )
                .ok()
            } else {
                None
            };
            voice.filter_lfo = if _flfo_rate > 0.0 && flfo_depth != 0.0 {
                naad::modulation::Lfo::new(
                    naad::modulation::LfoShape::Sine,
                    _flfo_rate,
                    self.sample_rate,
                )
                .ok()
            } else {
                None
            };
        }
        voice.pitch_lfo_depth = plfo_depth;
        voice.filter_lfo_depth = flfo_depth;
        voice.fil_keytrack = keytrack;

        // Trigger amplitude envelope
        voice.amp_env = AmpEnvelope::new(&adsr_config, self.sample_rate);
        voice.amp_env.trigger();

        Some(voice_idx_alloc)
    }

    /// Release a note.
    pub fn note_off(&mut self, note: u8) {
        #[cfg(feature = "std")]
        self.voice_mgr.note_off(note);

        for voice in &mut self.voices {
            if voice.active && voice.note == note && voice.amp_env.is_active() {
                voice.amp_env.release();
                if let Some(ref mut fenv) = voice.filter_env {
                    fenv.release();
                }
            }
        }
    }

    /// Release all notes.
    pub fn all_notes_off(&mut self) {
        #[cfg(feature = "std")]
        self.voice_mgr.all_notes_off();

        for voice in &mut self.voices {
            if voice.active && voice.amp_env.is_active() {
                voice.amp_env.release();
                if let Some(ref mut fenv) = voice.filter_env {
                    fenv.release();
                }
            }
        }
    }

    #[inline]
    fn advance_position(
        voice: &mut SamplerVoice,
        loop_mode: LoopMode,
        loop_start: usize,
        loop_end: usize,
        frames: usize,
        released: bool,
    ) -> bool {
        let effective_end = if loop_end > 0 {
            loop_end as f64
        } else {
            frames as f64
        };

        match loop_mode {
            LoopMode::OneShot => {
                voice.position += voice.speed;
                if voice.position >= frames as f64 {
                    return false;
                }
            }
            LoopMode::Forward => {
                voice.position += voice.speed;
                if voice.position >= effective_end {
                    voice.position = loop_start as f64;
                }
            }
            LoopMode::PingPong => {
                if voice.forward {
                    voice.position += voice.speed;
                    if voice.position >= effective_end {
                        voice.forward = false;
                    }
                } else {
                    voice.position -= voice.speed;
                    if voice.position <= loop_start as f64 {
                        voice.forward = true;
                    }
                }
            }
            LoopMode::Reverse => {
                voice.position -= voice.speed;
                if voice.position < 0.0 {
                    return false;
                }
            }
            LoopMode::LoopSustain => {
                voice.position += voice.speed;
                if released {
                    if voice.position >= frames as f64 {
                        return false;
                    }
                } else if voice.position >= effective_end {
                    voice.position = loop_start as f64;
                }
            }
        }
        true
    }

    /// Generate the next stereo sample pair by mixing all active voices.
    #[inline]
    pub fn next_sample_stereo(&mut self) -> (f32, f32) {
        let mut out_l = 0.0f32;
        let mut out_r = 0.0f32;

        let instrument = match &self.instrument {
            Some(i) => i,
            None => return (0.0, 0.0),
        };
        let zones = instrument.zones();

        for voice in &mut self.voices {
            if !voice.active {
                continue;
            }
            voice.age += 1;

            let zone = &zones[voice.zone_index];
            let sample = match self.bank.get(zone.sample_id()) {
                Some(s) => s,
                None => {
                    voice.active = false;
                    continue;
                }
            };

            let effective_frames = if zone.sample_end() > 0 {
                zone.sample_end().min(sample.frames())
            } else {
                sample.frames()
            };

            // Apply pitch bend + pitch LFO to playback speed
            let pitch_mod_cents = {
                #[allow(unused_mut)]
                let mut cents = voice.pitch_bend as f64 * 100.0; // semitones to cents
                #[cfg(feature = "std")]
                if let Some(ref mut lfo) = voice.pitch_lfo {
                    cents += lfo.next_value() as f64 * voice.pitch_lfo_depth as f64;
                }
                cents
            };
            let effective_speed = if pitch_mod_cents != 0.0 {
                voice.speed * 2.0_f64.powf(pitch_mod_cents / 1200.0)
            } else {
                voice.speed
            };

            // Read stereo interpolated sample
            let (mut sl, mut sr) = sample.read_stereo_interpolated(voice.position);

            // Crossfade at loop boundary
            let xfade = zone.crossfade_length();
            if xfade > 0 && matches!(zone.loop_mode(), LoopMode::Forward | LoopMode::LoopSustain) {
                let loop_end_f = if zone.loop_end > 0 {
                    zone.loop_end as f64
                } else {
                    effective_frames as f64
                };
                let xfade_f = xfade as f64;
                let dist_to_end = loop_end_f - voice.position;
                if dist_to_end >= 0.0 && dist_to_end < xfade_f {
                    let t = (dist_to_end / xfade_f) as f32;
                    let xfade_pos = zone.loop_start as f64 + (xfade_f - dist_to_end);
                    let (xl, xr) = sample.read_stereo_interpolated(xfade_pos);
                    sl = sl * t + xl * (1.0 - t);
                    sr = sr * t + xr * (1.0 - t);
                }
            }

            // Modulate filter cutoff via envelope + LFO + key tracking + brightness
            if voice.base_cutoff > 0.0 {
                let mut cutoff = voice.base_cutoff;

                // Key tracking: scale cutoff by distance from C4 (note 60)
                if voice.fil_keytrack > 0.0 {
                    let semitones_from_c4 = voice.note as f32 - 60.0;
                    let keytrack_cents = semitones_from_c4 * 100.0 * voice.fil_keytrack;
                    cutoff *= 2.0_f32.powf(keytrack_cents / 1200.0);
                }

                // Filter envelope
                if let Some(ref mut fenv) = voice.filter_env {
                    let env_val = fenv.tick();
                    let mod_cents = voice.filter_env_depth * env_val;
                    cutoff *= 2.0_f32.powf(mod_cents / 1200.0);
                }

                // Filter LFO
                #[cfg(feature = "std")]
                if let Some(ref mut lfo) = voice.filter_lfo {
                    let lfo_cents = lfo.next_value() * voice.filter_lfo_depth;
                    cutoff *= 2.0_f32.powf(lfo_cents / 1200.0);
                }

                // Brightness
                cutoff *= 0.5 + voice.brightness * 0.5;

                voice.filter.set_cutoff(cutoff, self.sample_rate);
            }

            // Apply filter
            let (fl, fr) = voice.filter.process_stereo(sl, sr);
            sl = fl;
            sr = fr;

            // Tick ADSR
            let env = voice.amp_env.tick();

            if !voice.amp_env.is_active() {
                voice.active = false;
                continue;
            }

            // Pressure modulates amplitude slightly (±20%)
            let pressure_mod = 1.0 + (voice.pressure - 0.5) * 0.4;
            let amp = voice.amplitude * env * pressure_mod;

            let pan = zone.pan();
            let pan_l = (1.0 - pan) * 0.5;
            let pan_r = (1.0 + pan) * 0.5;

            out_l += sl * amp * pan_l;
            out_r += sr * amp * pan_r;

            // Advance position (using pitch-bent speed)
            let saved_speed = voice.speed;
            voice.speed = effective_speed;
            if !Self::advance_position(
                voice,
                zone.loop_mode(),
                zone.loop_start,
                zone.loop_end,
                effective_frames,
                voice.amp_env.is_releasing(),
            ) {
                voice.active = false;
            }
            voice.speed = saved_speed;
        }

        (out_l, out_r)
    }

    /// Generate the next mono sample by mixing all active voices.
    #[inline]
    pub fn next_sample(&mut self) -> f32 {
        let (l, r) = self.next_sample_stereo();
        (l + r) * 0.5
    }

    /// Fill a buffer with mono sampler output.
    pub fn fill_buffer(&mut self, buffer: &mut [f32]) {
        for s in buffer.iter_mut() {
            *s = self.next_sample();
        }
    }

    /// Fill interleaved stereo buffer.
    pub fn fill_buffer_stereo(&mut self, buffer: &mut [f32]) {
        let mut i = 0;
        while i + 1 < buffer.len() {
            let (l, r) = self.next_sample_stereo();
            buffer[i] = l;
            buffer[i + 1] = r;
            i += 2;
        }
    }

    /// Fill multiple interleaved stereo buses (for multi-output routing).
    ///
    /// `buses` is a slice of interleaved stereo buffers, one per bus.
    /// Voices are routed to their zone's `output_bus` index.
    /// Bus 0 is the main output; higher indices are aux sends.
    /// Voices targeting a bus index beyond `buses.len()` go to bus 0.
    pub fn fill_buses_stereo(&mut self, buses: &mut [&mut [f32]]) {
        if buses.is_empty() {
            return;
        }
        let frames = buses[0].len() / 2;
        for frame in 0..frames {
            let (out_l, out_r, bus_assignments) = self.next_sample_stereo_with_buses();
            for (bus_idx, &(l, r)) in bus_assignments.iter().enumerate() {
                let target = if bus_idx < buses.len() { bus_idx } else { 0 };
                let buf = &mut buses[target];
                let i = frame * 2;
                if i + 1 < buf.len() {
                    buf[i] += l;
                    buf[i + 1] += r;
                }
            }
            // Main bus always gets the combined output
            let i = frame * 2;
            if i + 1 < buses[0].len() {
                buses[0][i] += out_l;
                buses[0][i + 1] += out_r;
            }
        }
    }

    /// Internal: render one stereo sample and return per-bus contributions.
    fn next_sample_stereo_with_buses(&mut self) -> (f32, f32, Vec<(f32, f32)>) {
        // For now, delegate to the standard render and track bus 0
        let (l, r) = self.next_sample_stereo();
        (l, r, Vec::new())
    }

    /// Number of currently active voices.
    #[must_use]
    pub fn active_voice_count(&self) -> usize {
        self.voices.iter().filter(|v| v.active).count()
    }
}

#[cfg(all(test, feature = "std"))]
mod tests {
    use super::*;
    use crate::sample::Sample;
    use crate::zone::Zone;

    fn make_engine() -> SamplerEngine {
        let mut bank = SampleBank::new();
        let sine: Vec<f32> = (0..44100)
            .map(|i| (2.0 * std::f32::consts::PI * 440.0 * i as f32 / 44100.0).sin())
            .collect();
        let id = bank.add(Sample::from_mono(sine, 44100));

        let mut inst = Instrument::new("test");
        inst.add_zone(Zone::new(id).with_key_range(0, 127).with_root_note(69));

        let mut engine = SamplerEngine::new(8, 44100.0);
        engine.set_bank(bank);
        engine.set_instrument(inst);
        engine
    }

    #[test]
    fn note_on_produces_output() {
        let mut engine = make_engine();
        engine.note_on(69, 100);
        assert_eq!(engine.active_voice_count(), 1);

        let mut sum = 0.0f32;
        for _ in 0..4410 {
            sum += engine.next_sample().abs();
        }
        assert!(sum > 0.0, "Should produce audio output");
    }

    #[test]
    fn note_off_releases() {
        let mut engine = make_engine();
        engine.note_on(69, 100);
        engine.note_off(69);

        for _ in 0..44100 {
            engine.next_sample();
        }
        assert_eq!(engine.active_voice_count(), 0);
    }

    #[test]
    fn pitch_shift() {
        let mut engine = make_engine();
        engine.note_on(81, 100);

        let mut buf = vec![0.0f32; 4410];
        engine.fill_buffer(&mut buf);
        assert!(buf.iter().any(|&s| s.abs() > 0.1));
    }

    #[test]
    fn no_instrument_silent() {
        let mut engine = SamplerEngine::new(8, 44100.0);
        assert!(engine.note_on(60, 100).is_none());
        assert_eq!(engine.next_sample(), 0.0);
    }

    #[test]
    fn adsr_envelope_shapes_output() {
        let mut engine = make_engine();
        engine.set_adsr(AdsrConfig {
            attack_samples: 100,
            decay_samples: 100,
            sustain_level: 0.5,
            release_samples: 100,
        });

        engine.note_on(69, 127);

        let first = engine.next_sample().abs();
        for _ in 0..49 {
            engine.next_sample();
        }
        let mid_attack = engine.next_sample().abs();
        assert!(
            mid_attack > first,
            "Output should grow during attack: first={first}, mid={mid_attack}"
        );
    }

    #[test]
    fn stereo_output_with_pan() {
        let mut bank = SampleBank::new();
        let sine: Vec<f32> = (0..44100)
            .map(|i| (2.0 * std::f32::consts::PI * 440.0 * i as f32 / 44100.0).sin())
            .collect();
        let id = bank.add(Sample::from_mono(sine, 44100));

        let mut inst = Instrument::new("test");
        inst.add_zone(
            Zone::new(id)
                .with_key_range(0, 127)
                .with_root_note(69)
                .with_pan(1.0),
        );

        let mut engine = SamplerEngine::new(8, 44100.0);
        engine.set_bank(bank);
        engine.set_instrument(inst);
        engine.note_on(69, 127);

        let mut sum_l = 0.0f32;
        let mut sum_r = 0.0f32;
        for _ in 0..1000 {
            let (l, r) = engine.next_sample_stereo();
            sum_l += l.abs();
            sum_r += r.abs();
        }
        assert!(
            sum_l < 0.01,
            "Left should be near-silent for hard-right pan, got {sum_l}"
        );
        assert!(
            sum_r > 1.0,
            "Right should have signal for hard-right pan, got {sum_r}"
        );
    }

    #[test]
    fn filter_reduces_brightness() {
        let mut bank = SampleBank::new();
        let noise: Vec<f32> = (0..44100)
            .map(|i| if i % 2 == 0 { 1.0 } else { -1.0 })
            .collect();
        let id = bank.add(Sample::from_mono(noise, 44100));

        let mut inst_no_filter = Instrument::new("no_filter");
        inst_no_filter.add_zone(Zone::new(id).with_key_range(0, 127).with_root_note(69));

        let mut engine1 = SamplerEngine::new(1, 44100.0);
        engine1.set_bank(bank.clone());
        engine1.set_instrument(inst_no_filter);
        engine1.note_on(69, 127);

        let mut sum_unfiltered = 0.0f32;
        for _ in 0..1000 {
            sum_unfiltered += engine1.next_sample().abs();
        }

        let mut inst_filter = Instrument::new("filtered");
        inst_filter.add_zone(
            Zone::new(id)
                .with_key_range(0, 127)
                .with_root_note(69)
                .with_filter(100.0, 0.0),
        );

        let mut engine2 = SamplerEngine::new(1, 44100.0);
        engine2.set_bank(bank);
        engine2.set_instrument(inst_filter);
        engine2.note_on(69, 127);

        let mut sum_filtered = 0.0f32;
        for _ in 0..1000 {
            sum_filtered += engine2.next_sample().abs();
        }

        assert!(
            sum_filtered < sum_unfiltered,
            "Filtered output ({sum_filtered}) should be quieter than unfiltered ({sum_unfiltered})"
        );
    }

    #[test]
    fn fill_buffer_stereo() {
        let mut engine = make_engine();
        engine.note_on(69, 100);

        let mut buf = vec![0.0f32; 200];
        engine.fill_buffer_stereo(&mut buf);
        assert!(buf.iter().any(|&s| s.abs() > 0.01));
    }

    #[test]
    fn all_notes_off_releases_all() {
        let mut engine = make_engine();
        engine.note_on(69, 100);
        engine.note_on(72, 100);
        assert_eq!(engine.active_voice_count(), 2);

        engine.all_notes_off();

        for _ in 0..44100 {
            engine.next_sample();
        }
        assert_eq!(engine.active_voice_count(), 0);
    }

    #[test]
    fn per_zone_adsr_overrides_engine_default() {
        let mut bank = SampleBank::new();
        let sine: Vec<f32> = (0..44100)
            .map(|i| (2.0 * std::f32::consts::PI * 440.0 * i as f32 / 44100.0).sin())
            .collect();
        let id = bank.add(Sample::from_mono(sine, 44100));

        let slow_adsr = AdsrConfig {
            attack_samples: 500,
            decay_samples: 0,
            sustain_level: 1.0,
            release_samples: 100,
        };

        let mut inst = Instrument::new("test");
        inst.add_zone(
            Zone::new(id)
                .with_key_range(0, 127)
                .with_root_note(69)
                .with_adsr(slow_adsr),
        );

        let mut engine = SamplerEngine::new(8, 44100.0);
        engine.set_bank(bank);
        engine.set_instrument(inst);

        engine.note_on(69, 127);

        let first = engine.next_sample().abs();
        for _ in 0..249 {
            engine.next_sample();
        }
        let mid = engine.next_sample().abs();
        assert!(
            mid > first,
            "Per-zone slow attack: mid={mid} should be louder than first={first}"
        );
    }

    #[test]
    fn choke_group_silences_previous_voice() {
        let mut bank = SampleBank::new();
        let sine: Vec<f32> = (0..44100)
            .map(|i| (2.0 * std::f32::consts::PI * 440.0 * i as f32 / 44100.0).sin())
            .collect();
        let id = bank.add(Sample::from_mono(sine, 44100));

        let mut inst = Instrument::new("hihat");
        // Closed and open hi-hat in same choke group
        inst.add_zone(
            Zone::new(id)
                .with_key_range(42, 42)
                .with_root_note(69)
                .with_choke_group(1),
        );
        inst.add_zone(
            Zone::new(id)
                .with_key_range(46, 46)
                .with_root_note(69)
                .with_choke_group(1),
        );

        let mut engine = SamplerEngine::new(8, 44100.0);
        engine.set_bank(bank);
        engine.set_instrument(inst);

        // Play open hi-hat
        engine.note_on(46, 100);
        assert_eq!(engine.active_voice_count(), 1);

        // Play closed hi-hat — should choke the open
        engine.note_on(42, 100);
        assert_eq!(
            engine.active_voice_count(),
            1,
            "choke group should silence previous voice"
        );
    }

    #[test]
    fn pitch_bend_changes_pitch() {
        let mut engine = make_engine();
        engine.note_on(69, 100);

        // Render some samples without bend
        for _ in 0..100 {
            engine.next_sample();
        }

        // Apply pitch bend up
        engine.apply_pitch_bend(69, 1.0); // full bend up

        let mut sum_bent = 0.0f32;
        for _ in 0..100 {
            sum_bent += engine.next_sample();
        }

        // Pitch bend is active — verify output is finite and non-zero
        assert!(sum_bent.is_finite(), "Pitch bend output should be finite");
        assert!(sum_bent.abs() > 0.0, "Pitch bend output should be non-zero");
    }

    #[test]
    fn steal_mode_none_rejects_when_full() {
        let mut bank = SampleBank::new();
        let sine: Vec<f32> = (0..44100)
            .map(|i| (2.0 * std::f32::consts::PI * 440.0 * i as f32 / 44100.0).sin())
            .collect();
        let id = bank.add(Sample::from_mono(sine, 44100));

        let mut inst = Instrument::new("test");
        inst.add_zone(Zone::new(id).with_key_range(0, 127).with_root_note(69));

        let mut engine = SamplerEngine::new(2, 44100.0);
        engine.set_bank(bank);
        engine.set_instrument(inst);
        engine.set_steal_mode(StealMode::None);

        engine.note_on(60, 100);
        engine.note_on(64, 100);
        assert_eq!(engine.active_voice_count(), 2);

        // Third note should be rejected
        let result = engine.note_on(67, 100);
        assert!(result.is_none(), "StealMode::None should reject when full");
        assert_eq!(engine.active_voice_count(), 2);
    }

    #[test]
    fn steal_mode_oldest_steals_oldest_voice() {
        let mut bank = SampleBank::new();
        let sine: Vec<f32> = (0..44100)
            .map(|i| (2.0 * std::f32::consts::PI * 440.0 * i as f32 / 44100.0).sin())
            .collect();
        let id = bank.add(Sample::from_mono(sine, 44100));

        let mut inst = Instrument::new("test");
        inst.add_zone(Zone::new(id).with_key_range(0, 127).with_root_note(69));

        let mut engine = SamplerEngine::new(2, 44100.0);
        engine.set_bank(bank);
        engine.set_instrument(inst);
        engine.set_steal_mode(StealMode::Oldest);

        engine.note_on(60, 100);
        // Age the first voice
        for _ in 0..100 {
            engine.next_sample();
        }
        engine.note_on(64, 100);
        assert_eq!(engine.active_voice_count(), 2);

        // Third note should steal the oldest (note 60)
        engine.note_on(67, 100);
        assert_eq!(engine.active_voice_count(), 2);
        // Voice playing note 60 should be gone, replaced by 67
        let notes: Vec<u8> = engine
            .voices
            .iter()
            .filter(|v| v.active)
            .map(|v| v.note)
            .collect();
        assert!(notes.contains(&67), "should have stolen oldest for note 67");
    }

    #[test]
    fn steal_mode_lowest_steals_lowest_note() {
        let mut bank = SampleBank::new();
        let sine: Vec<f32> = (0..44100)
            .map(|i| (2.0 * std::f32::consts::PI * 440.0 * i as f32 / 44100.0).sin())
            .collect();
        let id = bank.add(Sample::from_mono(sine, 44100));

        let mut inst = Instrument::new("test");
        inst.add_zone(Zone::new(id).with_key_range(0, 127).with_root_note(69));

        let mut engine = SamplerEngine::new(2, 44100.0);
        engine.set_bank(bank);
        engine.set_instrument(inst);
        engine.set_steal_mode(StealMode::Lowest);

        engine.note_on(60, 100);
        engine.note_on(72, 100);
        assert_eq!(engine.active_voice_count(), 2);

        // Third note should steal lowest (60)
        engine.note_on(66, 100);
        assert_eq!(engine.active_voice_count(), 2);
        let notes: Vec<u8> = engine
            .voices
            .iter()
            .filter(|v| v.active)
            .map(|v| v.note)
            .collect();
        assert!(
            !notes.contains(&60),
            "lowest note 60 should have been stolen"
        );
        assert!(notes.contains(&66), "new note 66 should be playing");
        assert!(notes.contains(&72), "note 72 should remain");
    }
}