135 lines
4.5 KiB
Swift
135 lines
4.5 KiB
Swift
// OfflineRender.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
import AudioToolbox
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
|
|
// MARK: - NRT engine factory
|
|
|
|
/// Creates an `NRTDriver`-backed `SynthesisEngine` and renders to a temporary CAF file.
|
|
///
|
|
/// - Parameters:
|
|
/// - duration: Render duration in seconds.
|
|
/// - blockSize: Audio block size. Defaults to 512.
|
|
/// - sampleRate: Engine sample rate. Defaults to 44100.
|
|
/// - numOutputChannels: Hardware output channel count. Defaults to 2.
|
|
/// - numInputChannels: Hardware input channel count. Defaults to 0.
|
|
/// - numAudioBuses: Total audio bus pool size. Defaults to 64.
|
|
/// - Returns: `(engine, outputURL)` — caller must delete `outputURL` when done.
|
|
@MainActor
|
|
func makeNRTEngine(
|
|
duration: Double,
|
|
blockSize: Int = 512,
|
|
sampleRate: Double = 44100.0,
|
|
numOutputChannels: Int = 2,
|
|
numInputChannels: Int = 0,
|
|
numAudioBuses: Int = 64
|
|
) -> (SynthesisEngine, URL) {
|
|
/// Number of control buses to allocate in the test engine.
|
|
let numControlBuses: Int = 512
|
|
let url: URL = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent(UUID().uuidString + ".caf")
|
|
let driver: NRTDriver = NRTDriver(outputURL: url, duration: duration)
|
|
let engine: SynthesisEngine = SynthesisEngine(
|
|
blockSize: blockSize,
|
|
sampleRate: sampleRate,
|
|
numOutputChannels: numOutputChannels,
|
|
numInputChannels: numInputChannels,
|
|
numAudioBuses: numAudioBuses,
|
|
numControlBuses: numControlBuses,
|
|
driver: driver
|
|
)
|
|
return (engine, url)
|
|
}
|
|
|
|
// MARK: - CAF reader
|
|
|
|
/// Reads all interleaved Float32 samples from a stereo CAF file written by NRTDriver.
|
|
///
|
|
/// - Parameters:
|
|
/// - url: Path of the CAF file.
|
|
/// - sampleRate: Sample rate of the file.
|
|
/// - Returns: Interleaved stereo samples, or an empty array on error.
|
|
func readStereoCAF(url: URL, sampleRate: Double = 44100.0) -> [Float] {
|
|
let stereoChannelCount: UInt32 = 2
|
|
let bytesPerSample: UInt32 = 4
|
|
let bitsPerSample: UInt32 = 32
|
|
let bytesPerFrame: UInt32 = stereoChannelCount * bytesPerSample
|
|
|
|
var fileRef: ExtAudioFileRef?
|
|
guard ExtAudioFileOpenURL(url as CFURL, &fileRef) == noErr,
|
|
let fileRef: ExtAudioFileRef else { return [] }
|
|
defer { ExtAudioFileDispose(fileRef) }
|
|
|
|
var clientFormat: AudioStreamBasicDescription = AudioStreamBasicDescription(
|
|
mSampleRate: sampleRate,
|
|
mFormatID: kAudioFormatLinearPCM,
|
|
mFormatFlags: kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked,
|
|
mBytesPerPacket: bytesPerFrame,
|
|
mFramesPerPacket: 1,
|
|
mBytesPerFrame: bytesPerFrame,
|
|
mChannelsPerFrame: stereoChannelCount,
|
|
mBitsPerChannel: bitsPerSample,
|
|
mReserved: 0
|
|
)
|
|
guard ExtAudioFileSetProperty(
|
|
fileRef,
|
|
kExtAudioFileProperty_ClientDataFormat,
|
|
UInt32(MemoryLayout<AudioStreamBasicDescription>.size),
|
|
&clientFormat
|
|
) == noErr else { return [] }
|
|
|
|
let readFrames: UInt32 = 4096
|
|
var samples: [Float] = [Float](repeating: 0, count: Int(readFrames) * 2)
|
|
var framesRead: UInt32 = readFrames
|
|
samples.withUnsafeMutableBufferPointer { ptr in
|
|
var bufList: AudioBufferList = AudioBufferList(
|
|
mNumberBuffers: 1,
|
|
mBuffers: AudioBuffer(
|
|
mNumberChannels: 2,
|
|
mDataByteSize: UInt32(ptr.count * MemoryLayout<Float>.size),
|
|
mData: ptr.baseAddress
|
|
)
|
|
)
|
|
_ = ExtAudioFileRead(fileRef, &framesRead, &bufList)
|
|
}
|
|
return Array(samples.prefix(Int(framesRead) * 2))
|
|
}
|
|
|
|
// MARK: - Signal analysis
|
|
|
|
/// Returns `true` if any sample in `samples` has absolute value greater than `threshold`.
|
|
func hasNonSilentOutput(_ samples: [Float], threshold: Float = 1e-6) -> Bool {
|
|
samples.contains { abs($0) > threshold }
|
|
}
|
|
|
|
// MARK: - TestRouteMaster
|
|
|
|
/// A minimal `SynthesizerBase` that reads a mono bus and routes it to hardware stereo output.
|
|
/// Used only in tests — not part of the public ShoeGaze API.
|
|
final class TestRouteMaster: SynthesizerBase {
|
|
|
|
private let inputBusIndex: Int
|
|
|
|
/// Gain applied before panning to hardware output.
|
|
private let outputGain: Double = 0.8
|
|
|
|
/// Stereo pan position (0 = centre).
|
|
private let pan: Double = 0.0
|
|
|
|
init(inputBusIndex: Int) {
|
|
self.inputBusIndex = inputBusIndex
|
|
super.init()
|
|
}
|
|
|
|
override func build() {
|
|
let sig: In.Audio = input(bus: inputBusIndex)
|
|
output(bus: 0, source: Pan2(input: sig * outputGain, pan: pan))
|
|
}
|
|
}
|