Skip to content

Agent audio visualizer #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2025 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import LiveKit
import SwiftUI

// TODO: Move this to SDK
extension Room {
var firstAgentParticipant: RemoteParticipant? {
remoteParticipants.values.first { $0.kind == .agent }
}
}

public struct AgentBarAudioVisualizer: View {
public let barColor: Color
public let barCount: Int
public let barCornerRadius: CGFloat
public let barSpacingFactor: CGFloat
public let barMinOpacity: Double
public let isCentered: Bool

@EnvironmentObject private var room: Room

public init(barColor: Color = .primary,
barCount: Int = 5,
barCornerRadius: CGFloat = 100,
barSpacingFactor: CGFloat = 0.015,
barMinOpacity: CGFloat = 0.35,
isCentered: Bool = true)
{
self.barColor = barColor
self.barCount = barCount
self.barCornerRadius = barCornerRadius
self.barSpacingFactor = barSpacingFactor
self.barMinOpacity = barMinOpacity
self.isCentered = isCentered
}

public var body: some View {
let firstAgentParticipant = room.firstAgentParticipant
let agentState = firstAgentParticipant?.agentState ?? .unknown
let firstAudioPublication = firstAgentParticipant?.firstAudioPublication as? RemoteTrackPublication
let track = firstAudioPublication?.track

let animation = agentState == .speaking
? nil
: Animation.easeInOut(duration: 1)
.repeatForever(autoreverses: true)
.speed(1)

BarAudioVisualizer(audioTrack: track as? AudioTrack,
barColor: barColor,
barCount: barCount,
barCornerRadius: barCornerRadius,
barSpacingFactor: barSpacingFactor,
barMinOpacity: barMinOpacity,
isCentered: isCentered)
.id(track)
.opacity(agentState == .speaking ? 1 : 0.3)
.animation(animation, value: agentState)
}
}
11 changes: 4 additions & 7 deletions Sources/LiveKitComponents/UI/Visualizer/Visualizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,23 @@ public class AudioProcessor: ObservableObject, AudioRenderer {
/// BarAudioVisualizer(audioTrack: audioTrack, barColor: .blue, barCount: 10)
/// ```
public struct BarAudioVisualizer: View {
public let barCount: Int
public let barColor: Color
public let barCount: Int
public let barCornerRadius: CGFloat
public let barSpacingFactor: CGFloat
public let barMinOpacity: Double
public let isCentered: Bool

public let audioTrack: AudioTrack

@StateObject private var audioProcessor: AudioProcessor

public init(audioTrack: AudioTrack,
barColor: Color = .white,
barCount: Int = 7,
public init(audioTrack: AudioTrack?,
barColor: Color = .primary,
barCount: Int = 5,
barCornerRadius: CGFloat = 100,
barSpacingFactor: CGFloat = 0.015,
barMinOpacity: CGFloat = 0.35,
isCentered: Bool = true)
{
self.audioTrack = audioTrack
self.barColor = barColor
self.barCount = barCount
self.barCornerRadius = barCornerRadius
Expand Down