0

I am making voice assistant on python.
Here is the code:

import os
import time
import playsound
import speech_recognition as sr
from gtts import gTTS


def speak(text):
    tts = gTTS(text=text, lang="en")
    filename = "voice.mp3"
    tts.save(filename)
    playsound.playsound(filename)


def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

    return said

text = get_audio()

if "hello" in text:
    speak("Hi, how are you?")

And when I say "hello" it's thinking for 2 seconds, and then says "Hi, how are you?"
Maybe it's because I'm saving mp3 file? How to make program answer without delay?

1 Answers1

0
import os
import time
import playsound
import speech_recognition as sr
from gtts import gTTS

starttime = time.perf_counter()

def speak(text):
    tts = gTTS(text=text, lang="en")
    filename = "voice.mp3"
    tts.save(filename)
    playsound.playsound(filename)

elapsed=time.perf_counter()-starttime    

def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

    return said

text = get_audio()

if "hello" in text:
    speak("Hi, how are you?")