683 lines
25 KiB
Swift
683 lines
25 KiB
Swift
// ProjectScaffolder.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
|
|
// MARK: - ProjectTemplate
|
|
|
|
/// Template style for a scaffolded Lyrebird project.
|
|
public enum ProjectTemplate: String, CaseIterable, Equatable {
|
|
/// Minimal engine setup with `CoreAudioDriver` and `RunLoop.main.run()`.
|
|
case basic = "basic"
|
|
/// Real-time demo: `SineTone` synthesizer with `@LiveParam` frequency and amplitude.
|
|
case demo = "demo"
|
|
/// GUI demo: `@UIParam` synthesizer with a `LyrebirdWindow` SwiftUI panel.
|
|
case uiDemo = "ui-demo"
|
|
/// Non-real-time render: `NRTDriver` writes a `.caf` file and exits.
|
|
case nrt = "nrt"
|
|
/// macOS `.app` bundle wrapping an AppKit + SwiftUI host of a `@UIParam` synth.
|
|
/// Generates a full `<name>.xcodeproj` referencing Lyrebird via remote SPM dep.
|
|
case macosUIXcode = "macos-ui-xcode"
|
|
/// iOS `.app` bundle with `iOSAudioDriver` + SwiftUI UI.
|
|
/// Generates a full `<name>.xcodeproj` referencing Lyrebird via remote SPM dep.
|
|
case iosUI = "ios-ui"
|
|
/// AUv3 host `.app` + `aumu` plugin extension wrapping a Lyrebird synth.
|
|
/// Generates a full `<name>.xcodeproj` with two targets and entitlements files.
|
|
case auv3 = "auv3"
|
|
|
|
/// Case-insensitive init from a raw string. Accepts hyphen-free aliases too.
|
|
public init?(string: String) {
|
|
switch string.lowercased() {
|
|
case "basic": self = .basic
|
|
case "demo": self = .demo
|
|
case "ui-demo", "uidemo", "ui_demo": self = .uiDemo
|
|
case "nrt": self = .nrt
|
|
case "macos-ui-xcode", "macosuixcode", "macos_ui_xcode":
|
|
self = .macosUIXcode
|
|
case "ios-ui", "iosui", "ios_ui":
|
|
self = .iosUI
|
|
case "auv3", "au-v3", "au_v3":
|
|
self = .auv3
|
|
default: return nil
|
|
}
|
|
}
|
|
|
|
/// True when the template emits a full `.xcodeproj` skeleton in addition
|
|
/// to the Swift Package. The simpler four templates (basic/demo/ui-demo/nrt)
|
|
/// rely on Xcode's "Open Package.swift" path; these three need a real
|
|
/// `.xcodeproj` because they ship an `.app` bundle, app entitlements, or
|
|
/// (auv3) an `.appex` extension target — none of which SwiftPM can express.
|
|
public var shipsXcodeProject: Bool {
|
|
switch self {
|
|
case .basic, .demo, .uiDemo, .nrt: return false
|
|
case .macosUIXcode, .iosUI, .auv3: return true
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - ProjectScaffolder
|
|
|
|
/// Scaffolds a new Lyrebird project as a Swift Package.
|
|
///
|
|
/// `lyrebird new <name>` creates:
|
|
///
|
|
/// ```
|
|
/// <name>/
|
|
/// ├── Package.swift
|
|
/// ├── <name>.xcworkspace/
|
|
/// │ └── contents.xcworkspacedata (references Package.swift — open in Xcode)
|
|
/// ├── Sources/
|
|
/// │ └── <name>/
|
|
/// │ └── main.swift (+ AppDelegate.swift, <name>Synth.swift for ui-demo)
|
|
/// └── Tests/
|
|
/// └── <name>Tests/
|
|
/// └── <name>Tests.swift
|
|
/// ```
|
|
///
|
|
/// Pass `--template demo`, `--template ui-demo`, or `--template nrt` to get richer
|
|
/// starting points.
|
|
public enum ProjectScaffolder {
|
|
|
|
// MARK: - Errors
|
|
|
|
/// Errors thrown by `ProjectScaffolder`.
|
|
public enum Error: Swift.Error, Equatable {
|
|
/// The project directory already exists.
|
|
case projectExists(name: String)
|
|
/// The project name is empty or contains characters not valid in a Swift module name.
|
|
case invalidName(name: String)
|
|
/// A file or directory could not be created.
|
|
case fileSystemError(path: String, reason: String)
|
|
}
|
|
|
|
// MARK: - Public API
|
|
|
|
/// Scaffold a new Lyrebird project named `name` inside `parentDirectory`.
|
|
///
|
|
/// - Parameters:
|
|
/// - name: The project name. Must be a non-empty identifier valid as a Swift module name.
|
|
/// - template: Starter template style (default `.basic`).
|
|
/// - parentDirectory: Directory in which to create the project folder.
|
|
/// Defaults to the current working directory.
|
|
/// - lyrebirdPath: Optional absolute path to a local Lyrebird checkout
|
|
/// to wire into the generated `Package.swift` as a `path:` dependency
|
|
/// (useful when developing against an unmerged Lyrebird branch).
|
|
/// `nil` → use the published GitHub package as the dependency.
|
|
/// - Throws: `ProjectScaffolder.Error` on name validation failure or file system errors.
|
|
public static func scaffold(
|
|
name: String,
|
|
template: ProjectTemplate = .basic,
|
|
in parentDirectory: String = FileManager.default.currentDirectoryPath,
|
|
lyrebirdPath: String? = nil
|
|
) throws {
|
|
try validateName(name)
|
|
|
|
let projectRoot: String = (parentDirectory as NSString).appendingPathComponent(name)
|
|
let fm: FileManager = FileManager.default
|
|
|
|
guard !fm.fileExists(atPath: projectRoot) else {
|
|
throw Error.projectExists(name: name)
|
|
}
|
|
|
|
do {
|
|
try fm.createDirectory(atPath: projectRoot,
|
|
withIntermediateDirectories: true,
|
|
attributes: nil)
|
|
} catch {
|
|
throw Error.fileSystemError(path: projectRoot,
|
|
reason: error.localizedDescription)
|
|
}
|
|
|
|
if template.shipsXcodeProject {
|
|
try scaffoldXcodeProject(name: name,
|
|
template: template,
|
|
projectRoot: projectRoot,
|
|
lyrebirdPath: lyrebirdPath,
|
|
fm: fm)
|
|
} else {
|
|
try scaffoldSwiftPackage(name: name,
|
|
template: template,
|
|
projectRoot: projectRoot,
|
|
lyrebirdPath: lyrebirdPath,
|
|
fm: fm)
|
|
}
|
|
}
|
|
|
|
// MARK: - SPM-only scaffold
|
|
|
|
private static func scaffoldSwiftPackage(
|
|
name: String,
|
|
template: ProjectTemplate,
|
|
projectRoot: String,
|
|
lyrebirdPath: String?,
|
|
fm: FileManager
|
|
) throws {
|
|
let sourcesDir: String = (projectRoot as NSString)
|
|
.appendingPathComponent("Sources/\(name)")
|
|
let testsDir: String = (projectRoot as NSString)
|
|
.appendingPathComponent("Tests/\(name)Tests")
|
|
let workspaceDir: String = (projectRoot as NSString)
|
|
.appendingPathComponent("\(name).xcworkspace")
|
|
|
|
for dir: String in [sourcesDir, testsDir, workspaceDir] {
|
|
do {
|
|
try fm.createDirectory(atPath: dir,
|
|
withIntermediateDirectories: true,
|
|
attributes: nil)
|
|
} catch {
|
|
try? fm.removeItem(atPath: projectRoot)
|
|
throw Error.fileSystemError(path: dir,
|
|
reason: error.localizedDescription)
|
|
}
|
|
}
|
|
|
|
var files: [(path: String, content: String)] = [
|
|
(
|
|
(projectRoot as NSString).appendingPathComponent("Package.swift"),
|
|
packageManifest(name: name, template: template, lyrebirdPath: lyrebirdPath)
|
|
),
|
|
(
|
|
(sourcesDir as NSString).appendingPathComponent("main.swift"),
|
|
mainSwift(name: name, template: template)
|
|
),
|
|
(
|
|
(testsDir as NSString).appendingPathComponent("\(name)Tests.swift"),
|
|
testsSwift(name: name)
|
|
),
|
|
(
|
|
(workspaceDir as NSString).appendingPathComponent("contents.xcworkspacedata"),
|
|
workspaceContents()
|
|
),
|
|
]
|
|
|
|
if template == .uiDemo {
|
|
files += [
|
|
(
|
|
(sourcesDir as NSString).appendingPathComponent("AppDelegate.swift"),
|
|
appDelegateSwift(name: name)
|
|
),
|
|
(
|
|
(sourcesDir as NSString).appendingPathComponent("\(name)Synth.swift"),
|
|
synthSwift(name: name)
|
|
),
|
|
]
|
|
}
|
|
|
|
try writeFiles(files.map { ($0.path, $0.content) },
|
|
fm: fm, projectRoot: projectRoot)
|
|
}
|
|
|
|
// MARK: - Xcode-project scaffold dispatch
|
|
|
|
private static func scaffoldXcodeProject(
|
|
name: String,
|
|
template: ProjectTemplate,
|
|
projectRoot: String,
|
|
lyrebirdPath: String?,
|
|
fm: FileManager
|
|
) throws {
|
|
// Every xcode-bearing template ships an `<name>.xcworkspace` that
|
|
// references the generated `<name>.xcodeproj`. The workspace is
|
|
// separate from the SPM-only `workspaceContents()` shape because
|
|
// there is no `Package.swift` at the project root here.
|
|
let workspaceDir: String = (projectRoot as NSString)
|
|
.appendingPathComponent("\(name).xcworkspace")
|
|
do {
|
|
try fm.createDirectory(atPath: workspaceDir,
|
|
withIntermediateDirectories: true,
|
|
attributes: nil)
|
|
} catch {
|
|
try? fm.removeItem(atPath: projectRoot)
|
|
throw Error.fileSystemError(path: workspaceDir,
|
|
reason: error.localizedDescription)
|
|
}
|
|
|
|
let workspaceFile: String = (workspaceDir as NSString)
|
|
.appendingPathComponent("contents.xcworkspacedata")
|
|
try writeFiles([(workspaceFile,
|
|
xcodeWorkspaceContents(name: name,
|
|
lyrebirdPath: lyrebirdPath))],
|
|
fm: fm, projectRoot: projectRoot)
|
|
|
|
switch template {
|
|
case .macosUIXcode:
|
|
try writeMacOSUIXcodeFiles(name: name, projectRoot: projectRoot,
|
|
lyrebirdPath: lyrebirdPath, fm: fm)
|
|
case .iosUI:
|
|
try writeIOSUIFiles(name: name, projectRoot: projectRoot,
|
|
lyrebirdPath: lyrebirdPath, fm: fm)
|
|
case .auv3:
|
|
try writeAUv3Files(name: name, projectRoot: projectRoot,
|
|
lyrebirdPath: lyrebirdPath, fm: fm)
|
|
case .basic, .demo, .uiDemo, .nrt:
|
|
// Unreachable — shipsXcodeProject filters these out at dispatch.
|
|
try? fm.removeItem(atPath: projectRoot)
|
|
throw Error.fileSystemError(
|
|
path: projectRoot,
|
|
reason: "scaffoldXcodeProject called with non-Xcode template \(template.rawValue)"
|
|
)
|
|
}
|
|
}
|
|
|
|
/// Workspace XML for xcode-bearing templates — references the generated
|
|
/// `<name>.xcodeproj`. When `lyrebirdPath` is set, the workspace also
|
|
/// references the Lyrebird `Package.swift` at that absolute path so
|
|
/// Xcode's IDE-level SPM resolver picks it up at workspace open.
|
|
/// Without this extra FileRef, opening the workspace in Xcode (vs.
|
|
/// running `xcodebuild` from the CLI) can fail with "Missing package
|
|
/// product 'LyrebirdUI'" — the IDE resolver doesn't always crawl into
|
|
/// member-project `XCLocalSwiftPackageReference` deps automatically.
|
|
public static func xcodeWorkspaceContents(
|
|
name: String,
|
|
lyrebirdPath: String? = nil
|
|
) -> String {
|
|
var refs: String = " <FileRef\n location = \"group:\(name).xcodeproj\">\n </FileRef>"
|
|
if let path: String = lyrebirdPath {
|
|
let escaped: String = pbxQuoteEscape(path)
|
|
refs += "\n <FileRef\n location = \"absolute:\(escaped)\">\n </FileRef>"
|
|
}
|
|
return """
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<Workspace
|
|
version = "1.0">
|
|
\(refs)
|
|
</Workspace>
|
|
"""
|
|
}
|
|
|
|
// MARK: - Template constants
|
|
|
|
// Module-internal so extension files (Xcode-template generators) can
|
|
// reach them. Single source of truth for the engine defaults written
|
|
// into every scaffolded `main.swift` / app delegate / iOS app shell.
|
|
static let scaffoldDefaultBlockSize: Int = 512
|
|
static let scaffoldDefaultSampleRate: Int = 44100
|
|
static let scaffoldDefaultOutputChannels: Int = 2
|
|
static let scaffoldDefaultInputChannels: Int = 2
|
|
static let scaffoldDefaultNRTDuration: Double = 4.0
|
|
|
|
// MARK: - Package manifest
|
|
|
|
/// `Package.swift` content for a project using the given template.
|
|
///
|
|
/// - Parameters:
|
|
/// - name: Module / package name written into the generated manifest.
|
|
/// Must already be validated as a legal Swift identifier.
|
|
/// - template: Starter template the manifest is tailored for. Controls
|
|
/// which target/product entries appear. Defaults to `.basic`.
|
|
/// - lyrebirdPath: When non-nil, Lyrebird is declared as a local SPM
|
|
/// dependency (`.package(path: ...)`) at this absolute path. When
|
|
/// nil, falls back to the published Git URL (``lyrebirdPackageURL``).
|
|
/// Used by `lyrebird new --lyrebird-path` for users who don't yet
|
|
/// have access to the public repository.
|
|
public static func packageManifest(
|
|
name: String,
|
|
template: ProjectTemplate = .basic,
|
|
lyrebirdPath: String? = nil
|
|
) -> String {
|
|
// byName resolves only when the product name matches the package
|
|
// name (`Lyrebird`). Sibling products (`LyrebirdUI`) need the
|
|
// explicit `.product(name:package:)` form regardless of whether
|
|
// the dep is local or remote.
|
|
let targetDeps: String
|
|
switch template {
|
|
case .basic, .demo, .nrt:
|
|
targetDeps = #""Lyrebird""#
|
|
case .uiDemo:
|
|
targetDeps = #""Lyrebird", .product(name: "LyrebirdUI", package: "Lyrebird")"#
|
|
case .macosUIXcode, .iosUI, .auv3:
|
|
// Xcode-bearing templates don't emit a `Package.swift` — dispatch
|
|
// never reaches this function for them. Defensive fallthrough.
|
|
targetDeps = #""Lyrebird", .product(name: "LyrebirdUI", package: "Lyrebird")"#
|
|
}
|
|
let depLine: String
|
|
if let path: String = lyrebirdPath {
|
|
// `name:` is required so byName target deps (e.g. `"Lyrebird"`)
|
|
// resolve — SPM otherwise uses the directory basename as the
|
|
// package identity, which is unlikely to match `"Lyrebird"`
|
|
// (worktree paths, renamed checkouts, etc.).
|
|
// Path escaped for safe embedding in a Swift string literal —
|
|
// same `\\` then `"` escape semantics as `pbxQuoteEscape`.
|
|
let escapedPath: String = pbxQuoteEscape(path)
|
|
depLine = #".package(name: "Lyrebird", path: "\#(escapedPath)"),"#
|
|
} else {
|
|
depLine = #".package(url: "\#(lyrebirdPackageURL)", from: "\#(lyrebirdMinimumVersion)"),"#
|
|
}
|
|
return """
|
|
// swift-tools-version: 6.0
|
|
import PackageDescription
|
|
|
|
let package = Package(
|
|
name: "\(name)",
|
|
platforms: [.macOS(.v15)],
|
|
dependencies: [
|
|
\(depLine)
|
|
],
|
|
targets: [
|
|
.executableTarget(
|
|
name: "\(name)",
|
|
dependencies: [\(targetDeps)]
|
|
),
|
|
.testTarget(
|
|
name: "\(name)Tests",
|
|
dependencies: ["\(name)"]
|
|
),
|
|
]
|
|
)
|
|
"""
|
|
}
|
|
|
|
// MARK: - main.swift templates
|
|
|
|
/// `main.swift` content for the given template (defaults to `.basic`).
|
|
public static func mainSwift(
|
|
name: String,
|
|
template: ProjectTemplate = .basic
|
|
) -> String {
|
|
switch template {
|
|
case .basic: return mainSwiftBasic(name: name)
|
|
case .demo: return mainSwiftDemo(name: name)
|
|
case .uiDemo: return mainSwiftUIDemo(name: name)
|
|
case .nrt: return mainSwiftNRT(name: name)
|
|
case .macosUIXcode, .iosUI, .auv3:
|
|
// Xcode-bearing templates don't emit a `main.swift` under
|
|
// `Sources/<name>/`. Dispatch never reaches this function for
|
|
// them. Defensive fallthrough to the basic skeleton.
|
|
return mainSwiftBasic(name: name)
|
|
}
|
|
}
|
|
|
|
// MARK: basic
|
|
|
|
private static func mainSwiftBasic(name: String) -> String {
|
|
"""
|
|
// \(name) — entry point
|
|
import Foundation
|
|
import LyrebirdSynth
|
|
|
|
// Configure the synthesis engine.
|
|
let engine = SynthesisEngine(
|
|
blockSize: \(scaffoldDefaultBlockSize),
|
|
sampleRate: \(scaffoldDefaultSampleRate),
|
|
numOutputChannels: \(scaffoldDefaultOutputChannels),
|
|
numInputChannels: \(scaffoldDefaultInputChannels),
|
|
driver: CoreAudioDriver()
|
|
)
|
|
|
|
// Add your Synthesizers here.
|
|
// try engine.add(MySynth())
|
|
|
|
do {
|
|
try engine.start()
|
|
} catch {
|
|
fatalError("Engine failed to start: \\(error)")
|
|
}
|
|
|
|
print("\\(name) running — press Ctrl+C to stop")
|
|
RunLoop.main.run()
|
|
"""
|
|
}
|
|
|
|
// MARK: demo
|
|
|
|
private static func mainSwiftDemo(name: String) -> String {
|
|
"""
|
|
// \(name) — real-time demo
|
|
import Foundation
|
|
import LyrebirdSynth
|
|
|
|
// MARK: - SineTone
|
|
|
|
final class SineTone: SynthesizerBase {
|
|
@LiveParam var freq: Double = 440.0
|
|
@LiveParam var amp: Double = 0.25
|
|
|
|
override func build() {
|
|
let osc = OscSin.Audio(freq: $freq)
|
|
output(bus: 0, source: Pan2(input: osc * $amp))
|
|
}
|
|
}
|
|
|
|
// MARK: - Main
|
|
|
|
let engine = SynthesisEngine(
|
|
blockSize: \(scaffoldDefaultBlockSize),
|
|
sampleRate: \(scaffoldDefaultSampleRate),
|
|
numOutputChannels: \(scaffoldDefaultOutputChannels),
|
|
numInputChannels: \(scaffoldDefaultInputChannels),
|
|
driver: CoreAudioDriver()
|
|
)
|
|
|
|
do {
|
|
try engine.start()
|
|
} catch {
|
|
fatalError("Engine failed to start: \\(error)")
|
|
}
|
|
|
|
let tone = SineTone()
|
|
do {
|
|
try engine.add(tone)
|
|
} catch {
|
|
fatalError("Engine.add failed: \\(error)")
|
|
}
|
|
|
|
print("\\(name) running — press Ctrl+C to stop")
|
|
print("Update tone.freq or tone.amp from the REPL to change parameters.")
|
|
RunLoop.main.run()
|
|
"""
|
|
}
|
|
|
|
// MARK: ui-demo (main.swift only — AppDelegate and Synth are separate files)
|
|
|
|
private static func mainSwiftUIDemo(name: String) -> String {
|
|
"""
|
|
// \(name) — GUI demo
|
|
import AppKit
|
|
import LyrebirdSynth
|
|
|
|
let engine = SynthesisEngine(
|
|
blockSize: \(scaffoldDefaultBlockSize),
|
|
sampleRate: \(scaffoldDefaultSampleRate),
|
|
numOutputChannels: \(scaffoldDefaultOutputChannels),
|
|
numInputChannels: 0,
|
|
driver: CoreAudioDriver()
|
|
)
|
|
|
|
let delegate = AppDelegate(engine: engine)
|
|
NSApplication.shared.setActivationPolicy(.regular)
|
|
NSApplication.shared.delegate = delegate
|
|
NSApplication.shared.run()
|
|
"""
|
|
}
|
|
|
|
// MARK: nrt
|
|
|
|
private static func mainSwiftNRT(name: String) -> String {
|
|
"""
|
|
// \(name) — non-real-time render
|
|
import Foundation
|
|
import LyrebirdSynth
|
|
|
|
// MARK: - \(name)Synth
|
|
|
|
final class \(name)Synth: SynthesizerBase {
|
|
override func build() {
|
|
let env = EnvelopeGen.Audio(
|
|
envelope: Envelope(
|
|
levels: [0, 1, 0.6, 0],
|
|
durations: [0.02, 0.46, 0.5],
|
|
curves: [0, 0, 0]
|
|
)
|
|
)
|
|
let osc = OscSin.Audio(freq: 440.0) * env * 0.3
|
|
output(bus: 0, source: Pan2(input: osc))
|
|
}
|
|
}
|
|
|
|
// MARK: - Render
|
|
|
|
let outputURL = URL(fileURLWithPath: "output.caf")
|
|
let renderDuration = \(scaffoldDefaultNRTDuration)
|
|
|
|
let engine = SynthesisEngine(
|
|
blockSize: \(scaffoldDefaultBlockSize),
|
|
sampleRate: \(scaffoldDefaultSampleRate),
|
|
numOutputChannels: \(scaffoldDefaultOutputChannels),
|
|
numInputChannels: 0,
|
|
driver: NRTDriver(outputURL: outputURL, duration: renderDuration)
|
|
)
|
|
|
|
// Add synthesizers BEFORE start() — NRTDriver renders synchronously inside start().
|
|
do {
|
|
try engine.add(\(name)Synth())
|
|
} catch {
|
|
fputs("engine.add failed: \\(error)\\n", stderr)
|
|
exit(1)
|
|
}
|
|
|
|
do {
|
|
try engine.start()
|
|
print("Rendered \\(renderDuration)s → \\(outputURL.path)")
|
|
} catch {
|
|
fputs("Render failed: \\(error)\\n", stderr)
|
|
exit(1)
|
|
}
|
|
"""
|
|
}
|
|
|
|
// MARK: - ui-demo additional files
|
|
|
|
/// `AppDelegate.swift` content for a ui-demo project.
|
|
public static func appDelegateSwift(name: String) -> String {
|
|
"""
|
|
import AppKit
|
|
import SwiftUI
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
|
|
final class AppDelegate: NSObject, NSApplicationDelegate {
|
|
let engine: SynthesisEngine
|
|
let synth: \(name)Synth
|
|
var window: LyrebirdWindow?
|
|
|
|
init(engine: SynthesisEngine) {
|
|
self.engine = engine
|
|
self.synth = \(name)Synth()
|
|
}
|
|
|
|
@MainActor
|
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
|
do {
|
|
try engine.start()
|
|
} catch {
|
|
print("Engine.start failed: \\(error)")
|
|
NSApp.terminate(nil)
|
|
return
|
|
}
|
|
do {
|
|
try engine.add(synth)
|
|
} catch {
|
|
print("Engine.add failed: \\(error)")
|
|
engine.stop()
|
|
NSApp.terminate(nil)
|
|
return
|
|
}
|
|
|
|
window = LyrebirdWindow(title: "\(name)", width: 320, height: 200)
|
|
window?.open {
|
|
VStack(spacing: 16) {
|
|
ParameterSlider("Frequency", node: self.synth.$freq, range: 110...1760)
|
|
ParameterKnob("Amplitude", node: self.synth.$amp, range: 0...1)
|
|
ParameterKnob("Pan", node: self.synth.$pan, range: -1...1)
|
|
}
|
|
.padding(24)
|
|
}
|
|
}
|
|
|
|
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
|
|
true
|
|
}
|
|
|
|
func applicationWillTerminate(_ notification: Notification) {
|
|
engine.stop()
|
|
}
|
|
}
|
|
"""
|
|
}
|
|
|
|
/// `<name>Synth.swift` content for a ui-demo project.
|
|
public static func synthSwift(name: String) -> String {
|
|
"""
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
|
|
final class \(name)Synth: SynthesizerBase {
|
|
@UIParam var freq: Double = 440.0
|
|
@UIParam var amp: Double = 0.25
|
|
@UIParam var pan: Double = 0.0
|
|
|
|
override func build() {
|
|
let osc = OscSin.Audio(freq: $freq)
|
|
output(bus: 0, source: Pan2(input: osc * $amp, pan: $pan))
|
|
}
|
|
}
|
|
"""
|
|
}
|
|
|
|
// MARK: - Xcode workspace
|
|
|
|
/// `contents.xcworkspacedata` body. Single `FileRef` to `Package.swift`
|
|
/// so opening the workspace in Xcode loads the SPM package with full
|
|
/// project navigation, debugging, and SwiftUI previews. Identical for
|
|
/// every template — the file lives at `<name>.xcworkspace/contents.xcworkspacedata`.
|
|
public static func workspaceContents() -> String {
|
|
"""
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<Workspace
|
|
version = "1.0">
|
|
<FileRef
|
|
location = "group:Package.swift">
|
|
</FileRef>
|
|
</Workspace>
|
|
"""
|
|
}
|
|
|
|
// MARK: - Tests template
|
|
|
|
/// Starter test file content (shared across all templates).
|
|
public static func testsSwift(name: String) -> String {
|
|
"""
|
|
import Testing
|
|
import LyrebirdSynth
|
|
|
|
@Suite("\(name)")
|
|
struct \(name)Tests {
|
|
|
|
@Test func engineStartsClean() throws {
|
|
let engine = SynthesisEngine(
|
|
blockSize: \(scaffoldDefaultBlockSize),
|
|
sampleRate: \(scaffoldDefaultSampleRate),
|
|
numOutputChannels: \(scaffoldDefaultOutputChannels),
|
|
numInputChannels: \(scaffoldDefaultInputChannels),
|
|
driver: OfflineDriver()
|
|
)
|
|
try engine.start()
|
|
engine.processBlock(inputSamples: [])
|
|
engine.stop()
|
|
#expect(engine.audioBuses[0].allSatisfy { $0 == 0.0 })
|
|
}
|
|
}
|
|
"""
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
/// Validate that `name` is a non-empty Swift-compatible identifier.
|
|
static func validateName(_ name: String) throws {
|
|
try validateSwiftIdentifier(name, invalidNameError: Error.invalidName(name: name))
|
|
}
|
|
}
|