About‎ > ‎Announcements‎ > ‎

Linux Speech-Dispatcher support

posted 20 Nov 2010 11:34 by James Holgate   [ updated 15 Dec 2010 23:51 ]

In the early days of Linux, it was sometimes very difficult to get your printer working properly. CUPS, the Common Unix Printing System, created a method that greatly simplified setting up printers. Now CUPS will automatically detect and set up most printers automatically when you plug them in to your computer.

Speech-Dispatcher aims to be a similar utility for speech synthesis. Speech programs differ in their command line syntax, and how they need the text to be encoded (UTF-8, iso-8859-15, LATIN1 etc.). Speech-Dispatcher is a middle step that allows different speech tools to use a common command syntax. Programmers have the opportunity to develop “speech” modules that actually output the text to an external braille reader or to a scrolling subtitle applet on the desktop. With Ubuntu Linux 10.10, speech-dispatcher and the associated python utilities are included in the default desktop installation.

Speech-Dispatcher uses espeak to read by default. If you want to use different voices or a different program,

  • Create a local configuration file by running a terminal setup program called spd-conf
  • Edit the configuration file manually by removing the “#” from the beginning of line in the configuration file that includes the module you want to use and specifying the module in the line that reads DefaultModule. For example, if you have installed mbrola and want to use it with espeak, you could open ~/.speech-dispatcher/config/speechd.config with text editor, remove the “#” from the line that reads #AddModule "espeak-mbrola-generic" "sd_generic" "espeak-mbrola-generic.conf" and change the Default Module line so that it reads DefaultModule espeak-mbrola-generic 
  • Restart the speech daemon. The most reliable way to do this is to restart your computer.
Speech-Dispatcher is currently in a state of development. It may not work properly on your particular computer. A computer that has a fresh install of the most recent version of Ubuntu will probably work. In the attached python script that was developed to use speech-dispatcher with the Read Text Extension, you may notice that the python code replaces single and double quotes with smart quotes. This is because Speech-Dispatcher did not handle regular quotes well on my computer. Speech-Dispatcher stopped talking when the string included a single quote and said “backslash” when it encountered a double quote. Speech-Dispatcher might not work if you log out and log in as a different user without restarting the computer.

The attached archive includes the python script and a sample speech-dispatcher configuration file from a Ubuntu 10.10 system that has the mbrola english male voice 1 (mbrola-en1) installed. It works for me, but your results may vary.

#!/usr/bin/env python
# -*- coding: UTF-8-*-
'''
To read a plain text file using Python with speech-dispatcher (import speechd)
  Designed for reading text aloud with a Linux platform like Ubuntu.
  If voices are missing or the speech-dispatcher is not correctly set up, the
  Python script will fail.
  
Read Text Extension for OpenOffice.org (for Linux) :
  See: http://sites.google.com/site/readtextextension/
  Tools > Add-Ons > Read Selection... Dialog setup:
  External program: /usr/bin/python
  Command line options: "(HOME)Downloads/spd_read_text_file.py" "(TMP)"
  or "(HOME)Downloads/spd_read_text_file.py" --language "fr" --voice "FEMALE1" --rate "-20" "(TMP)"

Copyright (c) 2010 James Holgate

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
'''
import getopt, sys, codecs

def usage():
  print ("")
  print ("Usage")
  print ('  '+ sys.argv[0] + ' [--output_module="xx"] [--language="xx"] [--voice="xx"] [--rate="nn"] input.txt')
  print ("  Use a specific output module")
  print ("    " + sys.argv[0] + ' --output_module "espeak-generic" "TextFile.txt"')
  print ("")
  print ("  Use a specific language - en, fr, es...")
  print ("    " + sys.argv[0] + ' --language "fr" "TextFile.txt"')
  print ("")
  print ("  Use a specific voice - MALE1, FEMALE1, MALE2, ... FEMALE3, CHILD_MALE, CHILD_FEMALE")
  print ("    " + sys.argv[0] + ' --voice "MALE1" "TextFile.txt"')
  print ("")
  print ("  To say the text slower - minimum -100")
  print ("    " + sys.argv[0] + ' --rate "-20" "TextFile.txt"')
  print ("")
  print ("  To say the text faster - maximum 100")
  print ("    " + sys.argv[0] + ' --rate "20" "TextFile.txt"')
  print ("")
  print ("  See also: <http://cvs.freebsoft.org/doc/speechd/speech-dispatcher.html>")
