This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
forked from mideind/GreynirServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeak.py
executable file
·269 lines (215 loc) · 7.37 KB
/
speak.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python
"""
Greynir: Natural language processing for Icelandic
Copyright (C) 2023 Miðeind ehf.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
Friendly command line interface for Icelandic speech synthesis.
Returns 0 on success, 1 on error.
Run the following command for a list of options:
./speak.py --help
"""
from typing import Optional, cast, List
import sys
import subprocess
import logging
from pathlib import Path
from shutil import which
from urllib.request import urlopen
import wave
import requests
from speech import (
text_to_audio_url,
DEFAULT_VOICE,
SUPPORTED_VOICES,
DEFAULT_TEXT_FORMAT,
DEFAULT_AUDIO_FORMAT,
SUPPORTED_AUDIO_FORMATS,
SUPPORTED_TEXT_FORMATS,
)
from speech.voices import suffix_for_audiofmt
from utility import sanitize_filename
def _die(msg: str, exit_code: int = 1) -> None:
print(msg, file=sys.stderr)
sys.exit(exit_code)
_DATA_URI_PREFIX = "data:"
def _is_data_uri(s: str) -> bool:
"""Returns whether a URL is a data URI (RFC2397). Tolerates upper/mixed case prefix."""
return s[: len(_DATA_URI_PREFIX)].lower() == _DATA_URI_PREFIX
def _is_file_uri(s: str) -> bool:
"""Returns whether a URL is a file URI (RFC8089)."""
return s.startswith("file://")
def _bytes4file_or_data_uri(uri: str) -> bytes:
"""Returns bytes of file at file URI (RFC8089) or in data URI (RFC2397)."""
with urlopen(uri) as response:
return response.read()
def _fetch_audio_bytes(url: str) -> Optional[bytes]:
"""Returns bytes of audio file at URL."""
if _is_data_uri(url) or _is_file_uri(url):
return _bytes4file_or_data_uri(url)
try:
r = requests.get(url, timeout=10)
if r.status_code != 200:
raise Exception(
f"Received HTTP status code {r.status_code} when fetching {url}"
)
return r.content
except Exception as e:
logging.error(f"Error fetching audio file: {e}")
def _write_wav(
fn: str, data: bytes, num_channels=1, sample_width=2, sample_rate=16000
) -> None:
"""Write audio data to WAV file. Defaults to 16-bit mono 16 kHz PCM."""
with wave.open(fn, "wb") as wav:
wav.setnchannels(num_channels)
wav.setsampwidth(sample_width)
wav.setframerate(sample_rate)
wav.writeframes(data)
def _play_audio_file(path: str) -> None:
"""Play audio file at path via command line player. This only works
on systems with afplay (macOS), mpv, mpg123 or cmdmp3 installed."""
AFPLAY = "/usr/bin/afplay" # afplay is only present on macOS systems
MPV = which("mpv") # mpv is a cross-platform player
MPG123 = which("mpg123") # mpg123 is a cross-platform player
CMDMP3 = which("cmdmp3") # cmdmp3 is a Windows command line mp3 player
cmd: Optional[List[str]] = None
if Path(AFPLAY).is_file():
cmd = [AFPLAY, path]
elif MPV:
cmd = [MPV, path]
elif MPG123:
cmd = [MPG123, "--quiet", path]
elif CMDMP3:
cmd = [CMDMP3, path]
if not cmd:
_die("Couldn't find suitable command line audio player.")
print(f"Playing file '{path}'")
subprocess.run(cast(List[str], cmd))
DEFAULT_TEXT = ["Góðan daginn og til hamingju með lífið."]
def main() -> None:
"""Main program function."""
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument(
"-v",
"--voice",
help="specify which voice to use",
default=DEFAULT_VOICE,
choices=list(SUPPORTED_VOICES),
)
parser.add_argument(
"-l",
"--list-voices",
help="print list of supported voices",
action="store_true",
)
parser.add_argument(
"-f",
"--audioformat",
help="select audio format",
default=DEFAULT_AUDIO_FORMAT,
choices=list(SUPPORTED_AUDIO_FORMATS),
)
parser.add_argument(
"-s",
"--speed",
help="set speech speed",
default=1.0,
type=float,
)
parser.add_argument(
"-t",
"--textformat",
help="set text format",
default=DEFAULT_TEXT_FORMAT,
choices=list(SUPPORTED_TEXT_FORMATS),
)
parser.add_argument(
"-o",
"--override",
help="override default audio output filename",
default="", # Empty string means use default filename
)
parser.add_argument(
"-w", "--wav", help="generate WAV file from PCM", action="store_true"
)
parser.add_argument(
"-u", "--url", help="dump audio URL to stdout", action="store_true"
)
parser.add_argument(
"-n", "--noplay", help="do not play resulting audio file", action="store_true"
)
parser.add_argument(
"-r", "--remove", help="remove audio file after playing", action="store_true"
)
parser.add_argument(
"text",
help="text to synthesize",
nargs="*",
default=DEFAULT_TEXT,
)
args = parser.parse_args()
if args.list_voices:
for voice in SUPPORTED_VOICES:
print(voice)
sys.exit(0)
if len(args.text) == 0:
_die("No text provided.")
text = " ".join(args.text).strip()
if not text:
_die("No text provided.")
if args.wav and args.audioformat != "pcm":
_die("WAV output flag only supported for PCM format.")
# Synthesize the text according to CLI options
url = text_to_audio_url(
text,
text_format=args.textformat,
audio_format=args.audioformat,
voice_id=args.voice,
speed=args.speed,
)
if not url:
_die("Error generating speech synthesis URL.")
# Command line flag specifies that we should just dump the URL to stdout
if args.url:
print(url)
sys.exit(0)
# Download
urldesc = f"data URI ({len(url)} bytes)" if _is_data_uri(url) else url
print(f"Fetching {urldesc}")
data: Optional[bytes] = _fetch_audio_bytes(url)
if not data:
_die("Unable to fetch audio data.")
assert data is not None # Silence typing complaints
if args.override:
# Override default filename
fn = args.override
else:
# Generate file name
fn = sanitize_filename(text)
fn = f"{fn}.{suffix_for_audiofmt(args.audioformat)}"
# Write audio data to file
print(f'Writing to file "{fn}".')
if args.wav:
_write_wav(fn, data)
else:
with open(fn, "wb") as f:
f.write(data)
# Play audio file using command line tool (if available)
if not args.noplay:
_play_audio_file(fn)
# Remove file after playing
if args.remove:
print(f'Deleting file "{fn}".')
Path(fn).unlink()
if __name__ == "__main__":
"""Perform speech synthesis of Icelandic text via the command line."""
main()