0
Q:

python text to speech

The best library because you dont have to save the
text file or open the file to start the speech

pip install pyttsx3

import pyttsx3
engine = pyttsx3.init()
engine.say("Hello world")
engine.runAndWait()
4
from gtts import gTTS
from playsound import  playsound

mytext="Hello Geek! How are you doing??"
language='en'
myobj=gTTS(text=mytext,lang=language,slow=True)
myobj.save("welcome1.mp3")
playsound("welcome1.mp3")
3
# Import the required module for text  
# to speech conversion 
from gtts import gTTS 
  
# This module is imported so that we can  
# play the converted audio 
import os 
  
# The text that you want to convert to audio 
mytext = 'Welcome to geeksforgeeks!'
  
# Language in which you want to convert 
language = 'en'
  
# Passing the text and language to the engine,  
# here we have marked slow=False. Which tells  
# the module that the converted audio should  
# have a high speed 
myobj = gTTS(text=mytext, lang=language, slow=False) 
  
# Saving the converted audio in a mp3 file named 
# welcome  
myobj.save("welcome.mp3") 
  
# Playing the converted file 
os.system("mpg321 welcome.mp3") 
8
#pip3 install pyttsx3
#apt-get install alsa-utils
import pyttsx3, time 
engine = pyttsx3.init() 
engine.say("Hi, I am text to speach") 
engine.runAndWait()
1
  import speech_recognition as sr


        def main():

            r = sr.Recognizer()

            with sr.Microphone() as source:
                r.adjust_for_ambient_noise(source)

                audio = r.listen(source)

                try:

                    print(r.recognize_google(audio))

                except Exception as e:
                    print("Error :  " + str(e))


                with open("recorded.wav", "wb") as f:
                    f.write(audio.get_wav_data())


        if __name__ == "__main__":
            main()
1

New to Communities?

Join the community