pythongoogle-cloud-platformtext-to-speechgoogle-colaboratorygtts

How to do text to speech conversion in Google Colab?


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.


Solution

  • 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.