Skip to content

Commit

Permalink
Code cleanup and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidliu committed Oct 7, 2024
1 parent 0e14fdb commit 7a85751
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import io.livekit.android.LiveKitOverrides
import io.livekit.android.RoomOptions
import io.livekit.android.room.Room
import io.livekit.android.room.RoomException
import io.livekit.android.util.LKLog
import io.livekit.android.util.flow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collectLatest
Expand Down Expand Up @@ -84,6 +85,10 @@ fun HandleRoomState(
)
}

private val DEFAULT_ERROR_HANDLER: ((Room, Exception?) -> Unit) = { _, e ->
LKLog.e(e) { "Error connecting to the server:" }
}

/**
* Remembers a new [Room] object.
*
Expand Down Expand Up @@ -112,7 +117,7 @@ fun rememberLiveKitRoom(
connectOptions: ConnectOptions? = null,
onConnected: (suspend CoroutineScope.(Room) -> Unit)? = null,
onDisconnected: (suspend CoroutineScope.(Room) -> Unit)? = null,
onError: ((Room, Exception?) -> Unit)? = null,
onError: ((Room, Exception?) -> Unit)? = DEFAULT_ERROR_HANDLER,
passedRoom: Room? = null,
): Room {
val context = LocalContext.current
Expand Down Expand Up @@ -154,7 +159,7 @@ fun rememberLiveKitRoom(
val ret: suspend CoroutineScope.(Room, Room.State) -> Unit = { r: Room, _: Room.State ->
if (once) {
once = false
r.localParticipant.setMicrophoneEnabled(video)
r.localParticipant.setMicrophoneEnabled(audio)
}
}
return@remember ret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ fun rememberAgentState(participant: Participant?): AgentState {
}

const val PARTICIPANT_ATTRIBUTE_LK_AGENT_STATE_KEY = "lk.agent.state"
const val PARTICIPANT_ATTRIBUTE_LK_AGENT_STATE_INITIALIZING = "initializing"
const val PARTICIPANT_ATTRIBUTE_LK_AGENT_STATE_LISTENING = "listening"
const val PARTICIPANT_ATTRIBUTE_LK_AGENT_STATE_THINKING = "thinking"
const val PARTICIPANT_ATTRIBUTE_LK_AGENT_STATE_SPEAKING = "speaking"
Expand All @@ -151,9 +152,10 @@ enum class AgentState {
companion object {
fun fromAttribute(attribute: String?): AgentState {
return when (attribute) {
"listening" -> LISTENING
"thinking" -> THINKING
"speaking" -> SPEAKING
PARTICIPANT_ATTRIBUTE_LK_AGENT_STATE_INITIALIZING -> INITIALIZING
PARTICIPANT_ATTRIBUTE_LK_AGENT_STATE_LISTENING -> LISTENING
PARTICIPANT_ATTRIBUTE_LK_AGENT_STATE_THINKING -> THINKING
PARTICIPANT_ATTRIBUTE_LK_AGENT_STATE_SPEAKING -> SPEAKING
else -> UNKNOWN
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import kotlin.math.round
import kotlin.math.sqrt

/**
* An audio visualizer for an audio [TrackReference].
*
* @param loPass the start index of the FFT samples to use (inclusive). 0 <= loPass < [hiPass].
* @param hiPass the end index of the FFT samples to use (exclusive). [loPass] < hiPass <= [FFTAudioAnalyzer.SAMPLE_SIZE].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,51 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.drawscope.Fill
import io.livekit.android.annotations.Beta
import io.livekit.android.compose.state.AgentState
import io.livekit.android.compose.state.VoiceAssistant
import io.livekit.android.compose.state.rememberVoiceAssistant
import io.livekit.android.compose.types.TrackReference
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive

private const val ON = 1.0f
private const val OFF = 0.3f

/**
* An audio visualizer for a [VoiceAssistant].
*
* @see [rememberVoiceAssistant]
*/
@Beta
@Composable
fun VoiceAssistantBarVisualizer(
agentState: String?,
voiceAssistant: VoiceAssistant,
modifier: Modifier = Modifier,
barCount: Int = 15,
loPass: Int = 50,
hiPass: Int = 150,
brush: Brush = SolidColor(Color.Black),
) {
VoiceAssistantBarVisualizer(
agentState = voiceAssistant.state,
audioTrackRef = voiceAssistant.audioTrack,
modifier = modifier,
barCount = barCount,
loPass = loPass,
hiPass = hiPass,
brush = brush,
)
}

/**
* An audio visualizer for a [VoiceAssistant].
*
* @see [rememberVoiceAssistant]
*/
@Beta
@Composable
fun VoiceAssistantBarVisualizer(
agentState: AgentState?,
audioTrackRef: TrackReference?,
modifier: Modifier = Modifier,
barCount: Int = 15,
Expand All @@ -61,33 +95,31 @@ fun VoiceAssistantBarVisualizer(

@Composable
private fun rememberVoiceAssistantAlphas(
agentState: String?,
agentState: AgentState?,
barCount: Int,
): FloatArray {
var sequenceIndex by remember(agentState, barCount) {
mutableIntStateOf(0)
}
val sequences = remember(agentState, barCount) {
when (agentState) {
"connecting",
"initializing" -> generateConnectingSequenceBar(barCount)
AgentState.CONNECTING,
AgentState.INITIALIZING -> generateConnectingSequenceBar(barCount)

"listening",
"thinking" -> generateListeningSequenceBar(barCount)
AgentState.LISTENING,
AgentState.THINKING -> generateListeningSequenceBar(barCount)

"speaking" -> generateSpeakingSequenceBar(barCount)
AgentState.SPEAKING -> generateSpeakingSequenceBar(barCount)
else -> listOf(FloatArray(0))
}
}
val intervalMs = remember(agentState) {
when (agentState) {
"connecting" -> 600L / barCount
"initializing" -> 1200L / barCount

"listening" -> 500L
"thinking" -> 150L

"speaking" -> -1L
AgentState.CONNECTING -> 600L / barCount
AgentState.INITIALIZING -> 1200L / barCount
AgentState.LISTENING -> 500L
AgentState.THINKING -> 150L
AgentState.SPEAKING -> -1L
else -> -1L
}
}
Expand Down

0 comments on commit 7a85751

Please sign in to comment.