|
| 1 | +/* |
| 2 | + * Copyright 2025 LiveKit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * Copyright 2025 LiveKit |
| 19 | + * |
| 20 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | + * you may not use this file except in compliance with the License. |
| 22 | + * You may obtain a copy of the License at |
| 23 | + * |
| 24 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | + * |
| 26 | + * Unless required by applicable law or agreed to in writing, software |
| 27 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | + * See the License for the specific language governing permissions and |
| 30 | + * limitations under the License. |
| 31 | + */ |
| 32 | + |
| 33 | +import LiveKit |
| 34 | +import SwiftUI |
| 35 | + |
| 36 | +/// A SwiftUI view that visualizes audio levels and agent states as a series of animated vertical bars. |
| 37 | +/// This visualizer is specifically designed to provide visual feedback for different agent states |
| 38 | +/// (connecting, initializing, listening, thinking, speaking) while also responding to real-time |
| 39 | +/// audio data when available. |
| 40 | +/// |
| 41 | +/// `AgentBarAudioVisualizer` displays bars whose heights and opacities dynamically |
| 42 | +/// reflect the magnitude of audio frequencies in real time, creating an |
| 43 | +/// interactive, visual representation of the audio track's spectrum. This |
| 44 | +/// visualizer can be customized in terms of bar count, color, corner radius, |
| 45 | +/// spacing, and whether the bars are centered based on frequency magnitude. |
| 46 | +/// |
| 47 | +/// Usage: |
| 48 | +/// ``` |
| 49 | +/// let audioTrack: AudioTrack = ... |
| 50 | +/// let agentState: AgentState = ... |
| 51 | +/// AgentBarAudioVisualizer(audioTrack: audioTrack, agentState: agentState) |
| 52 | +/// ``` |
| 53 | +/// |
| 54 | +/// - Parameters: |
| 55 | +/// - audioTrack: The `AudioTrack` providing audio data to be visualized. |
| 56 | +/// - agentState: Triggers transitions between visualizer animation states. |
| 57 | +/// - barColor: The color used to fill each bar, defaulting to white. |
| 58 | +/// - barCount: The number of bars displayed, defaulting to 7. |
| 59 | +/// - barCornerRadius: The corner radius applied to each bar, giving a |
| 60 | +/// rounded appearance. Defaults to 100. |
| 61 | +/// - barSpacingFactor: Determines the spacing between bars as a factor |
| 62 | +/// of view width. Defaults to 0.015. |
| 63 | +/// - isCentered: A Boolean indicating whether higher-decibel bars |
| 64 | +/// should be centered. Defaults to `true`. |
| 65 | +/// |
| 66 | +/// Example: |
| 67 | +/// ``` |
| 68 | +/// AgentBarAudioVisualizer(audioTrack: audioTrack, barColor: .blue, barCount: 10) |
| 69 | +/// ``` |
| 70 | +public struct AgentBarAudioVisualizer: View { |
| 71 | + public let barCount: Int |
| 72 | + public let barColor: Color |
| 73 | + public let barCornerRadius: CGFloat |
| 74 | + public let barSpacingFactor: CGFloat |
| 75 | + public let barMinOpacity: Double |
| 76 | + public let isCentered: Bool |
| 77 | + |
| 78 | + private let agentState: AgentState |
| 79 | + |
| 80 | + @StateObject private var audioProcessor: AudioProcessor |
| 81 | + |
| 82 | + @State private var animationProperties: PhaseAnimationProperties |
| 83 | + @State private var animationPhase: Int = 0 |
| 84 | + @State private var animationTask: Task<Void, Never>? |
| 85 | + |
| 86 | + public init(audioTrack: AudioTrack?, |
| 87 | + agentState: AgentState, |
| 88 | + barColor: Color = .primary, |
| 89 | + barCount: Int = 5, |
| 90 | + barCornerRadius: CGFloat = 100, |
| 91 | + barSpacingFactor: CGFloat = 0.015, |
| 92 | + barMinOpacity: CGFloat = 0.16, |
| 93 | + isCentered: Bool = true) |
| 94 | + { |
| 95 | + self.agentState = agentState |
| 96 | + |
| 97 | + self.barColor = barColor |
| 98 | + self.barCount = barCount |
| 99 | + self.barCornerRadius = barCornerRadius |
| 100 | + self.barSpacingFactor = barSpacingFactor |
| 101 | + self.barMinOpacity = Double(barMinOpacity) |
| 102 | + self.isCentered = isCentered |
| 103 | + |
| 104 | + _audioProcessor = StateObject(wrappedValue: AudioProcessor(track: audioTrack, |
| 105 | + bandCount: barCount, |
| 106 | + isCentered: isCentered)) |
| 107 | + |
| 108 | + animationProperties = PhaseAnimationProperties(barCount: barCount) |
| 109 | + } |
| 110 | + |
| 111 | + public var body: some View { |
| 112 | + GeometryReader { geometry in |
| 113 | + let highlightingSequence = animationProperties.highlightingSequence(agentState: agentState) |
| 114 | + let highlighted = highlightingSequence[animationPhase % highlightingSequence.count] |
| 115 | + let duration = animationProperties.duration(agentState: agentState) |
| 116 | + |
| 117 | + bars(geometry: geometry, highlighted: highlighted) |
| 118 | + .onAppear { |
| 119 | + animationTask?.cancel() |
| 120 | + animationTask = Task { |
| 121 | + while !Task.isCancelled { |
| 122 | + try? await Task.sleep(nanoseconds: UInt64(duration * Double(NSEC_PER_SEC))) |
| 123 | + withAnimation(.easeInOut) { animationPhase += 1 } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + .onDisappear { |
| 128 | + animationTask?.cancel() |
| 129 | + } |
| 130 | + .animation(.easeOut, value: agentState) |
| 131 | + .onChange(of: agentState) { _ in |
| 132 | + animationPhase = 0 |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @ViewBuilder |
| 138 | + private func bars(geometry: GeometryProxy, highlighted: PhaseAnimationProperties.HighlightedBars) -> some View { |
| 139 | + let barMinHeight = (geometry.size.width - geometry.size.width * barSpacingFactor * CGFloat(barCount + 2)) / CGFloat(barCount) |
| 140 | + HStack(alignment: .center, spacing: geometry.size.width * barSpacingFactor) { |
| 141 | + ForEach(0 ..< audioProcessor.bands.count, id: \.self) { index in |
| 142 | + VStack { |
| 143 | + Spacer() |
| 144 | + RoundedRectangle(cornerRadius: barMinHeight) |
| 145 | + .fill(barColor) |
| 146 | + .opacity(highlighted.contains(index) ? 1 : barMinOpacity) |
| 147 | + .frame(height: (geometry.size.height - barMinHeight) * CGFloat(audioProcessor.bands[index]) + barMinHeight) |
| 148 | + Spacer() |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + .padding(geometry.size.width * barSpacingFactor) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +extension AgentBarAudioVisualizer { |
| 157 | + private struct PhaseAnimationProperties { |
| 158 | + typealias HighlightedBars = Set<Int> |
| 159 | + |
| 160 | + private let barCount: Int |
| 161 | + private let veryLongDuration: TimeInterval = 1000 |
| 162 | + |
| 163 | + init(barCount: Int) { |
| 164 | + self.barCount = barCount |
| 165 | + } |
| 166 | + |
| 167 | + func duration(agentState: AgentState) -> TimeInterval { |
| 168 | + switch agentState { |
| 169 | + case .connecting, .initializing: return 2 / Double(barCount) |
| 170 | + case .listening: return 0.5 |
| 171 | + case .thinking: return 0.15 |
| 172 | + case .speaking: return veryLongDuration |
| 173 | + default: return veryLongDuration |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + func highlightingSequence(agentState: AgentState) -> [HighlightedBars] { |
| 178 | + switch agentState { |
| 179 | + case .connecting, .initializing: return (0 ..< barCount).map { HighlightedBars([$0, barCount - 1 - $0]) } |
| 180 | + case .thinking: return Array((0 ..< barCount) + (0 ..< barCount).reversed()).map { HighlightedBars([$0]) } |
| 181 | + case .listening: return barCount % 2 == 0 ? [[(barCount / 2) - 1, barCount / 2], []] : [[barCount / 2], []] |
| 182 | + case .speaking: return [HighlightedBars(0 ..< barCount)] |
| 183 | + default: return [[]] |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments