Permalink
Browse files

Create a v1 speech message format

  • Loading branch information...
osteele committed Feb 10, 2018
1 parent 16c53d2 commit 23c883ca3d77b39c1ba4e5aa06d94e5cca93b011
Showing with 37 additions and 1 deletion.
  1. +16 −1 scripts/tts_worker.py
  2. +21 −0 tests/tts_worker_test.py
@@ -18,6 +18,10 @@
logger = logging.getLogger('speaker')
logger.setLevel(logging.INFO)

# The current speech message version. Messages in this format are dictionaries
# `dict(version='1', text='Hello world')`
SPEECH_MESSAGE_VERSION = '1'

# The command-line program to use for speech synthesis.
DEFAULT_SPEECH_COMMAND = 'say' if platform.system() == 'Darwin' else 'espeak'
SPEECH_COMMAND = os.getenv('BEAR_SPEECH_COMMAND', DEFAULT_SPEECH_COMMAND)
@@ -26,15 +30,26 @@


def process_speech_message(message):
message = upgrade_speech_message(message)
logger.info(message)
message_text = message['message']
message_text = message['text']
res = subprocess.run([SPEECH_COMMAND, message_text],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if res.returncode != 0:
logger.error(res.stderr.decode().strip())


def upgrade_speech_message(message):
"""Return a current-version speech message."""
version = message.get('version')
if not version:
text = message['message']
return dict(version=SPEECH_MESSAGE_VERSION, text=text)
assert version == SPEECH_MESSAGE_VERSION
return message


@click.command()
@click.option('--topic', default='speak')
def main(topic):
@@ -10,8 +10,29 @@

@patch('subprocess.run')
def test_process_speech_message(run):
# Test an old-style message.
#
# Eventually we'll remove this test since `test_upgrade_speech_message`
# should handle this, but we'll use belt-and-suspenders to avoid breaking
# clients at first.
with patch('tts_worker.SPEECH_COMMAND', new='speech-command'):
tts_worker.process_speech_message(dict(message='Hello world'))
run.assert_called_with(['speech-command', 'Hello world'],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)

# Test a new-style message.
with patch('tts_worker.SPEECH_COMMAND', new='speech-command'):
tts_worker.process_speech_message(dict(message='Hello world'))
run.assert_called_with(['speech-command', 'Hello world'],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)


def test_upgrade_speech_message():
v0_message = dict(message='Hello world')
v1_message = dict(version='1', text='Hello world')
assert tts_worker.upgrade_speech_message(v0_message) == v1_message, \
"upgrades old-style messages"
assert tts_worker.upgrade_speech_message(v1_message) == v1_message, \
"preserves new-style messages"

0 comments on commit 23c883c

Please sign in to comment.