-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolly.py
96 lines (90 loc) · 1.65 KB
/
polly.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
"""Getting Started Example for Python 2.7+/3.3+"""
from boto3 import Session
from botocore.exceptions import BotoCoreError, ClientError
from contextlib import closing
import os
import sys
import subprocess
from tempfile import gettempdir
session = Session(profile_name="adminuser")
polly = session.client("polly")
phonemes = [
"b",
"d",
"f",
"ɣ",
"ɦ",
"j",
"k",
"l",
"m",
"n",
"ŋ",
"p",
"r",
"s",
"t",
"v",
"ʋ",
"x",
"z",
"t͡ɕ",
"ɡ",
"d͡ʑ",
"ɱ",
"ɲ",
"ɕ",
"ʑ",
"ʔ",
"ɑ",
"ɛ",
"ɪ",
"ɔ",
"ʏ",
"aː",
"eː",
"ə",
"i",
"oː",
"y",
"øː",
"u",
"ɑi",
"aːi",
"ʌu",
"ɛi",
"eːu",
"iu",
"ɔi",
"oːi",
"œy",
"ui",
"yu",
"ɛː",
"iː",
"ɔː",
"œː",
"uː",
"yː",
"ɑ̃ː",
"ɛ̃ː",
"ɔ̃ː"
]
for phoneme in phonemes:
try:
response = polly.synthesize_speech(TextType="ssml", Text=f'<speak><phoneme alphabet="ipa" ph="{phoneme}"></phoneme></speak>', OutputFormat="mp3", VoiceId="Lotte")
except (BotoCoreError, ClientError) as error:
print(error)
sys.exit(-1)
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
output = os.path.join("outputs", f'{phoneme}.mp3')
try:
with open(output, "wb") as file:
file.write(stream.read())
except IOError as error:
print(error)
sys.exit(-1)
else:
print("Could not stream audio")
sys.exit(-1)