Add Harmonizer pedal and a stepped selector-knob control
Harmonizer: two granular PitchShift voices at fixed musical intervals (ratio 2^(semitones/12) via Exp), mixed under the dry signal with V1/V2/ Direct level knobs. Two interval "knob pickers" snap through octave, fifth, fourth, and minor/major third and sixth above and below. New reusable control: - PedalSelectorKnob (model) + PedalSelectorKnobView (rotary knob that snaps to labelled stops; drag up/down to step, shows the stop caption). - Pedal.effectSelectors hook; PedalView renders a selector row. - PedalState.selectorValues for preset round-trip (forward-compatible). Registered + PedalType.harmonizer + enclosure color + palette slot. Tests: build/non-silent, interval-change-changes-output, selector preset round-trip, registry. Full suite 213 passing. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
// HarmonizerPedalTests.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("HarmonizerPedal")
|
||||
struct HarmonizerPedalTests {
|
||||
|
||||
/// Renders the pedal through an NRT engine with the given configuration and returns the output.
|
||||
private func render(
|
||||
configure: (HarmonizerPedal) -> Void
|
||||
) throws -> [Float] {
|
||||
let renderDuration: Double = 0.3
|
||||
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: HarmonizerPedal = HarmonizerPedal(
|
||||
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 = 4096
|
||||
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 buildsAndProducesNonSilentOutput() throws {
|
||||
let samples: [Float] = try render { pedal in
|
||||
pedal.direct = 1.0
|
||||
pedal.voice1Level = 0.6
|
||||
pedal.voice2Level = 0.5
|
||||
}
|
||||
#expect(!samples.isEmpty)
|
||||
#expect(hasNonSilentOutput(samples), "harmonizer output was silent")
|
||||
}
|
||||
|
||||
@Test func changingVoiceIntervalChangesOutput() throws {
|
||||
let third: [Float] = try render { pedal in
|
||||
pedal.direct = 0.0
|
||||
pedal.voice1Level = 1.0
|
||||
pedal.voice2Level = 0.0
|
||||
pedal.voice1 = 3.0 // minor third up
|
||||
}
|
||||
let fifth: [Float] = try render { pedal in
|
||||
pedal.direct = 0.0
|
||||
pedal.voice1Level = 1.0
|
||||
pedal.voice2Level = 0.0
|
||||
pedal.voice1 = 7.0 // perfect fifth up
|
||||
}
|
||||
#expect(hasNonSilentOutput(third), "minor-third voice was silent")
|
||||
#expect(hasNonSilentOutput(fifth), "fifth voice was silent")
|
||||
#expect(windowedSSD(third, fifth) > 0, "changing the interval did not change the output")
|
||||
}
|
||||
|
||||
@Test func harmonyVoicesDifferFromDirectOnly() throws {
|
||||
let directOnly: [Float] = try render { pedal in
|
||||
pedal.direct = 1.0
|
||||
pedal.voice1Level = 0.0
|
||||
pedal.voice2Level = 0.0
|
||||
}
|
||||
let withVoices: [Float] = try render { pedal in
|
||||
pedal.direct = 1.0
|
||||
pedal.voice1Level = 1.0
|
||||
pedal.voice2Level = 1.0
|
||||
}
|
||||
#expect(hasNonSilentOutput(directOnly), "direct-only render was silent")
|
||||
#expect(windowedSSD(directOnly, withVoices) > 0, "adding harmony voices did not change the output")
|
||||
}
|
||||
|
||||
@Test func exposesLevelKnobs() throws {
|
||||
let pedal: HarmonizerPedal = HarmonizerPedal()
|
||||
let labels: [String] = pedal.effectKnobs.map { $0.label }
|
||||
#expect(labels == ["V1 Level", "V2 Level", "Direct"])
|
||||
}
|
||||
|
||||
@Test func exposesTwoIntervalSelectors() throws {
|
||||
let pedal: HarmonizerPedal = HarmonizerPedal()
|
||||
let selectors: [PedalSelectorKnob] = pedal.effectSelectors
|
||||
#expect(selectors.map { $0.label } == ["Voice 1", "Voice 2"])
|
||||
// Both minor- and major-thirds above and below must be available.
|
||||
let titles: [String] = selectors.first?.stops.map { $0.title } ?? []
|
||||
#expect(titles.contains("m3"))
|
||||
#expect(titles.contains("M3"))
|
||||
#expect(titles.contains("-m3"))
|
||||
#expect(titles.contains("-M3"))
|
||||
}
|
||||
|
||||
@Test func presetRoundTripPreservesIntervalsAndLevels() throws {
|
||||
let pedal: HarmonizerPedal = HarmonizerPedal()
|
||||
pedal.voice1 = -3.0
|
||||
pedal.voice2 = 12.0
|
||||
pedal.voice1Level = 0.3
|
||||
|
||||
let state: PedalState = pedal.captureState()
|
||||
#expect(state.selectorValues == [-3.0, 12.0])
|
||||
|
||||
let restored: HarmonizerPedal = HarmonizerPedal()
|
||||
restored.applyState(state)
|
||||
#expect(restored.voice1 == -3.0)
|
||||
#expect(restored.voice2 == 12.0)
|
||||
#expect(restored.voice1Level == 0.3)
|
||||
}
|
||||
|
||||
@Test func registeredInBuiltInRegistry() throws {
|
||||
let pedal: Pedal? = PedalRegistry.makePedal(type: "Harmonizer")
|
||||
#expect(pedal is HarmonizerPedal)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user