Skip to content

Commit

Permalink
fix unnecessary render causing by userSpeaking check (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobars authored Jan 18, 2025
1 parent 6ba6da8 commit 4f3f2d7
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
MicVAD,
getDefaultRealTimeVADOptions,
} from "@ricky0123/vad-web"
import React, { useEffect, useReducer, useState } from "react"
import React, { useEffect, useState } from "react"

export { utils } from "@ricky0123/vad-web"

Expand Down Expand Up @@ -65,19 +65,16 @@ function useEventCallback<T extends (...args: any[]) => any>(fn: T): T {

export function useMicVAD(options: Partial<ReactRealTimeVADOptions>) {
const [reactOptions, vadOptions] = useOptions(options)
const [userSpeaking, updateUserSpeaking] = useReducer(
(state: boolean, isSpeechProbability: number) =>
isSpeechProbability > reactOptions.userSpeakingThreshold,
false
)
const [userSpeaking, updateUserSpeaking] = useState(false)
const [loading, setLoading] = useState(true)
const [errored, setErrored] = useState<false | string>(false)
const [listening, setListening] = useState(false)
const [vad, setVAD] = useState<MicVAD | null>(null)

const userOnFrameProcessed = useEventCallback(vadOptions.onFrameProcessed)
vadOptions.onFrameProcessed = useEventCallback((probs, frame) => {
updateUserSpeaking(probs.isSpeech)
const isSpeaking = probs.isSpeech > reactOptions.userSpeakingThreshold
updateUserSpeaking(isSpeaking)
userOnFrameProcessed(probs, frame)
})
const { onSpeechEnd, onSpeechStart, onSpeechRealStart, onVADMisfire } =
Expand Down

0 comments on commit 4f3f2d7

Please sign in to comment.