-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaisyMaker.py
122 lines (107 loc) · 4.05 KB
/
daisyMaker.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Copyright (c)2022 Hiroki Fujii,ACT laboratory All rights reserved.
import os
import shutil
import threading
import time
import traceback
import constants
import daisyBuilder
import utils
from comtypes import CoInitialize
from pydub import AudioSegment
from logging import getLogger
from errors import inputError, outputError
class daisyMaker(threading.Thread):
def __init__(self, inputFile, outputDir, parser, voice):
threading.Thread.__init__(self)
self.inputFile = inputFile
self.outputDir = outputDir
self.parser = parser
self.voice = voice
self.total = 0
self.count = 0
self.finished = False
self.exited = False
self.canceled = False
self.error = None
self.log=getLogger("%s.%s" % (constants.LOG_PREFIX, "daisyMaker"))
def cancel(self):
self.canceled = True
self.exited = True
def exit(self):
self.exited = True
def run(self):
CoInitialize()
try: index, meta = self.parser.parse(self.inputFile, phrase=True)
except Exception as e:
self.log.error(traceback.format_exc())
self.error = inputError(str(e))
return
outputDir = utils.addDirNameSuffix(os.path.join(self.outputDir, utils.makeFileName(meta["title"], "_")))
for i in index:
self.total += len(i["texts"])
_counter = 1
_outputCounter = 1
try:
os.makedirs(outputDir)
os.makedirs(utils.getTempDir(), exist_ok=True)
except Exception as e:
self.log.error(traceback.format_exc())
self.error = outputError(str(e))
return
for i in index:
if self.canceled: return
audioTmps = []
i["beginSeconds"] = []
i["endSeconds"] = []
i["durationSecond"] = 0.0
i["audioFile"] = None
audioOutput = None
for t in i["texts"]:
if self.canceled: return
fileName = os.path.join(utils.getTempDir(), "%08d.wav" %(_counter,))
try:
result = self.voice.generateWave(t, fileName)
except Exception as e:
self.log.error(traceback.format_exc())
self.error = e
return
audioTmps.append(fileName)
_counter += 1
self.count += 1
time.sleep(0.001)
for f in audioTmps:
if self.canceled: return
try:
audioTmp = (AudioSegment.from_file(f, "wav")) + (AudioSegment.silent(duration=500))
except Exception as e:
self.log.error(traceback.format_exc())
self.error = outputError(str(e))
return
if audioOutput == None:
i["beginSeconds"].append(0.0)
audioOutput = audioTmp
else:
i["beginSeconds"].append(audioOutput.duration_seconds)
audioOutput = audioOutput + audioTmp
i["endSeconds"].append(audioOutput.duration_seconds)
if audioOutput != None:
outputFile = os.path.join(outputDir, "audio%08d.mp3" %(_outputCounter,))
try: audioOutput.export(outputFile, format="mp3")
except Exception as e:
self.log.error(traceback.format_exc())
self.error = outputError(str(e))
return
i["audioFile"] = outputFile
i["durationSecond"] = i["endSeconds"][-1]
_outputCounter += 1
try:
shutil.rmtree(utils.getTempDir())
os.makedirs(utils.getTempDir())
except Exception as e:
self.log.error(traceback.format_exc())
self.error = outputError(str(e))
return
builder = daisyBuilder.DaisyBuilder()
builder.build(index, meta, outputDir)
self.finished = True