Files
Lyrebird/Sources/LyrebirdCLI/Resources/XcodeTemplates/LyrebirdBufferReader.xctemplate/___FILEBASENAME___.swift
T
2026-06-01 18:27:40 -07:00

88 lines
3.0 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.
//___FILEHEADER___
import Foundation
import LyrebirdSynth
// MARK: - ___FILEBASENAMEASIDENTIFIER___
/// Sample-playback Synthesizer that loads an audio file at init and plays it
/// through `PlayBuffer.Audio`. Output channel count matches the loaded buffer.
///
/// `LyrebirdBuffer.loadAllChannels(url:)` reads every channel into a single
/// multichannel buffer and records the file's sample rate on it; passing
/// `buffer.nativeRateScale(engineSampleRate:)` to `PlayBuffer.Audio` rate:
/// ensures the file plays back at the correct pitch / speed when the engine
/// runs at a different sample rate than the file.
///
/// ## Usage
/// ```swift
/// let player = try ___FILEBASENAMEASIDENTIFIER___(
/// fileURL: URL(fileURLWithPath: "/path/to/sample.wav"),
/// loop: true,
/// engineSampleRate: engine.sampleRate
/// )
/// try engine.add(player)
///
/// // Modulate playback rate at runtime:
/// player.rate = 0.5 // half speed / one octave down
/// ```
public final class ___FILEBASENAMEASIDENTIFIER___: SynthesizerBase {
// MARK: - Parameters
/// Playback rate multiplier. `1.0` = native pitch, `2.0` = +1 octave,
/// `0.5` = −1 octave. Negative values reverse playback if the underlying
/// PlayBuffer is configured to honour them.
@LiveParam public var rate: Double = 1.0
/// Output amplitude (linear gain).
@LiveParam public var amplitude: Double = 1.0
// MARK: - State
private let buffer: LyrebirdBuffer
private let loop: Bool
private let engineSampleRate: Double
// MARK: - Init
/// - Parameters:
/// - fileURL: Path to a WAV / AIFF / CAF audio file. Loaded synchronously
/// on a non-RT thread; do not call from the audio callback.
/// - loop: When `true`, playback restarts from frame 0 after the end.
/// When `false`, the generator emits silence after the final frame
/// (or frees the node if `doneAction == .freeNode`).
/// - engineSampleRate: The engine's sample rate. Passed at init so the
/// `build()` closure (which has no engine reference) can compute the
/// native-sample-rate compensation. Get from `engine.sampleRate`.
public init(
fileURL: URL,
loop: Bool = false,
engineSampleRate: Double
) throws {
self.buffer = try LyrebirdBuffer.loadAllChannels(url: fileURL)
self.loop = loop
self.engineSampleRate = engineSampleRate
super.init()
}
// MARK: - Build
public override func build() {
// Compose rate: user-controlled `rate` multiplier × the buffer's
// native-sample-rate compensation. nativeRateScale falls back to 1.0
// when the buffer carries no recorded rate.
let nativeScale = buffer.nativeRateScale(engineSampleRate: engineSampleRate)
let combinedRate = $rate * nativeScale
let player = PlayBuffer.Audio(
buffer: buffer,
loop: loop,
doneAction: loop ? .nothing : .freeNode,
rate: combinedRate
)
output(bus: 0, source: player * $amplitude)
}
}