beamer-core 0.2.3

Core abstractions for the Beamer audio plugin (AU, VST3) framework
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
//! Audio buffer abstractions for plugin processing.
//!
//! This module provides [`Buffer`] for main audio I/O and [`AuxiliaryBuffers`]
//! for sidechain and auxiliary bus access.
//!
//! # Architecture
//!
//! Audio processing in Beamer uses two separate buffer types:
//!
//! - **[`Buffer`]**: Main stereo/surround I/O - used by all plugins
//! - **[`AuxiliaryBuffers`]**: Sidechain and aux buses - used by multi-bus plugins
//!
//! This separation solves Rust's lifetime variance constraints with nested mutable
//! references while providing a clean, ergonomic API.
//!
//! # Real-Time Safety
//!
//! All buffer types use fixed-size stack storage with no heap allocations.
//! This guarantees real-time safety in audio processing callbacks.
//!
//! # Generic Sample Type
//!
//! All buffer types are generic over `S: Sample`, defaulting to `f32`. This enables
//! zero-cost generic processing for both 32-bit and 64-bit audio.
//!
//! # Example: Simple Gain Plugin
//!
//! ```ignore
//! fn process(&mut self, buffer: &mut Buffer, _aux: &mut AuxiliaryBuffers) {
//!     let gain = self.parameters.gain();
//!     for (input, output) in buffer.zip_channels() {
//!         for (i, o) in input.iter().zip(output.iter_mut()) {
//!             *o = *i * gain;
//!         }
//!     }
//! }
//! ```
//!
//! # Example: Block-Based Sidechain (RMS)
//!
//! ```ignore
//! fn process(&mut self, buffer: &mut Buffer, aux: &mut AuxiliaryBuffers) {
//!     // Analyze sidechain input (block-level RMS)
//!     let key_level = aux.sidechain()
//!         .map(|sc| sc.rms(0))  // RMS of first channel
//!         .unwrap_or(0.0);
//!
//!     // Apply compression based on sidechain
//!     let reduction = self.compute_gain_reduction(key_level);
//!     for output in buffer.outputs_mut() {
//!         for sample in output {
//!             *sample *= reduction;
//!         }
//!     }
//! }
//! ```
//!
//! # Example: Sample-Accurate Sidechain Processing
//!
//! For sample-by-sample sidechain access (e.g., gates, duckers, lookahead compressors):
//!
//! ```ignore
//! fn process(&mut self, buffer: &mut Buffer, aux: &mut AuxiliaryBuffers) {
//!     let sc = aux.sidechain();
//!
//!     for i in 0..buffer.num_samples() {
//!         // Get sidechain sample (returns S::ZERO if disconnected)
//!         let key = sc.as_ref()
//!             .map(|s| s.sample(0, i).to_f64().abs())
//!             .unwrap_or_else(|| buffer.input(0)[i].to_f64().abs());
//!
//!         // Compute per-sample gain reduction
//!         let gain = self.envelope.process(key);
//!
//!         // Apply to output
//!         buffer.output(0)[i] = buffer.input(0)[i] * Sample::from_f64(gain);
//!     }
//! }
//! ```

use crate::sample::Sample;
use crate::types::{MAX_AUX_BUSES, MAX_CHANNELS};

// =============================================================================
// Buffer - Main Audio I/O
// =============================================================================

