JamesBrill / react-speech-recognition

💬Speech recognition for your React app
https://webspeechrecognition.com/
MIT License
637 stars 116 forks source link

How to get the delay time between receiving the speech and generating the transcript #169

Open nhtoby311 opened 1 year ago

nhtoby311 commented 1 year ago

Since there is a delay between speaking and generating text transcript, I'm looking for a way to overcome this. I wonder if there is a way to get the time interval between the moment the user speak into the mic, and the time when the transcript is being generated?

dlato97 commented 7 months ago

You can overcome this by using a continuous SpeechRecognition.startListening({ continuous: true }); and manage manually delay between last word received in transcript and stopListening. Here an example that I use in my app:

` const DELAY_BEFORE_SEND_MESSAGE = 1500; const timeoutId = useRef<null | NodeJS.Timeout>(null);

       useEffect(() => {
          if (transcript) {
            if (timeoutId.current) {
              clearTimeout(timeoutId.current);
            }

            timeoutId.current = setTimeout(() => {
              SpeechRecognition.stopListening();
              // ACTION AFTER STOP LISTENING
            }, DELAY_BEFORE_SEND_MESSAGE);
          }

          return () => {
            if (timeoutId.current) {
              clearTimeout(timeoutId.current);
            }
          };
        }, [transcript]);

`