Serialize split-region topology in presets
Replace the flat per-board pedal list with an ordered [BoardSegmentState] (pedal | parallel | stereoSplit | mergeToMono) so parallel and stereo split regions — branch pedal chains plus SplitMode/StereoRoute — round-trip through saveRig/loadRig and rebuildBoard. - PedalBoard.orderedSegments tracks chain structure (add/split/splitStereo/ mergeToMono); captureState() walks it; rebuildSegments() replays it. - BoardState backward-compatible: decodes legacy flat `pedals` arrays; computed `pedals` exposes the linear subset; throws on a file missing both keys. BoardSegmentState wire discriminators documented. - applyState (lightweight recall) extended to branch pedals + routing modes. - Unknown pedal types skipped on load, including inside branches. - Tests: parallel / stereo / mergeToMono file round-trips, legacy decode, unknown-pedal-in-branch, branch-param recall. Suite 116/116; demo builds. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -87,6 +87,194 @@ struct PresetTests {
|
||||
#expect(pedal is PingPongDelayPedal, "Ping-Pong should be registered")
|
||||
}
|
||||
|
||||
// MARK: - Split-region topology round-trip
|
||||
|
||||
@Test func parallelTopologyRoundTripsThroughFile() throws {
|
||||
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
let preset: URL = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent(UUID().uuidString + ".json")
|
||||
defer { try? FileManager.default.removeItem(at: preset) }
|
||||
|
||||
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
||||
let board: PedalBoard = try sg.addBoard()
|
||||
try board.add(OverdrivePedal())
|
||||
let section: ParallelSection = try board.split()
|
||||
try section.branchA.add(DelayPedal())
|
||||
try section.branchB.add(FuzzPedal())
|
||||
try board.merge(section)
|
||||
try board.add(ReverbPedal())
|
||||
try sg.finalizeBoard(board)
|
||||
|
||||
// Non-default routing + branch param so the round-trip proves real capture.
|
||||
section.splitter.mode = .a
|
||||
if let delay: DelayPedal = section.branchA.pedals.first as? DelayPedal {
|
||||
delay.time = 0.6
|
||||
}
|
||||
|
||||
let before: ShoeGazeState = sg.captureState()
|
||||
try sg.saveRig(to: preset)
|
||||
try sg.loadRig(from: preset)
|
||||
let after: ShoeGazeState = sg.captureState()
|
||||
|
||||
#expect(before == after, "Parallel topology should round-trip through a file")
|
||||
let hasParallel: Bool = after.boards[0].segments.contains {
|
||||
if case .parallel = $0 { return true } else { return false }
|
||||
}
|
||||
#expect(hasParallel, "Reconstructed board should contain a parallel segment")
|
||||
}
|
||||
|
||||
@Test func stereoSplitTopologyRoundTripsThroughFile() throws {
|
||||
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
let preset: URL = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent(UUID().uuidString + ".json")
|
||||
defer { try? FileManager.default.removeItem(at: preset) }
|
||||
|
||||
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
||||
let board: PedalBoard = try sg.addBoard()
|
||||
try board.add(OverdrivePedal())
|
||||
let section: StereoSplitSection = try board.splitStereo()
|
||||
try section.left.add(DelayPedal())
|
||||
try section.right.add(PingPongDelayPedal())
|
||||
try board.collectStereo(section)
|
||||
try sg.finalizeBoard(board)
|
||||
|
||||
section.route = .rightOnly
|
||||
if let delay: DelayPedal = section.left.pedals.first as? DelayPedal {
|
||||
delay.time = 0.45
|
||||
}
|
||||
|
||||
let before: ShoeGazeState = sg.captureState()
|
||||
try sg.saveRig(to: preset)
|
||||
try sg.loadRig(from: preset)
|
||||
let after: ShoeGazeState = sg.captureState()
|
||||
|
||||
#expect(before == after, "Stereo split topology should round-trip through a file")
|
||||
#expect(sg.boards[0].width == 2, "Reconstructed board should be stereo")
|
||||
let hasStereo: Bool = after.boards[0].segments.contains {
|
||||
if case .stereoSplit = $0 { return true } else { return false }
|
||||
}
|
||||
#expect(hasStereo, "Reconstructed board should contain a stereo split segment")
|
||||
// Route survived.
|
||||
guard case .stereoSplit(let stereoState)? =
|
||||
after.boards[0].segments.first(where: {
|
||||
if case .stereoSplit = $0 { return true } else { return false }
|
||||
}) else {
|
||||
Issue.record("No stereo split segment after reload")
|
||||
return
|
||||
}
|
||||
#expect(stereoState.route == .rightOnly, "Route should be restored")
|
||||
}
|
||||
|
||||
@Test func mergeToMonoTopologyRoundTripsThroughFile() throws {
|
||||
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
let preset: URL = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent(UUID().uuidString + ".json")
|
||||
defer { try? FileManager.default.removeItem(at: preset) }
|
||||
|
||||
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
||||
let board: PedalBoard = try sg.addBoard()
|
||||
try board.add(OctaveUpSplitterPedal()) // widen → stereo
|
||||
try board.mergeToMono() // collapse → mono
|
||||
try board.add(ReverbPedal()) // mono pedal after merge
|
||||
try sg.finalizeBoard(board)
|
||||
#expect(board.width == 1, "Board should be mono after merge")
|
||||
|
||||
let before: ShoeGazeState = sg.captureState()
|
||||
try sg.saveRig(to: preset)
|
||||
try sg.loadRig(from: preset)
|
||||
let after: ShoeGazeState = sg.captureState()
|
||||
|
||||
#expect(before == after, "Widen→merge→mono topology should round-trip")
|
||||
#expect(sg.boards[0].width == 1, "Rebuilt board should be mono (merge replayed)")
|
||||
let hasMerge: Bool = after.boards[0].segments.contains {
|
||||
if case .mergeToMono = $0 { return true } else { return false }
|
||||
}
|
||||
#expect(hasMerge, "Reconstructed board should contain a mergeToMono segment")
|
||||
}
|
||||
|
||||
@Test func legacyFlatPedalsPresetStillDecodes() throws {
|
||||
// A board snapshot written before topology capture stored a flat `pedals` array and
|
||||
// no `segments` key. It must still decode, wrapping each pedal as a `.pedal` segment.
|
||||
let legacyJSON: String = """
|
||||
{"label":"Legacy","pedals":[\
|
||||
{"type":"Overdrive","knobValues":[0.5,1.0,1.0],"switchValues":[],"bypass":0.0},\
|
||||
{"type":"Delay","knobValues":[0.25,1.0,0.35,1.0],"switchValues":[],"bypass":0.0}]}
|
||||
"""
|
||||
let decoded: BoardState = try JSONDecoder().decode(
|
||||
BoardState.self,
|
||||
from: Data(legacyJSON.utf8)
|
||||
)
|
||||
#expect(decoded.label == "Legacy")
|
||||
#expect(decoded.segments.count == 2, "Legacy pedals should become two segments")
|
||||
#expect(decoded.pedals.count == 2, "Linear pedal view should expose both")
|
||||
#expect(decoded.pedals[0].type == "Overdrive")
|
||||
#expect(decoded.pedals[1].type == "Delay")
|
||||
}
|
||||
|
||||
@Test func unknownPedalInBranchIsSkippedOnLoad() throws {
|
||||
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
let preset: URL = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent(UUID().uuidString + ".json")
|
||||
defer { try? FileManager.default.removeItem(at: preset) }
|
||||
|
||||
// Parallel segment with an unknown pedal in branch A and a known one in branch B.
|
||||
let parallel: ParallelSectionState = ParallelSectionState(
|
||||
branchA: [PedalState(type: "GhostPedal", knobValues: [], bypass: 0.0)],
|
||||
branchB: [PedalState(type: "Fuzz", knobValues: [20.0, 1.0, 1.0], bypass: 0.0)],
|
||||
mode: .both
|
||||
)
|
||||
let boardState: BoardState = BoardState(label: "B", segments: [.parallel(parallel)])
|
||||
let state: ShoeGazeState = ShoeGazeState(boards: [boardState])
|
||||
try JSONEncoder().encode(state).write(to: preset)
|
||||
|
||||
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
||||
try sg.loadRig(from: preset)
|
||||
|
||||
let after: ShoeGazeState = sg.captureState()
|
||||
guard case .parallel(let rebuilt)? = after.boards.first?.segments.first(where: {
|
||||
if case .parallel = $0 { return true } else { return false }
|
||||
}) else {
|
||||
Issue.record("No parallel segment after load")
|
||||
return
|
||||
}
|
||||
#expect(rebuilt.branchA.isEmpty, "Unknown branch-A pedal should be skipped")
|
||||
#expect(rebuilt.branchB.count == 1, "Known branch-B pedal should rebuild")
|
||||
#expect(rebuilt.branchB.first?.type == "Fuzz")
|
||||
}
|
||||
|
||||
@Test func applyStateRecallsBranchParamsAndRoute() throws {
|
||||
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
||||
let board: PedalBoard = try sg.addBoard()
|
||||
let section: StereoSplitSection = try board.splitStereo()
|
||||
try section.left.add(DelayPedal())
|
||||
try section.right.add(FuzzPedal())
|
||||
try board.collectStereo(section)
|
||||
try sg.finalizeBoard(board)
|
||||
|
||||
let defaultTime: Double = 0.25 // DelayPedal default
|
||||
let baseline: ShoeGazeState = sg.captureState()
|
||||
|
||||
// Mutate a branch pedal param and the route, then recall the baseline.
|
||||
guard let delay: DelayPedal = section.left.pedals.first as? DelayPedal else {
|
||||
Issue.record("No DelayPedal in left branch")
|
||||
return
|
||||
}
|
||||
delay.time = 0.9
|
||||
section.route = .bypass
|
||||
|
||||
sg.applyState(baseline)
|
||||
|
||||
#expect(abs(delay.$time.value - defaultTime) < 1e-9, "Branch pedal param should be recalled")
|
||||
#expect(section.route == .stereo, "Route should be recalled")
|
||||
}
|
||||
|
||||
// MARK: - In-memory round-trip
|
||||
|
||||
@Test func captureStateRoundTripEquality() throws {
|
||||
|
||||
Reference in New Issue
Block a user