How to do text to speech conversion in Google Colab?
Asked Answered
D

2

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.

Deferral answered 19/8, 2019 at 19:47 Comment(0)
D
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.
Deferral answered 19/8, 2019 at 22:10 Comment(2)
but this are 2 different things here your first link is to a custom trained voice and not google ttsCambyses
@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.Deferral
K
0

The last line: display(Audio(sound_file, autoplay=True))

Karen answered 30/10, 2023 at 14:33 Comment(1)
@Deferral could you confirm that the last line should be display(Audio(sound_file, autoplay=True)) as @Karen are suggesting? If so please incorporate it in your answer.Shaddock

© 2022 - 2024 — McMap. All rights reserved.