如何在 Google Colab 中进行文本到语音转换?

JCh*_*hat 5 python text-to-speech google-cloud-platform google-colaboratory gtts

我知道像 Google Text to Speech 这样的库。然而,这在 Colab 中却不起作用。我最近在 Colab https://colab.research.google.com/github/tugstugi/pytorch-dc-tts/blob/master/notebooks/EnglishTTS.ipynb#scrollTo=jLU2p4Gq_12d中遇到了一个复杂的笔记本,我们可以在其中转换文本到演讲。但是,是否有一种简单的方法可以使用 Google Text to Speech 或 Google Colab 中的其他库?

这样我就提供了一个 String-"My name is XYZ"并在 Colab 笔记本中读出它。(这发生在我提供的链接中,但相当复杂)。

PS 如果可能的话,我希望音频能够自动播放,就像 GTTS 那样。在此笔记本中,我们需要单击“播放”按钮来输出语音。

JCh*_*hat 9

我终于解决了这个问题。一个简单的方法是结合使用 Google Text to Speech 和 IPython 的 Audio 方法。下面的代码片段只需几行即可为您完成这项工作!您还可以查看我在此处创建的 Colab 笔记本https://colab.research.google.com/drive/1wMg9ZV2WH2ugAC-6iZLUkEH3V6XxI3H - 演示这一点。

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.
Run Code Online (Sandbox Code Playgroud)