/// Main audio buffer for plugin processing.
///
/// Contains input and output channel slices for the primary audio bus.
/// This is what most plugins interact with - simple stereo or surround I/O.
///
/// # Type Parameter
///
/// `S` is the sample type, defaulting to `f32`. Use `Buffer<f64>` for
/// 64-bit double precision processing.
///
/// # Lifetime
///
/// The `'a` lifetime ties the buffer to the host's audio data. Buffers are
/// only valid within a single `process()` call.
///
/// # Channel Layout
///
/// Channels are indexed starting from 0:
/// - Stereo: 0 = Left, 1 = Right
/// - Surround: 0 = Left, 1 = Right, 2 = Center, etc.
///
/// # Real-Time Safety
///
/// This struct uses fixed-size stack storage. No heap allocations occur
/// during construction or use.
pub struct Buffer<'a, S: Sample = f32> {
    /// Input channel slices (immutable audio from host)
    /// Option<&[S]> is Copy, so [None; N] works
    inputs: [Option<&'a [S]>; MAX_CHANNELS],
    /// Output channel slices (mutable audio to host)
    outputs: [Option<&'a mut [S]>; MAX_CHANNELS],
    /// Number of active input channels
    num_input_channels: usize,
    /// Number of active output channels
    num_output_channels: usize,
    /// Number of samples in this processing block
    num_samples: usize,
}

impl<'a, S: Sample> Buffer<'a, S> {
    /// Create a new buffer from channel slices.
    ///
    /// This is called by the VST3 wrapper, not by plugin code.
    /// Channels beyond [`MAX_CHANNELS`] are silently ignored.
    #[inline]
    pub fn new(
        inputs: impl IntoIterator<Item = &'a [S]>,
        outputs: impl IntoIterator<Item = &'a mut [S]>,
        num_samples: usize,
    ) -> Self {
        let mut input_arr: [Option<&'a [S]>; MAX_CHANNELS] = [None; MAX_CHANNELS];
        let mut num_input_channels = 0;
        for (i, slice) in inputs.into_iter().take(MAX_CHANNELS).enumerate() {
            input_arr[i] = Some(slice);
            num_input_channels = i + 1;
        }

        // Can't use [None; N] for &mut because it's not Copy
        let mut output_arr: [Option<&'a mut [S]>; MAX_CHANNELS] = std::array::from_fn(|_| None);
        let mut num_output_channels = 0;
        for (i, slice) in outputs.into_iter().take(MAX_CHANNELS).enumerate() {
            output_arr[i] = Some(slice);
            num_output_channels = i + 1;
        }

        Self {
            inputs: input_arr,
            outputs: output_arr,
            num_input_channels,
            num_output_channels,
            num_samples,
        }
    }

    // =========================================================================
    // Buffer Info
    // =========================================================================

    /// Number of samples in this processing block.
    #[inline]
    pub fn num_samples(&self) -> usize {
        self.num_samples
    }

    /// Number of input channels.
    #[inline]
    pub fn num_input_channels(&self) -> usize {
        self.num_input_channels
    }

    /// Number of output channels.
    #[inline]
    pub fn num_output_channels(&self) -> usize {
        self.num_output_channels
    }

    /// Returns true if this is a stereo buffer (2 in, 2 out).
    #[inline]
    pub fn is_stereo(&self) -> bool {
        self.num_input_channels == 2 && self.num_output_channels == 2
    }

    /// Returns true if this is a mono buffer (1 in, 1 out).
    #[inline]
    pub fn is_mono(&self) -> bool {
        self.num_input_channels == 1 && self.num_output_channels == 1
    }

    // =========================================================================
    // Channel Access
    // =========================================================================

    /// Get an input channel by index.
    ///
    /// Returns an empty slice if the channel doesn't exist.
    #[inline]
    pub fn input(&self, channel: usize) -> &[S] {
        self.inputs
            .get(channel)
            .and_then(|opt| opt.as_ref())
            .map(|ch| &ch[..self.num_samples])
            .unwrap_or(&[])
    }

    /// Get a mutable output channel by index.
    ///
    /// # Panics
    ///
    /// Panics if the channel index is out of bounds.
    #[inline]
    pub fn output(&mut self, channel: usize) -> &mut [S] {
        let n = self.num_samples;
        self.outputs[channel]
            .as_mut()
            .map(|ch| &mut ch[..n])
            .expect("output channel out of bounds")
    }

    /// Try to get a mutable output channel by index.
    ///
    /// Returns `None` if the channel doesn't exist.
    #[inline]
    pub fn output_checked(&mut self, channel: usize) -> Option<&mut [S]> {
        let n = self.num_samples;
        self.outputs
            .get_mut(channel)
            .and_then(|opt| opt.as_mut())
            .map(|ch| &mut ch[..n])
    }

    // =========================================================================
    // Iterators
    // =========================================================================

    /// Iterate over all input channels.
    #[inline]
    pub fn inputs(&self) -> impl Iterator<Item = &[S]> + '_ {
        let n = self.num_samples;
        self.inputs[..self.num_input_channels]
            .iter()
            .filter_map(move |opt| opt.as_ref().map(|ch| &ch[..n]))
    }

    /// Iterate over all output channels mutably.
    #[inline]
    pub fn outputs_mut(&mut self) -> impl Iterator<Item = &mut [S]> + use<'_, 'a, S> {
        let n = self.num_samples;
        self.outputs[..self.num_output_channels]
            .iter_mut()
            .filter_map(move |opt| opt.as_mut().map(|ch| &mut ch[..n]))
    }

    /// Iterate over paired (input, output) channels.
    ///
    /// This is the most common pattern for in-place processing.
    /// Only yields channels that exist in both input and output.
    ///
    /// # Example
    ///
    /// ```ignore
    /// for (input, output) in buffer.zip_channels() {
    ///     for (i, o) in input.iter().zip(output.iter_mut()) {
    ///         *o = *i * gain;
    ///     }
    /// }
    /// ```
    #[inline]
    pub fn zip_channels(&mut self) -> impl Iterator<Item = (&[S], &mut [S])> + use<'_, 'a, S> {
        let n = self.num_samples;
        let num_pairs = self.num_input_channels.min(self.num_output_channels);
        self.inputs[..num_pairs]
            .iter()
            .zip(self.outputs[..num_pairs].iter_mut())
            .filter_map(move |(i_opt, o_opt)| {
                match (i_opt.as_ref(), o_opt.as_mut()) {
                    (Some(i), Some(o)) => Some((&i[..n], &mut o[..n])),
                    _ => None,
                }
            })
    }

    // =========================================================================
    // Bulk Operations
    // =========================================================================

    /// Copy all input channels to output channels.
    ///
    /// Useful for bypass or passthrough. Only copies channels that exist
    /// in both input and output.
    pub fn copy_to_output(&mut self) {
        let num_channels = self.num_input_channels.min(self.num_output_channels);
        let n = self.num_samples;
        for ch in 0..num_channels {
            if let (Some(input), Some(output)) = (self.inputs[ch].as_ref(), self.outputs[ch].as_mut()) {
                output[..n].copy_from_slice(&input[..n]);
            }
        }
    }

    /// Clear all output channels to silence.
    pub fn clear_outputs(&mut self) {
        let n = self.num_samples;
        for opt in self.outputs[..self.num_output_channels].iter_mut() {
            if let Some(output) = opt.as_mut() {
                output[..n].fill(S::ZERO);
            }
        }
    }

    /// Apply a gain factor to all output channels.
    pub fn apply_output_gain(&mut self, gain: S) {
        let n = self.num_samples;
        for opt in self.outputs[..self.num_output_channels].iter_mut() {
            if let Some(output) = opt.as_mut() {
                for sample in &mut output[..n] {
                    *sample = *sample * gain;
                }
            }
        }
    }
}

// =============================================================================
// AuxiliaryBuffers - Sidechain and Aux Buses
// =============================================================================

/// Auxiliary audio buffers for sidechain and multi-bus processing.
///
/// Contains all non-main audio buses. Most plugins don't need this -
/// they only use the main [`Buffer`].
///
/// # Type Parameter
///
/// `S` is the sample type, defaulting to `f32`. Use `AuxiliaryBuffers<f64>` for
/// 64-bit double precision processing.
///
/// # Bus Indexing
///
/// Auxiliary buses are indexed starting from 0:
/// - Bus 0: Sidechain (most common aux use case)
/// - Bus 1+: Additional auxiliary I/O
///
/// # Real-Time Safety
///
/// This struct uses fixed-size stack storage. No heap allocations occur
/// during construction or use.
///
/// # Example: Sidechain Access
///
/// ```ignore
/// if let Some(sidechain) = aux.sidechain() {
///     let key_signal = sidechain.input(0);
///     // Use for compression keying, ducking, etc.
/// }
/// ```
pub struct AuxiliaryBuffers<'a, S: Sample = f32> {
    /// Auxiliary input buses (e.g., sidechain inputs)
    /// Outer array: buses, Inner array: channels per bus
    inputs: [[Option<&'a [S]>; MAX_CHANNELS]; MAX_AUX_BUSES],
    /// Number of channels per input bus
    input_channel_counts: [usize; MAX_AUX_BUSES],
    /// Number of active input buses
    num_input_buses: usize,

    /// Auxiliary output buses (e.g., aux sends)
    outputs: [[Option<&'a mut [S]>; MAX_CHANNELS]; MAX_AUX_BUSES],
    /// Number of channels per output bus
    output_channel_counts: [usize; MAX_AUX_BUSES],
    /// Number of active output buses
    num_output_buses: usize,

    /// Number of samples in this processing block
    num_samples: usize,
}

impl<'a, S: Sample> AuxiliaryBuffers<'a, S> {
    /// Create new auxiliary buffers.
    ///
    /// This is called by the VST3 wrapper, not by plugin code.
    /// Buses/channels beyond the limits are silently ignored.
    #[inline]
    pub fn new(
        inputs: impl IntoIterator<Item = impl IntoIterator<Item = &'a [S]>>,
        outputs: impl IntoIterator<Item = impl IntoIterator<Item = &'a mut [S]>>,
        num_samples: usize,
    ) -> Self {
        // Initialize input buses
        let mut input_arr: [[Option<&'a [S]>; MAX_CHANNELS]; MAX_AUX_BUSES] =
            [[None; MAX_CHANNELS]; MAX_AUX_BUSES];
        let mut input_channel_counts = [0usize; MAX_AUX_BUSES];
        let mut num_input_buses = 0;

        for (bus_idx, bus) in inputs.into_iter().take(MAX_AUX_BUSES).enumerate() {
            let mut ch_count = 0;
            for (ch_idx, slice) in bus.into_iter().take(MAX_CHANNELS).enumerate() {
                input_arr[bus_idx][ch_idx] = Some(slice);
                ch_count = ch_idx + 1;
            }
            input_channel_counts[bus_idx] = ch_count;
            if ch_count > 0 {
                num_input_buses = bus_idx + 1;
            }
        }

        // Initialize output buses - need from_fn because &mut is not Copy
        let mut output_arr: [[Option<&'a mut [S]>; MAX_CHANNELS]; MAX_AUX_BUSES] =
            std::array::from_fn(|_| std::array::from_fn(|_| None));
        let mut output_channel_counts = [0usize; MAX_AUX_BUSES];
        let mut num_output_buses = 0;

        for (bus_idx, bus) in outputs.into_iter().take(MAX_AUX_BUSES).enumerate() {
            let mut ch_count = 0;
            for (ch_idx, slice) in bus.into_iter().take(MAX_CHANNELS).enumerate() {
                output_arr[bus_idx][ch_idx] = Some(slice);
                ch_count = ch_idx + 1;
            }
            output_channel_counts[bus_idx] = ch_count;
            if ch_count > 0 {
                num_output_buses = bus_idx + 1;
            }
        }

        Self {
            inputs: input_arr,
            input_channel_counts,
            num_input_buses,
            outputs: output_arr,
            output_channel_counts,
            num_output_buses,
            num_samples,
        }
    }

    /// Create empty auxiliary buffers (no aux buses).
    #[inline]
    pub fn empty() -> Self {
        Self {
            inputs: [[None; MAX_CHANNELS]; MAX_AUX_BUSES],
            input_channel_counts: [0; MAX_AUX_BUSES],
            num_input_buses: 0,
            outputs: std::array::from_fn(|_| std::array::from_fn(|_| None)),
            output_channel_counts: [0; MAX_AUX_BUSES],
            num_output_buses: 0,
            num_samples: 0,
        }
    }

    // =========================================================================
    // Info
    // =========================================================================

    /// Number of samples in this processing block.
    #[inline]
    pub fn num_samples(&self) -> usize {
        self.num_samples
    }

    /// Number of auxiliary input buses.
    #[inline]
    pub fn num_input_buses(&self) -> usize {
        self.num_input_buses
    }

    /// Number of auxiliary output buses.
    #[inline]
    pub fn num_output_buses(&self) -> usize {
        self.num_output_buses
    }

    /// Returns true if there are no auxiliary buses.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.num_input_buses == 0 && self.num_output_buses == 0
    }

    // =========================================================================
    // Bus Access
    // =========================================================================

    /// Get the sidechain input bus (auxiliary input bus 0).
    ///
    /// This is the most common aux use case. Returns `None` if no
    /// sidechain is connected.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let key_level = aux.sidechain()
    ///     .map(|sc| sc.rms(0))
    ///     .unwrap_or(0.0);
    /// ```
    #[inline]
    pub fn sidechain(&self) -> Option<AuxInput<'_, S>> {
        self.input(0)
    }

    /// Get an auxiliary input bus by index.
    ///
    /// Returns `None` if the bus doesn't exist or has no channels.
    #[inline]
    pub fn input(&self, bus: usize) -> Option<AuxInput<'_, S>> {
        if bus >= MAX_AUX_BUSES {
            return None;
        }
        let num_channels = self.input_channel_counts[bus];
        if num_channels == 0 {
            return None;
        }
        Some(AuxInput {
            channels: &self.inputs[bus][..num_channels],
            num_samples: self.num_samples,
        })
    }

    /// Get a mutable auxiliary output bus by index.
    ///
    /// Returns `None` if the bus doesn't exist or has no channels.
    #[inline]
    pub fn output(&mut self, bus: usize) -> Option<AuxOutput<'_, 'a, S>> {
        if bus >= MAX_AUX_BUSES {
            return None;
        }
        let num_channels = self.output_channel_counts[bus];
        if num_channels == 0 {
            return None;
        }
        let num_samples = self.num_samples;
        Some(AuxOutput {
            channels: &mut self.outputs[bus][..num_channels],
            num_samples,
        })
    }

    // =========================================================================
    // Iterators
    // =========================================================================

    /// Iterate over all auxiliary input buses.
    #[inline]
    pub fn iter_inputs(&self) -> impl Iterator<Item = AuxInput<'_, S>> + '_ {
        let num_samples = self.num_samples;
        self.inputs[..self.num_input_buses]
            .iter()
            .zip(self.input_channel_counts[..self.num_input_buses].iter())
            .filter(|(_, &count)| count > 0)
            .map(move |(channels, &count)| AuxInput {
                channels: &channels[..count],
                num_samples,
            })
    }

    /// Iterate over all auxiliary output buses mutably.
    #[inline]
    pub fn iter_outputs(&mut self) -> impl Iterator<Item = AuxOutput<'_, 'a, S>> + '_ {
        let num_samples = self.num_samples;
        let num_buses = self.num_output_buses;
        self.outputs[..num_buses]
            .iter_mut()
            .zip(self.output_channel_counts[..num_buses].iter())
            .filter(|(_, &count)| count > 0)
            .map(move |(channels, &count)| AuxOutput {
                channels: &mut channels[..count],
                num_samples,
            })
    }
}

// =============================================================================
// AuxInput - Immutable Auxiliary Input Bus
// =============================================================================

/// Immutable view of an auxiliary input bus.
///
/// Provides access to input channels for a single auxiliary bus
/// (typically sidechain). Created via [`AuxiliaryBuffers::sidechain()`]
/// or [`AuxiliaryBuffers::input()`].
///
/// # Type Parameter
///
/// `S` is the sample type, defaulting to `f32`.
///
/// # Example: Block-Based Analysis
///
/// ```ignore
/// if let Some(sc) = aux.sidechain() {
///     // Calculate RMS of sidechain for keying
///     let rms = sc.rms(0);
///
///     // Or iterate over all channels
///     for ch in sc.iter_inputs() {
///         // Process channel...
///     }
/// }
/// ```
///
/// # Example: Sample-by-Sample Access
///
/// ```ignore
/// if let Some(sc) = aux.sidechain() {
///     for i in 0..buffer.num_samples() {
///         // .sample() returns S::ZERO if channel/index missing
///         let key_l = sc.sample(0, i).to_f64().abs();
///         let key_r = sc.sample(1, i).to_f64().abs();
///         let key = (key_l + key_r) * 0.5;
///
///         // Use for envelope follower, gate, ducker, etc.
///     }
/// }
/// ```
pub struct AuxInput<'a, S: Sample = f32> {
    channels: &'a [Option<&'a [S]>],
    num_samples: usize,
}

impl<'a, S: Sample> AuxInput<'a, S> {
    /// Number of samples in each channel.
    #[inline]
    pub fn num_samples(&self) -> usize {
        self.num_samples
    }

    /// Number of channels in this bus.
    #[inline]
    pub fn num_channels(&self) -> usize {
        self.channels.len()
    }

    /// Get an input channel by index.
    ///
    /// Returns an empty slice if the channel doesn't exist.
    /// Matches [`Buffer::input()`] naming for API consistency.
    #[inline]
    pub fn input(&self, index: usize) -> &[S] {
        self.channels
            .get(index)
            .and_then(|opt| opt.as_ref())
            .map(|ch| &ch[..self.num_samples])
            .unwrap_or(&[])
    }

    /// Get a single sample from a channel.
    ///
    /// Returns [`S::ZERO`] if the channel or sample index doesn't exist.
    /// This is a convenience method for sample-by-sample processing.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Instead of:
    /// let sc = sidechain.input(0).get(i).copied().unwrap_or(S::ZERO);
    ///
    /// // Use:
    /// let sc = sidechain.sample(0, i);
    /// ```
    #[inline]
    pub fn sample(&self, channel: usize, index: usize) -> S {
        self.input(channel)
            .get(index)
            .copied()
            .unwrap_or(S::ZERO)
    }

    /// Iterate over all input channels.
    #[inline]
    pub fn iter_inputs(&self) -> impl Iterator<Item = &[S]> + '_ {
        let n = self.num_samples;
        self.channels
            .iter()
            .filter_map(move |opt| opt.as_ref().map(|ch| &ch[..n]))
    }

    // =========================================================================
    // Analysis Utilities
    // =========================================================================

    /// Calculate the RMS (root mean square) level of a channel.
    ///
    /// Returns zero if the channel doesn't exist or is empty.
    pub fn rms(&self, channel: usize) -> S {
        let ch = self.input(channel);
        if ch.is_empty() {
            return S::ZERO;
        }
        let sum: S = ch.iter().fold(S::ZERO, |acc, &s| acc + s * s);
        let len = S::from_f32(ch.len() as f32);
        (sum / len).sqrt()
    }

    /// Calculate the peak level of a channel.
    ///
    /// Returns zero if the channel doesn't exist or is empty.
    pub fn peak(&self, channel: usize) -> S {
        self.input(channel)
            .iter()
            .map(|&s| s.abs())
            .fold(S::ZERO, |a, b| a.max(b))
    }

    /// Calculate the average absolute level of a channel.
    ///
    /// Returns zero if the channel doesn't exist or is empty.
    pub fn average(&self, channel: usize) -> S {
        let ch = self.input(channel);
        if ch.is_empty() {
            return S::ZERO;
        }
        let sum: S = ch.iter().map(|&s| s.abs()).fold(S::ZERO, |a, b| a + b);
        let len = S::from_f32(ch.len() as f32);
        sum / len
    }
}

// =============================================================================
// AuxOutput - Mutable Auxiliary Output Bus
// =============================================================================

/// Mutable view of an auxiliary output bus.
///
/// Provides access to output channels for a single auxiliary bus
/// (e.g., aux sends, multi-out). Created via [`AuxiliaryBuffers::output()`].
///
/// # Type Parameter
///
/// `S` is the sample type, defaulting to `f32`.
///
/// # Lifetime Parameters
///
/// - `'borrow` - The borrow lifetime (from `&mut self` on `AuxiliaryBuffers`)
/// - `'data` - The underlying audio data lifetime
///
/// This separation is required because `&'a mut [&'a mut T]` is invariant
/// in Rust, which prevents returning borrowed data from methods.
///
/// # Example
///
/// ```ignore
/// if let Some(mut aux_out) = aux.output(0) {
///     // Write to aux output
///     for sample in aux_out.output(0) {
///         *sample = processed_signal;
///     }
/// }
/// ```
pub struct AuxOutput<'borrow, 'data, S: Sample = f32> {
    channels: &'borrow mut [Option<&'data mut [S]>],
    num_samples: usize,
}

impl<'borrow, 'data, S: Sample> AuxOutput<'borrow, 'data, S> {
    /// Number of samples in each channel.
    #[inline]
    pub fn num_samples(&self) -> usize {
        self.num_samples
    }

    /// Number of channels in this bus.
    #[inline]
    pub fn num_channels(&self) -> usize {
        self.channels.len()
    }

    /// Get a mutable output channel by index.
    ///
    /// Matches [`Buffer::output()`] naming for API consistency.
    ///
    /// # Panics
    ///
    /// Panics if the channel index is out of bounds.
    #[inline]
    pub fn output(&mut self, index: usize) -> &mut [S] {
        let n = self.num_samples;
        self.channels[index]
            .as_mut()
            .map(|ch| &mut ch[..n])
            .expect("aux output channel out of bounds")
    }

    /// Try to get a mutable output channel by index.
    ///
    /// Returns `None` if the channel doesn't exist.
    #[inline]
    pub fn output_checked(&mut self, index: usize) -> Option<&mut [S]> {
        let n = self.num_samples;
        self.channels
            .get_mut(index)
            .and_then(|opt| opt.as_mut())
            .map(|ch| &mut ch[..n])
    }

    /// Iterate over all output channels mutably.
    #[inline]
    pub fn iter_outputs(&mut self) -> impl Iterator<Item = &mut [S]> + use<'_, 'data, S> {
        let n = self.num_samples;
        self.channels
            .iter_mut()
            .filter_map(move |opt| opt.as_mut().map(|ch| &mut ch[..n]))
    }

    /// Clear all channels to silence.
    pub fn clear(&mut self) {
        let n = self.num_samples;
        for opt in self.channels.iter_mut() {
            if let Some(ch) = opt.as_mut() {
                ch[..n].fill(S::ZERO);
            }
        }
    }

    /// Fill all channels with a constant value.
    pub fn fill(&mut self, value: S) {
        let n = self.num_samples;
        for opt in self.channels.iter_mut() {
            if let Some(ch) = opt.as_mut() {
                ch[..n].fill(value);
            }
        }
    }
}