diff --git a/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift index c189564..da10429 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift @@ -123,6 +123,13 @@ public final class LooperPedal: Pedal { private var overdubRecorders: [RecordBuffer.Audio] = [] private var overdubGates: [ParameterNode] = [] + /// Per-track playback mute gate (`1` = audible, `0` = muted), smoothed by `crossfadeTime` and + /// multiplied into each track's player so a recorded track can be silenced. + private var muteGates: [ParameterNode] = [] + + /// Per-track mute state (`true` = muted). + private var trackMuted: [Bool] = [] + /// Non-looping recorder for track A's length-defining first take. private var masterRecorder: RecordBuffer.Audio? @@ -350,6 +357,9 @@ public final class LooperPedal: Pedal { let gate: ParameterNode = ParameterNode(0.0) overdubGates.append(gate) + let muteGate: ParameterNode = ParameterNode(1.0) // 1 = audible + muteGates.append(muteGate) + let overdub: RecordBuffer.Audio = RecordBuffer.Audio( input: dry, buffer: buffer, @@ -385,17 +395,19 @@ public final class LooperPedal: Pedal { masterRecorder = master trackHasContent = [Bool](repeating: false, count: LooperPedal.trackCount) + trackMuted = [Bool](repeating: false, count: LooperPedal.trackCount) // Loop bank audible only while playing and not muted; smoothed to avoid clicks. let combinedGate: any ValidGeneratorInput = $isPlaying * $loopOn let smoothedGate: Lag.Control = Lag.Control(input: combinedGate, time: $crossfadeTime) - // Sum all track players. Empty buffers contribute silence. Each player is already - // scaled by the `Playback` level, so `Playback` sets the loop amount against the live - // signal below. - var loopSum: any ValidGeneratorInput = players[0] + // Sum all track players, each scaled by its smoothed per-track mute gate. Empty buffers + // contribute silence. Each player is already scaled by the `Playback` level, so `Playback` + // sets the loop amount against the live signal below. + var loopSum: any ValidGeneratorInput = + players[0] * Lag.Control(input: muteGates[0], time: $crossfadeTime) for i: Int in 1 ..< players.count { - loopSum = loopSum + players[i] + loopSum = loopSum + players[i] * Lag.Control(input: muteGates[i], time: $crossfadeTime) } // The clean (dry) signal always passes through at full level; the loop bank is added on @@ -417,9 +429,33 @@ public final class LooperPedal: Pedal { } } - /// Selects the active track (the one Loop records / overdubs into). Tapping a timeline dot - /// calls this. Only track A is selectable while clear. Selecting a new track ends any overdub - /// on the previous one (it drops to playback); the new track records nothing until Loop. + /// Handles a tap on a track dot: selects the track, or — when it is already the selected track + /// and holds recorded material — toggles its mute. So a first tap focuses a track (to overdub + /// or record it), and a second tap on that same recorded track mutes/unmutes it. + public func tapTrack(_ index: Int) { + guard index >= 0, index < LooperPedal.trackCount, !buffers.isEmpty else { return } + if index == selectedTrack, trackHasContent(index) { + toggleTrackMute(index) + } else { + selectTrack(index) + } + } + + /// `true` if track `index` is currently muted. + public func isTrackMuted(_ index: Int) -> Bool { + index >= 0 && index < trackMuted.count && trackMuted[index] + } + + /// Toggles a recorded track's playback mute (smoothed via `crossfadeTime`). + private func toggleTrackMute(_ index: Int) { + guard trackMuted.indices.contains(index), muteGates.indices.contains(index) else { return } + trackMuted[index].toggle() + muteGates[index].value = trackMuted[index] ? 0.0 : 1.0 + } + + /// Selects the active track (the one Loop records / overdubs into). Only track A is selectable + /// while clear. Selecting a new track ends any overdub on the previous one (it drops to + /// playback); the new track records nothing until Loop. public func selectTrack(_ index: Int) { guard index >= 0, index < LooperPedal.trackCount, index != selectedTrack, !buffers.isEmpty else { return } switch phase { @@ -566,6 +602,12 @@ public final class LooperPedal: Pedal { for i: Int in trackHasContent.indices { trackHasContent[i] = false } + for i: Int in trackMuted.indices { + trackMuted[i] = false + } + for gate: ParameterNode in muteGates { + gate.value = 1.0 + } selectedTrack = 0 overdubbingTrack = nil masterLength = 0 @@ -596,8 +638,10 @@ public final class LooperPedal: Pedal { players.removeAll() overdubRecorders.removeAll() overdubGates.removeAll() + muteGates.removeAll() masterRecorder = nil trackHasContent = [] + trackMuted = [] selectedTrack = 0 overdubbingTrack = nil masterLength = 0 diff --git a/Sources/LyrebirdShoeGazeUI/LooperTimelineView.swift b/Sources/LyrebirdShoeGazeUI/LooperTimelineView.swift index 2128629..7935089 100644 --- a/Sources/LyrebirdShoeGazeUI/LooperTimelineView.swift +++ b/Sources/LyrebirdShoeGazeUI/LooperTimelineView.swift @@ -37,6 +37,9 @@ struct LooperTimelineView: View { private static let dotFilledOpacity: Double = 0.55 private static let dotEmptyOpacity: Double = 0.45 private static let activeRingWidth: CGFloat = 1.5 + private static let mutedDotOpacity: Double = 0.4 + private static let muteSlashOpacity: Double = 0.9 + private static let muteSlashWidth: CGFloat = 1.5 private static let indicatorFontSize: CGFloat = 11 private static let indicatorOpacity: Double = 0.85 private static let overdubRingColor: Color = Color.red @@ -53,6 +56,7 @@ struct LooperTimelineView: View { @State private var selectedTrack: Int = 0 @State private var overdubbingTrack: Int? @State private var trackContent: [Bool] = Array(repeating: false, count: LooperPedal.trackCount) + @State private var trackMuted: [Bool] = Array(repeating: false, count: LooperPedal.trackCount) @State private var stopArmed: Bool = false @State private var stateDescription: String = "" @@ -83,6 +87,7 @@ struct LooperTimelineView: View { selectedTrack = pedal.selectedTrackIndex overdubbingTrack = pedal.overdubbingTrackIndex trackContent = (0 ..< LooperPedal.trackCount).map { pedal.trackHasContent($0) } + trackMuted = (0 ..< LooperPedal.trackCount).map { pedal.isTrackMuted($0) } stopArmed = pedal.isStopAtLoopEndArmed stateDescription = pedal.stateDescription try? await Task.sleep(nanoseconds: LooperTimelineView.pollIntervalNanoseconds) @@ -93,15 +98,17 @@ struct LooperTimelineView: View { // MARK: - Subviews /// One tappable labelled dot per track: filled when it holds a loop, white-ringed when - /// selected, red-ringed while overdubbing. Tapping selects the track. + /// selected, red-ringed while overdubbing, dimmed with a slash when muted. Tapping selects the + /// track; tapping the already-selected recorded track mutes/unmutes it. private var trackDots: some View { HStack(spacing: LooperTimelineView.dotSpacing) { ForEach(0 ..< LooperPedal.trackCount, id: \.self) { index in let isSelected: Bool = index == selectedTrack let isOverdub: Bool = index == overdubbingTrack let hasContent: Bool = index < trackContent.count && trackContent[index] + let isMuted: Bool = index < trackMuted.count && trackMuted[index] Button { - pedal.selectTrack(index) + pedal.tapTrack(index) } label: { Text(LooperTimelineView.dotLabels[index]) .font(.system(size: LooperTimelineView.dotLabelFontSize, weight: .bold, design: .monospaced)) @@ -111,17 +118,34 @@ struct LooperTimelineView: View { Circle().fill(hasContent ? Color.green.opacity(LooperTimelineView.dotFilledOpacity) : Color(white: LooperTimelineView.dotBrightness)) ) + .overlay(muteSlash(isMuted)) .overlay( Circle().stroke(isOverdub ? LooperTimelineView.overdubRingColor : (isSelected ? Color.white : Color.clear), lineWidth: LooperTimelineView.activeRingWidth) ) + .opacity(isMuted ? LooperTimelineView.mutedDotOpacity : 1.0) } .buttonStyle(.plain) } } } + /// A diagonal slash drawn over a muted track's dot. + @ViewBuilder + private func muteSlash(_ muted: Bool) -> some View { + if muted { + GeometryReader { geometry in + Path { path in + path.move(to: CGPoint(x: 0, y: geometry.size.height)) + path.addLine(to: CGPoint(x: geometry.size.width, y: 0)) + } + .stroke(Color.white.opacity(LooperTimelineView.muteSlashOpacity), + lineWidth: LooperTimelineView.muteSlashWidth) + } + } + } + private var stateBadge: some View { HStack(spacing: LooperTimelineView.badgeInnerSpacing) { Circle() diff --git a/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift b/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift index 01eccea..4b72eb3 100644 --- a/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift @@ -530,6 +530,35 @@ struct LooperPedalTests { } } } + @Test func tapTrackSelectsThenMutesRecordedTrack() throws { + let (engine, clock, pedal): (SynthesisEngine, LyrebirdClock, LooperPedal) = + try makeQuantizedFixture(bpm: 120.0, sampleRate: 44100.0) + withExtendedLifetime(engine) { withExtendedLifetime(clock) { + pedal.clock = nil + pedal.pressLoop() // record A + pedal.pressLoop() // playing, A selected + has content + pedal.pressLoop() // end auto-overdub, just playing + #expect(pedal.selectedTrackIndex == 0) + #expect(pedal.trackHasContent(0)) + #expect(!pedal.isTrackMuted(0)) + + // Second tap on the selected, recorded track A mutes it; third un-mutes. + pedal.tapTrack(0) + #expect(pedal.isTrackMuted(0), "tapping the selected recorded track mutes it") + pedal.tapTrack(0) + #expect(!pedal.isTrackMuted(0), "tapping again un-mutes it") + + // Tapping a different track selects it (doesn't mute), leaving A audible. + pedal.tapTrack(1) + #expect(pedal.selectedTrackIndex == 1) + #expect(!pedal.isTrackMuted(0)) + #expect(!pedal.isTrackMuted(1)) + + pedal.resetLoop() + #expect(!pedal.isTrackMuted(0), "reset un-mutes all tracks") + } } + } + @Test func cycleSyncModeOscillatesThroughOptions() throws { let pedal: LooperPedal = LooperPedal() #expect(pedal.syncToBeat == 1.0) // Sync by default