-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_audio.py
80 lines (66 loc) · 2.46 KB
/
generate_audio.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
import os, sys, getopt
import utils
import constant
def printHelp() :
print('generate_audio.py -t <tts name> -o <output audio dir> -l <lower bound> -u <upper bound>')
print("or")
print('generate_audio.py --tts <tts name> --output-dir <output audio dir> --lower-bound <lower bound> --upper-bound <upper bound>')
def main(argv):
tts = ""
output_dir = ""
lower_bound = 0
upper_bound = 20000
try:
opts, args = getopt.getopt(argv,"ht:o:l:u:",["tts=", "output-dir=", "lower-bound=", "upper-bound="])
except getopt.GetoptError:
printHelp()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
printHelp()
sys.exit()
elif opt in ("-t", "--tts"):
tts = arg
elif opt in ("-o", "--output-dir"):
output_dir = arg
elif opt in ("-l", "--lower-bound"):
lower_bound = int(arg)
elif opt in ("-u", "--upper-bound"):
upper_bound = int(arg)
if upper_bound < lower_bound :
print("Lower bound must be less than upper bound")
sys.exit()
if tts != "" :
if output_dir == "" :
print("Please specify the output folder location")
elif output_dir[-1] != "/" :
print("Please put backslash (/) in the end of the folder")
else :
generateAudios(tts, output_dir, lower_bound, upper_bound)
else :
print("Please specify the used TTS")
def generateAudios(tts, output_dir, lower_bound=None, upper_bound=None) :
output_dir = output_dir + tts + "/"
if not os.path.exists(output_dir) :
os.makedirs(output_dir)
file = open(constant.CORPUS_FPATH)
corpus = file.readlines()
file.close()
if lower_bound < 0 :
print("Lower bound is less than zero")
sys.exit()
if lower_bound > len(corpus) :
print("Lower bound is greater than the size of corpus")
sys.exit()
if upper_bound > len(corpus) :
print("Upper bound is greater than the size of corpus")
sys.exit()
for i in range(lower_bound, upper_bound) :
text = corpus[i][:-1] # remove new line in the last sentence
fpath = output_dir + "audio-%d.wav" % (i + 1)
if not os.path.exists(fpath) :
print("Processing text: %d" % (i + 1))
utils.synthesizeSpeech(tts, text, fpath)
# print(text)
if __name__ == "__main__":
main(sys.argv[1:])