Text-to-speech is, in general, a non-trivial task. There are ready-made implementations that can be divided into two groups:
- Online services that implement this feature, such as Google.Translator or Yandex.Translate
- Local programs for different OS
There are many online services and many local programs. Wrappers are written to some of them - python modules that work with api services / programs. For example, here is an incomplete list of python-modules that convert text to speech using Google.Translator.
The most simple and convenient of them is gTTS , consider an example program on it:
from gtts import gTTS import pygame # ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΡΠ΅ΠΌ gTTS, ΠΎΠ΄Π½ΠΈΠΌ ΠΈΠ· ΡΠΏΠΎΡΠΎΠ±ΠΎΠ²: tts = gTTS(text='ΠΡΠΈΠ²Π΅Ρ, ΠΌΠΈΡ', lang='ru') # ΡΡΡΡΠΊΠΈΠΉ ΡΠ΅ΠΊΡΡ tts = gTTS(text='Hello world', lang='en') # Π°Π½Π³Π»ΠΈΠΉΡΠΊΠΈΠΉ ΡΠ΅ΠΊΡΡ tts = gTTS(text='Hello world', lang='en', slow=True) # Π°Π½Π³Π»ΠΈΠΉΡΠΊΠΈΠΉ ΡΠ΅ΠΊΡΡ, ΠΌΠ΅Π΄Π»Π΅Π½Π½ΠΎΠ΅ ΠΏΡΠΎΠΈΠ·Π½ΠΎΡΠ΅Π½ΠΈΠ΅ # ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΡΠ΅ΠΌ ΡΠ΅ΠΊΡΡ Π² ΡΠ΅ΡΡ, ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡ google-ΠΏΠ΅ΡΠ΅Π²ΠΎΠ΄ΡΠΈΠΊ # ΠΈ ΡΠΎΡ
ΡΠ°Π½ΡΠ΅ΠΌ ΠΏΠΎΠ»ΡΡΠΈΠ²ΡΠ΅ΡΡΡ ΡΠ΅ΡΡ Π² mp3 ΡΠ°ΠΉΠ» tts.save('text.mp3') # ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΡ pygame pygame.mixer.init() pygame.init() # Π·Π°Π³ΡΡΠΆΠ°Π΅ΠΌ ΡΠ΅ΡΡ ΠΈΠ· mp3 ΡΠ°ΠΉΠ»Π° pygame.mixer.music.load('text.mp3') pygame.mixer.music.play() # music.play() β Π½Π΅Π±Π»ΠΎΠΊΠΈΡΡΡΡΠΈΠΉ ΠΌΠ΅ΡΠΎΠ΄ # ΠΊΠΎΠ΄ Π½ΠΈΠΆΠ΅ Π±ΡΠ΄Π΅Ρ ΠΆΠ΄Π°ΡΡ, ΠΏΠΎΠΊΠ° ΡΠ΅ΡΡ Π·Π°ΠΊΠΎΠ½ΡΠΈΡ ΠΏΡΠΎΠΈΠ·Π½ΠΎΡΠΈΡΡΡ pygame.mixer.music.set_endevent(pygame.USEREVENT) pygame.event.wait()
This program gets speech using a google-translator, saves it to an mp3 file, then pygame plays this mp3 file. You can do without mp3 file using in-memory binary stream ( io.BytesIO ):
from gtts import gTTS import pygame import io tts = gTTS(text='ΠΡΠΈΠ²Π΅Ρ, ΠΌΠΈΡ', lang='ru') with io.BytesIO() as f: tts.write_to_fp(f) f.seek(0) pygame.mixer.init() pygame.init() pygame.mixer.music.load(f) pygame.mixer.music.play() pygame.mixer.music.set_endevent(pygame.USEREVENT) pygame.event.wait()
ΠΊΠ°ΠΊ ΡΠ΄Π΅Π»Π°ΡΡ ΡΡΠΎΠ±Ρ ΠΎΠ½ ΡΠΈΡΠ°Π» (ΠΏΡΠΎΠΈΠ·Π½ΠΎΡΠΈΠ») ΠΊΠ°ΠΆΠ΄ΡΡ Π±ΡΠΊΠ²Ρ ΠΈΠ· ΡΠΏΠΈΡΠΊΠ°- do we need each letter separately, or all the same to read the words? - diraria