Repository Public
1// ORIX Sonata2// File: Packages/SonataUI/Tests/SonataUITests/LabInstrumentSourceTests.swift3// Description: The source Instrument Lab generates compiles without any diagnostic for every4// registry engine and preset, with parameters at their defaults and at the ends of their ranges.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SonataBridge10import Testing11@testable import SonataUI1213@Test func numbersAndLiteralsAreLexable() {14 #expect(InstrumentSource.number(0.015) == "0.015")15 #expect(InstrumentSource.number(1e-5) == "0.00001")16 #expect(InstrumentSource.number(3) == "3")17 #expect(InstrumentSource.number(-0.0000001) == "0")18 #expect(InstrumentSource.literal(0.005, unit: "seconds") == "5ms")19 #expect(InstrumentSource.literal(2, unit: "seconds") == "2s")20 #expect(InstrumentSource.literal(-8, unit: "decibel") == "-8dB")21 #expect(InstrumentSource.literal(440, unit: "hertz") == "440Hz")22 #expect(InstrumentSource.literal(2.6, unit: "count") == "3")23 #expect(InstrumentSource.noteLiteral(60) == "C4" && InstrumentSource.noteLiteral(61) == "C#4" && InstrumentSource.noteLiteral(21) == "A0")24 #expect(InstrumentSource.sanitizedName("A4") == "InstA4")25 #expect(InstrumentSource.sanitizedName("crystal harp!") == "Crystalharp")26 #expect(InstrumentSource.sanitizedName("") == "Lab")27 #expect(InstrumentSource.isWritable("envelope.attack") && InstrumentSource.isWritable("drawbar.f16") && !InstrumentSource.isWritable("drawbar.16"))28}2930@Test func registryDecodesWithPresetValues() {31 let r = LabRegistry.shared32 #expect(r.engines.count >= 13 && r.effects.count >= 15 && r.patchNodes.count >= 20)33 #expect(r.effect("reverb")?.presets.contains { $0.name == "hall" } == true)34 #expect(r.effects.contains { $0.presets.contains { !$0.values.isEmpty } }, "preset values are decoded")35 let p = r.engine("synth")!.params.first { $0.name == "filter.cutoff" }!36 #expect(p.isLogarithmic)37 #expect(abs(p.value(position: p.position(1000)) - 1000) < 1e-6)38}3940@Test func generatedSourceCompilesForEveryEngineAndPreset() async throws {41 let session = SonataCompilerSession()42 var compiled = 043 for engine in LabRegistry.shared.engines {44 let pitch = min(max(60, engine.range[0]), engine.range[1])45 var drafts: [InstrumentDraft] = []46 var base = InstrumentDraft()47 base.model = engine.model48 base.name = "Lab_" + engine.model49 drafts.append(base)50 for preset in engine.presets {51 var d = base52 d.preset = preset.name53 drafts.append(d)54 }55 // Every writable parameter set explicitly: defaults, minima, maxima.56 for pick in 0..<3 {57 var d = base58 for p in engine.params where InstrumentSource.isWritable(p.name) {59 if p.isChoice {60 let c = p.choices ?? []61 d.choices[p.name] = pick == 0 ? (p.defaultChoice ?? c.first) : (pick == 1 ? c.first : c.last)62 } else if p.isList {63 d.lists[p.name] = p.unit == "hertz_list" ? [220, 440, 660] : [1, 2.01, 3.5]64 } else {65 d.values[p.name] = pick == 0 ? p.default : (pick == 1 ? p.min : p.max)66 }67 }68 drafts.append(d)69 }70 for d in drafts {71 let source = InstrumentSource.composition(d, engine: engine, notes: [.init(midi: pitch, start: 0, length: 0.5, velocity: 0.8)])72 let result = try await session.compile(name: "lab", source: source)73 #expect(result.ok, "\(engine.model) \(d.preset ?? "-"): \(result.diagnostics.map { "\($0.code) \($0.message)" })\n\(source)")74 #expect(result.diagnostics.isEmpty, "\(engine.model) \(d.preset ?? "-"): \(result.diagnostics.map { "\($0.code) \($0.message)" })")75 #expect(result.timeline?.tracks.first?.notes.count == 1)76 #expect(abs((result.timeline?.durationSec ?? 0) - 0.5) < 1e-9)77 compiled += 178 }79 }80 #expect(compiled >= 16 * 4)81 // The empty audition program (no note) also compiles.82 let engine = LabRegistry.shared.engine("piano")!83 var d = InstrumentDraft()84 d.model = "piano"85 let empty = try await session.compile(name: "lab", source: InstrumentSource.composition(d, engine: engine, notes: []))86 #expect(empty.ok && empty.timeline?.tracks.first?.notes.isEmpty == true)87}8889@Test func patchGraphRegeneratesToTheSameGraph() async throws {90 let session = SonataCompilerSession()91 let engine = try #require(LabRegistry.shared.engine("patch"))92 let bodies = [93 InstrumentDraft.defaultGraph,94 """95 node osc1 = saw(freq: note.freq)96 node osc2 = pulse(freq: note.freq * 1.005, width: 0.3)97 node amp = adsr(attack: 8ms, decay: 250ms, sustain: 0.6, release: 400ms)98 node wobble = lfo(rate: 5.5Hz)99 node tone = lowpass(in: osc1 + osc2 * 0.6, cutoff: 400Hz + amp * 2400 * note.velocity + wobble * 60, q: 2.2)100 node echo = delay(in: mixed, time: 11ms)101 node mixed = tone + echo * 0.35102 out saturate(in: mixed, drive: 1.5) * amp103 """,104 """105 node strike = adsr(attack: 0ms, decay: 12ms, sustain: 0, release: 10ms)106 node ring = adsr(attack: 1ms, decay: 2.5s, sustain: 0, release: 2s)107 node burst = noise() * strike108 node m1 = modal(in: burst, freq: note.freq, decay: 2.2s)109 node m2 = modal(in: burst, freq: note.freq * 2.76, decay: 1.4s)110 out (m1 + m2 * 0.5) * ring * note.velocity111 """,112 ]113 for body in bodies {114 var draft = InstrumentDraft()115 draft.model = "patch"116 draft.graph = body117 let note = [InstrumentSource.TestNote(midi: 60, start: 0, length: 0.25, velocity: 0.8)]118 let first = try await session.compile(name: "lab", source: InstrumentSource.composition(draft, engine: engine, notes: note))119 #expect(first.ok, "\(first.diagnostics.map(\.message))")120 let graph = try #require(PatchGraph.parse(programJSON: await session.exportJSON("program")))121 #expect(graph.nodes.count >= 4)122 // Regenerate the block from the compiled graph, compile it, and compare the structure.123 draft.graph = graph.source(registry: LabRegistry.shared)124 let second = try await session.compile(name: "lab", source: InstrumentSource.composition(draft, engine: engine, notes: note))125 #expect(second.ok, "\(second.diagnostics.map { "\($0.code) \($0.message)" })\n\(draft.graph ?? "")")126 let again = try #require(PatchGraph.parse(programJSON: await session.exportJSON("program")))127 #expect(again.shape == graph.shape, "\(graph.shape)\nvs\n\(again.shape)")128 #expect(again.source(registry: LabRegistry.shared) == draft.graph, "regeneration is a fixed point")129 }130}131