-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsay.py
More file actions
52 lines (47 loc) · 2.49 KB
/
say.py
File metadata and controls
52 lines (47 loc) · 2.49 KB
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
import argparse
import os
import subprocess
import tempfile
RUN_DIR = '/run/user/%d' % os.getuid()
def say(text, lang='en-US', volume=60, pitch=130, speed=100, device='default', engine='pico'):
"""
Speaks the provided text.
Args:
text: The text you want to speak.
lang: The language to use. Supported languages are:
en-US, en-GB, de-DE, es-ES, fr-FR, it-IT.
volume: Volume level for the converted audio. The normal volume level is
100. Valid volume levels are between 0 (no audible output) and 500 (increasing the
volume by a factor of 5). Values higher than 100 might result in degraded signal
quality due to saturation effects (clipping) and is not recommended. To instead adjust
the volume output of your device, enter ``alsamixer`` at the command line.
pitch: The pitch level for the voice. The normal pitch level is 100, the allowed values lie
between 50 (one octave lower) and 200 (one octave higher).
speed: The speed of the voice. The normal speed level is 100, the allowed values lie
between 20 (slowing down by a factor of 5) and 500 (speeding up by a factor of 5).
device: The PCM device name. Leave as ``default`` to use the default ALSA soundcard.
"""
data = "<volume level='%d'><pitch level='%d'><speed level='%d'>%s</speed></pitch></volume>" % \
(volume, pitch, speed, text)
if engine == 'gtts':
cmd = 'mpg123 -q "http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=' + text + '&tl=' + lang + '"'
subprocess.check_call(cmd, shell=True)
else:
with tempfile.NamedTemporaryFile(suffix='.wav', dir=RUN_DIR) as f:
cmd = 'pico2wave --wave %s --lang %s "%s" && aplay -q -D %s %s' % \
(f.name, lang, data, device, f.name)
subprocess.check_call(cmd, shell=True)
def _main():
parser = argparse.ArgumentParser(description='TTS')
parser.add_argument('--lang', default='en-US')
parser.add_argument('--volume', type=int, default=60)
parser.add_argument('--pitch', type=int, default=130)
parser.add_argument('--speed', type=int, default=100)
parser.add_argument('--device', default='default')
parser.add_argument('--engine', default='pico')
parser.add_argument('text', help='path to disk image file ')
args = parser.parse_args()
say(args.text, lang=args.lang, volume=args.volume, pitch=args.pitch, speed=args.speed,
device=args.device, engine=args.engine)
if __name__ == '__main__':
_main()