0

I am trying to create a voice assistant using, gtts(Google text to speech).

I tried doing this but for audio output, it was able to write something that first saves the file and then plays it using audio player (in ubuntu), how to make it in the way it just speaks rather than saving it just like it does using engine.say() in pyttsx3

def speak(temp):
voice = gTTS(text=temp, lang="en")
voice.save("/home/vikrant/temp.mp3")
opener ="open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, "/home/vikrant/temp.mp3"])

It saves the file and plays it using audio player, I just want to play it directly because it doesn't make sense for the assistant to open player every time to speak something.

1 Answers1

0

Well, I kinda solved it myself

"""This function will take a string as input
convert it into voice and save it as a file temp.mp3
play it and delete it"""
def speak(temp):
    voice = gTTS(text=temp, lang="en")
    voice.save("/home/vikrant/temp.mp3")
    opener ="open" if sys.platform == "darwin" else "xdg-open"
    subprocess.call([opener, "/home/vikrant/temp.mp3"])
    print("Speaking.....")
    time.sleep(1)
    os.remove("/home/vikrant/temp.mp3")

it works

  • tip: use /tmp for temp files, in case your progress gets killed, etc the temp file will be removed at next reboot. – jp_ Jul 19 '23 at 14:46