Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add offline tts using pico #5005

Merged
merged 9 commits into from
Jan 4, 2017
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions homeassistant/components/tts/picotts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Support for the picotts speech service.
For more information see...
"""
import os
import tempfile
import shutil
import subprocess
import logging
import voluptuous as vol

from homeassistant.components.tts import Provider, PLATFORM_SCHEMA, CONF_LANG

_LOGGER = logging.getLogger(__name__)

SUPPORT_LANGUAGES = ['en-US', 'en-GB', 'de-DE', 'es-ES', 'fr-FR', 'it-IT']

DEFAULT_LANG = 'en-US'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_LANG, default=DEFAULT_LANG): vol.In(SUPPORT_LANGUAGES),
})


def get_engine(hass, config):
"""Setup pico speech component."""
if shutil.which("pico2wave") is None:
_LOGGER.error("'pico2wave' was not found")
return False
return PicoProvider()


class PicoProvider(Provider):
"""pico speech api provider."""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

def get_tts_audio(self, message):
"""Load TTS."""
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
fname = f.name
cmd = ['pico2wave', '--wave', fname, '-l', self.language, message]
subprocess.call(cmd)
data = None
try:
with open(fname, 'rb') as voice:
data = voice.read()
except OSError:
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to return (None, None) to indicate nothing got returned.

finally:
os.remove(fname)
if data:
return ("wav", data)