Repository: RapidWareTech/pyttsx Branch: master Commit: 1a84ee339719 Files: 28 Total size: 102.1 KB Directory structure: gitextract_nedunrbu/ ├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README.rst ├── docs/ │ ├── Makefile │ ├── changelog.rst │ ├── conf.py │ ├── drivers.rst │ ├── engine.rst │ ├── index.rst │ └── install.rst ├── pyttsx/ │ ├── __init__.py │ ├── driver.py │ ├── drivers/ │ │ ├── __init__.py │ │ ├── _espeak.py │ │ ├── dummy.py │ │ ├── espeak.py │ │ ├── nsss.py │ │ └── sapi5.py │ ├── engine.py │ └── voice.py ├── setup.py └── tests/ ├── manual/ │ └── run.py └── unit/ ├── test_all.py ├── test_lifecycle.py ├── test_prop.py ├── test_say.py └── test_setup.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ *.pyc build/ dist/ pyttsx.egg-info/ _build/ ================================================ FILE: LICENSE ================================================ pyttsx Copyright (c) 2009, 2013 Peter Parente 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. ================================================ FILE: MANIFEST.in ================================================ recursive-include docs * ================================================ FILE: README.rst ================================================ ====== pyttsx ====== Cross-platform Python wrapper for text-to-speech synthesis Help Wanted =========== As you can probably tell, I have not had time or in some cases the resources (e.g., specific versions of OSes) to maintain pyttsx very well for some time now. If you are using pyttsx in your day to day work and would like to take over as maintainer of the project, please let me know. Quickstart ========== :: import pyttsx engine = pyttsx.init() engine.say('Greetings!') engine.say('How are you today?') engine.runAndWait() See http://pyttsx.readthedocs.org/ for documentation of the full API. Included drivers ================ * nsss - NSSpeechSynthesizer on Mac OS X 10.5 and higher * sapi5 - SAPI5 on Windows XP, Windows Vista, and (untested) Windows 7 * espeak - eSpeak on any distro / platform that can host the shared library (e.g., Ubuntu / Fedora Linux) Contributing drivers ==================== Email the author if you have wrapped or are interested in wrapping another text-to-speech engine for use with pyttsx. Project links ============= * Python Package Index for downloads (http://pypi.python.org/pyttsx) * GitHub site for source, bugs, and q&a (https://github.com/parente/pyttsx) * ReadTheDocs for docs (http://pyttsx.readthedocs.org) See Also ======== https://github.com/parente/espeakbox - espeak in a 16.5 MB Docker container with a simple REST API License ======= Copyright (c) 2009, 2013 Peter Parente All rights reserved. http://creativecommons.org/licenses/BSD/ ================================================ FILE: docs/Makefile ================================================ # Makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build PAPER = # Internal variables. PAPEROPT_a4 = -D latex_paper_size=a4 PAPEROPT_letter = -D latex_paper_size=letter ALLSPHINXOPTS = -d _build/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . .PHONY: help clean html dirhtml pickle json htmlhelp qthelp latex changes linkcheck doctest help: @echo "Please use \`make ' where is one of" @echo " html to make standalone HTML files" @echo " dirhtml to make HTML files named index.html in directories" @echo " pickle to make pickle files" @echo " json to make JSON files" @echo " htmlhelp to make HTML files and a HTML help project" @echo " qthelp to make HTML files and a qthelp project" @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" @echo " changes to make an overview of all changed/added/deprecated items" @echo " linkcheck to check all external links for integrity" @echo " doctest to run all doctests embedded in the documentation (if enabled)" clean: -rm -rf _build/* html: $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) _build/html @echo @echo "Build finished. The HTML pages are in _build/html." dirhtml: $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) _build/dirhtml @echo @echo "Build finished. The HTML pages are in _build/dirhtml." pickle: $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) _build/pickle @echo @echo "Build finished; now you can process the pickle files." json: $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) _build/json @echo @echo "Build finished; now you can process the JSON files." htmlhelp: $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) _build/htmlhelp @echo @echo "Build finished; now you can run HTML Help Workshop with the" \ ".hhp project file in _build/htmlhelp." qthelp: $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) _build/qthelp @echo @echo "Build finished; now you can run "qcollectiongenerator" with the" \ ".qhcp project file in _build/qthelp, like this:" @echo "# qcollectiongenerator _build/qthelp/pyttsx.qhcp" @echo "To view the help file:" @echo "# assistant -collectionFile _build/qthelp/pyttsx.qhc" latex: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) _build/latex @echo @echo "Build finished; the LaTeX files are in _build/latex." @echo "Run \`make all-pdf' or \`make all-ps' in that directory to" \ "run these through (pdf)latex." changes: $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) _build/changes @echo @echo "The overview file is in _build/changes." linkcheck: $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) _build/linkcheck @echo @echo "Link check complete; look for any errors in the above output " \ "or in _build/linkcheck/output.txt." doctest: $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) _build/doctest @echo "Testing of doctests in the sources finished, look at the " \ "results in _build/doctest/output.txt." ================================================ FILE: docs/changelog.rst ================================================ Changelog --------- Version 1.2 ~~~~~~~~~~~ * Added pip install instructions to doc. * Fixed voice selection to use VoiceLocaleIdentifier on OS X instead of deprecated VoiceLanguage Version 1.1 ~~~~~~~~~~~ * Fixed compatibility with pip * Fixed espeak crash when running on Natty (https://github.com/parente/pyttsx/issues/3) Version 1.0 ~~~~~~~~~~~ First release ================================================ FILE: docs/conf.py ================================================ # -*- coding: utf-8 -*- # # pyttsx documentation build configuration file, created by # sphinx-quickstart on Sun Nov 1 09:40:19 2009. # # This file is execfile()d with the current directory set to its containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys, os # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. #sys.path.append(os.path.abspath('.')) # -- General configuration ----------------------------------------------------- # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = [] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8' # The master toctree document. master_doc = 'index' # General information about the project. project = u'pyttsx' copyright = u'2009, 2013 Peter Parente' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. version = '1.2' # The full version, including alpha/beta/rc tags. release = '1.2' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. #language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of documents that shouldn't be included in the build. #unused_docs = [] # List of directories, relative to source directory, that shouldn't be searched # for source files. exclude_trees = ['_build'] # The reST default role (used for this markup: `text`) to use for all documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # -- Options for HTML output --------------------------------------------------- # The theme to use for HTML and HTML Help pages. Major themes that come with # Sphinx are currently 'default' and 'sphinxdoc'. html_theme = 'default' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. #html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. #html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". #html_title = None # A shorter title for the navigation bar. Default is the same as html_title. #html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. #html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_use_modindex = True # If false, no index is generated. #html_use_index = True # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. html_show_sourcelink = False # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = '' # Output file base name for HTML help builder. htmlhelp_basename = 'pyttsxdoc' # -- Options for LaTeX output -------------------------------------------------- # The paper size ('letter' or 'a4'). #latex_paper_size = 'letter' # The font size ('10pt', '11pt' or '12pt'). #latex_font_size = '10pt' # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ ('index', 'pyttsx.tex', u'pyttsx Documentation', u'Peter Parente', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # Additional stuff for the LaTeX preamble. #latex_preamble = '' # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_use_modindex = True ================================================ FILE: docs/drivers.rst ================================================ Implementing drivers -------------------- You can implement new drivers for the :mod:`pyttsx.Engine` by: #. Creating a Python module with the name of your new driver. #. Implementing the required driver factory function and class in your module. #. Using methods on a :class:`pyttsx.driver.DriverProxy` instance provided by the :class:`pyttsx.Engine` to control the event queue and notify applications about events. The Driver interface ~~~~~~~~~~~~~~~~~~~~ All drivers must implement the following factory function and driver interface. .. module:: pyttsx.drivers :synopsis: The package containing the available driver implementations .. function:: buildDriver(proxy : pyttsx.driver.DriverProxy) -> pyttsx.drivers.DriverDelegate Instantiates delegate subclass declared in this module. :param proxy: Proxy instance provided by a :class:`pyttsx.Engine` instance. .. class:: DriverDelegate .. note:: The :class:`DriverDelegate` class is not actually declared in :mod:`pyttsx.drivers` and cannot server as a base class. It is only here for the purpose of documenting the interface all drivers must implement. .. method:: __init__(proxy : pyttsx.drivers.DriverProxy, *args, **kwargs) -> None Constructor. Must store the proxy reference. :param proxy: Proxy instance provided by the :func:`buildDriver` function. .. method:: destroy() -> Optional. Invoked by the :class:`pyttsx.driver.DriverProxy` when it is being destroyed so this delegate can clean up any synthesizer resources. If not implemented, the proxy proceeds safely. .. method:: endLoop() -> None Immediately ends a running driver event loop. .. method:: getProperty(name : string) -> object Immediately gets the named property value. At least those properties listed in the :meth:`pyttsx.Engine.getProperty` documentation must be supported. :param name: Name of the property to query. :return: Value of the property at the time of this invocation. .. method:: say(text : unicode, name : string) -> None Immediately speaks an utterance. The speech must be output according to the current property values applied at the time of this invocation. Before this method returns, it must invoke :meth:`pyttsx.driver.DriverProxy.setBusy` with value :const:`True` to stall further processing of the command queue until the output completes or is interrupted. This method must trigger one and only one `started-utterance` notification when output begins, one `started-word` notification at the start of each word in the utterance, and a `finished-utterance` notification when output completes. :param text: Text to speak. :param name: Name to associate with the utterance. Included in notifications about this utterance. .. method:: setProperty(name : string, value : object) -> None Immediately sets the named property value. At least those properties listed in the :meth:`pyttsx.Engine.setProperty` documentation must be supported. After setting the property, the driver must invoke :meth:`pyttsx.driver.DriverProxy.setBusy` with value :const:`False` to pump the command queue. :param name: Name of the property to change. :param value: Value to set. .. method:: startLoop() Immediately starts an event loop. The loop is responsible for sending notifications about utterances and pumping the command queue by using methods on the :class:`pyttsx.driver.DriverProxy` object given to the factory function that created this object. .. method:: stop() Immediately stops the current utterance output. This method must trigger a `finished-utterance` notification if called during on-going output. It must trigger no notification if there is no ongoing output. After stopping the output and sending any required notification, the driver must invoke :meth:`pyttsx.driver.DriverProxy.setBusy` with value :const:`False` to pump the command queue. The DriverProxy interface ~~~~~~~~~~~~~~~~~~~~~~~~~ .. module:: pyttsx.driver :synopsis: The module containing the driver proxy implementation The :func:`pyttsx.drivers.buildDriver` factory receives an instance of a :class:`DriverProxy` class and provides it to the :class:`pyttsx.drivers.DriverDelegate` it constructs. The driver delegate can invoke the following public methods on the proxy instance. All other public methods found in the code are reserved for use by an :class:`pyttsx.Engine` instance. .. class:: DriverProxy .. method:: isBusy() -> bool Gets if the proxy is busy and cannot process the next command in the queue or not. :return: True means busy, False means idle. .. method:: notify(topic : string, **kwargs) -> None Fires a notification. :param topic: The name of the notification. :kwargs: Name/value pairs associated with the topic. .. method:: setBusy(busy : bool) -> None Sets the proxy to busy so it cannot continue to pump the command queue or idle so it can process the next command. :param busy: True to set busy, false to set idle ================================================ FILE: docs/engine.rst ================================================ .. module:: pyttsx :synopsis: The root pyttsx package defining the engine factory function Using pyttsx ------------ An application invokes the :func:`pyttsx.init` factory function to get a reference to a :class:`pyttsx.Engine` instance. During construction, the engine initializes a :class:`pyttsx.driver.DriverProxy` object responsible for loading a speech engine driver implementation from the :mod:`pyttsx.drivers` module. After construction, an application uses the engine object to register and unregister event callbacks; produce and stop speech; get and set speech engine properties; and start and stop event loops. The Engine factory ~~~~~~~~~~~~~~~~~~ .. function:: init([driverName : string, debug : bool]) -> pyttsx.Engine Gets a reference to an engine instance that will use the given driver. If the requested driver is already in use by another engine instance, that engine is returned. Otherwise, a new engine is created. :param driverName: Name of the :mod:`pyttsx.drivers` module to load and use. Defaults to the best available driver for the platform, currently: * `sapi5` - SAPI5 on Windows * `nsss` - NSSpeechSynthesizer on Mac OS X * `espeak` - eSpeak on every other platform :param debug: Enable debug output or not. :raises ImportError: When the requested driver is not found :raises RuntimeError: When the driver fails to initialize The Engine interface ~~~~~~~~~~~~~~~~~~~~ .. module:: pyttsx.engine :synopsis: The module containing the engine implementation .. class:: Engine Provides application access to text-to-speech synthesis. .. method:: connect(topic : string, cb : callable) -> dict Registers a callback for notifications on the given topic. :param topic: Name of the event to subscribe to. :param cb: Function to invoke when the event fires. :return: A token that the caller can use to unsubscribe the callback later. The following are the valid topics and their callback signatures. .. describe:: started-utterance Fired when the engine begins speaking an utterance. The associated callback must have the folowing signature. .. function:: onStartUtterance(name : string) -> None :param name: Name associated with the utterance. .. describe:: started-word Fired when the engine begins speaking a word. The associated callback must have the folowing signature. .. function:: onStartWord(name : string, location : integer, length : integer) :param name: Name associated with the utterance. .. describe:: finished-utterance Fired when the engine finishes speaking an utterance. The associated callback must have the folowing signature. .. function:: onFinishUtterance(name : string, completed : bool) -> None :param name: Name associated with the utterance. :param completed: True if the utterance was output in its entirety or not. .. describe:: error Fired when the engine encounters an error. The associated callback must have the folowing signature. .. function:: onError(name : string, exception : Exception) -> None :param name: Name associated with the utterance that caused the error. :param exception: Exception that was raised. .. method:: disconnect(token : dict) Unregisters a notification callback. :param token: Token returned by :meth:`connect` associated with the callback to be disconnected. .. method:: endLoop() -> None Ends a running event loop. If :meth:`startLoop` was called with `useDriverLoop` set to True, this method stops processing of engine commands and immediately exits the event loop. If it was called with False, this method stops processing of engine commands, but it is up to the caller to end the external event loop it started. :raises RuntimeError: When the loop is not running .. method:: getProperty(name : string) -> object Gets the current value of an engine property. :param name: Name of the property to query. :return: Value of the property at the time of this invocation. The following property names are valid for all drivers. .. describe:: rate Integer speech rate in words per minute. Defaults to 200 word per minute. .. describe:: voice String identifier of the active voice. .. describe:: voices List of :class:`pyttsx.voice.Voice` descriptor objects. .. describe:: volume Floating point volume in the range of 0.0 to 1.0 inclusive. Defaults to 1.0. .. method:: isBusy() -> bool Gets if the engine is currently busy speaking an utterance or not. :return: True if speaking, false if not. .. method:: runAndWait() -> None Blocks while processing all currently queued commands. Invokes callbacks for engine notifications appropriately. Returns when all commands queued before this call are emptied from the queue. .. method:: say(text : unicode, name : string) -> None Queues a command to speak an utterance. The speech is output according to the properties set before this command in the queue. :param text: Text to speak. :param name: Name to associate with the utterance. Included in notifications about this utterance. .. method:: setProperty(name, value) -> None Queues a command to set an engine property. The new property value affects all utterances queued after this command. :param name: Name of the property to change. :param value: Value to set. The following property names are valid for all drivers. .. describe:: rate Integer speech rate in words per minute. .. describe:: voice String identifier of the active voice. .. describe:: volume Floating point volume in the range of 0.0 to 1.0 inclusive. .. method:: startLoop([useDriverLoop : bool]) -> None Starts running an event loop during which queued commands are processed and notifications are fired. :param useDriverLoop: True to use the loop provided by the selected driver. False to indicate the caller will enter its own loop after invoking this method. The caller's loop must pump events for the driver in use so that pyttsx notifications are delivered properly (e.g., SAPI5 requires a COM message pump). Defaults to True. .. method:: stop() -> None Stops the current utterance and clears the command queue. The Voice metadata ~~~~~~~~~~~~~~~~~~ .. module:: pyttsx.voice :synopsis: The module containing the voice structure implementation .. class:: Voice Contains information about a speech synthesizer voice. .. attribute:: age Integer age of the voice in years. Defaults to :const:`None` if unknown. .. attribute:: gender String gender of the voice: `male`, `female`, or `neutral`. Defaults to :const:`None` if unknown. .. attribute:: id String identifier of the voice. Used to set the active voice via :meth:`pyttsx.engine.Engine.setPropertyValue`. This attribute is always defined. .. attribute:: languages List of string languages supported by this voice. Defaults to an empty list of unknown. .. attribute:: name Human readable name of the voice. Defaults to :const:`None` if unknown. Examples ~~~~~~~~ Speaking text ############# .. sourcecode:: python import pyttsx engine = pyttsx.init() engine.say('Sally sells seashells by the seashore.') engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() Listening for events #################### .. sourcecode:: python import pyttsx def onStart(name): print 'starting', name def onWord(name, location, length): print 'word', name, location, length def onEnd(name, completed): print 'finishing', name, completed engine = pyttsx.init() engine.connect('started-utterance', onStart) engine.connect('started-word', onWord) engine.connect('finished-utterance', onEnd) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() Interrupting an utterance ######################### .. sourcecode:: python import pyttsx def onWord(name, location, length): print 'word', name, location, length if location > 10: engine.stop() engine = pyttsx.init() engine.connect('started-word', onWord) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() Changing voices ############### .. sourcecode:: python engine = pyttsx.init() voices = engine.getProperty('voices') for voice in voices: engine.setProperty('voice', voice.id) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() Changing speech rate #################### .. sourcecode:: python engine = pyttsx.init() rate = engine.getProperty('rate') engine.setProperty('rate', rate+50) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() Changing volume ############### .. sourcecode:: python engine = pyttsx.init() volume = engine.getProperty('volume') engine.setProperty('volume', volume-0.25) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() Running a driver event loop ########################### .. sourcecode:: python engine = pyttsx.init() def onStart(name): print 'starting', name def onWord(name, location, length): print 'word', name, location, length def onEnd(name, completed): print 'finishing', name, completed if name == 'fox': engine.say('What a lazy dog!', 'dog') elif name == 'dog': engine.endLoop() engine = pyttsx.init() engine.connect('started-utterance', onStart) engine.connect('started-word', onWord) engine.connect('finished-utterance', onEnd) engine.say('The quick brown fox jumped over the lazy dog.', 'fox') engine.startLoop() Using an external event loop ############################ .. sourcecode:: python engine = pyttsx.init() engine.say('The quick brown fox jumped over the lazy dog.', 'fox') engine.startLoop(False) # engine.iterate() must be called inside externalLoop() externalLoop() engine.endLoop() ================================================ FILE: docs/index.rst ================================================ ================================== pyttsx - Text-to-speech x-platform ================================== This documentation describes the pyttsx Python package v |release| and was rendered on |today|. .. rubric:: Table of Contents .. toctree:: :maxdepth: 2 install engine drivers changelog .. rubric:: Project Links * `Project home page at GitHub`__ * `Package listing in PyPI`__ * `Documentation at ReadTheDocs`__ __ https://github.com/parente/pyttsx __ http://pypi.python.org/pypi/pyttsx __ https://pyttsx.readthedocs.org/ ================================================ FILE: docs/install.rst ================================================ Installing pyttsx ----------------- Tested versions ~~~~~~~~~~~~~~~ Version |version| of pyttsx includes drivers for the following text-to-speech synthesizers. Only operating systems on which a driver is tested and known to work are listed. The drivers may work on other systems. * SAPI5 on Windows XP, Windows Vista, and Windows 7 * NSSpeechSynthesizer on Mac OS X 10.5 (Leopard), 10.6 (Snow Leopard), 10.7 (Lion), and 10.8 (Mountain Lion). * `espeak`_ on 32-bit Ubuntu Desktop Edition 8.10 (Intrepid), 9.04 (Jaunty), 9.10 (Karmic), and 12.04 (Precise). The :func:`pyttsx.init` documentation explains how to select a specific synthesizer by name as well as the default for each platform. Using pip to install system-wide ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If you have pip installed, you can use it to install pyttsx in the system site-packages folder. On Windows ########## First install the `pywin32-extensions `_ package using its Windows installer. Then use pip to install pyttsx. .. code-block:: bash $ pip install pyttsx On OSX or Linux ############### .. code-block:: bash $ sudo pip install pyttsx Using pip to install in a virtualenv ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If you have virtualenv_ installed with pip_, you can use pip to install a copy of pyttsx in the virtual environment folder. On Windows ########## You'll need to install the `pywin32-extensions `_ package system-wide using its Windows installer. Then you'll need to give your virtualenv access to the system site-packages in order to install pyttsx. .. code-block:: bash $ virtualenv --system-site-packages myproj New python executable in myproj/bin/python Installing setuptools............done. Installing pip...............done. $ myproj\Scripts\activate (myproj)$ pip install pyttsx On OSX ###### Unless you wish to compile your own version of pyobjc (a lengthy process), you will need to give your virtualenv access to the system site-packages folder. .. code-block:: bash $ virtualenv --system-site-packages myproj New python executable in myproj/bin/python Installing setuptools............done. Installing pip...............done. $ . myproj/bin/activate (myproj)$ pip install pyttsx ... Successfully installed pyttsx Cleaning up... On Linux ######## pyttsx requires no Python dependencies on Linux. You can cut-off the pyttsx virtualenv from the system site-packages. code-block:: bash $ virtualenv --no-site-packages myproj New python executable in myproj/bin/python Installing setuptools............done. Installing pip...............done. $ . myproj/bin/activate (myproj)$ pip install pyttsx ... Successfully installed pyttsx Cleaning up... .. _espeak: http://espeak.sourceforge.net/ .. _virtualenv: https://pypi.python.org/pypi/virtualenv/1.10.1 .. _pip: https://pypi.python.org/pypi/pip ================================================ FILE: pyttsx/__init__.py ================================================ ''' pyttsx package. Copyright (c) 2009, 2013 Peter Parente 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. ''' from __future__ import absolute_import from .engine import Engine import weakref _activeEngines = weakref.WeakValueDictionary() def init(driverName=None, debug=False): ''' Constructs a new TTS engine instance or reuses the existing instance for the driver name. @param driverName: Name of the platform specific driver to use. If None, selects the default driver for the operating system. @type: str @param debug: Debugging output enabled or not @type debug: bool @return: Engine instance @rtype: L{engine.Engine} ''' try: eng = _activeEngines[driverName] except KeyError: eng = Engine(driverName, debug) _activeEngines[driverName] = eng return eng ================================================ FILE: pyttsx/driver.py ================================================ ''' Proxy for drivers. Copyright (c) 2009, 2013 Peter Parente 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 sys import traceback import weakref import importlib class DriverProxy(object): ''' Proxy to a driver implementation. @ivar _module: Module containing the driver implementation @type _module: module @ivar _engine: Reference to the engine that owns the driver @type _engine: L{engine.Engine} @ivar _queue: Queue of commands outstanding for the driver @type _queue: list @ivar _busy: True when the driver is busy processing a command, False when not @type _busy: bool @ivar _name: Name associated with the current utterance @type _name: str @ivar _debug: Debugging output enabled or not @type _debug: bool @ivar _iterator: Driver iterator to invoke when in an external run loop @type _iterator: iterator ''' def __init__(self, engine, driverName, debug): ''' Constructor. @param engine: Reference to the engine that owns the driver @type engine: L{engine.Engine} @param driverName: Name of the driver module to use under drivers/ or None to select the default for the platform @type driverName: str @param debug: Debugging output enabled or not @type debug: bool ''' if driverName is None: # pick default driver for common platforms if sys.platform == 'darwin': driverName = 'nsss' elif sys.platform == 'win32': driverName = 'sapi5' else: driverName = 'espeak' # import driver module name = 'pyttsx.drivers.%s' % driverName self._module = importlib.import_module(name) # build driver instance self._driver = self._module.buildDriver(weakref.proxy(self)) # initialize refs self._engine = engine self._queue = [] self._busy = True self._name = None self._iterator = None self._debug = debug def __del__(self): try: self._driver.destroy() except (AttributeError, TypeError): pass def _push(self, mtd, args, name=None): ''' Adds a command to the queue. @param mtd: Method to invoke to process the command @type mtd: method @param args: Arguments to apply when invoking the method @type args: tuple @param name: Name associated with the command @type name: str ''' self._queue.append((mtd, args, name)) self._pump() def _pump(self): ''' Attempts to process the next command in the queue if one exists and the driver is not currently busy. ''' while (not self._busy) and len(self._queue): cmd = self._queue.pop(0) self._name = cmd[2] try: cmd[0](*cmd[1]) except Exception as e: self.notify('error', exception=e) if self._debug: traceback.print_exc() def notify(self, topic, **kwargs): ''' Sends a notification to the engine from the driver. @param topic: Notification topic @type topic: str @param kwargs: Arbitrary keyword arguments @type kwargs: dict ''' kwargs['name'] = self._name self._engine._notify(topic, **kwargs) def setBusy(self, busy): ''' Called by the driver to indicate it is busy. @param busy: True when busy, false when idle @type busy: bool ''' self._busy = busy if not self._busy: self._pump() def isBusy(self): ''' @return: True if the driver is busy, false if not @rtype: bool ''' return self._busy def say(self, text, name): ''' Called by the engine to push a say command onto the queue. @param text: Text to speak @type text: unicode @param name: Name to associate with the utterance @type name: str ''' self._push(self._driver.say, (text,), name) def stop(self): ''' Called by the engine to stop the current utterance and clear the queue of commands. ''' # clear queue up to first end loop command while(True): try: mtd, args, name = self._queue[0] except IndexError: break if(mtd == self._engine.endLoop): break self._queue.pop(0) self._driver.stop() def getProperty(self, name): ''' Called by the engine to get a driver property value. @param name: Name of the property @type name: str @return: Property value @rtype: object ''' return self._driver.getProperty(name) def setProperty(self, name, value): ''' Called by the engine to set a driver property value. @param name: Name of the property @type name: str @param value: Property value @type value: object ''' self._push(self._driver.setProperty, (name, value)) def runAndWait(self): ''' Called by the engine to start an event loop, process all commands in the queue at the start of the loop, and then exit the loop. ''' self._push(self._engine.endLoop, tuple()) self._driver.startLoop() def startLoop(self, useDriverLoop): ''' Called by the engine to start an event loop. ''' if useDriverLoop: self._driver.startLoop() else: self._iterator = self._driver.iterate() def endLoop(self, useDriverLoop): ''' Called by the engine to stop an event loop. ''' self._queue = [] self._driver.stop() if useDriverLoop: self._driver.endLoop() else: self._iterator = None self.setBusy(True) def iterate(self): ''' Called by the engine to iterate driver commands and notifications from within an external event loop. ''' try: next(self._iterator) except StopIteration: pass ================================================ FILE: pyttsx/drivers/__init__.py ================================================ ''' Speech driver implementations. Copyright (c) 2009, 2013 Peter Parente 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. ''' ================================================ FILE: pyttsx/drivers/_espeak.py ================================================ '''espeak.py a thin ctypes wrapper for the espeak dll Gary Bishop July 2007 Modified October 2007 for the version 2 interface to espeak and more pythonic interfaces Free for any use. ''' from __future__ import print_function import ctypes from ctypes import cdll, c_int, c_char_p, c_wchar_p, POINTER, c_short, c_uint, c_long, c_void_p from ctypes import CFUNCTYPE, byref, Structure, Union, c_wchar, c_ubyte, c_ulong import time def cfunc(name, dll, result, *args): '''build and apply a ctypes prototype complete with parameter flags''' atypes = [] aflags = [] for arg in args: atypes.append(arg[1]) aflags.append((arg[2], arg[0]) + arg[3:]) return CFUNCTYPE(result, *atypes)((name, dll), tuple(aflags)) dll = cdll.LoadLibrary('libespeak.so.1') # constants and such from speak_lib.h EVENT_LIST_TERMINATED = 0 EVENT_WORD = 1 EVENT_SENTENCE = 2 EVENT_MARK = 3 EVENT_PLAY = 4 EVENT_END = 5 EVENT_MSG_TERMINATED = 6 class numberORname(Union): _fields_ = [ ('number', c_int), ('name', c_char_p) ] class EVENT(Structure): _fields_ = [ ('type', c_int), ('unique_identifier', c_uint), ('text_position', c_int), ('length', c_int), ('audio_position', c_int), ('sample', c_int), ('user_data', c_void_p), ('id', numberORname) ] AUDIO_OUTPUT_PLAYBACK = 0 AUDIO_OUTPUT_RETRIEVAL = 1 AUDIO_OUTPUT_SYNCHRONOUS = 2 AUDIO_OUTPUT_SYNCH_PLAYBACK = 3 EE_OK = 0 EE_INTERNAL_ERROR = -1 EE_BUFFER_FULL = 1 EE_NOT_FOUND = 2 Initialize = cfunc('espeak_Initialize', dll, c_int, ('output', c_int, 1, AUDIO_OUTPUT_PLAYBACK), ('bufflength', c_int, 1, 100), ('path', c_char_p, 1, None), ('option', c_int, 1, 0)) Initialize.__doc__ = '''Must be called before any synthesis functions are called. output: the audio data can either be played by eSpeak or passed back by the SynthCallback function. buflength: The length in mS of sound buffers passed to the SynthCallback function. path: The directory which contains the espeak-data directory, or NULL for the default location. options: bit 0: 1=allow espeakEVENT_PHONEME events. Returns: sample rate in Hz, or -1 (EE_INTERNAL_ERROR).''' t_espeak_callback = CFUNCTYPE(c_int, POINTER(c_short), c_int, POINTER(EVENT)) cSetSynthCallback = cfunc('espeak_SetSynthCallback', dll, None, ('SynthCallback', t_espeak_callback, 1)) SynthCallback = None def SetSynthCallback(cb): global SynthCallback SynthCallback = t_espeak_callback(cb) cSetSynthCallback(SynthCallback) SetSynthCallback.__doc__ = '''Must be called before any synthesis functions are called. This specifies a function in the calling program which is called when a buffer of speech sound data has been produced. The callback function is of the form: int SynthCallback(short *wav, int numsamples, espeak_EVENT *events); wav: is the speech sound data which has been produced. NULL indicates that the synthesis has been completed. numsamples: is the number of entries in wav. This number may vary, may be less than the value implied by the buflength parameter given in espeak_Initialize, and may sometimes be zero (which does NOT indicate end of synthesis). events: an array of espeak_EVENT items which indicate word and sentence events, and also the occurance if and