Add Bass Octaver pedal (Boss OC-2 style)
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>
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
// BassOctaverPedalTests.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("BassOctaverPedal")
|
||||
struct BassOctaverPedalTests {
|
||||
|
||||
/// Renders the pedal through an NRT engine with the given configuration and returns the
|
||||
/// output samples.
|
||||
private func render(
|
||||
configure: (BassOctaverPedal) -> Void
|
||||
) throws -> [Float] {
|
||||
let renderDuration: Double = 0.2
|
||||
let numAudioBuses: Int = 64
|
||||
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
|
||||
duration: renderDuration,
|
||||
numAudioBuses: numAudioBuses
|
||||
)
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||||
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||||
defer {
|
||||
engine.busAllocator.releaseAudioBus(srcBus)
|
||||
engine.busAllocator.releaseAudioBus(dstBus)
|
||||
}
|
||||
|
||||
let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index)
|
||||
let pedal: BassOctaverPedal = BassOctaverPedal(
|
||||
inputBus: srcBus.index,
|
||||
outputBus: dstBus.index
|
||||
)
|
||||
configure(pedal)
|
||||
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
|
||||
|
||||
try engine.add(source)
|
||||
try engine.add(pedal, position: NodePosition.after(source))
|
||||
try engine.add(master, position: NodePosition.after(pedal))
|
||||
try engine.start()
|
||||
|
||||
return readStereoCAF(url: url)
|
||||
}
|
||||
|
||||
/// Sum of squared differences over a stable window of two renders.
|
||||
private func windowedSSD(_ a: [Float], _ b: [Float]) -> Double {
|
||||
let skipSamples: Int = 2048
|
||||
let windowSize: Int = 2048
|
||||
guard a.count >= skipSamples + windowSize,
|
||||
b.count >= skipSamples + windowSize else { return 0.0 }
|
||||
let aWindow: ArraySlice<Float> = a[skipSamples..<(skipSamples + windowSize)]
|
||||
let bWindow: ArraySlice<Float> = b[skipSamples..<(skipSamples + windowSize)]
|
||||
return zip(aWindow, bWindow).reduce(0.0) { acc, pair in
|
||||
let diff: Double = Double(pair.0 - pair.1)
|
||||
return acc + diff * diff
|
||||
}
|
||||
}
|
||||
|
||||
@Test func flipFlopModeProducesNonSilentOutput() throws {
|
||||
let samples: [Float] = try render { pedal in
|
||||
pedal.mode = 0.0 // flip-flop
|
||||
pedal.oct1 = 1.0
|
||||
pedal.direct = 0.0
|
||||
}
|
||||
#expect(!samples.isEmpty)
|
||||
#expect(hasNonSilentOutput(samples), "flip-flop octave was silent")
|
||||
}
|
||||
|
||||
@Test func trackingModeProducesNonSilentOutput() throws {
|
||||
let samples: [Float] = try render { pedal in
|
||||
pedal.mode = 1.0 // tracking
|
||||
pedal.oct1 = 1.0
|
||||
pedal.direct = 0.0
|
||||
}
|
||||
#expect(!samples.isEmpty)
|
||||
#expect(hasNonSilentOutput(samples), "tracking octave was silent")
|
||||
}
|
||||
|
||||
@Test func flipFlopAndTrackingModesDiffer() throws {
|
||||
let flip: [Float] = try render { pedal in
|
||||
pedal.mode = 0.0
|
||||
pedal.oct1 = 1.0
|
||||
pedal.direct = 0.0
|
||||
}
|
||||
let track: [Float] = try render { pedal in
|
||||
pedal.mode = 1.0
|
||||
pedal.oct1 = 1.0
|
||||
pedal.direct = 0.0
|
||||
}
|
||||
#expect(hasNonSilentOutput(flip), "flip render was silent")
|
||||
#expect(hasNonSilentOutput(track), "tracking render was silent")
|
||||
#expect(windowedSSD(flip, track) > 0, "flip-flop and tracking modes produced identical output")
|
||||
}
|
||||
|
||||
@Test func subOctaveVoicesDifferFromDirectOnly() throws {
|
||||
let directOnly: [Float] = try render { pedal in
|
||||
pedal.oct1 = 0.0
|
||||
pedal.oct2 = 0.0
|
||||
pedal.direct = 1.0
|
||||
}
|
||||
let withSubs: [Float] = try render { pedal in
|
||||
pedal.mode = 0.0
|
||||
pedal.oct1 = 1.0
|
||||
pedal.oct2 = 1.0
|
||||
pedal.direct = 1.0
|
||||
}
|
||||
#expect(hasNonSilentOutput(directOnly), "direct-only render was silent")
|
||||
#expect(windowedSSD(directOnly, withSubs) > 0, "adding sub-octaves did not change the output")
|
||||
}
|
||||
|
||||
@Test func exposesOctaveAndDirectKnobs() throws {
|
||||
let pedal: BassOctaverPedal = BassOctaverPedal()
|
||||
let labels: [String] = pedal.effectKnobs.map { $0.label }
|
||||
#expect(labels == ["OCT 1", "OCT 2", "Direct"])
|
||||
}
|
||||
|
||||
@Test func exposesTwoStateModeSwitch() throws {
|
||||
let pedal: BassOctaverPedal = BassOctaverPedal()
|
||||
let switches: [PedalSwitch] = pedal.effectSwitches
|
||||
#expect(switches.map { $0.label } == ["Mode"])
|
||||
#expect(switches.first?.options.map { $0.title } == ["Flip", "Track"])
|
||||
}
|
||||
|
||||
@Test func presetRoundTripPreservesMode() throws {
|
||||
let pedal: BassOctaverPedal = BassOctaverPedal()
|
||||
pedal.mode = 1.0
|
||||
pedal.oct2 = 0.5
|
||||
|
||||
let state: PedalState = pedal.captureState()
|
||||
|
||||
let restored: BassOctaverPedal = BassOctaverPedal()
|
||||
restored.applyState(state)
|
||||
#expect(restored.mode == 1.0)
|
||||
#expect(restored.oct2 == 0.5)
|
||||
}
|
||||
|
||||
@Test func registeredInBuiltInRegistry() throws {
|
||||
let pedal: Pedal? = PedalRegistry.makePedal(type: "Bass Octaver")
|
||||
#expect(pedal is BassOctaverPedal)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
// 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)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user