56 lines
1.8 KiB
Swift
56 lines
1.8 KiB
Swift
//___FILEHEADER___
|
|
//
|
|
// Top-level main.swift — drop this into an executableTarget's Sources directory.
|
|
// NRT mode renders synchronously inside engine.start(); the call blocks until
|
|
// `duration` seconds of audio have been written to `outputURL`.
|
|
|
|
import Foundation
|
|
import LyrebirdSynth
|
|
|
|
// MARK: - Synthesizer
|
|
|
|
/// Replace this with your own Synthesizer. NRT renders whatever is in the engine
|
|
/// at the moment `start()` is called — Synthesizers added later via
|
|
/// `engine.add(_:)` will not be heard because the render loop has already exited.
|
|
final class ___FILEBASENAMEASIDENTIFIER______Synth: SynthesizerBase {
|
|
override func build() {
|
|
let env = EnvelopeGen.Audio(
|
|
envelope: Envelope(
|
|
levels: [0.0, 1.0, 0.6, 0.0],
|
|
durations: [0.02, 0.46, 0.5],
|
|
curves: [.lin, .exponential, .exponential]
|
|
),
|
|
doneAction: .freeNode
|
|
)
|
|
let osc = OscSin.Audio(freq: 440.0) * env * 0.3
|
|
output(bus: 0, source: Pan2(input: osc))
|
|
}
|
|
}
|
|
|
|
// MARK: - Render
|
|
|
|
let outputURL: URL = URL(fileURLWithPath: "output.caf")
|
|
let renderDuration: Double = 4.0
|
|
let blockSize: Int = 512
|
|
let sampleRate: Double = 44100
|
|
let numOutputChannels: Int = 2
|
|
|
|
let engine = SynthesisEngine(
|
|
blockSize: blockSize,
|
|
sampleRate: sampleRate,
|
|
numOutputChannels: numOutputChannels,
|
|
numInputChannels: 0,
|
|
driver: NRTDriver(outputURL: outputURL, duration: renderDuration)
|
|
)
|
|
|
|
// Synthesizers must be added BEFORE start() — NRTDriver renders the full
|
|
// duration synchronously inside start() and returns once the file is closed.
|
|
do {
|
|
try engine.add(___FILEBASENAMEASIDENTIFIER______Synth())
|
|
try engine.start()
|
|
print("Rendered \(renderDuration)s → \(outputURL.path)")
|
|
} catch {
|
|
fputs("Render failed: \(error)\n", stderr)
|
|
exit(1)
|
|
}
|