-1

I already understand how to use gTTS, (Google's Text to Speech module), but I am not sure how to make everything it says into input audio! Basically, I want to make the program that says things through my mic, and anyone listening on the other end will be able to hear this!

CypherX
  • 7,019
  • 3
  • 25
  • 37
JetJadeja
  • 11
  • 1
  • 1

1 Answers1

4

Solution

You could use the following methods.

Method-1: Microsoft speech engine (Tested on Windows 10)

On Windows 10 platform, you could use the following alternative.

import win32com.client as wincl
speaker = wincl.Dispatch("SAPI.SpVoice")
speaker.Speak("Hello World")

Method-2: Use gtts + pygame (OS Independent Solution)

Here you could use gtts to save the text as an mp3 file and then load and play it using pygame.

Step-1: Saving Text as MP3 file

#pip install gTTS
from gtts import gTTS
tts = gTTS(text='Good morning', lang='en')
tts.save("good.mp3")

Step-2: Loading and Playing the MP3 file

#pip install pygame
from pygame import mixer

mixer.init()
mixer.music.load('good.mp3')
mixer.music.play()

Another option: Use VLC MediaPlayer to play the MP3 file.

#pip install python-vlc
import vlc
p = vlc.MediaPlayer("good.mp3")
p.play()

Method-3: Directly Text-to-Speech Playing from Buffer

No need to save as an mp3 file.

Source:

  1. gTTS direct output
  2. https://gtts.readthedocs.io/en/latest/module.html#playing-sound-directly
  3. https://gist.github.com/lamegaton/1c7f383d6434a9b02f5652ddbee258d9
from gtts import gTTS
from io import BytesIO

# Use gTTS to Store Speech on Buffer
tts = gTTS(text='Good morning', lang='en')
mp3 = BytesIO()
tts.write_to_fp(mp3)
mp3.seek(0)

# Play from Buffer
mixer.init()
mixer.music.load(mp3)
mixer.music.play()

References

  1. https://pythonprogramminglanguage.com/text-to-speech/
  2. https://pypi.org/project/gTTS/
  3. Playing mp3 song on python
CypherX
  • 7,019
  • 3
  • 25
  • 37
  • 1
    I use Mac, sorry for not specifying, is there anything else? – JetJadeja Oct 29 '19 at 20:00
  • 1
    Nice! Does this play through the microphone? So if I have friends listening through a voice chat like discord, perhaps, will it will play the noise and they will hear it. – JetJadeja Oct 30 '19 at 03:24
  • @jetlikesferraris Yes. I tried all of them and they do play through a microphone. Please consider **voting up** and **accepting** if you think this answer helped. For accepting this answer you will need to click on the “tick mark” on the left of the answer. And it will turn green. – CypherX Oct 30 '19 at 16:41