diff --git a/LICENSE b/LICENSE index 6e5f047..a612ad9 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,373 @@ -MIT License - -Copyright (c) 2023 AkulAI - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +Mozilla Public License Version 2.0 +================================== + +1. Definitions +-------------- + +1.1. "Contributor" + means each individual or legal entity that creates, contributes to + the creation of, or owns Covered Software. + +1.2. "Contributor Version" + means the combination of the Contributions of others (if any) used + by a Contributor and that particular Contributor's Contribution. + +1.3. "Contribution" + means Covered Software of a particular Contributor. + +1.4. "Covered Software" + means Source Code Form to which the initial Contributor has attached + the notice in Exhibit A, the Executable Form of such Source Code + Form, and Modifications of such Source Code Form, in each case + including portions thereof. + +1.5. "Incompatible With Secondary Licenses" + means + + (a) that the initial Contributor has attached the notice described + in Exhibit B to the Covered Software; or + + (b) that the Covered Software was made available under the terms of + version 1.1 or earlier of the License, but not also under the + terms of a Secondary License. + +1.6. "Executable Form" + means any form of the work other than Source Code Form. + +1.7. "Larger Work" + means a work that combines Covered Software with other material, in + a separate file or files, that is not Covered Software. + +1.8. "License" + means this document. + +1.9. "Licensable" + means having the right to grant, to the maximum extent possible, + whether at the time of the initial grant or subsequently, any and + all of the rights conveyed by this License. + +1.10. "Modifications" + means any of the following: + + (a) any file in Source Code Form that results from an addition to, + deletion from, or modification of the contents of Covered + Software; or + + (b) any new file in Source Code Form that contains any Covered + Software. + +1.11. "Patent Claims" of a Contributor + means any patent claim(s), including without limitation, method, + process, and apparatus claims, in any patent Licensable by such + Contributor that would be infringed, but for the grant of the + License, by the making, using, selling, offering for sale, having + made, import, or transfer of either its Contributions or its + Contributor Version. + +1.12. "Secondary License" + means either the GNU General Public License, Version 2.0, the GNU + Lesser General Public License, Version 2.1, the GNU Affero General + Public License, Version 3.0, or any later versions of those + licenses. + +1.13. "Source Code Form" + means the form of the work preferred for making modifications. + +1.14. "You" (or "Your") + means an individual or a legal entity exercising rights under this + License. For legal entities, "You" includes any entity that + controls, is controlled by, or is under common control with You. For + purposes of this definition, "control" means (a) the power, direct + or indirect, to cause the direction or management of such entity, + whether by contract or otherwise, or (b) ownership of more than + fifty percent (50%) of the outstanding shares or beneficial + ownership of such entity. + +2. License Grants and Conditions +-------------------------------- + +2.1. Grants + +Each Contributor hereby grants You a world-wide, royalty-free, +non-exclusive license: + +(a) under intellectual property rights (other than patent or trademark) + Licensable by such Contributor to use, reproduce, make available, + modify, display, perform, distribute, and otherwise exploit its + Contributions, either on an unmodified basis, with Modifications, or + as part of a Larger Work; and + +(b) under Patent Claims of such Contributor to make, use, sell, offer + for sale, have made, import, and otherwise transfer either its + Contributions or its Contributor Version. + +2.2. Effective Date + +The licenses granted in Section 2.1 with respect to any Contribution +become effective for each Contribution on the date the Contributor first +distributes such Contribution. + +2.3. Limitations on Grant Scope + +The licenses granted in this Section 2 are the only rights granted under +this License. No additional rights or licenses will be implied from the +distribution or licensing of Covered Software under this License. +Notwithstanding Section 2.1(b) above, no patent license is granted by a +Contributor: + +(a) for any code that a Contributor has removed from Covered Software; + or + +(b) for infringements caused by: (i) Your and any other third party's + modifications of Covered Software, or (ii) the combination of its + Contributions with other software (except as part of its Contributor + Version); or + +(c) under Patent Claims infringed by Covered Software in the absence of + its Contributions. + +This License does not grant any rights in the trademarks, service marks, +or logos of any Contributor (except as may be necessary to comply with +the notice requirements in Section 3.4). + +2.4. Subsequent Licenses + +No Contributor makes additional grants as a result of Your choice to +distribute the Covered Software under a subsequent version of this +License (see Section 10.2) or under the terms of a Secondary License (if +permitted under the terms of Section 3.3). + +2.5. Representation + +Each Contributor represents that the Contributor believes its +Contributions are its original creation(s) or it has sufficient rights +to grant the rights to its Contributions conveyed by this License. + +2.6. Fair Use + +This License is not intended to limit any rights You have under +applicable copyright doctrines of fair use, fair dealing, or other +equivalents. + +2.7. Conditions + +Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted +in Section 2.1. + +3. Responsibilities +------------------- + +3.1. Distribution of Source Form + +All distribution of Covered Software in Source Code Form, including any +Modifications that You create or to which You contribute, must be under +the terms of this License. You must inform recipients that the Source +Code Form of the Covered Software is governed by the terms of this +License, and how they can obtain a copy of this License. You may not +attempt to alter or restrict the recipients' rights in the Source Code +Form. + +3.2. Distribution of Executable Form + +If You distribute Covered Software in Executable Form then: + +(a) such Covered Software must also be made available in Source Code + Form, as described in Section 3.1, and You must inform recipients of + the Executable Form how they can obtain a copy of such Source Code + Form by reasonable means in a timely manner, at a charge no more + than the cost of distribution to the recipient; and + +(b) You may distribute such Executable Form under the terms of this + License, or sublicense it under different terms, provided that the + license for the Executable Form does not attempt to limit or alter + the recipients' rights in the Source Code Form under this License. + +3.3. Distribution of a Larger Work + +You may create and distribute a Larger Work under terms of Your choice, +provided that You also comply with the requirements of this License for +the Covered Software. If the Larger Work is a combination of Covered +Software with a work governed by one or more Secondary Licenses, and the +Covered Software is not Incompatible With Secondary Licenses, this +License permits You to additionally distribute such Covered Software +under the terms of such Secondary License(s), so that the recipient of +the Larger Work may, at their option, further distribute the Covered +Software under the terms of either this License or such Secondary +License(s). + +3.4. Notices + +You may not remove or alter the substance of any license notices +(including copyright notices, patent notices, disclaimers of warranty, +or limitations of liability) contained within the Source Code Form of +the Covered Software, except that You may alter any license notices to +the extent required to remedy known factual inaccuracies. + +3.5. Application of Additional Terms + +You may choose to offer, and to charge a fee for, warranty, support, +indemnity or liability obligations to one or more recipients of Covered +Software. However, You may do so only on Your own behalf, and not on +behalf of any Contributor. You must make it absolutely clear that any +such warranty, support, indemnity, or liability obligation is offered by +You alone, and You hereby agree to indemnify every Contributor for any +liability incurred by such Contributor as a result of warranty, support, +indemnity or liability terms You offer. You may include additional +disclaimers of warranty and limitations of liability specific to any +jurisdiction. + +4. Inability to Comply Due to Statute or Regulation +--------------------------------------------------- + +If it is impossible for You to comply with any of the terms of this +License with respect to some or all of the Covered Software due to +statute, judicial order, or regulation then You must: (a) comply with +the terms of this License to the maximum extent possible; and (b) +describe the limitations and the code they affect. Such description must +be placed in a text file included with all distributions of the Covered +Software under this License. Except to the extent prohibited by statute +or regulation, such description must be sufficiently detailed for a +recipient of ordinary skill to be able to understand it. + +5. Termination +-------------- + +5.1. The rights granted under this License will terminate automatically +if You fail to comply with any of its terms. However, if You become +compliant, then the rights granted under this License from a particular +Contributor are reinstated (a) provisionally, unless and until such +Contributor explicitly and finally terminates Your grants, and (b) on an +ongoing basis, if such Contributor fails to notify You of the +non-compliance by some reasonable means prior to 60 days after You have +come back into compliance. Moreover, Your grants from a particular +Contributor are reinstated on an ongoing basis if such Contributor +notifies You of the non-compliance by some reasonable means, this is the +first time You have received notice of non-compliance with this License +from such Contributor, and You become compliant prior to 30 days after +Your receipt of the notice. + +5.2. If You initiate litigation against any entity by asserting a patent +infringement claim (excluding declaratory judgment actions, +counter-claims, and cross-claims) alleging that a Contributor Version +directly or indirectly infringes any patent, then the rights granted to +You by any and all Contributors for the Covered Software under Section +2.1 of this License shall terminate. + +5.3. In the event of termination under Sections 5.1 or 5.2 above, all +end user license agreements (excluding distributors and resellers) which +have been validly granted by You or Your distributors under this License +prior to termination shall survive termination. + +************************************************************************ +* * +* 6. Disclaimer of Warranty * +* ------------------------- * +* * +* Covered Software is provided under this License on an "as is" * +* basis, without warranty of any kind, either expressed, implied, or * +* statutory, including, without limitation, warranties that the * +* Covered Software is free of defects, merchantable, fit for a * +* particular purpose or non-infringing. The entire risk as to the * +* quality and performance of the Covered Software is with You. * +* Should any Covered Software prove defective in any respect, You * +* (not any Contributor) assume the cost of any necessary servicing, * +* repair, or correction. This disclaimer of warranty constitutes an * +* essential part of this License. No use of any Covered Software is * +* authorized under this License except under this disclaimer. * +* * +************************************************************************ + +************************************************************************ +* * +* 7. Limitation of Liability * +* -------------------------- * +* * +* Under no circumstances and under no legal theory, whether tort * +* (including negligence), contract, or otherwise, shall any * +* Contributor, or anyone who distributes Covered Software as * +* permitted above, be liable to You for any direct, indirect, * +* special, incidental, or consequential damages of any character * +* including, without limitation, damages for lost profits, loss of * +* goodwill, work stoppage, computer failure or malfunction, or any * +* and all other commercial damages or losses, even if such party * +* shall have been informed of the possibility of such damages. This * +* limitation of liability shall not apply to liability for death or * +* personal injury resulting from such party's negligence to the * +* extent applicable law prohibits such limitation. Some * +* jurisdictions do not allow the exclusion or limitation of * +* incidental or consequential damages, so this exclusion and * +* limitation may not apply to You. * +* * +************************************************************************ + +8. Litigation +------------- + +Any litigation relating to this License may be brought only in the +courts of a jurisdiction where the defendant maintains its principal +place of business and such litigation shall be governed by laws of that +jurisdiction, without reference to its conflict-of-law provisions. +Nothing in this Section shall prevent a party's ability to bring +cross-claims or counter-claims. + +9. Miscellaneous +---------------- + +This License represents the complete agreement concerning the subject +matter hereof. If any provision of this License is held to be +unenforceable, such provision shall be reformed only to the extent +necessary to make it enforceable. Any law or regulation which provides +that the language of a contract shall be construed against the drafter +shall not be used to construe this License against a Contributor. + +10. Versions of the License +--------------------------- + +10.1. New Versions + +Mozilla Foundation is the license steward. Except as provided in Section +10.3, no one other than the license steward has the right to modify or +publish new versions of this License. Each version will be given a +distinguishing version number. + +10.2. Effect of New Versions + +You may distribute the Covered Software under the terms of the version +of the License under which You originally received the Covered Software, +or under the terms of any subsequent version published by the license +steward. + +10.3. Modified Versions + +If you create software not governed by this License, and you want to +create a new license for such software, you may create and use a +modified version of this License if you rename the license and remove +any references to the name of the license steward (except to note that +such modified license differs from this License). + +10.4. Distributing Source Code Form that is Incompatible With Secondary +Licenses + +If You choose to distribute Source Code Form that is Incompatible With +Secondary Licenses under the terms of this version of the License, the +notice described in Exhibit B of this License must be attached. + +Exhibit A - Source Code Form License Notice +------------------------------------------- + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + +If it is not possible or desirable to put the notice in a particular +file, then You may include the notice in a location (such as a LICENSE +file in a relevant directory) where a recipient would be likely to look +for such a notice. + +You may add additional accurate notices of copyright ownership. + +Exhibit B - "Incompatible With Secondary Licenses" Notice +--------------------------------------------------------- + + This Source Code Form is "Incompatible With Secondary Licenses", as + defined by the Mozilla Public License, v. 2.0. diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..5e471d7 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,14 @@ +# file GENERATED by distutils, do NOT edit +setup.cfg +setup.py +pyttsx3\__init__.py +pyttsx3\driver.py +pyttsx3\engine.py +pyttsx3\six.py +pyttsx3\voice.py +pyttsx3\drivers\__init__.py +pyttsx3\drivers\_espeak.py +pyttsx3\drivers\dummy.py +pyttsx3\drivers\espeak.py +pyttsx3\drivers\nsss.py +pyttsx3\drivers\sapi5.py diff --git a/README.md b/README.md index acd8dcc..ad6438c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,108 @@ -# rlvoice -A STT engine for AkulAI and more. +
Credits to nateshmbhat for creating the original library pyttsx3
+ +

