Speak service

What version of raspbian do you use ?
before, I have to copy the deb package from debian repository

for example for jessie
https://packages.debian.org/jessie/libttspico-utils

ok got this but i am running into sudo error:

Dec 15 12:45:07 Home-Assistant hass[682]:
Dec 15 12:46:22 Home-Assistant sudo[3113]: pam_unix(sudo:auth): conversation fai                                                                     led

dunno how to fix this
i used this code from zouden

now i get:

Dec 15 13:00:10 Home-Assistant pulseaudio[5081]: [pulseaudio] server-lookup.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
Dec 15 13:00:10 Home-Assistant pulseaudio[5081]: [pulseaudio] main.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
Dec 15 13:00:10 Home-Assistant hass[4141]: 16-12-15 13:00:10 ERROR (Thread-4) [custom_components.notify.speech] Error trying to say: hello

try speaker-test to be sure your audio is working

How to do this? i can play a sound with omx player . i have already an automation to play files but i cannot use the tts

from command line type “speaker-test”

speaker plays sound yes

from command line ,try :

pico2wave -l=en-US -w=test.wav “Hello”
and then
aplay test.wav

strange this works

so i should use YOUR code?

Are you using the same user ?

with hass user nothing to hear … with pi user i hear

I don’t know about you, but I couldn’t get it working at first until I realize the language code I set was wrong.

Initially I used language: en-EN. Then everything is fine after I changed it to language: en-US.

@doudz,

Thanks for sharing this amazing component. Now, my imagination is running wild.

  1. How do I change the voice to male?
  2. The voice sounds quite robotic to me. Do you know of any alternative which is more natural?
  3. Now, the speaker must be attached to the host that is running HA. How about other speakers in other rooms that are plug in to other RPi? I would love to send speech notifications to other location as well.

better version

“”"
Support for tts speech notification services.
“”"
import logging
import subprocess
import tempfile
import os
import voluptuous as vol
import shutil

CONF_LANGUAGE = ‘language’
from homeassistant.components.notify import (
BaseNotificationService, PLATFORM_SCHEMA)
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(name)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_LANGUAGE,default=‘en-US’): cv.string,
})

def get_service(hass, config):
“”“Get the tts notification service.”“”
if shutil.which(“pico2wave”) is None:
_LOGGER.error(“‘pico2wave’ was not found”)
return False
if shutil.which(“aplay”) is None:
_LOGGER.error(“‘aplay’ was not found”)
return False

language = config[CONF_LANGUAGE]
return TTSNotificationService(language)

class TTSNotificationService(BaseNotificationService):
“”“Implement the notification service for the TTS service.”“”

def __init__(self, language):
    """Initialize the service."""
    self.language = language or 'fr-FR'
def send_message(self, message="", **kwargs):
    """Say a message using tts"""
    try:
        say(message,self.language)
    except:
        _LOGGER.error('Error trying to say: %s', message)

def say(text,lang):
with tempfile.NamedTemporaryFile(suffix=‘.wav’, delete=False) as f:
fname = f.name
text = text.replace(‘"’,’ ‘)
text = ‘“{0}”’.format(text)
cmd = [‘pico2wave’, ‘–wave’, fname, ‘-l’, lang, text,’&&',‘aplay’,fname]
try:
subprocess.call(cmd)
except Exception as e:
_LOGGER.error(‘Error trying to say: %s’, e)
os.remove(fname)

there’s no many voices using picotts (this is the default android tts system)
an alternative could be using talkey which offers more tts system and so different voice quality
the best quality (for french) is using google TTS but it means requiring internet connection because it works online only

currently the speaker must be on the host running HA yes
to speak over an other Rpi you should better send the text message over the network and the final Rpi should have some kind of network speak service that simply say what it receive.
It’s better than sending the generated wav
Or we can imagine a remote Rpi having a streaming player and sending the sound to that server

1 Like

Hi, can you put the indented version please

My HA couldn’t start after changing to this code.

Can you please post the code with right indented?

oh my fault
this is the working version

“”"
Support for tts speech notification services.
“”"
import logging
import subprocess
import tempfile
import os
import voluptuous as vol
import shutil

CONF_LANGUAGE = ‘language’
from homeassistant.components.notify import (
BaseNotificationService, PLATFORM_SCHEMA)
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(name)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_LANGUAGE,default=‘en-US’): cv.string,
})

def get_service(hass, config):
“”“Get the tts notification service.”“”
if shutil.which(“pico2wave”) is None:
_LOGGER.error(“‘pico2wave’ was not found”)
return False
if shutil.which(“aplay”) is None:
_LOGGER.error(“‘aplay’ was not found”)
return False

language = config[CONF_LANGUAGE]
return TTSNotificationService(language)

class TTSNotificationService(BaseNotificationService):
“”“Implement the notification service for the TTS service.”“”

def __init__(self, language):
    """Initialize the service."""
    self.language = language or 'fr-FR'
def send_message(self, message="", **kwargs):
    """Say a message using tts"""
    try:
        say(message,self.language)
    except:
        _LOGGER.error('Error trying to say: %s', message)

def say(text,lang):
with tempfile.NamedTemporaryFile(suffix=‘.wav’, delete=False) as f:
fname = f.name
text = text.replace(‘"’,’ ')
text = ‘“{0}”’.format(text)
try:
subprocess.call([‘pico2wave’, ‘–wave’, fname, ‘-l’, lang, text])
subprocess.call([‘aplay’,fname])
except Exception as e:
_LOGGER.error(‘Error trying to say: %s’, e)
os.remove(fname)