mtrack 0.12.0

A multitrack audio and MIDI player for live performances.
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
// Copyright (C) 2026 Michael Wilson <mike@mdwn.dev>
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//

//! Voice management for polyphonic sample playback.
//!
//! Handles voice allocation, stealing, and release group behavior.
//! Voice management is source-agnostic — it works with any trigger source
//! (MIDI, audio triggers, etc.) via generic release groups.

use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Instant;

use tracing::{debug, warn};

use crate::config::samples::{ReleaseBehavior, RetriggerBehavior};
use crate::playsync::CancelHandle;

/// Global voice ID counter.
static NEXT_VOICE_ID: AtomicU64 = AtomicU64::new(1);

/// Represents an active voice playing a sample.
pub(super) struct Voice {
    /// Unique ID for this voice.
    id: u64,
    /// The sample name being played.
    sample_name: String,
    /// Optional release group for voice management (e.g. "midi:10:36", "kick").
    /// Voices in the same release group can be released together.
    release_group: Option<String>,
    /// What to do when this voice's release group is released.
    release_behavior: ReleaseBehavior,
    /// When this voice started playing.
    start_time: Instant,
    /// The audio source ID in the mixer (used for testing/debugging).
    #[allow(dead_code)]
    mixer_source_id: u64,
    /// Cancel handle for stopping this voice without lock contention.
    cancel_handle: CancelHandle,
    /// Scheduled sample at which this voice should stop (for sample-accurate cuts).
    cancel_at_sample: Arc<AtomicU64>,
    /// Shared flag set by the mixer when the audio source finishes playing.
    is_finished: Arc<AtomicBool>,
}

impl Voice {
    /// Creates a new voice.
    pub(super) fn new(
        sample_name: String,
        release_group: Option<String>,
        release_behavior: ReleaseBehavior,
        mixer_source_id: u64,
        cancel_handle: CancelHandle,
        cancel_at_sample: Arc<AtomicU64>,
        is_finished: Arc<AtomicBool>,
    ) -> Self {
        Self {
            id: NEXT_VOICE_ID.fetch_add(1, Ordering::Relaxed),
            sample_name,
            release_group,
            release_behavior,
            start_time: Instant::now(),
            mixer_source_id,
            cancel_handle,
            cancel_at_sample,
            is_finished,
        }
    }

    /// Checks if this voice belongs to the given release group.
    fn matches_release(&self, group: &str) -> bool {
        self.release_group.as_deref() == Some(group)
    }

    /// Returns a clone of this voice's cancel handle.
    fn cancel_handle(&self) -> CancelHandle {
        self.cancel_handle.clone()
    }

    /// Returns a clone of this voice's cancel_at_sample.
    fn cancel_at_sample(&self) -> Arc<AtomicU64> {
        self.cancel_at_sample.clone()
    }
}

/// Manages active voices for sample playback.
pub(super) struct VoiceManager {
    /// Active voices.
    voices: Vec<Voice>,
    /// Global maximum voices limit.
    max_voices: u32,
    /// Per-sample voice limits (sample_name -> max_voices).
    sample_limits: HashMap<String, u32>,
}

impl VoiceManager {
    /// Creates a new voice manager.
    pub(super) fn new(max_voices: u32) -> Self {
        Self {
            voices: Vec::new(),
            max_voices,
            sample_limits: HashMap::new(),
        }
    }

    /// Sets the per-sample voice limit.
    pub(super) fn set_sample_limit(&mut self, sample_name: &str, limit: u32) {
        self.sample_limits.insert(sample_name.to_string(), limit);
    }

    /// Removes voices whose audio source has finished playing in the mixer.
    /// This prevents PlayToCompletion voices from accumulating indefinitely.
    fn sweep_finished(&mut self) {
        self.voices
            .retain(|v| !v.is_finished.load(Ordering::Relaxed));
    }