Offline Text To Speech (TTS) converter for Python

+ + +[![Downloads](https://pepy.tech/badge/pyttsx3)](https://pepy.tech/project/pyttsx3) ![Downloads](https://pepy.tech/badge/pyttsx3/week) [![](https://img.shields.io/github/languages/code-size/nateshmbhat/pyttsx3.svg?style=plastic)](https://github.com/nateshmbhat/pyttsx3) [![](https://img.shields.io/github/license/nateshmbhat/pyttsx3?style=plastic)](https://github.com/nateshmbhat/pyttsx3) [![](https://img.shields.io/pypi/v/pyttsx3.svg?style=plastic)](https://pypi.org/project/pyttsx3/) [![](https://img.shields.io/github/languages/top/nateshmbhat/pyttsx3.svg?style=plastic)](https://github.com/nateshmbhat/pyttsx3) [![](https://img.shields.io/badge/author-nateshmbhat-green.svg)](https://github.com/nateshmbhat) + + +`RLVoice` is a text-to-speech conversion library in Python. Unlike alternative libraries, **it works offline**. + +## Installation : + + + pip install rlvoice + +> If you get installation errors , make sure you first upgrade your wheel version using : +`pip install --upgrade wheel` + +### Linux installation requirements : + ++ If you are on a linux system and if the voice output is not working , then : + + Install espeak , ffmpeg and libespeak1 as shown below: + + ``` + sudo apt update && sudo apt install espeak ffmpeg libespeak1 + ``` + + +## Features : + +- ✨Fully **OFFLINE** text to speech conversion +- 🎈 Choose among different voices installed in your system +- 🎛 Control speed/rate of speech +- 🎚 Tweak Volume +- 📀 Save the speech audio as a file +- ❤️ Simple, powerful, & intuitive API + + +## Usage : + +```python3 +import rlvoice + +engine = rlvoice.init() +engine.say("I will speak this text") +engine.runAndWait() +``` + +**Single line usage with speak function with default options** + +```python3 +import rlvoice + +rlvoice.speak("I will speak this text") +``` + + +**Changing Voice , Rate and Volume :** + +```python3 +import rlvoice + +engine = rlvoice.init() # object creation + +""" RATE""" +rate = engine.getProperty('rate') # getting details of current speaking rate +print(rate) # printing current voice rate +engine.setProperty('rate', 125) # setting up new voice rate + +"""VOLUME""" +volume = engine.getProperty('volume') # getting to know current volume level (min=0 and max=1) +print(volume) # printing current volume level +engine.setProperty('volume', 1.0) # setting up volume level between 0 and 1 + +"""VOICE""" +voices = engine.getProperty('voices') # getting details of current voice +# engine.setProperty('voice', voices[0].id) #changing index, changes voices. o for male +engine.setProperty('voice', voices[1].id) # changing index, changes voices. 1 for female + +engine.say("Hello World!") +engine.say('My current speaking rate is ' + str(rate)) +engine.runAndWait() +engine.stop() + +"""Saving Voice to a file""" +# On linux make sure that 'espeak' and 'ffmpeg' are installed +engine.save_to_file('Hello World', 'test.mp3') +engine.runAndWait() + +``` + + +Full documentation is located in the ```docs``` folder. + + +#### Included TTS engines: + +* sapi5 +* nsss +* espeak + +Feel free to wrap another text-to-speech engine for use with ``rlvoice``. + +### Project Links : + +* PyPI (https://pypi.python.org) +* GitHub (https://github.com/Akul-AI/rlvoice) diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..ee31cb7 --- /dev/null +++ b/README.rst @@ -0,0 +1,90 @@ +***************************************************** +pyttsx3 (offline TTS for Python 3) +***************************************************** + +``pyttsx3`` is a text-to-speech conversion library in Python. Unlike alternative libraries, it works offline, and is compatible with both Python 2 and 3. + +Installation +************ +:: + + pip install pyttsx3 + + +> If you get installation errors , make sure you first upgrade your wheel version using : +`pip install --upgrade wheel` + +**Linux installation requirements :** +##################################### + ++ If you are on a linux system and if the voice output is not working , then : + +Install espeak , ffmpeg and libespeak1 as shown below: + +:: + + sudo apt update && sudo apt install espeak ffmpeg libespeak1 + + +Usage : +************ +:: + + import pyttsx3 + engine = pyttsx3.init() + engine.say("I will speak this text") + engine.runAndWait() + + +**Changing Voice , Rate and Volume :** + +:: + + import pyttsx3 + engine = pyttsx3.init() # object creation + + """ RATE""" + rate = engine.getProperty('rate') # getting details of current speaking rate + print (rate) #printing current voice rate + engine.setProperty('rate', 125) # setting up new voice rate + + + """VOLUME""" + volume = engine.getProperty('volume') #getting to know current volume level (min=0 and max=1) + print (volume) #printing current volume level + engine.setProperty('volume',1.0) # setting up volume level between 0 and 1 + + """VOICE""" + voices = engine.getProperty('voices') #getting details of current voice + #engine.setProperty('voice', voices[0].id) #changing index, changes voices. o for male + engine.setProperty('voice', voices[1].id) #changing index, changes voices. 1 for female + + engine.say("Hello World!") + engine.say('My current speaking rate is ' + str(rate)) + engine.runAndWait() + engine.stop() + + """Saving Voice to a file""" + # On linux make sure that 'espeak' and 'ffmpeg' are installed + engine.save_to_file('Hello World', 'test.mp3') + engine.runAndWait() + + +**Full documentation of the Library** +##################################### + +See the included docs folder + +Included TTS engines: +********************* +* sapi5 +* nsss +* espeak + +Feel free to wrap another text-to-speech engine for use with ``pyttsx3``. + +Project Links: +************** + +* PyPI (https://pypi.python.org) +* GitHub (https://github.com/Akul-AI/rlvoice) diff --git a/docs/drivers.rst b/docs/drivers.rst new file mode 100644 index 0000000..cb9ace9 --- /dev/null +++ b/docs/drivers.rst @@ -0,0 +1,102 @@ +Implementing drivers +-------------------- + +You can implement new drivers for the :mod:`pyttsx3.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:`pyttsx3.driver.DriverProxy` instance provided by the :class:`pyttsx3.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:: pyttsx3.drivers + :synopsis: The package containing the available driver implementations + +.. function:: buildDriver(proxy : pyttsx3.driver.DriverProxy) -> pyttsx3.drivers.DriverDelegate + + Instantiates delegate subclass declared in this module. + + :param proxy: Proxy instance provided by a :class:`pyttsx3.Engine` instance. + +.. class:: DriverDelegate + + .. note:: The :class:`DriverDelegate` class is not actually declared in :mod:`pyttsx3.drivers` and cannot serve as a base class. It is only here for the purpose of documenting the interface all drivers must implement. + + .. method:: __init__(proxy : pyttsx3.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:`pyttsx3.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:`pyttsx3.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:`pyttsx3.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:`pyttsx3.Engine.setProperty` documentation must be supported. After setting the property, the driver must invoke :meth:`pyttsx3.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:`pyttsx3.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:`pyttsx3.driver.DriverProxy.setBusy` with value :const:`False` to pump the command queue. + +The DriverProxy interface +~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. module:: pyttsx3.driver + :synopsis: The module containing the driver proxy implementation + +The :func:`pyttsx3.drivers.buildDriver` factory receives an instance of a :class:`DriverProxy` class and provides it to the :class:`pyttsx3.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:`pyttsx3.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 diff --git a/docs/engine.rst b/docs/engine.rst new file mode 100644 index 0000000..abec9d4 --- /dev/null +++ b/docs/engine.rst @@ -0,0 +1,323 @@ +.. module:: pyttsx3 + :synopsis: The root pyttsx3 package defining the engine factory function + +Using pyttsx3 +------------ + +An application invokes the :func:`pyttsx3.init` factory function to get a reference to a :class:`pyttsx3.Engine` instance. During construction, the engine initializes a :class:`pyttsx3.driver.DriverProxy` object responsible for loading a speech engine driver implementation from the :mod:`pyttsx3.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]) -> pyttsx3.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:`pyttsx3.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:: pyttsx3.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:`pyttsx3.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 pyttsx3 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:: pyttsx3.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:`pyttsx3.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 pyttsx3 + engine = pyttsx3.init() + engine.say('Sally sells seashells by the seashore.') + engine.say('The quick brown fox jumped over the lazy dog.') + engine.runAndWait() + + +Saving voice to a file +###################### + +.. sourcecode:: python + + import pyttsx3 + engine = pyttsx3.init() + engine.save_to_file('Hello World' , 'test.mp3') + engine.runAndWait() + + + +Listening for events +#################### + +.. sourcecode:: python + + import pyttsx3 + 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 = pyttsx3.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 pyttsx3 + def onWord(name, location, length): + print 'word', name, location, length + if location > 10: + engine.stop() + engine = pyttsx3.init() + engine.connect('started-word', onWord) + engine.say('The quick brown fox jumped over the lazy dog.') + engine.runAndWait() + +Changing voices +############### + +.. sourcecode:: python + + engine = pyttsx3.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 = pyttsx3.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 = pyttsx3.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 = pyttsx3.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 = pyttsx3.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 = pyttsx3.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() \ No newline at end of file diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..6876369 --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,26 @@ +================================== +pyttsx3 - Text-to-speech x-platform +================================== + +This documentation describes the pyttsx3 Python package v |release| and was rendered on |today|. + +.. rubric:: Table of Contents + +.. toctree:: + :maxdepth: 2 + + support + engine + drivers + + + +.. rubric:: Project Links + +* `Project home page at GitHub`__ +* `Package listing in PyPI`__ +* `Documentation at ReadTheDocs`__ + +__ https://github.com/nateshmbhat/pyttsx3 +__ http://pypi.python.org/pypi/pyttsx3 +__ https://pyttsx3.readthedocs.org/ \ No newline at end of file diff --git a/docs/install.rst b/docs/install.rst new file mode 100644 index 0000000..26e5187 --- /dev/null +++ b/docs/install.rst @@ -0,0 +1,93 @@ +Installing pyttsx3 +----------------- + +Tested versions +~~~~~~~~~~~~~~~ + +Version |version| of pyttsx3 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:`pyttsx3.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 pyttsx3 in the system site-packages folder. + +On Windows +########## + +First install the `pywin32-extensions `_ package using its Windows installer. Then use pip to install pyttsx3. + +.. code-block:: bash + + $ pip install pyttsx3 + +On OSX or Linux +############### + +.. code-block:: bash + + $ sudo pip install pyttsx3 + +Using pip to install in a virtualenv +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If you have virtualenv_ installed with pip_, you can use pip to install a copy of pyttsx3 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 pyttsx3. + +.. 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 pyttsx3 + +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 pyttsx3 + ... + Successfully installed pyttsx3 + Cleaning up... + +On Linux +######## + +pyttsx3 requires no Python dependencies on Linux. You can cut-off the pyttsx3 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 pyttsx3 + ... + Successfully installed pyttsx3 + Cleaning up... + + +.. _espeak: http://espeak.sourceforge.net/ +.. _virtualenv: https://pypi.python.org/pypi/virtualenv/1.10.1 +.. _pip: https://pypi.python.org/pypi/pip +.. _ffmpeg: https://www.ffmpeg.org/ diff --git a/docs/support.rst b/docs/support.rst new file mode 100644 index 0000000..028edb6 --- /dev/null +++ b/docs/support.rst @@ -0,0 +1,10 @@ +Supported synthesizers +---------------------- + +Version |version| of pyttsx3 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 and Windows Vista and Windows 8,8.1 , 10 +* NSSpeechSynthesizer on Mac OS X 10.5 (Leopard) and 10.6 (Snow Leopard) +* espeak on Ubuntu Desktop Edition 8.10 (Intrepid), 9.04 (Jaunty), and 9.10 (Karmic) + +The :func:`pyttsx3.init` documentation explains how to select a specific synthesizer by name as well as the default for each platform. \ No newline at end of file diff --git a/example/main.py b/example/main.py new file mode 100644 index 0000000..8aac3a6 --- /dev/null +++ b/example/main.py @@ -0,0 +1,34 @@ +import rlvoice +engine = rlvoice.init() # object creation + +""" RATE""" +rate = engine.getProperty('rate') # getting details of current speaking rate +print (rate) #printing current voice rate +engine.setProperty('rate', 125) # setting up new voice rate + + +"""VOLUME""" +volume = engine.getProperty('volume') #getting to know current volume level (min=0 and max=1) +print (volume) #printing current volume level +engine.setProperty('volume',1.0) # setting up volume level between 0 and 1 + +"""VOICE""" +voices = engine.getProperty('voices') #getting details of current voice +#engine.setProperty('voice', voices[0].id) #changing index, changes voices. o for male +engine.setProperty('voice', voices[1].id) #changing index, changes voices. 1 for female + +"""PITCH""" +pitch = engine.getProperty('pitch') #Get current pitch value +print(pitch) #Print current pitch value +engine.setProperty('pitch', 75) #Set the pitch (default 50) to 75 out of 100 + +engine.say("Hello World!") +engine.say('My current speaking rate is ' + str(rate)) +engine.runAndWait() +engine.stop() + + +"""Saving Voice to a file""" +# On linux make sure that 'espeak' and 'ffmpeg' are installed +engine.save_to_file('Hello World', 'test.mp3') +engine.runAndWait() \ No newline at end of file diff --git a/example/repeatvoice.py b/example/repeatvoice.py new file mode 100644 index 0000000..27d123c --- /dev/null +++ b/example/repeatvoice.py @@ -0,0 +1,22 @@ +import rlvoice # rlvoice is a text-to-speech conversion library in Python +import speech_recognition as s #Google Speech API in Python + +#Functional programming Model + +def text_to_speech(text): + #engine connects us to hardware in this case + eng= rlvoice.init() + #Engine created + eng.say(text) + #Runs for small duration of time ohterwise we may not be able to hear + eng.runAndWait() + + +def speech_to_text(): + r=s.Recognizer()# an object r which recognises the voice + with s.Microphone() as source: + #when using with statement. The with statement itself ensures proper acquisition and release of resources + print(r.recognize_google(audio)) + text_to_speech(r.recognize_google(audio)) + +speech_to_text() diff --git a/example/voicefile.mp3 b/example/voicefile.mp3 new file mode 100644 index 0000000..a5ec83f Binary files /dev/null and b/example/voicefile.mp3 differ diff --git a/makefile b/makefile new file mode 100644 index 0000000..0bf7b97 --- /dev/null +++ b/makefile @@ -0,0 +1,13 @@ +clean: + rm -rf dist/ + rm -rf build/ +build: + pip3 install wheel + python3 setup.py bdist_wheel +upload: + pip3 install twine + python3 -m twine upload dist/*.whl +deploy: + make clean + make build + make upload diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..aedc347 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +# see setup.py +# pyttsx3 only requires `espeak` driver/library which is system-dependent + +### Ubuntu +#$ sudo apt install espeak ffmpeg + +### Mac OS X (os.platform == 'Darwin') +# pyobjc>=2.4 + +### Windows (os.platform == 'Windows') +# pypiwin32 + diff --git a/rlvoice/__init__.py b/rlvoice/__init__.py new file mode 100644 index 0000000..0e1c3d1 --- /dev/null +++ b/rlvoice/__init__.py @@ -0,0 +1,32 @@ +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 + + +def speak(text): + engine = init() + engine.say(text) + engine.runAndWait() + + diff --git a/rlvoice/driver.py b/rlvoice/driver.py new file mode 100644 index 0000000..708efab --- /dev/null +++ b/rlvoice/driver.py @@ -0,0 +1,223 @@ +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 = 'rlvoice.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 save_to_file(self, text, filename, 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.save_to_file, (text, filename), name) + + 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 diff --git a/rlvoice/drivers/__init__.py b/rlvoice/drivers/__init__.py new file mode 100644 index 0000000..687eccf --- /dev/null +++ b/rlvoice/drivers/__init__.py @@ -0,0 +1,23 @@ + +''' +Utility functions to help with Python 2/3 compatibility +''' +from .. import six + +def toUtf8(value): + ''' + Takes in a value and converts it to a text (unicode) type. Then decodes that + type to a byte array encoded in utf-8. In 2.X the resulting object will be a + str and in 3.X the resulting object will be bytes. In both 2.X and 3.X any + object can be passed in and the object's __str__ will be used (or __repr__ if + __str__ is not defined) if the object is not already a text type. + ''' + return six.text_type(value).encode('utf-8') + +def fromUtf8(value): + ''' + Takes in a byte array encoded as utf-8 and returns a text (unicode) type. In + 2.X we expect a str type and return a unicde type. In 3.X we expect a bytes + type and return a str type. + ''' + return value.decode('utf-8') diff --git a/rlvoice/drivers/_espeak.py b/rlvoice/drivers/_espeak.py new file mode 100644 index 0000000..80f3acb --- /dev/null +++ b/rlvoice/drivers/_espeak.py @@ -0,0 +1,495 @@ + + +from __future__ import print_function +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, Structure, Union, c_wchar, c_ubyte, c_ulong, byref +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 = None + +def load_linux_ep(): + global dll + try: dll = cdll.LoadLibrary('libespeak.so.1') + except Exception as e: return False + else: return True + +def load_linux_epng(): + global dll + try: dll = cdll.LoadLibrary('libespeak-ng.so.1') + except Exception as e: return False + else: return True + +def load_linux_epng2(): + global dll + try: dll = cdll.LoadLibrary('/usr/local/lib/libespeak-ng.so.1') + except Exception as e: return False + else: return True + +def load_windows_epng1(): + global dll + try: dll = cdll.LoadLibrary('libespeak-ng.dll') + except Exception as e: return False + else: return True + +def load_windows_epng2(): + global dll + try: dll = cdll.LoadLibrary('C:\\Program Files\\eSpeak NG\\libespeak-ng.dll') + except Exception as e: return False + else: return True + +def load_windows_epng3(): + global dll + try: dll = cdll.LoadLibrary('C:\\Program Files (x86)\\eSpeak NG\\libespeak-ng.dll') + except Exception as e: return False + else: return True + +try: + load_linux_ep() or load_linux_epng() or load_linux_epng2() or load_windows_epng1() or load_windows_epng2() or load_windows_epng3() +except Exception as exp: + print("Exception: " + str(exp) + "\n") + print("This means you probably do not have eSpeak or eSpeak-ng installed!") + import sys + sys.exit() + +# 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