Search code examples
pythonaudiotext-to-speech

Google Cloud Text-to-Speech Audio to Browser


I use google-cloud text-to-speech API using python3 on linux.

The mp3 plays with os.startfile(), which opens a player.

Instead sending mp3 file & using mp3 player, I need to play audio through the browser.

I have tried:

  1. “Python Media Player” —-defunct.
  2. “20.1. webbrowser”— defunct.
  3. “Rhythmbox” —simply another player.
  4. “Pygame” — overkill.
  5. "Pyglet" - overkill
  6. SoX and pySoX, —Don’t appear to play the files they manipulate.
  7. I read part of Schwoebel’s “An Introduction to Voice Computing in Python.”
  8. "gl_talk" — Wasn't able to implement; very little documentation.

I need advice on how to mimic what google has done in the browser as in the link here:

https://cloud.google.com/text-to-speech


Solution

  • You can use the gTTS module to do text-to-speech in Python. And then you can use another module to play the sound using Python as well.

    First install the following modules...

    pip install gTTS
    
    pip install playsound
    

    Then you can do this...

    from gtts import gTTS
    import playsound
    tts = gTTS('hello')
    tts.save('hello.mp3')
    
    playsound.playsound('hello.mp3')
    

    This code should give you the same results as google in browser, because gtts uses the same API. If you have any problems you can comment and I will definitely reply :)