-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextToSpeech.py
66 lines (52 loc) · 1.58 KB
/
TextToSpeech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import re
import wave
import pyaudio
import _thread
import time
from pydub import AudioSegment
class TextToSpeech:
CHUNK = 1024
def __init__(self, words_pron_dict:str = 'cmudict-0.7b.txt'):
self._l = {}
self._load_words(words_pron_dict)
def _load_words(self, words_pron_dict:str):
print("Loading dictionary...")
with open(words_pron_dict, 'r') as file:
for line in file:
if not line.startswith(';;;'):
key, val = line.split(' ',2)
self._l[key] = re.findall(r"[A-Z]+",val)
def get_pronunciation(self, str_input):
list_pron = []
for word in re.findall(r"[\w']+",str_input.upper()):
if word in self._l:
list_pron += self._l[word]
else:
list_pron += ["umm"]
list_pron += ['0']
print(list_pron)
output_sound = AudioSegment.from_wav("sounds/0.wav")
for pron in list_pron:
output_sound += AudioSegment.from_wav("sounds/"+pron+".wav")
output_sound.export("output.wav", format="wav")
try:
wf = wave.open("output.wav", 'rb')
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(TextToSpeech.CHUNK)
while data:
stream.write(data)
data = wf.readframes(TextToSpeech.CHUNK)
stream.stop_stream()
stream.close()
p.terminate()
except:
pass
if __name__ == '__main__':
tts = TextToSpeech()
tts.get_pronunciation('Enter a word or phrase')
while True:
tts.get_pronunciation(input('Enter a word or phrase: '))