Add Stereo Unzip: process a stereo signal's L/R independently

A stereo-output pedal (Ping-Pong, Octave Splitter) now shows a
"Stereo flow / Split L/R" toggle on its card:

- Stereo flow (default): unchanged broadcast behavior — following pedals
  process both channels as one stereo flow.
- Split L/R: inserts a Stereo Unzip region right after the pedal, routing
  the L channel into one lane and R into the other for independent
  processing, then recombining to stereo with the same route/solo/bypass
  control as the stereo split.

Engine/core:
- StereoUnzipSplitter: 2-in -> 3-out node (L->lane L, R->lane R, and a
  centered 0.5*L+0.5*R dry tap for the bypass route).
- PedalBoard.unzipStereo() mirrors splitStereo() but reads both tails and
  requires width 2; reuses collectStereo / StereoSplitSection /
  StereoRouteCollector. New stereoUnzipSections (cleared on teardown).
- BoardSegment/.stereoUnzip + StereoUnzipState round-trip via preset
  capture/rebuild/applyState.
- ShoeGaze.insertStereoUnzip(afterSegment:)/isStereoUnzip(afterSegment:)
  drive the toggle; invalid insert (mono tail) throws
  cannotUnzipMonoChain and the atomic rebuild keeps the board intact.
- StereoSplitSection.splitter relaxed to `any Synthesizer` (stored only)
  so it can hold either splitter type.

UI: per-pedal toggle + STEREO UNZIP region rendering in BoardFlowView.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-22 10:21:18 -07:00
parent d8142c9d7d
commit 1a09cf9cf8
12 changed files with 455 additions and 7 deletions
@@ -299,6 +299,111 @@ struct BoardEditAPITests {
}
}
// MARK: - Stereo unzip (process L/R of a stereo signal independently)
private func unzipState(_ board: PedalBoard) -> StereoUnzipState? {
for segment: BoardSegmentState in board.captureState().segments {
if case .stereoUnzip(let state) = segment { return state }
}
return nil
}
@Test func insertStereoUnzipAfterStereoPedalFillsLanesIndependently() throws {
// Ping-Pong makes the chain stereo; unzip after it → two independent L/R lanes.
let (sg, engine, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Ping-Pong"])
defer { try? FileManager.default.removeItem(at: url) }
#expect(board.width == 2, "Ping-Pong should make the board stereo")
// Ping-Pong is segment 0; insert the unzip right after it (segment 1).
var live: PedalBoard = try sg.insertStereoUnzip(afterSegment: 0, in: board)
#expect(sg.isStereoUnzip(afterSegment: 0, in: live), "toggle should report unzip present")
live = try sg.appendPedal(type: "Fuzz", to: live, lane: .left(region: 1))
live = try sg.appendPedal(type: "Reverb", to: live, lane: .right(region: 1))
guard let state: StereoUnzipState = unzipState(live) else {
Issue.record("No stereo unzip segment")
return
}
#expect(state.left.map { $0.type } == ["Fuzz"], "left lane = Fuzz")
#expect(state.right.map { $0.type } == ["Reverb"], "right lane = Reverb")
#expect(live.width == 2, "board stays stereo after unzip + recombine")
let samples: [Float] = try render(engine, url)
#expect(hasNonSilentOutput(samples), "unzip board should render audio")
}
@Test func insertStereoUnzipOnMonoTailThrowsAndKeepsBoard() throws {
// Overdrive keeps the chain mono → unzip after it is invalid; rebuild throws, board intact.
let (sg, _, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Overdrive"])
defer { try? FileManager.default.removeItem(at: url) }
#expect(throws: ShoeGazeError.cannotUnzipMonoChain) {
try sg.insertStereoUnzip(afterSegment: 0, in: board)
}
#expect(names(sg.boards[0]) == ["Overdrive"], "board unchanged after failed unzip")
#expect(sg.boards[0].width == 1)
}
@Test func isStereoUnzipFalseWhenNextSegmentIsNotUnzip() throws {
let (sg, _, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Ping-Pong", "Reverb"]) // two pedals, no unzip
defer { try? FileManager.default.removeItem(at: url) }
// Ping-Pong is segment 0; segment 1 is the Reverb pedal, not an unzip.
#expect(!sg.isStereoUnzip(afterSegment: 0, in: board))
// Out-of-range (last segment) is also false.
#expect(!sg.isStereoUnzip(afterSegment: 1, in: board))
}
@Test func stereoUnzipToggleRoundTrip() throws {
let (sg, _, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Octave Splitter"]) // widener → stereo
defer { try? FileManager.default.removeItem(at: url) }
#expect(board.width == 2)
var live: PedalBoard = try sg.insertStereoUnzip(afterSegment: 0, in: board)
#expect(sg.isStereoUnzip(afterSegment: 0, in: live), "unzip present after insert")
live = try sg.removeSegment(at: 1, from: live)
#expect(!sg.isStereoUnzip(afterSegment: 0, in: live), "unzip removed by toggle-off")
}
@Test func stereoUnzipRoundTripsThroughFile() throws {
let (sg, _, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Ping-Pong"])
defer { try? FileManager.default.removeItem(at: url) }
var live: PedalBoard = try sg.insertStereoUnzip(afterSegment: 0, in: board)
live = try sg.appendPedal(type: "Delay", to: live, lane: .left(region: 1))
live = try sg.appendPedal(type: "Reverb", to: live, lane: .right(region: 1))
if let section: StereoSplitSection = live.stereoUnzipSections.first {
section.route = .rightOnly
}
let preset: URL = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString + ".json")
defer { try? FileManager.default.removeItem(at: preset) }
try sg.saveRig(to: preset)
let (engine2, url2): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
defer { try? FileManager.default.removeItem(at: url2) }
let sg2: ShoeGaze = ShoeGaze(engine: engine2)
try sg2.loadRig(from: preset)
guard let restored: StereoUnzipState = unzipState(sg2.boards[0]) else {
Issue.record("Unzip region lost on reload")
return
}
#expect(restored.left.map { $0.type } == ["Delay"], "left lane round-trips")
#expect(restored.right.map { $0.type } == ["Reverb"], "right lane round-trips")
#expect(restored.route == .rightOnly, "route round-trips")
#expect(sg2.boards[0].width == 2, "reloaded board stays stereo")
let samples: [Float] = try render(engine2, url2)
#expect(hasNonSilentOutput(samples), "reloaded unzip rig renders")
}
@Test func parallelSplitBranchEditing() throws {
let (sg, engine, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Overdrive"])