SPB Git

Repository Public

LabMathTests.swift 4.8 KB 105 lines swift

1// ORIX Sonata2// File: Packages/SonataUI/Tests/SonataUITests/LabMathTests.swift3// Description: WaveLab measurement logic: trigger, period, peak interpolation validated on known4// sines through the same spectrum path the views use, colour maps, history ring, edit list.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SonataBridge10import Testing11@testable import SonataUI1213private func sine(_ hz: Double, amplitude: Double, count: Int, rate: Double = 48_000, phase: Double = 0) -> [Float] {14    (0..<count).map { Float(amplitude * sin(2 * .pi * hz * Double($0) / rate + phase)) }15}1617@Test func triggerFindsTheRisingCrossing() {18    let x = sine(1000, amplitude: 0.5, count: 480, phase: .pi)  // starts going down19    let c = LabMath.risingCrossing(x, level: 0)20    // Half a period (24 samples) after the start the sine rises through zero.21    #expect(abs((c ?? -1) - 24) < 0.01)22    #expect(LabMath.risingCrossing(x, level: 0.9) == nil, "never reaches 0.9")23    #expect(LabMath.risingCrossing([], level: 0) == nil)24    let level = LabMath.risingCrossing(x, level: 0.25)!25    #expect(abs(Double(x[Int(level)]) - 0.25) < 0.04)26}2728@Test func periodOfAKnownSine() {29    for hz in [100.0, 440.0, 997.0, 3000.0] {30        let p = LabMath.period(sine(hz, amplitude: 0.3, count: 9600), level: 0)31        #expect(abs(48_000 / (p ?? 1) - hz) / hz < 2e-4, "\(hz) Hz measured \(48_000 / (p ?? 1))")32    }33    #expect(LabMath.period([Float](repeating: 0, count: 100), level: 0) == nil)34    let s = LabMath.stats(sine(100, amplitude: 0.5, count: 4800).map { $0 + 0.1 })35    #expect(abs(s.dc - 0.1) < 1e-4 && abs(s.peak - 0.6) < 1e-3)36}3738@Test func spectrumPeaksOfKnownSinesInHzAndDbfs() throws {39    // Off-bin tones through the calibrated spectrum the Spectrum view uses, then the same peak picker.40    let n = 819241    let binHz = 48_000.0 / Double(n)42    let mix = zip(sine(440, amplitude: 0.5, count: n), sine(1234.5, amplitude: 0.05, count: n)).map { $0 + $1 }43    for window in [FFTWindow.hann, .blackman, .blackmanHarris] {44        let db = try #require(SonataAnalysis.spectrum(mix, window: window))45        let peaks = LabMath.topPeaks(db, binHz: binHz, count: 2, floorDb: -80)46        #expect(peaks.count == 2)47        #expect(abs(peaks[0].hz - 440) < 0.5, "\(window.title): \(peaks[0].hz) Hz")48        #expect(abs(peaks[0].db - (-6.02)) < 0.35, "\(window.title): \(peaks[0].db) dBFS")49        #expect(abs(peaks[1].hz - 1234.5) < 0.6)50        #expect(abs(peaks[1].db - (-26.02)) < 0.35)51    }52    #expect(LabMath.topPeaks([Float](repeating: -200, count: 100), binHz: 1, count: 3, floorDb: -120).isEmpty)53}5455@Test func frequencyAxisRoundTrips() {56    for log in [true, false] {57        for hz in [20.0, 440, 1000, 19_000] {58            let p = LabMath.axisPosition(hz: hz, lo: 20, hi: 20_000, log: log)59            #expect(abs(LabMath.axisFrequency(position: p, lo: 20, hi: 20_000, log: log) - hz) < 1e-6 * hz)60        }61    }62    #expect(LabMath.frequencyTicks(lo: 20, hi: 20_000, log: true) == [20, 50, 100, 200, 500, 1000, 2000, 5000, 10_000, 20_000])63    #expect(!LabMath.frequencyTicks(lo: 0, hi: 24_000, log: false).isEmpty)64}6566@Test func colourMapsRiseMonotonicallyInLuminance() {67    for map in ColorMap.allCases {68        var last = -1.069        for i in 0...100 {70            let l = map.luminance(Double(i) / 100)71            #expect(l >= last - 1e-9, "\(map.rawValue) at \(i)")72            last = l73        }74        #expect(map.rgb(.nan) == map.rgb(0))75        #expect(map.rgb(7) == map.rgb(1))76        #expect(map.table().count == 1024)77    }78}7980@Test func historyIsBoundedAndOrdered() {81    var h = SpectrumHistory(capacity: 4)82    for i in 0..<10 { h.push([Float(i), 0]) }83    #expect(h.count == 4 && h.pushed == 10 && h.bins == 2)84    #expect((0..<4).map { h[$0][0] } == [6, 7, 8, 9])85    h.push([1, 2, 3])86    #expect(h.count == 1 && h.bins == 3, "a different bin count restarts the history")87}8889@Test func editListIsNonDestructiveAndExact() {90    let source: [[Float]] = [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [1, 1, 1, 1, 1, 1]]91    var list = WaveEditList()92    list.edits = [.reverse(0..<3), .gain(3..<6, db: -6.0206), .fadeIn(0..<3), .silence(5..<99)]93    let out = list.apply(to: source)94    #expect(out[0][0] == 0 && abs(out[0][1] - 0.1) < 1e-6 && abs(out[0][2] - 0.1) < 1e-6)95    #expect(abs(out[0][3] - 0.2) < 1e-5 && out[0][5] == 0 && out[1][5] == 0)96    #expect(source[0][0] == 0.1, "the source is untouched")97    list.edits.removeAll()98    #expect(list.apply(to: source) == source, "an empty list returns the source exactly")99    list.edits = [.normalize(peakDb: -6.0206)]100    let n = list.apply(to: source)101    #expect(abs(n.flatMap { $0 }.map(abs).max()! - 0.5) < 1e-5)102    list.edits = [.fadeOut(0..<6)]103    #expect(list.apply(to: source)[1].last == 0 && list.apply(to: source)[1].first == 1)104}105
Files main
Menu

Theme