39 lines
1.5 KiB
Swift
39 lines
1.5 KiB
Swift
// ResultBuffer.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
/// Factories for Generator output / scratch buffers.
|
||
///
|
||
/// These helpers replace the boilerplate `[Double](repeating: 0, count: lyrebirdDefaultResultBufferSize)`
|
||
/// and `[[Double](repeating: 0, count: lyrebirdDefaultResultBufferSize)]` literals scattered
|
||
/// across the codebase. Centralising the allocation site keeps the buffer-size constant in one
|
||
/// place and removes a recurring magic-number-ish literal that style review flags.
|
||
///
|
||
/// Both helpers are `@inlinable` and called exclusively from `init` paths — never on the RT
|
||
/// thread.
|
||
|
||
/// One channel (row) of pre-allocated, zero-filled output samples.
|
||
///
|
||
/// - Parameter count: Sample count. Defaults to `lyrebirdDefaultResultBufferSize` (512).
|
||
/// - Returns: A `[Double]` of length `count`, zero-filled.
|
||
@inlinable
|
||
public func makeSampleBuffer(_ count: Int = lyrebirdDefaultResultBufferSize) -> [Double] {
|
||
[Double](repeating: 0, count: count)
|
||
}
|
||
|
||
/// A pre-allocated, zero-filled `_result` buffer with the requested channel count.
|
||
///
|
||
/// Each row is sized to `lyrebirdDefaultResultBufferSize`.
|
||
///
|
||
/// - Parameter channels: Number of channels (rows). Defaults to `1` (mono).
|
||
/// - Returns: `[[Double]]` shaped as `channels × lyrebirdDefaultResultBufferSize`.
|
||
@inlinable
|
||
public func makeResultBuffer(channels: Int = 1) -> [[Double]] {
|
||
Array(
|
||
repeating: makeSampleBuffer(),
|
||
count: channels
|
||
)
|
||
}
|