/
TextToSpeech.py
80 lines (67 loc) · 2.31 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from gtts import gTTS
import os, shutil
from pydub import AudioSegment
file1 = open('script.txt', 'r')
count = 0
paraCount = 0;
mytext = ""
language = 'en'
#initialize starting sound file
data = "Conversion of text to speech by group 2"
emptyWav = gTTS(text=data, lang=language, slow=False)
emptyWav.save("script.wav")
#add silence
start = AudioSegment.from_file("script.wav")
silence = AudioSegment.from_wav("3-second_Silence.wav")
paragraphs = []
folder = 'Paragraphs/'
#create directory
try:
os.mkdir(folder)
except OSError:
print ("Creation of the directory %s failed (could already exist)" % folder)
else:
print ("Successfully created the directory %s " % folder)
#delete files inside directory
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
print ("Reading the text file...")
#read and create individual speech files as paragraphs
while True:
count += 1
# Get next line from file
line = file1.readline()
# if line is empty
# end of file is reached
if not line:
paraCount += 1
myobj = gTTS(text=mytext, lang=language, slow=False)
myobj.save("Paragraphs/paragraph%d.wav" % (paraCount))
paragraphs.append(AudioSegment.from_file("Paragraphs/paragraph%d.wav" % (paraCount)))
paragraphs.append(silence)
mytext = ""
break
if line.strip():
mytext = mytext + " " + line.strip()
else:
paraCount += 1
myobj = gTTS(text=mytext, lang=language, slow=False)
myobj.save("Paragraphs/paragraph%d.wav" % (paraCount))
paragraphs.append(AudioSegment.from_file("Paragraphs/paragraph%d.wav" % (paraCount)))
paragraphs.append(silence)
mytext = ""
#concatenate all paragraphs as final output
combined_sounds = start + silence
for sounds in paragraphs:
combined_sounds = combined_sounds + sounds
combined_sounds.export("script.wav", format="wav")
file1.close()
print("Done! Opening audio file.")
os.system("script.wav")