5

I am aware of libraries like Google Text to Speech. However, the same does not work in Colab. I recently came across a complex notebook in Colab https://colab.research.google.com/github/tugstugi/pytorch-dc-tts/blob/master/notebooks/EnglishTTS.ipynb#scrollTo=jLU2p4Gq_12d in which we can convert text to speech. But, is there a simple way of using the Google Text to Speech or other library in Google Colab?

Such that I provide a String- "My name is XYZ" and it is spoken out in the Colab notebook. (This happens in the link I provided, but is quite complex).

P.S. I would like the audio to be played automatically if possible, like GTTS does. In this notebook, we need to click the Play button for the speech output.

JChat
  • 784
  • 2
  • 13
  • 33

1 Answers1

9

I finally sorted this out. A simple way is to use Google Text to Speech in conjunction with IPython's Audio method. The following code snippet can do the job for you in a few lines! You can also check out the Colab notebook I created here https://colab.research.google.com/drive/1wMg9ZV2WH2ugAC-6iZLUkEH3V6XxI3H- demonstrating this.

from gtts import gTTS #Import Google Text to Speech
from IPython.display import Audio #Import Audio method from IPython's Display Class
tts = gTTS('hello joyjit') #Provide the string to convert to speech
tts.save('1.wav') #save the string converted to speech as a .wav file
sound_file = '1.wav'
Audio(sound_file, autoplay=True) 

#Autoplay = True will play the sound automatically
#If you would not like to play the sound automatically, simply pass Autoplay = False.
JChat
  • 784
  • 2
  • 13
  • 33
  • but this are 2 different things here your first link is to a custom trained voice and not google tts – user3548161 Jan 09 '21 at 11:31
  • @user3548161-I am not sure with what you mean- This question (and answer) address custom speech based on custom text provided by the user in Colab. It uses the gtts for this purpose by importing the relevant methods for generation an audio (speech) file given custom text by the user. Hope this addresses your concern. – JChat Jan 09 '21 at 18:04