feat(karplus): collapse tuning UI to a Free/Pitched switch + one Root knob

Replace the four-control tuning surface (continuous Tune Hz knob + Tuning
selector + separate Note and Octave selectors) with a single Free/Pitched
switch and one chromatic Root selector that steps every semitone from E1
(MIDI 28) to E5 (MIDI 76), captioned by note name ("E1", "A3", "E5"). The
Root selector is shown only in Pitched mode; in Free mode the continuous
Tune Hz knob drives the root as before.

- Drop the `note`/`octave` @UIParams and `noteFrequency(pitchClass:octave:)`;
  add a `pitchedNote` @UIParam (MIDI note number, A3=57=220 Hz default) and
  `frequency(forMidiNote:)` / `caption(forMidiNote:)` helpers.
- Knob positions are unchanged (Tune always present), so positional presets
  stay aligned; only the trailing selector set varies by mode, with Voicing
  pinned at index 0.
- Document the switch-before-selector pass order in Pedal.applyState as
  contractual: a mode-dependent selector set (like this one) relies on it.

798 tests pass; lyrebird_external_review clean (no blocking findings).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-24 17:29:15 -08:00
parent 097c38283e
commit 3e3f86b737
3 changed files with 171 additions and 80 deletions
@@ -364,53 +364,76 @@ struct KarplusResonatorPedalTests {
// MARK: - Wiring
@Test func exposesKnobsAndSelectors() throws {
@Test func exposesControls() throws {
let pedal: KarplusResonatorPedal = KarplusResonatorPedal()
#expect(pedal.effectKnobs.map { $0.label } == ["Tune", "Sustain", "Tone", "Sensitivity"])
let selectors: [PedalSelectorKnob] = pedal.effectSelectors
#expect(selectors.map { $0.label } == ["Voicing", "Tuning", "Note", "Octave"])
#expect(selectors.first?.stops.map { $0.title } == ["Drone", "Octaves", "Shimmer"])
let tuning: PedalSelectorKnob? = selectors.first { $0.label == "Tuning" }
#expect(tuning?.stops.map { $0.title } == ["Free", "Note"])
let note: PedalSelectorKnob? = selectors.first { $0.label == "Note" }
#expect(note?.stops.count == 12)
#expect(note?.stops.first?.title == "C")
// One Tuning switch, Free/Pitched.
#expect(pedal.effectSwitches.map { $0.label } == ["Tuning"])
let tuning: PedalSwitch? = pedal.effectSwitches.first { $0.label == "Tuning" }
#expect(tuning?.options.map { $0.title } == ["Free", "Pitched"])
#expect(tuning?.rebuildsOnChange == true)
// Default (Free) shows only the Voicing selector — the Root note knob is hidden.
#expect(pedal.effectSelectors.map { $0.label } == ["Voicing"])
#expect(pedal.effectSelectors.first?.stops.map { $0.title } == ["Drone", "Octaves", "Shimmer"])
// Pitched reveals the Root chromatic selector at index 1, spanning E1 … E5.
pedal.tuning = 1.0
#expect(pedal.effectSelectors.map { $0.label } == ["Voicing", "Root"])
let root: PedalSelectorKnob? = pedal.effectSelectors.first { $0.label == "Root" }
#expect(root?.stops.count == 49) // MIDI 28 … 76 inclusive
#expect(root?.stops.first?.title == "E1")
#expect(root?.stops.last?.title == "E5")
}
@Test func noteFrequencyMapsEqualTemperament() throws {
// A4 = 440, so A3 = 220 and A4 = 440; C4 (middle C, MIDI 60) ≈ 261.63.
#expect(abs(KarplusResonatorPedal.noteFrequency(pitchClass: 9, octave: 3) - 220.0) < 0.01)
#expect(abs(KarplusResonatorPedal.noteFrequency(pitchClass: 9, octave: 4) - 440.0) < 0.01)
#expect(abs(KarplusResonatorPedal.noteFrequency(pitchClass: 0, octave: 4) - 261.6256) < 0.01)
@Test func frequencyMapsEqualTemperament() throws {
// A4 = 440 (MIDI 69), so A3 (MIDI 57) = 220; C4 (middle C, MIDI 60) ≈ 261.63.
#expect(abs(KarplusResonatorPedal.frequency(forMidiNote: 57) - 220.0) < 0.01)
#expect(abs(KarplusResonatorPedal.frequency(forMidiNote: 69) - 440.0) < 0.01)
#expect(abs(KarplusResonatorPedal.frequency(forMidiNote: 60) - 261.6256) < 0.01)
#expect(KarplusResonatorPedal.caption(forMidiNote: 28) == "E1")
#expect(KarplusResonatorPedal.caption(forMidiNote: 57) == "A3")
#expect(KarplusResonatorPedal.caption(forMidiNote: 76) == "E5")
#expect(KarplusResonatorPedal.caption(forMidiNote: 61) == "C♯4") // sharp pitch class
}
@Test func noteModeTunesTheRootToThePitch() throws {
// Tuning = Note, A3, Drone voicing → the ring's fundamental sits at 220 Hz regardless of the
// 110 Hz pluck or the Tune Hz knob.
@Test func pitchedModeTunesTheRootToThePitch() throws {
// Tuning = Pitched, A3 (MIDI 57), Drone voicing → the ring's fundamental sits at 220 Hz
// regardless of the 110 Hz pluck or the Tune Hz knob.
let out: [Float] = try renderPluck { pedal in
pedal.mix = 1.0; pedal.sustain = 0.9; pedal.sensitivity = 0.6
pedal.tune = 60.0 // deliberately off, to prove Note overrides it
pedal.tuning = 1.0; pedal.note = 9.0; pedal.octave = 3.0
pedal.tune = 60.0 // deliberately off, to prove Pitched overrides it
pedal.tuning = 1.0; pedal.pitchedNote = 57.0
}
let f: Double = fundamentalHz(out, skip: tailSkipFrames, window: tailWindowFrames, minHz: 120.0, maxHz: 400.0)
#expect(abs(f - 220.0) / 220.0 < 0.1, "Note A3 should ring near 220 Hz, got \(f)")
#expect(abs(f - 220.0) / 220.0 < 0.1, "Pitched A3 should ring near 220 Hz, got \(f)")
}
@Test func noteModeOctaveRaisesThePitch() throws {
// A3 vs A4 — one octave up should roughly double the ring fundamental.
@Test func pitchedModeOctaveRaisesThePitch() throws {
// A3 (MIDI 57) vs A4 (MIDI 69) — one octave up should roughly double the ring fundamental.
let a3: [Float] = try renderPluck { pedal in
pedal.mix = 1.0; pedal.sustain = 0.9; pedal.sensitivity = 0.6
pedal.tuning = 1.0; pedal.note = 9.0; pedal.octave = 3.0
pedal.tuning = 1.0; pedal.pitchedNote = 57.0
}
let a4: [Float] = try renderPluck { pedal in
pedal.mix = 1.0; pedal.sustain = 0.9; pedal.sensitivity = 0.6
pedal.tuning = 1.0; pedal.note = 9.0; pedal.octave = 4.0
pedal.tuning = 1.0; pedal.pitchedNote = 69.0
}
let f3: Double = fundamentalHz(a3, skip: tailSkipFrames, window: tailWindowFrames, minHz: 120.0, maxHz: 700.0)
let f4: Double = fundamentalHz(a4, skip: tailSkipFrames, window: tailWindowFrames, minHz: 120.0, maxHz: 700.0)
#expect(f4 > f3 * 1.5, "A4 should ring well above A3: a4 \(f4) vs a3 \(f3)")
}
@Test func pitchedModeReachesTrebleStaffTop() throws {
// The Root selector's top stop is E5 (MIDI 76 ≈ 659 Hz). Drone voicing shares the root period.
let e5: Double = 659.25
let out: [Float] = try renderPluck { pedal in
pedal.mix = 1.0; pedal.sustain = 0.9; pedal.sensitivity = 0.6
pedal.tuning = 1.0; pedal.pitchedNote = 76.0; pedal.voicing = 0.0
}
let f: Double = fundamentalHz(out, skip: tailSkipFrames, window: tailWindowFrames, minHz: 300.0, maxHz: 900.0)
#expect(abs(f - e5) / e5 < 0.1, "Pitched E5 should ring near \(e5) Hz, got \(f)")
}
@Test func presetRoundTripPreservesControls() throws {
let pedal: KarplusResonatorPedal = KarplusResonatorPedal()
pedal.tune = 123.4
@@ -418,9 +441,8 @@ struct KarplusResonatorPedalTests {
pedal.tone = 0.6
pedal.sensitivity = 0.3
pedal.voicing = 2.0
pedal.tuning = 1.0
pedal.note = 4.0
pedal.octave = 5.0
pedal.tuning = 1.0 // Pitched, so the Root selector is captured
pedal.pitchedNote = 64.0 // E4
let state: PedalState = pedal.captureState()
let restored: KarplusResonatorPedal = KarplusResonatorPedal()
@@ -432,8 +454,38 @@ struct KarplusResonatorPedalTests {
#expect(restored.sensitivity == 0.3)
#expect(restored.voicing == 2.0)
#expect(restored.tuning == 1.0)
#expect(restored.note == 4.0)
#expect(restored.octave == 5.0)
#expect(restored.pitchedNote == 64.0)
}
@Test func freeStateRestoresOntoAPitchedPedal() throws {
// A Free preset applied to an instance already in Pitched mode: the switch pass writes
// tuning = 0 before effectSelectors is re-read, so the selector list collapses to [Voicing]
// and the stale Root value is dropped — no positional corruption.
let saved: KarplusResonatorPedal = KarplusResonatorPedal() // defaults to Free
saved.voicing = 1.0
saved.tune = 200.0
let state: PedalState = saved.captureState()
#expect(state.selectorValues.count == 1, "a Free preset should carry only the Voicing selector")
let target: KarplusResonatorPedal = KarplusResonatorPedal()
target.tuning = 1.0 // put it in Pitched first
target.pitchedNote = 76.0
target.applyState(state)
#expect(target.tuning == 0.0, "restoring a Free preset should return the switch to Free")
#expect(target.voicing == 1.0)
#expect(target.tune == 200.0)
#expect(target.effectSelectors.map { $0.label } == ["Voicing"])
}
@Test func pitchedNoteClampsOutOfRange() throws {
// A pitchedNote past the Root selector's E1…E5 window still rings (clamped at build time),
// never crashing or going silent.
let out: [Float] = try renderPluck { pedal in
pedal.mix = 1.0; pedal.sustain = 0.9; pedal.sensitivity = 0.6
pedal.tuning = 1.0; pedal.pitchedNote = 200.0 // far above E5
}
#expect(hasNonSilentOutput(out), "out-of-range pitchedNote should clamp and still ring")
}
@Test func registeredInBuiltInRegistry() throws {