diff --git a/src/aiy/_drivers/_tts.py b/src/aiy/_drivers/_tts.py
index a941b479..c223479b 100644
--- a/src/aiy/_drivers/_tts.py
+++ b/src/aiy/_drivers/_tts.py
@@ -33,13 +33,15 @@ def create_say(player):
return functools.partial(say, player, lang=lang)
-def say(player, words, lang='en-US'):
+def say(player, words, lang='en-US', volume=60, pitch=130):
"""Say the given words with TTS.
Args:
player: To play the text-to-speech audio.
words: string to say aloud.
lang: language for the text-to-speech engine.
+ volume: volume for the text-to-speech engine.
+ pitch: pitch for the text-to-speech engine.
"""
try:
(fd, tts_wav) = tempfile.mkstemp(suffix='.wav', dir=TMP_DIR)
@@ -47,7 +49,8 @@ def say(player, words, lang='en-US'):
logger.exception('Using fallback directory for TTS output')
(fd, tts_wav) = tempfile.mkstemp(suffix='.wav')
os.close(fd)
- words = '%s' % words
+ words = '' + words + ''
try:
subprocess.call(['pico2wave', '--lang', lang, '-w', tts_wav, words])
player.play_wav(tts_wav)
diff --git a/src/aiy/audio.py b/src/aiy/audio.py
index 7711acc7..7f766bbd 100644
--- a/src/aiy/audio.py
+++ b/src/aiy/audio.py
@@ -28,6 +28,8 @@
_voicehat_recorder = None
_voicehat_player = None
_status_ui = None
+_tts_volume = 60
+_tts_pitch = 130
class _WaveDump(object):
@@ -108,15 +110,24 @@ def play_audio(audio_data):
player.play_bytes(audio_data, sample_width=AUDIO_SAMPLE_SIZE, sample_rate=AUDIO_SAMPLE_RATE_HZ)
-def say(words, lang=None):
+def say(words, lang=None, volume=None, pitch=None):
"""Says the given words in the given language with Google TTS engine.
- If lang is specified, e.g. "en-US', it will be used to say the given words.
+ If lang is specified, e.g. "en-US", it will be used to say the given words.
Otherwise, the language from aiy.i18n will be used.
+ volume (optional) volume used to say the given words.
+ pitch (optional) pitch to say the given words.
+ Example: aiy.audio.say('This is an example', lang="en-US", volume=75, pitch=135)
+ Any of the optional variables can be left out.
"""
+
if not lang:
lang = aiy.i18n.get_language_code()
- aiy._drivers._tts.say(aiy.audio.get_player(), words, lang=lang)
+ if not volume:
+ volume = aiy.audio.get_tts_volume()
+ if not pitch:
+ pitch = aiy.audio.get_tts_pitch()
+ aiy._drivers._tts.say(aiy.audio.get_player(), words, lang=lang, volume=volume, pitch=pitch)
def get_status_ui():
@@ -129,3 +140,23 @@ def get_status_ui():
if not _status_ui:
_status_ui = aiy._drivers._StatusUi()
return _status_ui
+
+
+def set_tts_volume(volume):
+ global _tts_volume
+ _tts_volume = volume
+
+
+def get_tts_volume():
+ global _tts_volume
+ return _tts_volume
+
+
+def set_tts_pitch(pitch):
+ global _tts_pitch
+ _tts_pitch = pitch
+
+
+def get_tts_pitch():
+ global _tts_pitch
+ return _tts_pitch