# end of usage() ############################

def main():
  try:
    import speechd
    client = speechd.SSIPClient('ReadTextExtensionPythonScript')
  except ImportError:
    print 'I did not find the speechd voice synthesis resources!'
    print str(err)
    usage()
    sys.exit(2)  
  try:
    opts, args = getopt.getopt(sys.argv[1:], "holvr", ["help", "output_module=","language=","voice=","rate="])
  except getopt.GetoptError, err:
    # print help information and exit:
    print str(err) # will print something like "option -a not recognized"
    usage()
    sys.exit(2)
  for o, a in opts:
    if o in ("-h", "--help"):
      usage()
      sys.exit()
    elif o in ("-o", "--output_module"):
      client.set_output_module(a)
    elif o in ("-l", "--language"): # 2 letters lowercase - fr Français, de Deutsch...
      client.set_language(a[:2].lower())
    elif o in ("-v", "--voice"): # MALE1, MALE2 ...
      client.set_voice(a.upper())
    elif o in ("-r", "--rate"):
      client.set_rate( int(a) )
    else:
      assert False, "unhandled option"
    # ...
  try:       
    f = codecs.open(sys.argv[-1],mode='r',encoding=sys.getfilesystemencoding())
  except IOError:
    print 'I was unable to open the file you specified!'
    usage()
  else:
    # 2010.11.20 - Ubuntu 10.10 - espeak-mbrola-generic; espeak-generic
    # speechd freezes up with a single quote & says "backslash" for a double quote
    # so we replace these characters with smart quotes in the unicode string
    s = f.read().replace(u"'",u"´").replace(u' "',u" “").replace(u'"',u'”')
    f.close()
    client.set_punctuation(speechd.PunctuationMode.SOME)
    client.speak(s)
    client.close()
    sys.exit(0)
# end of main() ############################

if __name__ == "__main__":
  main()

Installing mbrola

  • Open a terminal.
  • Make sure espeak 1.44 or newer is installed. To check the version: espeak --help | grep "text-to-speech"
  • Install mbrola voices. In Ubuntu or Debian Linux, you have several options. For example,
    • You can search for mbrola using the Synaptic package manager
    • Open a terminal and type sudo apt-get install mbrola mbrola-en1 mbrola-fr4 mbrola-us1
  • See also: Mbrola voices for Ubuntu and Debian

Setting up Speech-Dispatcher

  • In a terminal, run spd-conf. You can accept the defaults if you plan to edit the configuration file by hand.
  • Test the speech dispatcher by running spd-say "Hello, 123."
  • If required, edit the ~/.speech-dispatcher/config/speechd.config with a text editor as described above or replace the configuration file with the configuration file in the attached archive.
  • The most reliable way to restart the Speech-Dispatcher with the new settings is to restart the computer.
  • Test the speech dispatcher by opening a terminal and running spd-say "Hello, 123."

Using the Python script

  • Show the Read Selection dialog with Tools → Add-Ons → Read Selection...
  • Select Read the Selection with another program
  • Browse to the path to the script – for example, ~/.gnome2/nautilus-scripts/spd_read_text_file.py. Note that if the file path includes a period as the first character, it is normally hidden in the file browser. You can either enable showing hidden files, or type the file path in the location bar.
  • To use the default voice and rate, in the command box, enter "(TMP)"
  • To use an alternate language, voice or rate, in the command box, enter --language "en" --voice "FEMALE1" --rate "-30" "(TMP)"
  • Click OK.


Troubleshooting

Check the basic functioning of espeak. Open a terminal window and enter date | espeak --
See if the speech-dispatcher is working. Open a terminal window and enter date | spd-say -e
If the speech-dispatcher refuses the connection, try restarting your computer. 
Make the python script executable with chmod a+rx ~/.gnome2/nautilus-scripts/spd_read_text_file.py
Check that you have installed the language resources for the languages and programs you are trying to use.
Check the Speech-Dispatcher page for more information.
See also: Speech-Dispatcher on Ubuntu 8.10! by Anthony Sales.


Comments