Files
Lyrebird/Sources/Lyrebird/GeneratorCore/ResultBuffer.swift
T
2026-06-01 18:27:40 -07:00

39 lines
1.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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
)
}