Skip to content

Commit

Permalink
Expose tts volume and pitch to the user code like we do with lang. (g…
Browse files Browse the repository at this point in the history
…oogle#185)

* Expose volume and pitch for tts so they can be changed in the user code.

* Corrected typo

* Corrected some python style errors.

* Volume and pitch removed from create_say(). Removed redundant global declaration

* Sorry for the confusion. Looked at the wrong line.
  • Loading branch information
divx118 authored and drigz committed Nov 27, 2017
1 parent 9a80eab commit e31c12d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/aiy/_drivers/_tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,24 @@ 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)
except IOError:
logger.exception('Using fallback directory for TTS output')
(fd, tts_wav) = tempfile.mkstemp(suffix='.wav')
os.close(fd)
words = '<volume level="60"><pitch level="130">%s</pitch></volume>' % words
words = '<volume level="' + str(volume) + '"><pitch level="' + str(pitch) + \
'">' + words + '</pitch></volume>'
try:
subprocess.call(['pico2wave', '--lang', lang, '-w', tts_wav, words])
player.play_wav(tts_wav)
Expand Down
37 changes: 34 additions & 3 deletions src/aiy/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
_voicehat_recorder = None
_voicehat_player = None
_status_ui = None
_tts_volume = 60
_tts_pitch = 130


class _WaveDump(object):
Expand Down Expand Up @@ -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():
Expand All @@ -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

0 comments on commit e31c12d

Please sign in to comment.