2ba57cadd6
Monophonic octave-down pedal with OCT1/OCT2/Direct knobs and a Mode switch between two algorithms: - Flip-flop (default): a hysteresis comparator clocks a T flip-flop divider chain (new SubOctaveDivider generator) to produce square sub-octaves one and two octaves down, amplitude-followed off the dry signal. Time-domain — tracks low bass with no detection floor. - Tracking: FreqTracker drives square oscillators at half and quarter the detected frequency. Cleaner, but limited by the tracker's ~90 Hz floor; flip-flop is the bass mode. SubOctaveDivider classifies its input as generator-or-scalar at init to keep _computeSamples allocation-free, uses a shared file-scope zero block, bounds-checks the input read, and traps on divisor < 2. Tests: BassOctaverPedal integration (both modes non-silent, modes differ, sub-octaves change output, knobs/switch/preset/registry) and SubOctaveDivider deterministic frequency-division (divisor 2 -> /2, divisor 4 -> /4, /4 one octave below /2). 127 tests pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
113 lines
4.2 KiB
Swift
113 lines
4.2 KiB
Swift
// SubOctaveDividerTests.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("SubOctaveDivider")
|
|
struct SubOctaveDividerTests {
|
|
|
|
/// Minimal synth: a sine oscillator into a `SubOctaveDivider`, written to a mono bus.
|
|
private final class DividerSynth: SynthesizerBase {
|
|
private let oscFreq: Double
|
|
private let divisor: Int
|
|
private let outBus: Int
|
|
|
|
init(oscFreq: Double, divisor: Int, outBus: Int) {
|
|
self.oscFreq = oscFreq
|
|
self.divisor = divisor
|
|
self.outBus = outBus
|
|
super.init()
|
|
}
|
|
|
|
override func build() {
|
|
let osc: OscSin.Audio = OscSin.Audio(freq: oscFreq)
|
|
let divider: SubOctaveDivider = SubOctaveDivider(input: osc, divisor: divisor)
|
|
output(bus: outBus, source: divider)
|
|
}
|
|
}
|
|
|
|
/// Renders `oscFreq` through a divider and returns the measured output fundamental in Hz,
|
|
/// estimated from rising zero-crossings of the left channel.
|
|
private func measuredOutputFreq(
|
|
oscFreq: Double,
|
|
divisor: Int,
|
|
sampleRate: Double = 44100.0
|
|
) throws -> Double {
|
|
let renderDuration: Double = 0.5
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
|
|
duration: renderDuration,
|
|
sampleRate: sampleRate
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
|
defer { engine.busAllocator.releaseAudioBus(dstBus) }
|
|
|
|
let synth: DividerSynth = DividerSynth(
|
|
oscFreq: oscFreq,
|
|
divisor: divisor,
|
|
outBus: dstBus.index
|
|
)
|
|
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
|
|
try engine.add(synth)
|
|
try engine.add(master, position: NodePosition.after(synth))
|
|
try engine.start()
|
|
|
|
let samples: [Float] = readStereoCAF(url: url, sampleRate: sampleRate)
|
|
// Left channel = even interleaved indices.
|
|
let stride: Int = 2
|
|
var crossings: Int = 0
|
|
var prev: Float = 0.0
|
|
var frameCount: Int = 0
|
|
var index: Int = 0
|
|
while index < samples.count {
|
|
let cur: Float = samples[index]
|
|
if prev <= 0.0 && cur > 0.0 { crossings += 1 }
|
|
prev = cur
|
|
frameCount += 1
|
|
index += stride
|
|
}
|
|
let seconds: Double = Double(frameCount) / sampleRate
|
|
guard seconds > 0.0 else { return 0.0 }
|
|
return Double(crossings) / seconds
|
|
}
|
|
|
|
@Test func divideByTwoHalvesFrequency() throws {
|
|
let oscFreq: Double = 440.0
|
|
let measured: Double = try measuredOutputFreq(oscFreq: oscFreq, divisor: 2)
|
|
let expected: Double = oscFreq / 2.0
|
|
let tolerance: Double = 0.2
|
|
#expect(measured > 0.0, "divide-by-2 produced no crossings")
|
|
#expect(abs(measured - expected) / expected < tolerance,
|
|
"divide-by-2: expected ≈\(expected) Hz, measured \(measured) Hz")
|
|
}
|
|
|
|
@Test func divideByFourQuartersFrequency() throws {
|
|
let oscFreq: Double = 440.0
|
|
let measured: Double = try measuredOutputFreq(oscFreq: oscFreq, divisor: 4)
|
|
let expected: Double = oscFreq / 4.0
|
|
let tolerance: Double = 0.25
|
|
#expect(measured > 0.0, "divide-by-4 produced no crossings")
|
|
#expect(abs(measured - expected) / expected < tolerance,
|
|
"divide-by-4: expected ≈\(expected) Hz, measured \(measured) Hz")
|
|
}
|
|
|
|
@Test func divideByFourIsOneOctaveBelowDivideByTwo() throws {
|
|
let oscFreq: Double = 440.0
|
|
let two: Double = try measuredOutputFreq(oscFreq: oscFreq, divisor: 2)
|
|
let four: Double = try measuredOutputFreq(oscFreq: oscFreq, divisor: 4)
|
|
#expect(four > 0.0, "divide-by-4 produced no crossings")
|
|
let ratio: Double = two / four
|
|
// The two voices should sit exactly one octave apart (ratio ≈ 2).
|
|
#expect(abs(ratio - 2.0) < 0.4, "expected ÷4 one octave below ÷2; ratio \(ratio)")
|
|
}
|
|
}
|