Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/TunerTapTests.swift
T
josh 947dfe427a Add looper pedal: beat-quantized transport, undo, reverse, timeline
Extend the looper into a double-width tempo-aware pedal:

- Beat-quantized transport: record start and loop close both snap to the
  next beat via a LyrebirdRoutine waiting on the shared clock; loop length
  is an exact whole-beat span. Falls back to immediate/unquantized when no
  clock is present.
- Loop-length read-outs (frames/seconds/beats), transport state, and a
  sample-clock-derived play-head position for the UI timeline.
- Overdub undo (pre-dub snapshot + restore), reverse playback (live rate
  flip), and feedback-decay overdub (overdub preLevel via a Feedback knob).
- Inject the shared LyrebirdClock + SynthesisEngine into Pedal via
  PedalBoard/ChainRouter so tempo-aware pedals can reach them.
- New double-width LooperTimelineView (state badge, length read-out,
  sweeping play-head) wired into PedalView via Pedal.widthUnits.

Concurrency hardening from RT-safety review: a transportGeneration counter
drops superseded deferred beat/undo callbacks that LyrebirdRoutine.stop()
cannot recall once enqueued; the undo restore is deferred past the RT
overdub gate-close; transport routines use non-blocking stop().

Note: this commit also snapshots pre-existing uncommitted work-in-progress
already in the tree (MIDI control refactor, Tuner, Tempo, preset changes),
which is interleaved with the looper changes through shared files and could
not be split into a separately-building commit.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 09:22:20 -07:00

92 lines
3.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// TunerTapTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
import LyrebirdUI
@testable import LyrebirdShoeGaze
@MainActor
@Suite("TunerTap")
struct TunerTapTests {
/// A constant-frequency sine source — same shape as `LooperPedalTests.ToneSource`.
private final class ToneSource: SynthesizerBase {
private let targetBus: Int
private let toneFreq: Double
private let amplitude: Double
init(targetBus: Int, freq: Double, amplitude: Double = 0.5) {
self.targetBus = targetBus
self.toneFreq = freq
self.amplitude = amplitude
super.init()
}
override func build() {
output(bus: targetBus, source: OscSin.Audio(freq: toneFreq, mul: amplitude))
}
}
@Test func detectsKnownFrequencyWithinTolerance() throws {
let toneFreq: Double = 220.0
// The YIN tracker decimates 8×, so its 512-sample window needs ~4096 input samples
// to fill plus several hops to converge — render generously.
let renderDuration: Double = 2.0
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration)
defer { try? FileManager.default.removeItem(at: url) }
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let scratchControlBus: ControlBus = try engine.busAllocator.allocateControlBus()
defer {
engine.busAllocator.releaseAudioBus(srcBus)
engine.busAllocator.releaseControlBus(scratchControlBus)
}
let source: ToneSource = ToneSource(targetBus: srcBus.index, freq: toneFreq)
let tap: TunerTap = TunerTap(inputBusIndex: srcBus.index, scratchControlBus: scratchControlBus)
try engine.add(source)
try engine.add(tap, position: NodePosition.after(source))
try engine.start()
let capture: TunerPitchCapture = try #require(tap.pitchCapture)
let detected: Double = capture.detectedFrequencyHz
let tolerance: Double = 5.0
#expect(abs(detected - toneFreq) < tolerance, "Expected ~\(toneFreq) Hz, got \(detected) Hz")
#expect(capture.detectedConfidence > 0.5, "A pure tone should track with high confidence")
}
@Test func detectsLowGuitarEString() throws {
// E2 ≈ 82.4 Hz — below the undecimated FreqTracker floor (~173 Hz), so this only
// works because the tuner tap decimates before YIN analysis.
let toneFreq: Double = 82.41
let renderDuration: Double = 3.0
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration)
defer { try? FileManager.default.removeItem(at: url) }
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let scratchControlBus: ControlBus = try engine.busAllocator.allocateControlBus()
defer {
engine.busAllocator.releaseAudioBus(srcBus)
engine.busAllocator.releaseControlBus(scratchControlBus)
}
let source: ToneSource = ToneSource(targetBus: srcBus.index, freq: toneFreq)
let tap: TunerTap = TunerTap(inputBusIndex: srcBus.index, scratchControlBus: scratchControlBus)
try engine.add(source)
try engine.add(tap, position: NodePosition.after(source))
try engine.start()
let capture: TunerPitchCapture = try #require(tap.pitchCapture)
let detected: Double = capture.detectedFrequencyHz
let tolerance: Double = 3.0
#expect(abs(detected - toneFreq) < tolerance, "Expected ~\(toneFreq) Hz (low E), got \(detected) Hz")
}
}