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:
2026-06-21 20:43:03 -07:00
parent c677e124f4
commit 2ba57cadd6
5 changed files with 635 additions and 0 deletions
@@ -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)
}
}