b6fdee8bfc
Consolidated release: the cross-platform Linux port (engine, generators, CLI, REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive. See RELEASES.md and LINUX_README.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
88 lines
3.5 KiB
Swift
88 lines
3.5 KiB
Swift
// InterleaveHelpers.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Channel-layout conversion shared by audio drivers whose hardware API wants
|
|
// interleaved frames while the engine works in per-channel planar buses.
|
|
//
|
|
// Deliberately free of any platform import: the ALSA driver that consumes these
|
|
// only compiles on Linux, but the conversion itself is ordinary arithmetic and
|
|
// is the easiest part of a driver to get subtly wrong (transposed indices
|
|
// produce audio that is quiet, channel-swapped, or aliased rather than silent).
|
|
// Keeping it here means it compiles — and is tested — on every platform.
|
|
|
|
/// Writes `frameCount` frames of planar `buses` into an interleaved buffer.
|
|
///
|
|
/// Channels beyond `buses.count`, and frames beyond a bus's own length, are
|
|
/// written as silence, so a short or missing bus can never read out of bounds.
|
|
///
|
|
/// - Parameters:
|
|
/// - buses: Planar per-channel sample buffers. Only the first `channelCount`
|
|
/// are read.
|
|
/// - out: Destination for `frameCount * channelCount` interleaved samples.
|
|
/// Must have room for all of them.
|
|
/// - frameCount: Frames to write.
|
|
/// - channelCount: Channels per frame in `out`.
|
|
@_spi(LyrebirdExtensions)
|
|
public func lyrebirdInterleave(
|
|
_ buses: [[Float]],
|
|
into out: UnsafeMutablePointer<Float>,
|
|
frameCount: Int,
|
|
channelCount: Int
|
|
) {
|
|
for channel: Int in 0..<channelCount {
|
|
// Binding the row to a local `[Float]` would retain/release it once per
|
|
// channel per block on the render thread; borrowing it in place through
|
|
// `withUnsafeBufferPointer` does not.
|
|
guard channel < buses.count else {
|
|
for frame: Int in 0..<frameCount {
|
|
out[frame * channelCount + channel] = 0
|
|
}
|
|
continue
|
|
}
|
|
buses[channel].withUnsafeBufferPointer { src in
|
|
guard let base: UnsafePointer<Float> = src.baseAddress else { return }
|
|
let available: Int = min(frameCount, src.count)
|
|
for frame: Int in 0..<available {
|
|
out[frame * channelCount + channel] = base[frame]
|
|
}
|
|
for frame: Int in available..<frameCount {
|
|
out[frame * channelCount + channel] = 0
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Scatters `frameCount` interleaved frames into planar `buses`.
|
|
///
|
|
/// The inverse of ``lyrebirdInterleave(_:into:frameCount:channelCount:)``.
|
|
/// Buses shorter than `frameCount` are filled as far as they go; channels
|
|
/// beyond `buses.count` are dropped. Nothing is resized, so this is safe to
|
|
/// call on the render thread once the buses are pre-allocated.
|
|
///
|
|
/// - Parameters:
|
|
/// - src: Source of `frameCount * channelCount` interleaved samples.
|
|
/// - buses: Planar destination buffers, pre-allocated by the caller.
|
|
/// - frameCount: Frames to read.
|
|
/// - channelCount: Channels per frame in `src`.
|
|
@_spi(LyrebirdExtensions)
|
|
public func lyrebirdDeinterleave(
|
|
_ src: UnsafePointer<Float>,
|
|
into buses: inout [[Float]],
|
|
frameCount: Int,
|
|
channelCount: Int
|
|
) {
|
|
let channels: Int = min(channelCount, buses.count)
|
|
for channel: Int in 0..<channels {
|
|
buses[channel].withUnsafeMutableBufferPointer { dst in
|
|
guard let base: UnsafeMutablePointer<Float> = dst.baseAddress else { return }
|
|
let available: Int = min(frameCount, dst.count)
|
|
for frame: Int in 0..<available {
|
|
base[frame] = src[frame * channelCount + channel]
|
|
}
|
|
}
|
|
}
|
|
}
|