Files
Lyrebird/Sources/Lyrebird/Engine/NRTJob.swift
T

115 lines
4.7 KiB
Swift

// NRTJob.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// One unit of work for `EngineHost.renderConcurrent(jobs:)`.
///
/// Each job carries the parameters needed to spin up a fresh
/// `SynthesisEngine` with an `NRTDriver`, build the synthesis graph,
/// and render to a destination file.
///
/// **Engine isolation.** Each job constructs its own `SynthesisEngine`
/// — engines are never shared across jobs. The binding
/// system guarantees that `Synthesizer` instances added to one job's
/// engine cannot leak into another job's engine. Build a fresh
/// graph inside `build` per job; do not capture and reuse synthesizer
/// instances across jobs.
///
/// **No host registration.** Pool jobs do not register their engines
/// in the host's role registry. The engine's lifetime is bounded by
/// the `render` closure body — it deinits when the closure returns,
/// at which point `EngineLifecycleCoordinator.shared.unregister(...)`
/// fires automatically.
public struct NRTJob: Sendable {
/// Destination file URL the rendered audio writes to. The file is
/// created by the driver at `engine.start()` time, using ``format``.
public let outputURL: URL
/// Container and PCM encoding for ``outputURL``. Maps to
/// `NRTDriver.init(format:)`. Defaults to `.cafFloat32` — CAF is
/// 64-bit-sized (no 4 GB cap) and carries any channel count, which
/// suits long offline renders and multichannel stems.
public let format: AudioFileFormat
/// Render duration in seconds. Maps to `NRTDriver.init(duration:)`.
public let duration: Double
/// Audio render block size in samples. Defaults to 512 — matches
/// `lyrebirdDefaultResultBufferSize` and the engine-level cap.
public let blockSize: Int
/// Sample rate in Hz.
public let sampleRate: Double
/// Number of hardware output channels. Typical: 2 for stereo, 4
/// for quad NRT batch.
public let numOutputChannels: Int
/// Number of hardware input channels the engine reserves. The NRT
/// driver clamps the input file's channel count against this value
/// at render time, so jobs that read fewer channels than the file
/// contains receive only the first `numInputChannels` channels; jobs
/// that reserve more than the file provides receive silence on the
/// excess channels. Pass `0` for pure synthesis jobs (the default);
/// pass the channel count you want exposed to the synth graph when
/// `inputURL` is set.
public let numInputChannels: Int
/// Optional source input file URL passed through to
/// `NRTDriver(inputURL:)`. `nil` for pure synthesis jobs. To
/// actually receive samples from the input file, `numInputChannels`
/// must be ≥ 1 — the host passes `numInputChannels` straight
/// through to `SynthesisEngine.init`, and the driver clamps file
/// channels against it.
public let inputURL: URL?
/// Seconds to skip into `inputURL` before render begins.
public let inputStartOffset: Double
/// Closure invoked once per render with the fresh engine. Add
/// synthesizers via `engine.add(_:)` here, *before* the host
/// calls `engine.start()`. The closure may throw — a throw
/// propagates via the `AsyncThrowingStream` returned from
/// `renderConcurrent`.
///
/// **Threading.** The closure runs on a background `Task` executor
/// — not the main thread. If your build logic needs to touch
/// `@MainActor`-isolated state (e.g. an `@Observable` parameter
/// tree owned by the UI), capture only the value-typed parameters
/// into the closure and read them inside, or pre-build a
/// `@Sendable` snapshot before assembling the job list.
///
/// `@Sendable` so the host can dispatch the closure onto a
/// background task.
public let build: @Sendable (SynthesisEngine) throws -> Void
public init(
outputURL: URL,
duration: Double,
format: AudioFileFormat = .cafFloat32,
blockSize: Int = lyrebirdDefaultResultBufferSize,
sampleRate: Double = lyrebirdDefaultSampleRate,
numOutputChannels: Int = 2,
numInputChannels: Int = 0,
inputURL: URL? = nil,
inputStartOffset: Double = 0.0,
build: @escaping @Sendable (SynthesisEngine) throws -> Void
) {
self.outputURL = outputURL
self.format = format
self.duration = duration
self.blockSize = blockSize
self.sampleRate = sampleRate
self.numOutputChannels = numOutputChannels
self.numInputChannels = numInputChannels
self.inputURL = inputURL
self.inputStartOffset = inputStartOffset
self.build = build
}
}