    /// Adds a new voice, potentially stealing old voices if limits are exceeded.
    /// Returns the cancel_at_sample Arcs for any voices that should be stopped.
    /// The caller can set these to schedule the stop at a specific sample time.
    pub(super) fn add_voice(
        &mut self,
        voice: Voice,
        retrigger: RetriggerBehavior,
    ) -> Vec<Arc<AtomicU64>> {
        // Sweep finished voices before checking limits.
        self.sweep_finished();

        let mut voices_to_stop = Vec::new();

        // Handle retrigger behavior
        match retrigger {
            RetriggerBehavior::Cut => {
                // Stop all existing voices for this sample
                for v in self.voices.iter() {
                    if v.sample_name == voice.sample_name {
                        voices_to_stop.push(v.cancel_at_sample());
                    }
                }
                self.voices.retain(|v| v.sample_name != voice.sample_name);
            }
            RetriggerBehavior::Polyphonic => {
                // Check per-sample limit
                if let Some(&limit) = self.sample_limits.get(&voice.sample_name) {
                    let count = self
                        .voices
                        .iter()
                        .filter(|v| v.sample_name == voice.sample_name)
                        .count();
                    if count >= limit as usize {
                        // Steal oldest voice for this sample
                        if let Some(oldest) = self
                            .voices
                            .iter()
                            .filter(|v| v.sample_name == voice.sample_name)
                            .min_by_key(|v| v.start_time)
                        {
                            voices_to_stop.push(oldest.cancel_at_sample());
                            let oldest_id = oldest.id;
                            self.voices.retain(|v| v.id != oldest_id);
                            debug!(
                                sample = voice.sample_name,
                                limit, "Per-sample voice limit reached, stealing oldest"
                            );
                        }
                    }
                }
            }
        }

        // Check global limit
        if self.voices.len() >= self.max_voices as usize {
            // Steal oldest voice globally
            if let Some(oldest) = self.voices.iter().min_by_key(|v| v.start_time) {
                voices_to_stop.push(oldest.cancel_at_sample());
                let oldest_id = oldest.id;
                self.voices.retain(|v| v.id != oldest_id);
                warn!(
                    max_voices = self.max_voices,
                    "Global voice limit reached, stealing oldest"
                );
            }
        }

        self.voices.push(voice);
        voices_to_stop
    }

    /// Releases voices matching the given release group.
    /// Each voice's own ReleaseBehavior determines whether it is stopped.
    /// Returns the cancel handles for voices that should be stopped or faded.
    pub(super) fn release(&mut self, group: &str) -> Vec<CancelHandle> {
        let mut to_stop = Vec::new();

        for v in self.voices.iter() {
            if v.matches_release(group) {
                match v.release_behavior {
                    ReleaseBehavior::PlayToCompletion => {
                        // Let this voice play to completion
                    }
                    ReleaseBehavior::Stop | ReleaseBehavior::Fade => {
                        // Note: Fade currently behaves like Stop (immediate stop, no fade-out)
                        to_stop.push(v.cancel_handle());
                    }
                }
            }
        }

        // Remove only the voices that were stopped (not PlayToCompletion ones)
        self.voices.retain(|v| {
            !v.matches_release(group) || v.release_behavior == ReleaseBehavior::PlayToCompletion
        });

        to_stop
    }

    /// Returns the current number of active voices.
    pub(super) fn active_count(&self) -> usize {
        self.voices.len()
    }

    /// Clears all voices.
    /// Returns the cancel handles for all voices that should be stopped.
    pub(super) fn clear(&mut self) -> Vec<CancelHandle> {
        let handles: Vec<CancelHandle> = self.voices.iter().map(|v| v.cancel_handle()).collect();
        self.voices.clear();
        handles
    }
}

