-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
57 lines (44 loc) · 1.24 KB
/
test.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
import pdftotext
import asyncio
# import nltk
# nltk.download('punkt')
from nltk.tokenize import sent_tokenize
from msspeech import MSSpeech
rep = {
'\n': ' ',
'fi': 'fi',
'ff': 'ff'
}
async def setup():
print("Fetching...")
voices = await mss.get_voices_list()
print("\nVoices:")
for voice in voices:
if voice["Locale"] == "en-US":
print(voice["FriendlyName"].split(" ")[1], end=' ')
print(voice["Gender"][0])
await mss.set_voice(voice["Name"])
await mss.set_rate(60)
await mss.set_pitch(0)
await mss.set_volume(1)
async def synth(p, n):
output = f"{n:03}.mp3"
await mss.synthesize(p.strip(), output)
print('.', end='', flush=True)
with open("cosmos.pdf", "rb") as pdf:
pages = pdftotext.PDF(pdf)
pRange = input(f"Pick reading range (1-{len(pages)}): ")
first, last = [int(x)-1 for x in pRange.split('-')]
mss = MSSpeech()
asyncio.run(setup())
print("\nSynthesizing", end='')
for pg in range(first, last):
sent = sent_tokenize(pages[pg])
def correct(s, rep):
return ''.join(rep.get(c, c) for c in s)
clean = [correct(s, rep) for s in sent]
# Combine sentences into groups of 5
paras = (' '.join(clean[s:s+5]) for s in range(0, len(clean), 5))
for n, p in enumerate(paras):
asyncio.run(synth(p, n+1))
print("\nDone.")