forked from CollectivaT-dev/catotron-cpu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharticle.py
50 lines (42 loc) · 1.47 KB
/
article.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
import os
import sys
import subprocess
from synthesizer import Synthesizer
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
synthesizer = Synthesizer()
t_model_path = os.path.join(PROJECT_PATH, 'models/upc_pau_tacotron2.pt')
v_model_path = os.path.join(PROJECT_PATH, 'models/melgan_onapau_catotron.pt')
synthesizer.load(t_model_path, v_model_path)
def main(path):
files = os.listdir(path)
files.sort()
p_wavs = []
for f in files:
if f.startswith('para') and f.endswith('.txt'):
p_file = synthesize_paragraph(os.path.join(path,f))
p_wavs.append(p_file)
p_wavs.append('silence_short.wav')
args = ['sox'] + p_wavs + [os.path.join(path, 'article_full.wav')]
popen = subprocess.Popen(args)
popen.wait()
def synthesize_paragraph(f):
sentences = []
print(f)
for i, line in enumerate(open(f).readlines()):
print(line)
outfile = f.replace('.txt','_s%i.wav'%i)
sentences.append(outfile)
sentences.append('silence_short.wav')
if not os.path.isfile(outfile):
audio = synthesizer.synthesize(line)
with open(outfile, 'wb') as out:
out.write(audio)
paragraph_file = f.replace('.txt','.wav')
if not os.path.isfile(paragraph_file):
args = ['sox'] + sentences + [paragraph_file]
popen = subprocess.Popen(args)
popen.wait()
return paragraph_file
if __name__ == "__main__":
path = sys.argv[1]
main(path)