impl std::fmt::Debug for VoiceManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("VoiceManager")
            .field("active_voices", &self.voices.len())
            .field("max_voices", &self.max_voices)
            .finish()
    }
}

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

    fn make_voice(sample: &str, release_group: Option<&str>, id: u64) -> Voice {
        make_voice_with_behavior(sample, release_group, id, ReleaseBehavior::Stop)
    }

    fn make_voice_with_behavior(
        sample: &str,
        release_group: Option<&str>,
        id: u64,
        release_behavior: ReleaseBehavior,
    ) -> Voice {
        Voice::new(
            sample.to_string(),
            release_group.map(|s| s.to_string()),
            release_behavior,
            id,
            CancelHandle::new(),
            Arc::new(AtomicU64::new(0)),
            Arc::new(AtomicBool::new(false)),
        )
    }

    #[test]
    fn test_voice_release_matching() {
        let voice = make_voice("test", Some("midi:10:60"), 1);

        assert!(voice.matches_release("midi:10:60"));
        assert!(!voice.matches_release("midi:10:61"));
        assert!(!voice.matches_release("midi:11:60"));

        // Voice without release group matches nothing
        let voice2 = make_voice("test", None, 2);
        assert!(!voice2.matches_release("midi:10:60"));
        assert!(!voice2.matches_release("anything"));
    }

    #[test]
    fn test_voice_manager_cut_retrigger() {
        let mut manager = VoiceManager::new(32);

        let voice1 = make_voice("kick", Some("midi:10:36"), 1);
        let stopped = manager.add_voice(voice1, RetriggerBehavior::Cut);
        assert!(stopped.is_empty());
        assert_eq!(manager.active_count(), 1);

        // Add another voice for the same sample - should cut the previous
        let voice2 = make_voice("kick", Some("midi:10:36"), 2);
        let stopped = manager.add_voice(voice2, RetriggerBehavior::Cut);
        assert_eq!(stopped.len(), 1); // One voice should be stopped
        assert_eq!(manager.active_count(), 1);
    }

    #[test]
    fn test_voice_manager_polyphonic() {
        let mut manager = VoiceManager::new(32);
        manager.set_sample_limit("snare", 4);

        // Add 4 voices - should all be allowed
        for i in 1..=4 {
            let voice = make_voice("snare", Some("midi:10:38"), i);
            let stopped = manager.add_voice(voice, RetriggerBehavior::Polyphonic);
            assert!(stopped.is_empty());
        }
        assert_eq!(manager.active_count(), 4);

        // Add 5th voice - should steal oldest
        let voice5 = make_voice("snare", Some("midi:10:38"), 5);
        let stopped = manager.add_voice(voice5, RetriggerBehavior::Polyphonic);
        assert_eq!(stopped.len(), 1); // Voice 1 was oldest
        assert_eq!(manager.active_count(), 4);
    }

    #[test]
    fn test_voice_manager_global_limit() {
        let mut manager = VoiceManager::new(3);

        for i in 1..=3 {
            let voice = make_voice(&format!("sample{}", i), Some("midi:10:36"), i);
            let stopped = manager.add_voice(voice, RetriggerBehavior::Polyphonic);
            assert!(stopped.is_empty());
        }

        // Add 4th voice - should steal oldest globally
        let voice4 = make_voice("sample4", Some("midi:10:36"), 4);
        let stopped = manager.add_voice(voice4, RetriggerBehavior::Polyphonic);
        assert_eq!(stopped.len(), 1);
        assert_eq!(manager.active_count(), 3);
    }

    #[test]
    fn test_release_stop() {
        let mut manager = VoiceManager::new(32);

        let voice1 = make_voice("kick", Some("midi:10:36"), 1);
        let voice2 = make_voice("snare", Some("midi:10:38"), 2);
        manager.add_voice(voice1, RetriggerBehavior::Polyphonic);
        manager.add_voice(voice2, RetriggerBehavior::Polyphonic);

        // Release for kick group should stop only the kick
        let stopped = manager.release("midi:10:36");
        assert_eq!(stopped.len(), 1);
        assert_eq!(manager.active_count(), 1);
    }

    #[test]
    fn test_release_play_to_completion() {
        let mut manager = VoiceManager::new(32);

        let voice = make_voice_with_behavior(
            "kick",
            Some("midi:10:36"),
            1,
            ReleaseBehavior::PlayToCompletion,
        );
        manager.add_voice(voice, RetriggerBehavior::Polyphonic);

        // Voice with PlayToCompletion should not be stopped on release
        let stopped = manager.release("midi:10:36");
        assert!(stopped.is_empty());
        assert_eq!(manager.active_count(), 1);
    }

    #[test]
    fn test_release_custom_group() {
        let mut manager = VoiceManager::new(32);

        let voice1 = make_voice("cymbal", Some("cymbal"), 1);
        let voice2 = make_voice("cymbal", Some("cymbal"), 2);
        let voice3 = make_voice("kick", Some("kick"), 3);
        manager.add_voice(voice1, RetriggerBehavior::Polyphonic);
        manager.add_voice(voice2, RetriggerBehavior::Polyphonic);
        manager.add_voice(voice3, RetriggerBehavior::Polyphonic);

        // Release cymbal group should stop both cymbal voices
        let stopped = manager.release("cymbal");
        assert_eq!(stopped.len(), 2);
        assert_eq!(manager.active_count(), 1);
    }

    #[test]
    fn test_voice_manager_clear() {
        let mut manager = VoiceManager::new(32);

        let voice1 = make_voice("kick", Some("midi:10:36"), 1);
        let voice2 = make_voice("snare", Some("midi:10:38"), 2);
        manager.add_voice(voice1, RetriggerBehavior::Polyphonic);
        manager.add_voice(voice2, RetriggerBehavior::Polyphonic);
        assert_eq!(manager.active_count(), 2);

        let handles = manager.clear();
        assert_eq!(handles.len(), 2);
        assert_eq!(manager.active_count(), 0);
    }

    #[test]
    fn test_voice_manager_debug_format() {
        let manager = VoiceManager::new(16);
        let debug_str = format!("{:?}", manager);
        assert!(debug_str.contains("VoiceManager"));
        assert!(debug_str.contains("active_voices"));
        assert!(debug_str.contains("max_voices"));
        assert!(debug_str.contains("16"));
    }

    #[test]
    fn test_voice_manager_sweep_finished() {
        let mut manager = VoiceManager::new(32);

        let is_finished = Arc::new(AtomicBool::new(false));
        let voice = Voice::new(
            "test".to_string(),
            None,
            ReleaseBehavior::Stop,
            1,
            CancelHandle::new(),
            Arc::new(AtomicU64::new(0)),
            is_finished.clone(),
        );
        manager.add_voice(voice, RetriggerBehavior::Polyphonic);
        assert_eq!(manager.active_count(), 1);

        // Mark as finished and add another voice (triggers sweep)
        is_finished.store(true, Ordering::Relaxed);
        let voice2 = make_voice("other", None, 2);
        manager.add_voice(voice2, RetriggerBehavior::Polyphonic);
        // First voice should have been swept, only second remains
        assert_eq!(manager.active_count(), 1);
    }

    #[test]
    fn test_release_nonexistent_group() {
        let mut manager = VoiceManager::new(32);
        let voice = make_voice("kick", Some("midi:10:36"), 1);
        manager.add_voice(voice, RetriggerBehavior::Polyphonic);

        // Release a group that no voice belongs to
        let stopped = manager.release("nonexistent");
        assert!(stopped.is_empty());
        assert_eq!(manager.active_count(), 1);
    }

    #[test]
    fn test_voice_without_release_group() {
        let mut manager = VoiceManager::new(32);
        let voice = make_voice("ambient", None, 1);
        manager.add_voice(voice, RetriggerBehavior::Polyphonic);

        // Releasing any group should not affect a voice with no release group
        let stopped = manager.release("midi:10:36");
        assert!(stopped.is_empty());
        assert_eq!(manager.active_count(), 1);
    }

    #[test]
    fn test_release_fade_behavior_stops_voice() {
        let mut manager = VoiceManager::new(32);

        let voice = make_voice_with_behavior("pad", Some("pad"), 1, ReleaseBehavior::Fade);
        manager.add_voice(voice, RetriggerBehavior::Polyphonic);

        // Fade currently behaves like Stop
        let stopped = manager.release("pad");
        assert_eq!(stopped.len(), 1);
        assert_eq!(manager.active_count(), 0);
    }

    #[test]
    fn test_set_sample_limit_directly() {
        let mut manager = VoiceManager::new(32);
        manager.set_sample_limit("snare", 2);

        // Add 2 voices — both allowed
        let voice1 = make_voice("snare", None, 1);
        let voice2 = make_voice("snare", None, 2);
        assert!(manager
            .add_voice(voice1, RetriggerBehavior::Polyphonic)
            .is_empty());
        assert!(manager
            .add_voice(voice2, RetriggerBehavior::Polyphonic)
            .is_empty());

        // 3rd voice should steal oldest
        let voice3 = make_voice("snare", None, 3);
        let stopped = manager.add_voice(voice3, RetriggerBehavior::Polyphonic);
        assert_eq!(stopped.len(), 1);
        assert_eq!(manager.active_count(), 2);
    }

    #[test]
    fn test_polyphonic_no_sample_limit_respects_global() {
        let mut manager = VoiceManager::new(2);
        // No per-sample limit set

        let voice1 = make_voice("a", None, 1);
        let voice2 = make_voice("b", None, 2);
        assert!(manager
            .add_voice(voice1, RetriggerBehavior::Polyphonic)
            .is_empty());
        assert!(manager
            .add_voice(voice2, RetriggerBehavior::Polyphonic)
            .is_empty());

        // 3rd voice hits global limit
        let voice3 = make_voice("c", None, 3);
        let stopped = manager.add_voice(voice3, RetriggerBehavior::Polyphonic);
        assert_eq!(stopped.len(), 1);
        assert_eq!(manager.active_count(), 2);
    }

    #[test]
    fn test_cut_retrigger_different_sample_no_cut() {
        let mut manager = VoiceManager::new(32);

        let voice1 = make_voice("kick", None, 1);
        manager.add_voice(voice1, RetriggerBehavior::Cut);

        // Different sample name — should not cut the kick
        let voice2 = make_voice("snare", None, 2);
        let stopped = manager.add_voice(voice2, RetriggerBehavior::Cut);
        assert!(stopped.is_empty());
        assert_eq!(manager.active_count(), 2);
    }

    #[test]
    fn test_sweep_finished_multiple() {
        let mut manager = VoiceManager::new(32);

        let finished1 = Arc::new(AtomicBool::new(false));
        let finished2 = Arc::new(AtomicBool::new(false));
        let voice1 = Voice::new(
            "a".to_string(),
            None,
            ReleaseBehavior::Stop,
            1,
            CancelHandle::new(),
            Arc::new(AtomicU64::new(0)),
            finished1.clone(),
        );
        let voice2 = Voice::new(
            "b".to_string(),
            None,
            ReleaseBehavior::Stop,
            2,
            CancelHandle::new(),
            Arc::new(AtomicU64::new(0)),
            finished2.clone(),
        );
        let voice3 = make_voice("c", None, 3);

        manager.add_voice(voice1, RetriggerBehavior::Polyphonic);
        manager.add_voice(voice2, RetriggerBehavior::Polyphonic);
        manager.add_voice(voice3, RetriggerBehavior::Polyphonic);
        assert_eq!(manager.active_count(), 3);

        // Mark first two as finished
        finished1.store(true, Ordering::Relaxed);
        finished2.store(true, Ordering::Relaxed);

        // Adding a new voice triggers sweep
        let voice4 = make_voice("d", None, 4);
        manager.add_voice(voice4, RetriggerBehavior::Polyphonic);
        // Only voice3 and voice4 should remain
        assert_eq!(manager.active_count(), 2);
    }

    #[test]
    fn test_release_mixed_behaviors_in_same_group() {
        let mut manager = VoiceManager::new(32);

        // Two voices in the same group with different ReleaseBehaviors
        let voice1 =
            make_voice_with_behavior("ride", Some("cymbal"), 1, ReleaseBehavior::PlayToCompletion);
        let voice2 = make_voice_with_behavior("hi-hat", Some("cymbal"), 2, ReleaseBehavior::Stop);
        manager.add_voice(voice1, RetriggerBehavior::Polyphonic);
        manager.add_voice(voice2, RetriggerBehavior::Polyphonic);

        // Release should only stop the hi-hat (Stop), not the ride (PlayToCompletion)
        let stopped = manager.release("cymbal");
        assert_eq!(stopped.len(), 1);
        assert_eq!(manager.active_count(), 1); // ride still playing
    }
}