feat(karplus): add Free-Hz vs Note+Octave tuning modes

The Karplus Resonator root can now be set two ways, via a Tuning selector:
- Free: the continuous Tune Hz knob (unchanged behaviour, still the default).
- Note: an equal-tempered pitch chosen with Note (C…B) and Octave (1…6)
  selectors. A4 = 440, so A3 = 220 Hz, etc.

noteFrequency() computes the root from scientific-pitch note/octave; the
string bank reads whichever root the mode selects. The per-string delay line
is widened to 0.08 s so the lowest Note-mode roots (down to a sub-C1 octave
string) fit without the KarplusStrong frequency clamp engaging.

Tests: equal-temperament mapping, Note-mode A3→220 Hz ring, octave shift,
and selector/preset round-trip. Full suite 794 green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-24 14:55:40 -08:00
parent d1f823e54c
commit 89a34016de
2 changed files with 134 additions and 13 deletions
@@ -352,12 +352,51 @@ struct KarplusResonatorPedalTests {
// MARK: - Wiring
@Test func exposesKnobsAndVoicingSelector() throws {
@Test func exposesKnobsAndSelectors() 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"])
#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")
}
@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 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.
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
}
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)")
}
@Test func noteModeOctaveRaisesThePitch() throws {
// A3 vs A4 — 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
}
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
}
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 presetRoundTripPreservesControls() throws {
@@ -367,6 +406,9 @@ 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
let state: PedalState = pedal.captureState()
let restored: KarplusResonatorPedal = KarplusResonatorPedal()
@@ -377,6 +419,9 @@ struct KarplusResonatorPedalTests {
#expect(restored.tone == 0.6)
#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)
}
@Test func registeredInBuiltInRegistry() throws {