Repository Public
1// ORIX Sonata2// File: Packages/SonataUI/Tests/SonataUITests/LabViewsLogicTests.swift3// Description: Logic behind the lab views: ideal string, ADSR reference, pan law, graph summary,4// spectrogram engine on a known file, and an end-to-end analysis of a lab instrument.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SonataBridge10import Testing11@testable import SonataUI1213@Test func idealStringIsDimensionallyValidated() {14 // A4 on a 0.65 m string: T = mu * (2 L f)^2.15 let mu = 0.00038, l = 0.6516 let t = mu * pow(2 * l * 440, 2)17 #expect(abs((IdealStringCalculator.frequency(length: l, tension: t, linearDensity: mu) ?? 0) - 440) < 1e-9)18 #expect(IdealStringCalculator.frequency(length: 0, tension: 70, linearDensity: mu) == nil)19 #expect(IdealStringCalculator.frequency(length: l, tension: -1, linearDensity: mu) == nil)20 #expect(IdealStringCalculator.frequency(length: l, tension: 70, linearDensity: .nan) == nil)21}2223@Test func adsrReferenceAndPanLaw() {24 let e = ADSRSpec(attack: 0.1, decay: 0.2, sustain: 0.5, release: 0.4)25 #expect(e.level(at: 0.05, gate: 1) == 0.5)26 #expect(e.level(at: 0.1, gate: 1) == 1)27 #expect(abs(e.level(at: 0.2, gate: 1) - 0.75) < 1e-12)28 #expect(e.level(at: 0.9, gate: 1) == 0.5)29 #expect(abs(e.level(at: 1.2, gate: 1) - 0.25) < 1e-12)30 #expect(e.level(at: 2, gate: 1) == 0)31 let c = SpatialLabView.gains(pan: 0)32 #expect(abs(20 * log10(c.left) + 3.0103) < 1e-3 && abs(c.left - c.right) < 1e-12)33 #expect(SpatialLabView.gains(pan: -1).right < 1e-12 && abs(SpatialLabView.gains(pan: 1).right - 1) < 1e-12)34 let g = SpatialLabView.gains(pan: 0.37)35 #expect(abs(g.left * g.left + g.right * g.right - 1) < 1e-12, "constant power")36}3738@Test func graphSummaryReadsTheCompiledGraph() async throws {39 let session = SonataCompilerSession()40 let engine = try #require(LabRegistry.shared.engine("synth"))41 var d = InstrumentDraft()42 d.model = "synth"43 let r = try await session.compile(name: "lab", source: InstrumentSource.composition(d, engine: engine, notes: [.init(midi: 60, start: 0, length: 0.2, velocity: 0.8)]))44 #expect(r.ok)45 let summary = try #require(GraphSummary.parse(await session.exportJSON("graph"), timeline: r.timeline))46 #expect(summary.sampleRate == 48_000)47 #expect(summary.nodes.contains { $0.op == "instrument" && $0.detail.contains("lab") })48 #expect(summary.nodes.contains { $0.op == "output" })49 #expect(summary.nodes.last?.op == "output", "listed in processing order")50 #expect(GraphSummary.parse("not json", timeline: nil) == nil)51}5253@Test @MainActor func labAnalysisOfARenderedNoteMeasuresItsPitch() async throws {54 // The whole Instrument Lab path without a device: generate source, compile, render, measure.55 UserDefaults.standard.set("synth", forKey: "instrumentLabModel")56 let lab = InstrumentLabModel(document: DocumentModel(source: ""))57 lab.select(model: "synth")58 lab.analysisPitch = 6959 await lab.refresh()60 #expect(lab.compiles && lab.diagnostics.isEmpty)61 let a = try #require(lab.analysis)62 let f0 = try #require(a.measurement?.fundamentalHz)63 #expect(abs(1200 * log2(f0 / 440)) < 5, "measured \(f0) Hz for A4")64 #expect(a.peakDb < 0 && a.peakDb > -40)65 #expect(a.spectrumDb.count == a.windowFrames / 2 + 1)66 let peak = try #require(LabMath.topPeaks(a.spectrumDb, binHz: a.binHz, count: 1, floorDb: -80).first)67 #expect(abs(peak.hz / 440 - (peak.hz / 440).rounded()) < 0.02, "strongest partial is a harmonic of 440 Hz: \(peak.hz)")68 #expect((a.envelope?.peak ?? 0) > 0.01)69 #expect(lab.definition.contains("model synth"))70 // Spectrogram engine over the rendered note: every column has size/2 + 1 bins and the history is bounded.71 let engine = SpectrogramEngine(capacity: 40)72 engine.analyse(a.audio, size: 1024, hop: 256, window: .hann)73 #expect(engine.history.count > 4 && engine.history.count <= 40 && engine.history.bins == 513)74 #expect(engine.csv().split(separator: "\n").count == engine.history.count + 1)75 let image = engine.image(width: 60, height: 40, lo: 30, hi: 24_000, log: true, floorDb: -100, map: .cividis)76 #expect(image?.width == 60 && image?.height == 40)77 #expect((engine.value(column: engine.history.count / 2, hz: 440) ?? -200) > -60)78 lab.restoreDocument()79}80