truce_core/buffer.rs
1use truce_params::sample::Sample;
2
3/// Non-interleaved audio buffer. Borrows host memory through the
4/// format wrapper.
5///
6/// Generic over the sample type `S` (the plugin's chosen precision,
7/// `f32` or `f64`). The format wrapper bridges between host-buffer
8/// precision and `S` at the block boundary - see
9/// [`RawBufferScratch::build`]. Plugin code under
10/// `use truce::prelude::*;` (f32) or `use truce::prelude64::*;` (f64)
11/// sees `AudioBuffer<S>` with `S` already picked.
12///
13/// **In-place I/O.** Some hosts (Reaper, pluginval) pass the same
14/// buffer for both input and output of a given channel. By default
15/// the wrapper copies the aliased inputs into per-channel scratch so
16/// `input(ch)` and `output(ch)` are disjoint `&[S]` / `&mut [S]` -
17/// no plugin code change required. Plugins that opt into
18/// `Plugin::supports_in_place() = true` skip the copy and must use
19/// [`Self::in_out_mut`] for channels where [`Self::is_in_place`]
20/// returns `true`.
21pub struct AudioBuffer<'a, S: Sample = f32> {
22 inputs: &'a [&'a [S]],
23 outputs: &'a mut [&'a mut [S]],
24 /// Bit `ch` is set when `inputs[ch]` and `outputs[ch]` point to
25 /// the same host memory. Channels ≥ 64 are always reported as
26 /// non-aliased - formats with that many channels are exotic
27 /// enough to be a follow-up.
28 in_place_mask: u64,
29 offset: usize,
30 num_samples: usize,
31}
32
33impl<'a, S: Sample> AudioBuffer<'a, S> {
34 /// Safe wrapper around [`Self::from_slices`] for callers that hold their
35 /// own owned `Vec<Vec<S>>` (e.g. `truce-driver`'s test harness).
36 /// Forwards to the unsafe constructor - the borrow checker proves
37 /// the lifetime invariants the `unsafe fn` requires when both
38 /// slice arrays and the buffer itself live in the same scope.
39 /// `num_samples > slice length` still asserts in debug builds.
40 pub fn from_slices_checked(
41 inputs: &'a [&'a [S]],
42 outputs: &'a mut [&'a mut [S]],
43 num_samples: usize,
44 ) -> Self {
45 // SAFETY: caller hands us references that the borrow checker
46 // already proved valid for `'a`; the debug-mode assertions
47 // inside `from_slices` cover the `num_samples` bound.
48 unsafe { Self::from_slices(inputs, outputs, num_samples) }
49 }
50
51 /// Create a buffer from pre-split channel slices.
52 /// Used by format wrappers after converting from host-specific buffer types.
53 ///
54 /// # Safety
55 /// The caller must ensure the slices are valid for the lifetime `'a`
56 /// and that `num_samples` does not exceed any slice's length.
57 ///
58 /// # Panics
59 ///
60 /// In debug builds only, panics if any input channel aliases an
61 /// output channel or `num_samples` exceeds the length of any
62 /// input/output slice. Release builds skip these checks (they're
63 /// safety preconditions, not runtime invariants).
64 pub unsafe fn from_slices(
65 inputs: &'a [&'a [S]],
66 outputs: &'a mut [&'a mut [S]],
67 num_samples: usize,
68 ) -> Self {
69 #[cfg(debug_assertions)]
70 {
71 // Verify no input channel aliases any output channel.
72 for (i, inp) in inputs.iter().enumerate() {
73 let i_start = inp.as_ptr() as usize;
74 let i_end = i_start + std::mem::size_of_val(*inp);
75 for (o, out) in outputs.iter().enumerate() {
76 let o_start = out.as_ptr() as usize;
77 let o_end = o_start + std::mem::size_of_val(*out);
78 assert!(
79 i_end <= o_start || o_end <= i_start,
80 "AudioBuffer: input channel {i} and output channel {o} alias \
81 - pass disjoint slices or use RawBufferScratch::build which \
82 handles aliasing automatically",
83 );
84 }
85 }
86 // Verify num_samples doesn't exceed any slice length.
87 for (i, inp) in inputs.iter().enumerate() {
88 assert!(
89 num_samples <= inp.len(),
90 "AudioBuffer: num_samples ({num_samples}) exceeds input channel {i} length ({})",
91 inp.len(),
92 );
93 }
94 for (o, out) in outputs.iter().enumerate() {
95 assert!(
96 num_samples <= out.len(),
97 "AudioBuffer: num_samples ({num_samples}) exceeds output channel {o} length ({})",
98 out.len(),
99 );
100 }
101 }
102 AudioBuffer {
103 inputs,
104 outputs,
105 in_place_mask: 0,
106 offset: 0,
107 num_samples,
108 }
109 }
110
111 /// Set the in-place mask. Called by format wrappers (or
112 /// `RawBufferScratch::build`) after construction once they've
113 /// determined which channels alias on the host side.
114 #[inline]
115 pub fn set_in_place_mask(&mut self, mask: u64) {
116 self.in_place_mask = mask;
117 }
118
119 /// `true` when the host passes a single buffer for both input and
120 /// output of `ch` (in-place I/O). Use [`Self::in_out_mut`] to read
121 /// and write that buffer directly when this returns `true`.
122 #[must_use]
123 pub fn is_in_place(&self, ch: usize) -> bool {
124 ch < 64 && (self.in_place_mask >> ch) & 1 == 1
125 }
126
127 /// Read+write slice for an in-place channel - the same memory the
128 /// host gave us for both input and output. Each sample reads as
129 /// the input value before the plugin overwrites it.
130 ///
131 /// Only meaningful when [`Self::is_in_place`] returns `true`. On a
132 /// non-in-place channel this returns the output slice with no
133 /// input data in it; reading is allowed but produces uninitialized
134 /// host-buffer contents.
135 pub fn in_out_mut(&mut self, ch: usize) -> &mut [S] {
136 let end = self.offset + self.num_samples;
137 &mut self.outputs[ch][self.offset..end]
138 }
139
140 #[must_use]
141 pub fn num_samples(&self) -> usize {
142 self.num_samples
143 }
144
145 #[must_use]
146 pub fn num_input_channels(&self) -> usize {
147 self.inputs.len()
148 }
149
150 #[must_use]
151 pub fn num_output_channels(&self) -> usize {
152 self.outputs.len()
153 }
154
155 #[must_use]
156 pub fn input(&self, channel: usize) -> &[S] {
157 let end = self.offset + self.num_samples;
158 &self.inputs[channel][self.offset..end]
159 }
160
161 pub fn output(&mut self, channel: usize) -> &mut [S] {
162 let end = self.offset + self.num_samples;
163 &mut self.outputs[channel][self.offset..end]
164 }
165
166 /// Number of channels (min of input and output).
167 #[must_use]
168 pub fn channels(&self) -> usize {
169 self.inputs.len().min(self.outputs.len())
170 }
171
172 /// Get an input/output pair for a channel. Useful for in-place processing.
173 pub fn io_pair(&mut self, in_ch: usize, out_ch: usize) -> (&[S], &mut [S]) {
174 let end = self.offset + self.num_samples;
175 let input = &self.inputs[in_ch][self.offset..end];
176 let output = &mut self.outputs[out_ch][self.offset..end];
177 (input, output)
178 }
179
180 /// Get an input/output pair for the same channel index. Shorthand for `io_pair(ch, ch)`.
181 pub fn io(&mut self, ch: usize) -> (&[S], &mut [S]) {
182 self.io_pair(ch, ch)
183 }
184
185 /// Iterate per-channel, in fixed-size `N`-sample chunks. The
186 /// last chunk of each channel may be shorter than `N`; it's
187 /// yielded as a [`ChunkItem::Tail`] with the actual remaining
188 /// length, and the caller falls back to scalar for it. Full
189 /// `N`-sample chunks arrive as [`ChunkItem::Full`] carrying
190 /// `&[S; N]` / `&mut [S; N]` stack arrays - exactly the shape
191 /// the per-op SIMD primitives in `truce-simd` expect.
192 ///
193 /// Iteration order is channel-major: all chunks of channel 0,
194 /// then all chunks of channel 1, etc. Matches the natural
195 /// orientation for per-channel state (biquad coefficients,
196 /// per-channel meters) and lets the caller read its smoothed
197 /// params once per chunk instead of once per sample.
198 ///
199 /// The returned object is a "lending iterator" - it doesn't
200 /// implement [`Iterator`] because each yielded item borrows
201 /// from the iterator itself. Use `while let Some(chunk) = …
202 /// .next()`:
203 ///
204 /// ```ignore
205 /// let mut chunks = buffer.chunks_mut::<32>();
206 /// while let Some(chunk) = chunks.next() {
207 /// match chunk {
208 /// ChunkItem::Full { ch, inp, out } => {
209 /// // SIMD-friendly path, inp / out are &[f32; 32]
210 /// }
211 /// ChunkItem::Tail { ch, inp, out } => {
212 /// // scalar fallback for the trailing samples
213 /// }
214 /// }
215 /// }
216 /// ```
217 ///
218 /// Const-generic `N` is the chunk size; pick it to match the
219 /// SIMD width × unroll factor for your inner op (32 / 64 are
220 /// good defaults for current Apple Silicon + `x86_64`).
221 pub fn chunks_mut<const N: usize>(&mut self) -> ChunksMut<'_, 'a, S, N> {
222 ChunksMut {
223 buffer: self,
224 ch: 0,
225 pos: 0,
226 }
227 }
228
229 /// Iterate per-frame and hand a fixed-size `(input, output)`
230 /// stack-array pair to `tick`. Sized at the type level by const
231 /// generic `N`, which must equal [`Self::channels`].
232 ///
233 /// `io()` / `io_pair()` give a per-channel slice view, which is
234 /// the right shape for "process channel `ch` in isolation"
235 /// loops. But libraries that expect a per-frame `(in: &[S],
236 /// out: &mut [S])` callback - `fundsp::AudioUnit::tick`,
237 /// `nih_plug`'s frame iterators, custom per-sample DSP nodes -
238 /// can't take that shape directly without either copying inputs
239 /// into a scratch first (heap allocation on the audio thread)
240 /// or fighting the borrow checker over two simultaneous `&mut`
241 /// borrows of the buffer.
242 ///
243 /// This helper does the per-frame transpose in-place against a
244 /// stack-allocated `[S; N]` pair, calls `tick` `num_samples()`
245 /// times, and writes back. No heap, no borrow gymnastics at the
246 /// call site:
247 ///
248 /// ```ignore
249 /// // Stereo plugin delegating per-frame DSP to fundsp:
250 /// buffer.for_each_frame::<2, _>(|frame_in, frame_out| {
251 /// self.graph.tick(frame_in, frame_out);
252 /// });
253 /// ```
254 ///
255 /// `&[S; N]` deref-coerces to `&[S]` at the call site, so
256 /// callers can pass the arrays straight to slice-taking APIs
257 /// like fundsp's `tick`.
258 ///
259 /// # Panics
260 ///
261 /// Debug builds panic if `N != self.channels()`. Release builds
262 /// rely on the same precondition without checking; reading past
263 /// the actual channel count would index out of bounds anyway.
264 pub fn for_each_frame<const N: usize, F>(&mut self, mut tick: F)
265 where
266 F: FnMut(&[S; N], &mut [S; N]),
267 {
268 debug_assert_eq!(
269 N,
270 self.channels(),
271 "for_each_frame::<{N}> requires the buffer to have exactly {N} channels"
272 );
273 let mut frame_in = [S::default(); N];
274 let mut frame_out = [S::default(); N];
275 let end = self.offset + self.num_samples;
276 for i in self.offset..end {
277 for (ch, slot) in frame_in.iter_mut().enumerate() {
278 *slot = self.inputs[ch][i];
279 }
280 tick(&frame_in, &mut frame_out);
281 for (ch, sample) in frame_out.iter().enumerate() {
282 self.outputs[ch][i] = *sample;
283 }
284 }
285 }
286
287 /// Peak absolute value across an output channel, returned as `f32`
288 /// because meters / UI display always work in `f32` regardless of
289 /// the plugin's internal precision.
290 ///
291 /// Short-circuits and returns `f32::NAN` on the **first** NaN
292 /// sample seen, so meters can flag runaway plugins instead of
293 /// silently reporting "peaks within range" while NaN poison
294 /// spreads downstream.
295 #[must_use]
296 pub fn output_peak(&self, ch: usize) -> f32 {
297 let end = self.offset + self.num_samples;
298 let mut peak = 0.0f32;
299 for &b in &self.outputs[ch][self.offset..end] {
300 let v = b.to_f32();
301 if v.is_nan() {
302 return f32::NAN;
303 }
304 let abs = v.abs();
305 if abs > peak {
306 peak = abs;
307 }
308 }
309 peak
310 }
311
312 /// Return a sub-block view covering samples `start..start+len`.
313 ///
314 /// The returned buffer borrows `self` exclusively - you cannot use
315 /// the original buffer while the slice is alive.
316 ///
317 /// # Panics
318 /// Panics if `start + len > self.num_samples()`.
319 pub fn slice(&mut self, start: usize, len: usize) -> AudioBuffer<'_, S> {
320 assert!(
321 start + len <= self.num_samples,
322 "slice({start}, {len}) out of bounds for buffer of {} samples",
323 self.num_samples,
324 );
325 let new_offset = self.offset + start;
326 // SAFETY: We construct an AudioBuffer<'a, S> and transmute to AudioBuffer<'_, S>.
327 // These have identical memory layout (lifetimes are erased at runtime).
328 // This is sound because:
329 // 1. &mut self prevents the caller from using self while the slice exists
330 // 2. The underlying channel memory lives for 'a which outlives '_
331 // 3. Bounds are checked by the assert above
332 let self_ptr: *mut Self = self;
333 unsafe {
334 let s = &mut *self_ptr;
335 std::mem::transmute::<AudioBuffer<'a, S>, AudioBuffer<'_, S>>(AudioBuffer {
336 inputs: s.inputs,
337 outputs: &mut *s.outputs,
338 in_place_mask: s.in_place_mask,
339 offset: new_offset,
340 num_samples: len,
341 })
342 }
343 }
344}
345
346/// One yielded chunk from [`AudioBuffer::chunks_mut`].
347///
348/// `Full` is the SIMD-friendly path: `inp` and `out` are stack
349/// arrays of exactly `N` elements, ready to feed `truce-simd`'s
350/// block ops. `Tail` is the trailing fragment when `num_samples()`
351/// isn't a multiple of `N`; fall back to a scalar loop.
352pub enum ChunkItem<'b, S: Sample, const N: usize> {
353 /// Full N-sample chunk. The `&[S; N]` / `&mut [S; N]` are the
354 /// shape `truce-simd` ops are written against - no slice
355 /// length check at the call site.
356 Full {
357 /// Channel index this chunk belongs to.
358 ch: usize,
359 /// Sample offset within the audio block this chunk starts
360 /// at. Use this when indexing into a precomputed envelope
361 /// array - `chunks_mut` iterates channel-major, so the
362 /// envelope (typically read once per audio block via
363 /// `read_block::<num_samples>()`) is shared across all
364 /// channel passes.
365 sample: usize,
366 /// Read-only N-sample input slice.
367 inp: &'b [S; N],
368 /// Mutable N-sample output slice.
369 out: &'b mut [S; N],
370 },
371 /// Trailing chunk when `num_samples()` isn't a multiple of `N`.
372 /// Length is in `(0, N)`. Fall back to scalar processing.
373 Tail {
374 /// Channel index this chunk belongs to.
375 ch: usize,
376 /// Sample offset within the audio block this chunk starts at.
377 sample: usize,
378 /// Read-only tail input slice; length < N.
379 inp: &'b [S],
380 /// Mutable tail output slice; length < N.
381 out: &'b mut [S],
382 },
383}
384
385/// Lending iterator returned by [`AudioBuffer::chunks_mut`].
386///
387/// Does not implement [`Iterator`] because each yielded
388/// [`ChunkItem`] borrows from the iterator itself - the standard
389/// "GATs would help here" pattern. Drive it with `while let
390/// Some(chunk) = chunks.next()` instead. See
391/// [`AudioBuffer::chunks_mut`] for a worked example.
392pub struct ChunksMut<'b, 'a, S: Sample, const N: usize> {
393 buffer: &'b mut AudioBuffer<'a, S>,
394 /// Current channel being walked.
395 ch: usize,
396 /// Position within the current channel, relative to
397 /// `buffer.offset`. Advances by N each Full chunk, then jumps
398 /// to `num_samples` for the Tail (or directly past it when
399 /// `num_samples` is a multiple of N).
400 pos: usize,
401}
402
403impl<S: Sample, const N: usize> ChunksMut<'_, '_, S, N> {
404 /// Yield the next chunk, or `None` when every channel has been
405 /// fully walked.
406 ///
407 /// Method-on-self rather than `Iterator::next` because each
408 /// yielded [`ChunkItem`] borrows from `self`; GATs would be
409 /// needed to express that through the `Iterator` trait.
410 #[allow(clippy::should_implement_trait, clippy::missing_panics_doc)]
411 pub fn next(&mut self) -> Option<ChunkItem<'_, S, N>> {
412 loop {
413 if self.ch >= self.buffer.outputs.len() {
414 return None;
415 }
416 let ns = self.buffer.num_samples;
417 if self.pos >= ns {
418 self.ch += 1;
419 self.pos = 0;
420 continue;
421 }
422 let abs_start = self.buffer.offset + self.pos;
423 let remaining = ns - self.pos;
424 let take = remaining.min(N);
425 let abs_end = abs_start + take;
426 let ch = self.ch;
427 let sample = self.pos;
428
429 let inp_slice = &self.buffer.inputs[ch][abs_start..abs_end];
430 let out_slice: &mut [S] = &mut self.buffer.outputs[ch][abs_start..abs_end];
431
432 self.pos += take;
433
434 // Full vs Tail by length: full chunks convert to `&[S;
435 // N]` / `&mut [S; N]` for the SIMD-friendly path; tails
436 // fall back to slice form.
437 return Some(if take == N {
438 ChunkItem::Full {
439 ch,
440 sample,
441 // Length-checked above; `try_into` here is a
442 // free reinterpret.
443 inp: inp_slice.try_into().expect("len == N by construction"),
444 out: out_slice.try_into().expect("len == N by construction"),
445 }
446 } else {
447 ChunkItem::Tail {
448 ch,
449 sample,
450 inp: inp_slice,
451 out: out_slice,
452 }
453 });
454 }
455 }
456}
457
458/// Scratch space for [`RawBufferScratch::build`].
459///
460/// Callers allocate this on the stack and pass it to `build`. The
461/// buffer borrows the slices stored here, so this struct must outlive
462/// the returned `AudioBuffer`.
463///
464/// Generic over the plugin's sample type `S`. When the host buffer
465/// matches `S`, slices point into host memory (zero-copy). When the
466/// host buffer is a different precision, the input is widened/narrowed
467/// into per-channel scratch; the output is rendered into scratch and
468/// the wrapper copies + casts it back to the host buffer at the end
469/// of the block via [`Self::finish_widening_f32`].
470pub struct RawBufferScratch<S: Sample = f32> {
471 pub input_slices: Vec<&'static [S]>,
472 pub output_slices: Vec<&'static mut [S]>,
473 /// Per-channel input copies. Used (a) when the host passes the
474 /// same buffer for input and output (in-place processing - VST3
475 /// spec allows this and several real DAWs use it for effects),
476 /// or (b) when the host buffer precision differs from `S` and
477 /// we widen/narrow on the way in. In either case the slice the
478 /// plugin sees points into the matching slot here.
479 input_copies: Vec<Vec<S>>,
480 /// Per-channel output scratch. Only populated by [`Self::build`]
481 /// when the host buffer precision differs from `S`; the wrapper
482 /// copies + casts these back to the host buffer at the end of the
483 /// block via [`Self::finish_widening_f32`].
484 output_buffers: Vec<Vec<S>>,
485}
486
487impl<S: Sample> RawBufferScratch<S> {
488 /// Build an `AudioBuffer<S>` from raw `f32` host pointers - the
489 /// common case (CLAP, LV2, AAX always; VST3/VST2/AU 32-bit mode).
490 ///
491 /// When `S = f32`, slices point directly into host memory (modulo
492 /// in-place input copying). When `S = f64`, every channel is
493 /// widened into per-channel scratch and the wrapper must call
494 /// [`Self::finish_widening_f32`] at the end of the block to copy
495 /// the rendered samples back to the host's `f32` output pointers.
496 ///
497 /// # Safety
498 /// - `inputs` must point to `num_in` valid `*const f32` pointers
499 /// (each non-null pointer must address at least `num_frames`
500 /// readable samples; null is allowed and yields an empty slice).
501 /// - `outputs` must point to `num_out` valid `*mut f32` pointers
502 /// (each non-null pointer must address at least `num_frames`
503 /// writable samples; null is allowed and yields an empty slice).
504 /// - The pointed-to memory must remain valid for the lifetime of
505 /// the returned `AudioBuffer`.
506 pub unsafe fn build(
507 &mut self,
508 inputs: *const *const f32,
509 outputs: *mut *mut f32,
510 num_in: u32,
511 num_out: u32,
512 num_frames: u32,
513 supports_in_place: bool,
514 ) -> AudioBuffer<'_, S> {
515 // SAFETY: forwarded - caller's contract is the same.
516 unsafe {
517 self.build_inner(
518 inputs,
519 outputs,
520 num_in,
521 num_out,
522 num_frames,
523 supports_in_place,
524 )
525 }
526 }
527
528 /// Copy + narrow the rendered `S` output back to the host's
529 /// `f32` output pointers. No-op when `S = f32` (the slices the
530 /// plugin wrote already point directly at host memory).
531 ///
532 /// # Safety
533 /// `outputs` and `num_out` / `num_frames` must match the values
534 /// passed to the prior [`Self::build`] call on this scratch.
535 pub unsafe fn finish_widening_f32(
536 &self,
537 outputs: *mut *mut f32,
538 num_out: u32,
539 num_frames: u32,
540 ) {
541 // When the plugin is `f32` we wrote straight into host memory.
542 if std::any::TypeId::of::<S>() == std::any::TypeId::of::<f32>() {
543 return;
544 }
545 unsafe {
546 let nf = num_frames as usize;
547 for ch in 0..(num_out as usize) {
548 let ptr = *outputs.add(ch);
549 if ptr.is_null() {
550 continue;
551 }
552 let host = std::slice::from_raw_parts_mut(ptr, nf);
553 let plugin_out = &self.output_buffers[ch];
554 for (h, &p) in host.iter_mut().zip(plugin_out.iter()) {
555 *h = p.to_f32();
556 }
557 }
558 }
559 }
560
561 unsafe fn build_inner<'a>(
562 &'a mut self,
563 inputs: *const *const f32,
564 outputs: *mut *mut f32,
565 num_in: u32,
566 num_out: u32,
567 num_frames: u32,
568 supports_in_place: bool,
569 ) -> AudioBuffer<'a, S> {
570 const MAX_CHANNELS_TRACKED: usize = 64;
571 // Whether the plugin's chosen precision matches the host's.
572 // When matched, we zero-copy host pointers into the slice
573 // arrays; when not, we widen/narrow through input_copies and
574 // output_buffers.
575 let same_precision = std::any::TypeId::of::<S>() == std::any::TypeId::of::<f32>();
576
577 unsafe {
578 let nf = num_frames as usize;
579 let num_out_u = num_out as usize;
580 let num_in_u = num_in as usize;
581 debug_assert!(
582 num_out_u <= MAX_CHANNELS_TRACKED,
583 "RawBufferScratch::build: alias detection only covers up to {MAX_CHANNELS_TRACKED} \
584 output channels; got {num_out_u}. Channels beyond the cap won't be \
585 detected as aliased.",
586 );
587 let out_ptrs: [Option<*mut f32>; MAX_CHANNELS_TRACKED] = std::array::from_fn(|ch| {
588 if ch < num_out_u {
589 let p = *outputs.add(ch);
590 if p.is_null() { None } else { Some(p) }
591 } else {
592 None
593 }
594 });
595 let aliases_any_output = |in_ptr: *const f32| -> bool {
596 let in_start = in_ptr as usize;
597 let in_end = in_start + nf * std::mem::size_of::<f32>();
598 out_ptrs
599 .iter()
600 .take(num_out_u.min(MAX_CHANNELS_TRACKED))
601 .any(|o| {
602 o.is_some_and(|op| {
603 let o_start = op as usize;
604 let o_end = o_start + nf * std::mem::size_of::<f32>();
605 !(in_end <= o_start || o_end <= in_start)
606 })
607 })
608 };
609
610 // Grow per-channel scratch slots if the bus widened or
611 // we're widening precision and need every channel copied.
612 while self.input_copies.len() < num_in_u {
613 self.input_copies.push(Vec::new());
614 }
615 if !same_precision {
616 while self.output_buffers.len() < num_out_u {
617 self.output_buffers.push(Vec::new());
618 }
619 }
620
621 self.input_slices.clear();
622 self.input_slices.reserve(num_in_u);
623 let mut in_place_mask: u64 = 0;
624 for ch in 0..num_in_u {
625 let ptr = *inputs.add(ch);
626 let slice: &[S] = if ptr.is_null() {
627 &[]
628 } else if aliases_any_output(ptr) {
629 if ch < 64 {
630 in_place_mask |= 1 << ch;
631 }
632 if supports_in_place && same_precision {
633 // Plugin opted in: hand it nothing through
634 // input(ch); it must read+write via in_out_mut.
635 // Only supported in the same-precision case;
636 // the cross-precision path always copies.
637 &[]
638 } else {
639 // Snapshot the input (and widen if needed)
640 // before the plugin overwrites the shared
641 // buffer.
642 let host = std::slice::from_raw_parts(ptr, nf);
643 let copy = &mut self.input_copies[ch];
644 copy.clear();
645 copy.reserve(nf);
646 for &h in host {
647 copy.push(S::from_f32(h));
648 }
649 let p = copy.as_ptr();
650 let l = copy.len();
651 // SAFETY: `copy` lives as long as `self`, which
652 // outlives the returned `AudioBuffer<'a>`.
653 std::slice::from_raw_parts(p, l)
654 }
655 } else if same_precision {
656 // SAFETY: the in-precision case is `&[f32]`. We
657 // transmute via raw parts because the function
658 // signature is generic over S but the runtime
659 // branch knows S == f32.
660 let raw = ptr.cast::<S>();
661 std::slice::from_raw_parts(raw, nf)
662 } else {
663 // Different precision, no aliasing: widen into scratch.
664 let host = std::slice::from_raw_parts(ptr, nf);
665 let copy = &mut self.input_copies[ch];
666 copy.clear();
667 copy.reserve(nf);
668 for &h in host {
669 copy.push(S::from_f32(h));
670 }
671 let p = copy.as_ptr();
672 let l = copy.len();
673 std::slice::from_raw_parts(p, l)
674 };
675 self.input_slices.push(slice);
676 }
677
678 self.output_slices.clear();
679 self.output_slices.reserve(num_out_u);
680 for ch in 0..num_out_u {
681 let ptr = *outputs.add(ch);
682 let slice: &mut [S] = if ptr.is_null() {
683 &mut []
684 } else if same_precision {
685 // SAFETY: same-precision branch - host pointer is
686 // already `*mut S` modulo runtime type identity.
687 let raw = ptr.cast::<S>();
688 std::slice::from_raw_parts_mut(raw, nf)
689 } else {
690 // Different precision: render into per-channel
691 // scratch; finish_widening_f32 copies+narrows back.
692 let buf = &mut self.output_buffers[ch];
693 buf.clear();
694 buf.resize(nf, S::default());
695 let p = buf.as_mut_ptr();
696 let l = buf.len();
697 std::slice::from_raw_parts_mut(p, l)
698 };
699 self.output_slices.push(slice);
700 }
701
702 // SAFETY: Same transmute pattern as AudioBuffer::slice().
703 // RawBufferScratch stores 'static slices but we return AudioBuffer<'a>.
704 let self_ptr: *mut Self = self;
705 let s = &mut *self_ptr;
706 let mut buf = std::mem::transmute::<AudioBuffer<'static, S>, AudioBuffer<'a, S>>(
707 AudioBuffer::from_slices(&s.input_slices, &mut s.output_slices, nf),
708 );
709 buf.set_in_place_mask(in_place_mask);
710 buf
711 }
712 }
713
714 /// Pre-allocate the per-channel scratch vectors so `build` runs
715 /// allocation-free for buses up to `num_in` × `num_out` channels
716 /// and blocks up to `max_frames`. Idempotent and growth-only.
717 pub fn ensure_capacity(&mut self, num_in: usize, num_out: usize, max_frames: usize) {
718 if self.input_slices.capacity() < num_in {
719 self.input_slices
720 .reserve_exact(num_in - self.input_slices.capacity());
721 }
722 if self.output_slices.capacity() < num_out {
723 self.output_slices
724 .reserve_exact(num_out - self.output_slices.capacity());
725 }
726 while self.input_copies.len() < num_in {
727 self.input_copies.push(Vec::with_capacity(max_frames));
728 }
729 for buf in &mut self.input_copies {
730 if buf.capacity() < max_frames {
731 buf.reserve_exact(max_frames - buf.capacity());
732 }
733 }
734 while self.output_buffers.len() < num_out {
735 self.output_buffers.push(Vec::with_capacity(max_frames));
736 }
737 for buf in &mut self.output_buffers {
738 if buf.capacity() < max_frames {
739 buf.reserve_exact(max_frames - buf.capacity());
740 }
741 }
742 }
743}
744
745impl<S: Sample> Default for RawBufferScratch<S> {
746 fn default() -> Self {
747 Self {
748 input_slices: Vec::with_capacity(2),
749 output_slices: Vec::with_capacity(2),
750 input_copies: Vec::with_capacity(2),
751 output_buffers: Vec::with_capacity(2),
752 }
753 }
754}