diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..dd84ea7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,38 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. iOS] + - Browser [e.g. chrome, safari] + - Version [e.g. 22] + +**Smartphone (please complete the following information):** + - Device: [e.g. iPhone6] + - OS: [e.g. iOS8.1] + - Browser [e.g. stock browser, safari] + - Version [e.g. 22] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/custom.md b/.github/ISSUE_TEMPLATE/custom.md new file mode 100644 index 0000000..48d5f81 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/custom.md @@ -0,0 +1,10 @@ +--- +name: Custom issue template +about: Describe this issue template's purpose here. +title: '' +labels: '' +assignees: '' + +--- + + diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..bbcbbe7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/workflows/python-macos-say_test.yml b/.github/workflows/python-macos-say_test.yml new file mode 100644 index 0000000..c9823ba --- /dev/null +++ b/.github/workflows/python-macos-say_test.yml @@ -0,0 +1,36 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: Python say test + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: macos-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.10 + uses: actions/setup-python@v3 + with: + python-version: "3.10" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pyobjc==9.0.1 + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: pyttsx4_test2 + run: | + echo "start" + python -X faulthandler ./pyttsx4_say_test.py + echo "finish" + diff --git a/.github/workflows/python-macos-test.yml b/.github/workflows/python-macos-test.yml new file mode 100644 index 0000000..e557013 --- /dev/null +++ b/.github/workflows/python-macos-test.yml @@ -0,0 +1,40 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: Python application + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: macos-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.10 + uses: actions/setup-python@v3 + with: + python-version: "3.10" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pyobjc + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: pyttsx4_test1 + run: | + echo "start" + python -X faulthandler ./pyttsx4_test.py + echo "finish" + - name: Archive production artifacts + uses: actions/upload-artifact@v3 + with: + name: save_to_file + path: . diff --git a/.github/workflows/pythonpublish.yml b/.github/workflows/pythonpublish.yml deleted file mode 100644 index cd5f1d0..0000000 --- a/.github/workflows/pythonpublish.yml +++ /dev/null @@ -1,31 +0,0 @@ -# This workflows will upload a Python Package using Twine when a release is created -# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries - -name: Build and upload Python Package - -on: - release: - types: [created] - -jobs: - deploy: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: Set up Python - uses: actions/setup-python@v1 - with: - python-version: '3.x' - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install setuptools wheel twine - - name: Build and publish - env: - TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} - TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} - run: | - python setup.py sdist bdist_wheel - twine upload dist/*.whl diff --git a/.gitignore b/.gitignore index 1eb6fff..46a0334 100644 --- a/.gitignore +++ b/.gitignore @@ -105,3 +105,5 @@ docs/make.bat # vscode .vscode/ +.idea/* +old1/* diff --git a/README.md b/README.md index c0e3eba..f3cb249 100644 --- a/README.md +++ b/README.md @@ -1,97 +1,192 @@ -

- -

-

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) +[![Downloads](https://static.pepy.tech/personalized-badge/pyttsx4?period=total&units=international_system&left_color=black&right_color=green&left_text=downloads)](https://pepy.tech/project/pyttsx4) +[![Downloads](https://static.pepy.tech/personalized-badge/pyttsx4?period=month&units=international_system&left_color=black&right_color=green&left_text=downloads/month)](https://pepy.tech/project/pyttsx4) -`pyttsx3` is a text-to-speech conversion library in Python. Unlike alternative libraries, **it works offline**. +the code is mostly from pyttsx3. -Buy me a coffee 😇Buy me a coffee 😇 +only because the repo pyttsx3 does not update for years and some new feature i want is not here, i cloned this repo. -## Installation : +feature: +# supported engines: - pip install pyttsx3 +``` +1 nsss +2 sapi5 +3 espeak +4 coqui_ai_tts +``` -> If you get installation errors , make sure you first upgrade your wheel version using : -`pip install --upgrade wheel` +# basic features: -### Linux installation requirements : +1 say +``` +engine = pyttsx4.init() +engine.say('this is an english text to voice test.') +engine.runAndWait() +``` +2 save to file -+ If you are on a linux system and if the voice output is not working , then : +``` +import pyttsx4 - Install espeak , ffmpeg and libespeak1 as shown below: +engine = pyttsx4.init() +engine.save_to_file('i am Hello World, i am a programmer. i think life is short.', 'test1.wav') +engine.runAndWait() - ``` - sudo apt update && sudo apt install espeak ffmpeg libespeak1 - ``` +``` -## Features : +# extra 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 +1 memory support for sapi5, nsss, espeak. +NOTE: the memory is just raw adc data, wav header has to be added if you want to save to wav file. +``` +import pyttsx4 +from io import BytesIO +from pydub import AudioSegment +from pydub.playback import play +import os +import sys + +engine = pyttsx4.init() +b = BytesIO() +engine.save_to_file('i am Hello World', b) +engine.runAndWait() +#the bs is raw data of the audio. +bs=b.getvalue() +# add an wav file format header +b=bytes(b'RIFF')+ (len(bs)+38).to_bytes(4, byteorder='little')+b'WAVEfmt\x20\x12\x00\x00' \ + b'\x00\x01\x00\x01\x00' \ + b'\x22\x56\x00\x00\x44\xac\x00\x00' +\ + b'\x02\x00\x10\x00\x00\x00data' +(len(bs)).to_bytes(4, byteorder='little')+bs +# changed to BytesIO +b=BytesIO(b) +audio = AudioSegment.from_file(b, format="wav") +play(audio) + +sys.exit(0) +``` -## Usage : -```python3 -import pyttsx3 -engine = pyttsx3.init() -engine.say("I will speak this text") +2 cloning voice +``` +# only coqui_ai_tts engine support cloning voice. +engine = pyttsx4.init('coqui_ai_tts') +engine.setProperty('speaker_wav', './docs/i_have_a_dream_10s.wav') + +engine.say('this is an english text to voice test, listen it carefully and tell who i am.') engine.runAndWait() + + ``` -**Single line usage with speak function with default options** +voice clone test1: -```python3 -import pyttsx3 -pyttsx3.speak("I will speak this text") +![speaker_wav_test_1](./docs/i_have_a_dream_10s.wav) +![the output1](./docs/test_mtk.wav) + + +voice clone test2: + +![speaker_wav_test_2](./docs/the_ballot_or_the_bullet_15s.wav) +![the output2](./docs/test_mx.wav) + + + +---------------- + + + + + + + +the changelog: + +1. add memory support for sapi5 +2. add memory support for espeak(espeak is not tested). + eg: + +``` +b = BytesIO() +engine.save_to_file('i am Hello World', b) +engine.runAndWait() ``` - -**Changing Voice , Rate and Volume :** +3. fix VoiceAge key error + -```python3 -import pyttsx3 -engine = pyttsx3.init() # object creation +4. fix for sapi save_to_file when it run on machine without outputsream device. -""" 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 +5. fix save_to_file does not work on mac os ventura error. --3.0.6 +6. add pitch support for sapi5(not tested yet). --3.0.8 -"""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 +7. fix nsss engine: Import super from objc to fix AttributeError by @matt-oakes. -"""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 +8. add tts support: + deep-learning text to voice backend: -engine.say("Hello World!") -engine.say('My current speaking rate is ' + str(rate)) +just say: +``` +engine = pyttsx4.init('coqui_ai_tts') +engine.say('this is an english text to voice test.') engine.runAndWait() -engine.stop() +``` +cloning someones voice: -"""Saving Voice to a file""" -# On linux make sure that 'espeak' and 'ffmpeg' are installed -engine.save_to_file('Hello World', 'test.mp3') +``` +engine = pyttsx4.init('coqui_ai_tts') +engine.setProperty('speaker_wav', './someones_voice.wav') + +engine.say('this is an english text to voice test.') engine.runAndWait() ``` +demo output: + +![test2](./docs/test2.wav) + + + + + +NOTE: + +if save_to_file with BytesIO, there is no wav header in the BytesIO. +the format of the bytes data is that 2-bytes = one sample. + +if you want to add a header, the format of the data is: +1-channel. 2-bytes of sample width. 22050-framerate. + +how to add a wav header in memory:https://github.com/Jiangshan00001/pyttsx4/issues/2 + + +# how to use: + +install: +``` +pip install pyttsx4 +``` + +use: + +``` +import pyttsx4 +engine = pyttsx4.init() +``` + +the other usage is the same as the pyttsx3 + + + +---------------------- @@ -106,10 +201,12 @@ https://pyttsx3.readthedocs.io/en/latest/ * nsss * espeak -Feel free to wrap another text-to-speech engine for use with ``pyttsx3``. +Feel free to wrap another text-to-speech engine for use with ``pyttsx4``. ### Project Links : * PyPI (https://pypi.python.org) -* GitHub (https://github.com/nateshmbhat/pyttsx3) +* GitHub (https://github.com/Jiangshan00001/pyttsx4) * Full Documentation (https://pyttsx3.readthedocs.org) + + diff --git a/README.rst b/README.rst index d38da06..d8410f5 100644 --- a/README.rst +++ b/README.rst @@ -1,92 +1,34 @@ -***************************************************** -pyttsx3 (offline TTS for Python 3) -***************************************************** +``pyttsx4`` is a text-to-speech conversion library in Python. -``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 -************ -:: +it is all from pyttsx3: - 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 +* GitHub (https://github.com/nateshmbhat/pyttsx3) +* Full Documentation (https://pyttsx3.readthedocs.org) - """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 +the changelog and update is listed below: - 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() +1. add memory support for sapi5 +2. add memory support for espeak(espeak is not tested). + eg: + +``` +b = BytesIO() +engine.save_to_file('i am Hello World', b) +engine.runAndWait() +``` +3. fix VoiceAge key error -**Full documentation of the Library** -##################################### -https://pyttsx3.readthedocs.io/en/latest/ +4. fix for sapi save_to_file when it run on machine without outputsream device. +5. fix save_to_file does not work on mac os ventura error. --3.0.6 -Included TTS engines: -********************* -* sapi5 -* nsss -* espeak +6. add pitch support for sapi5(not tested yet). --3.0.8 -Feel free to wrap another text-to-speech engine for use with ``pyttsx3``. -Project Links: -************** -* PyPI (https://pypi.python.org) -* GitHub (https://github.com/nateshmbhat/pyttsx3) -* Full Documentation (https://pyttsx3.readthedocs.org) diff --git a/docs/engine.rst b/docs/engine.rst index abec9d4..cde80ba 100644 --- a/docs/engine.rst +++ b/docs/engine.rst @@ -61,7 +61,7 @@ The Engine interface .. describe:: finished-utterance - Fired when the engine finishes speaking an utterance. The associated callback must have the folowing signature. + Fired when the engine finishes speaking an utterance. The associated callback must have the following signature. .. function:: onFinishUtterance(name : string, completed : bool) -> None @@ -320,4 +320,4 @@ Using an external event loop engine.startLoop(False) # engine.iterate() must be called inside externalLoop() externalLoop() - engine.endLoop() \ No newline at end of file + engine.endLoop() diff --git a/docs/i_have_a_dream_10s.wav b/docs/i_have_a_dream_10s.wav new file mode 100644 index 0000000..ba89c83 Binary files /dev/null and b/docs/i_have_a_dream_10s.wav differ diff --git a/docs/test2.wav b/docs/test2.wav new file mode 100644 index 0000000..0f9584e Binary files /dev/null and b/docs/test2.wav differ diff --git a/docs/test_mtk.wav b/docs/test_mtk.wav new file mode 100644 index 0000000..7b61f02 Binary files /dev/null and b/docs/test_mtk.wav differ diff --git a/docs/test_mx.wav b/docs/test_mx.wav new file mode 100644 index 0000000..4199632 Binary files /dev/null and b/docs/test_mx.wav differ diff --git a/docs/the_ballot_or_the_bullet_15s.wav b/docs/the_ballot_or_the_bullet_15s.wav new file mode 100644 index 0000000..0fbecde Binary files /dev/null and b/docs/the_ballot_or_the_bullet_15s.wav differ diff --git a/pyttsx3/six.py b/pyttsx3/six.py deleted file mode 100644 index 575bd43..0000000 --- a/pyttsx3/six.py +++ /dev/null @@ -1,836 +0,0 @@ - - -from __future__ import absolute_import - -import functools -import itertools -import operator -import sys -import types - -__author__ = "Benjamin Peterson " -__version__ = "1.9.0" - - -# Useful for very coarse version differentiation. -PY2 = sys.version_info[0] == 2 -PY3 = sys.version_info[0] == 3 - -if PY3: - string_types = str, - integer_types = int, - class_types = type, - text_type = str - binary_type = bytes - - MAXSIZE = sys.maxsize -else: - string_types = basestring, - integer_types = (int, long) - class_types = (type, types.ClassType) - text_type = unicode - binary_type = str - - if sys.platform.startswith("java"): - # Jython always uses 32 bits. - MAXSIZE = int((1 << 31) - 1) - else: - # It's possible to have sizeof(long) != sizeof(Py_ssize_t). - class X(object): - def __len__(self): - return 1 << 31 - try: - len(X()) - except OverflowError: - # 32-bit - MAXSIZE = int((1 << 31) - 1) - else: - # 64-bit - MAXSIZE = int((1 << 63) - 1) - del X - - -def _add_doc(func, doc): - """Add documentation to a function.""" - func.__doc__ = doc - - -def _import_module(name): - """Import module, returning the module after the last dot.""" - __import__(name) - return sys.modules[name] - - -class _LazyDescr(object): - - def __init__(self, name): - self.name = name - - def __get__(self, obj, tp): - result = self._resolve() - setattr(obj, self.name, result) # Invokes __set__. - try: - # This is a bit ugly, but it avoids running this again by - # removing this descriptor. - delattr(obj.__class__, self.name) - except AttributeError: - pass - return result - - -class MovedModule(_LazyDescr): - - def __init__(self, name, old, new=None): - super(MovedModule, self).__init__(name) - if PY3: - if new is None: - new = name - self.mod = new - else: - self.mod = old - - def _resolve(self): - return _import_module(self.mod) - - def __getattr__(self, attr): - _module = self._resolve() - value = getattr(_module, attr) - setattr(self, attr, value) - return value - - -class _LazyModule(types.ModuleType): - - def __init__(self, name): - super(_LazyModule, self).__init__(name) - self.__doc__ = self.__class__.__doc__ - - def __dir__(self): - attrs = ["__doc__", "__name__"] - attrs += [attr.name for attr in self._moved_attributes] - return attrs - - # Subclasses should override this - _moved_attributes = [] - - -class MovedAttribute(_LazyDescr): - - def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None): - super(MovedAttribute, self).__init__(name) - if PY3: - if new_mod is None: - new_mod = name - self.mod = new_mod - if new_attr is None: - if old_attr is None: - new_attr = name - else: - new_attr = old_attr - self.attr = new_attr - else: - self.mod = old_mod - if old_attr is None: - old_attr = name - self.attr = old_attr - - def _resolve(self): - module = _import_module(self.mod) - return getattr(module, self.attr) - - -class _SixMetaPathImporter(object): - """ - A meta path importer to import six.moves and its submodules. - - This class implements a PEP302 finder and loader. It should be compatible - with Python 2.5 and all existing versions of Python3 - """ - - def __init__(self, six_module_name): - self.name = six_module_name - self.known_modules = {} - - def _add_module(self, mod, *fullnames): - for fullname in fullnames: - self.known_modules[self.name + "." + fullname] = mod - - def _get_module(self, fullname): - return self.known_modules[self.name + "." + fullname] - - def find_module(self, fullname, path=None): - if fullname in self.known_modules: - return self - return None - - def __get_module(self, fullname): - try: - return self.known_modules[fullname] - except KeyError: - raise ImportError("This loader does not know module " + fullname) - - def load_module(self, fullname): - try: - # in case of a reload - return sys.modules[fullname] - except KeyError: - pass - mod = self.__get_module(fullname) - if isinstance(mod, MovedModule): - mod = mod._resolve() - else: - mod.__loader__ = self - sys.modules[fullname] = mod - return mod - - def is_package(self, fullname): - """ - Return true, if the named module is a package. - - We need this method to get correct spec objects with - Python 3.4 (see PEP451) - """ - return hasattr(self.__get_module(fullname), "__path__") - - def get_code(self, fullname): - """Return None - - Required, if is_package is implemented""" - self.__get_module(fullname) # eventually raises ImportError - return None - get_source = get_code # same as get_code - - -_importer = _SixMetaPathImporter(__name__) - - -class _MovedItems(_LazyModule): - """Lazy loading of moved objects""" - __path__ = [] # mark as package - - -_moved_attributes = [ - MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"), - MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"), - MovedAttribute("filterfalse", "itertools", "itertools", - "ifilterfalse", "filterfalse"), - MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"), - MovedAttribute("intern", "__builtin__", "sys"), - MovedAttribute("map", "itertools", "builtins", "imap", "map"), - MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"), - MovedAttribute("reload_module", "__builtin__", "imp", "reload"), - MovedAttribute("reduce", "__builtin__", "functools"), - MovedAttribute("shlex_quote", "pipes", "shlex", "quote"), - MovedAttribute("StringIO", "StringIO", "io"), - MovedAttribute("UserDict", "UserDict", "collections"), - MovedAttribute("UserList", "UserList", "collections"), - MovedAttribute("UserString", "UserString", "collections"), - MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"), - MovedAttribute("zip", "itertools", "builtins", "izip", "zip"), - MovedAttribute("zip_longest", "itertools", "itertools", - "izip_longest", "zip_longest"), - - MovedModule("builtins", "__builtin__"), - MovedModule("configparser", "ConfigParser"), - MovedModule("copyreg", "copy_reg"), - MovedModule("dbm_gnu", "gdbm", "dbm.gnu"), - MovedModule("_dummy_thread", "dummy_thread", "_dummy_thread"), - MovedModule("http_cookiejar", "cookielib", "http.cookiejar"), - MovedModule("http_cookies", "Cookie", "http.cookies"), - MovedModule("html_entities", "htmlentitydefs", "html.entities"), - MovedModule("html_parser", "HTMLParser", "html.parser"), - MovedModule("http_client", "httplib", "http.client"), - MovedModule("email_mime_multipart", "email.MIMEMultipart", - "email.mime.multipart"), - MovedModule("email_mime_nonmultipart", - "email.MIMENonMultipart", "email.mime.nonmultipart"), - MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"), - MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"), - MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"), - MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"), - MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"), - MovedModule("cPickle", "cPickle", "pickle"), - MovedModule("queue", "Queue"), - MovedModule("reprlib", "repr"), - MovedModule("socketserver", "SocketServer"), - MovedModule("_thread", "thread", "_thread"), - MovedModule("tkinter", "Tkinter"), - MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"), - MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"), - MovedModule("tkinter_scrolledtext", "ScrolledText", - "tkinter.scrolledtext"), - MovedModule("tkinter_simpledialog", "SimpleDialog", - "tkinter.simpledialog"), - MovedModule("tkinter_tix", "Tix", "tkinter.tix"), - MovedModule("tkinter_ttk", "ttk", "tkinter.ttk"), - MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"), - MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"), - MovedModule("tkinter_colorchooser", "tkColorChooser", - "tkinter.colorchooser"), - MovedModule("tkinter_commondialog", "tkCommonDialog", - "tkinter.commondialog"), - MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"), - MovedModule("tkinter_font", "tkFont", "tkinter.font"), - MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"), - MovedModule("tkinter_tksimpledialog", "tkSimpleDialog", - "tkinter.simpledialog"), - MovedModule("urllib_parse", __name__ + - ".moves.urllib_parse", "urllib.parse"), - MovedModule("urllib_error", __name__ + - ".moves.urllib_error", "urllib.error"), - MovedModule("urllib", __name__ + ".moves.urllib", - __name__ + ".moves.urllib"), - MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"), - MovedModule("xmlrpc_client", "xmlrpclib", "xmlrpc.client"), - MovedModule("xmlrpc_server", "SimpleXMLRPCServer", "xmlrpc.server"), - MovedModule("winreg", "_winreg"), -] -for attr in _moved_attributes: - setattr(_MovedItems, attr.name, attr) - if isinstance(attr, MovedModule): - _importer._add_module(attr, "moves." + attr.name) -del attr - -_MovedItems._moved_attributes = _moved_attributes - -moves = _MovedItems(__name__ + ".moves") -_importer._add_module(moves, "moves") - - -class Module_six_moves_urllib_parse(_LazyModule): - """Lazy loading of moved objects in six.moves.urllib_parse""" - - -_urllib_parse_moved_attributes = [ - MovedAttribute("ParseResult", "urlparse", "urllib.parse"), - MovedAttribute("SplitResult", "urlparse", "urllib.parse"), - MovedAttribute("parse_qs", "urlparse", "urllib.parse"), - MovedAttribute("parse_qsl", "urlparse", "urllib.parse"), - MovedAttribute("urldefrag", "urlparse", "urllib.parse"), - MovedAttribute("urljoin", "urlparse", "urllib.parse"), - MovedAttribute("urlparse", "urlparse", "urllib.parse"), - MovedAttribute("urlsplit", "urlparse", "urllib.parse"), - MovedAttribute("urlunparse", "urlparse", "urllib.parse"), - MovedAttribute("urlunsplit", "urlparse", "urllib.parse"), - MovedAttribute("quote", "urllib", "urllib.parse"), - MovedAttribute("quote_plus", "urllib", "urllib.parse"), - MovedAttribute("unquote", "urllib", "urllib.parse"), - MovedAttribute("unquote_plus", "urllib", "urllib.parse"), - MovedAttribute("urlencode", "urllib", "urllib.parse"), - MovedAttribute("splitquery", "urllib", "urllib.parse"), - MovedAttribute("splittag", "urllib", "urllib.parse"), - MovedAttribute("splituser", "urllib", "urllib.parse"), - MovedAttribute("uses_fragment", "urlparse", "urllib.parse"), - MovedAttribute("uses_netloc", "urlparse", "urllib.parse"), - MovedAttribute("uses_params", "urlparse", "urllib.parse"), - MovedAttribute("uses_query", "urlparse", "urllib.parse"), - MovedAttribute("uses_relative", "urlparse", "urllib.parse"), -] -for attr in _urllib_parse_moved_attributes: - setattr(Module_six_moves_urllib_parse, attr.name, attr) -del attr - -Module_six_moves_urllib_parse._moved_attributes = _urllib_parse_moved_attributes - -_importer._add_module(Module_six_moves_urllib_parse(__name__ + ".moves.urllib_parse"), - "moves.urllib_parse", "moves.urllib.parse") - - -class Module_six_moves_urllib_error(_LazyModule): - """Lazy loading of moved objects in six.moves.urllib_error""" - - -_urllib_error_moved_attributes = [ - MovedAttribute("URLError", "urllib2", "urllib.error"), - MovedAttribute("HTTPError", "urllib2", "urllib.error"), - MovedAttribute("ContentTooShortError", "urllib", "urllib.error"), -] -for attr in _urllib_error_moved_attributes: - setattr(Module_six_moves_urllib_error, attr.name, attr) -del attr - -Module_six_moves_urllib_error._moved_attributes = _urllib_error_moved_attributes - -_importer._add_module(Module_six_moves_urllib_error(__name__ + ".moves.urllib.error"), - "moves.urllib_error", "moves.urllib.error") - - -class Module_six_moves_urllib_request(_LazyModule): - """Lazy loading of moved objects in six.moves.urllib_request""" - - -_urllib_request_moved_attributes = [ - MovedAttribute("urlopen", "urllib2", "urllib.request"), - MovedAttribute("install_opener", "urllib2", "urllib.request"), - MovedAttribute("build_opener", "urllib2", "urllib.request"), - MovedAttribute("pathname2url", "urllib", "urllib.request"), - MovedAttribute("url2pathname", "urllib", "urllib.request"), - MovedAttribute("getproxies", "urllib", "urllib.request"), - MovedAttribute("Request", "urllib2", "urllib.request"), - MovedAttribute("OpenerDirector", "urllib2", "urllib.request"), - MovedAttribute("HTTPDefaultErrorHandler", "urllib2", "urllib.request"), - MovedAttribute("HTTPRedirectHandler", "urllib2", "urllib.request"), - MovedAttribute("HTTPCookieProcessor", "urllib2", "urllib.request"), - MovedAttribute("ProxyHandler", "urllib2", "urllib.request"), - MovedAttribute("BaseHandler", "urllib2", "urllib.request"), - MovedAttribute("HTTPPasswordMgr", "urllib2", "urllib.request"), - MovedAttribute("HTTPPasswordMgrWithDefaultRealm", - "urllib2", "urllib.request"), - MovedAttribute("AbstractBasicAuthHandler", "urllib2", "urllib.request"), - MovedAttribute("HTTPBasicAuthHandler", "urllib2", "urllib.request"), - MovedAttribute("ProxyBasicAuthHandler", "urllib2", "urllib.request"), - MovedAttribute("AbstractDigestAuthHandler", "urllib2", "urllib.request"), - MovedAttribute("HTTPDigestAuthHandler", "urllib2", "urllib.request"), - MovedAttribute("ProxyDigestAuthHandler", "urllib2", "urllib.request"), - MovedAttribute("HTTPHandler", "urllib2", "urllib.request"), - MovedAttribute("HTTPSHandler", "urllib2", "urllib.request"), - MovedAttribute("FileHandler", "urllib2", "urllib.request"), - MovedAttribute("FTPHandler", "urllib2", "urllib.request"), - MovedAttribute("CacheFTPHandler", "urllib2", "urllib.request"), - MovedAttribute("UnknownHandler", "urllib2", "urllib.request"), - MovedAttribute("HTTPErrorProcessor", "urllib2", "urllib.request"), - MovedAttribute("urlretrieve", "urllib", "urllib.request"), - MovedAttribute("urlcleanup", "urllib", "urllib.request"), - MovedAttribute("URLopener", "urllib", "urllib.request"), - MovedAttribute("FancyURLopener", "urllib", "urllib.request"), - MovedAttribute("proxy_bypass", "urllib", "urllib.request"), -] -for attr in _urllib_request_moved_attributes: - setattr(Module_six_moves_urllib_request, attr.name, attr) -del attr - -Module_six_moves_urllib_request._moved_attributes = _urllib_request_moved_attributes - -_importer._add_module(Module_six_moves_urllib_request(__name__ + ".moves.urllib.request"), - "moves.urllib_request", "moves.urllib.request") - - -class Module_six_moves_urllib_response(_LazyModule): - """Lazy loading of moved objects in six.moves.urllib_response""" - - -_urllib_response_moved_attributes = [ - MovedAttribute("addbase", "urllib", "urllib.response"), - MovedAttribute("addclosehook", "urllib", "urllib.response"), - MovedAttribute("addinfo", "urllib", "urllib.response"), - MovedAttribute("addinfourl", "urllib", "urllib.response"), -] -for attr in _urllib_response_moved_attributes: - setattr(Module_six_moves_urllib_response, attr.name, attr) -del attr - -Module_six_moves_urllib_response._moved_attributes = _urllib_response_moved_attributes - -_importer._add_module(Module_six_moves_urllib_response(__name__ + ".moves.urllib.response"), - "moves.urllib_response", "moves.urllib.response") - - -class Module_six_moves_urllib_robotparser(_LazyModule): - """Lazy loading of moved objects in six.moves.urllib_robotparser""" - - -_urllib_robotparser_moved_attributes = [ - MovedAttribute("RobotFileParser", "robotparser", "urllib.robotparser"), -] -for attr in _urllib_robotparser_moved_attributes: - setattr(Module_six_moves_urllib_robotparser, attr.name, attr) -del attr - -Module_six_moves_urllib_robotparser._moved_attributes = _urllib_robotparser_moved_attributes - -_importer._add_module(Module_six_moves_urllib_robotparser(__name__ + ".moves.urllib.robotparser"), - "moves.urllib_robotparser", "moves.urllib.robotparser") - - -class Module_six_moves_urllib(types.ModuleType): - """Create a six.moves.urllib namespace that resembles the Python 3 namespace""" - __path__ = [] # mark as package - parse = _importer._get_module("moves.urllib_parse") - error = _importer._get_module("moves.urllib_error") - request = _importer._get_module("moves.urllib_request") - response = _importer._get_module("moves.urllib_response") - robotparser = _importer._get_module("moves.urllib_robotparser") - - def __dir__(self): - return ['parse', 'error', 'request', 'response', 'robotparser'] - - -_importer._add_module(Module_six_moves_urllib(__name__ + ".moves.urllib"), - "moves.urllib") - - -def add_move(move): - """Add an item to six.moves.""" - setattr(_MovedItems, move.name, move) - - -def remove_move(name): - """Remove item from six.moves.""" - try: - delattr(_MovedItems, name) - except AttributeError: - try: - del moves.__dict__[name] - except KeyError: - raise AttributeError("no such move, %r" % (name,)) - - -if PY3: - _meth_func = "__func__" - _meth_self = "__self__" - - _func_closure = "__closure__" - _func_code = "__code__" - _func_defaults = "__defaults__" - _func_globals = "__globals__" -else: - _meth_func = "im_func" - _meth_self = "im_self" - - _func_closure = "func_closure" - _func_code = "func_code" - _func_defaults = "func_defaults" - _func_globals = "func_globals" - - -try: - advance_iterator = next -except NameError: - def advance_iterator(it): - return it.next() -next = advance_iterator - - -try: - callable = callable -except NameError: - def callable(obj): - return any("__call__" in klass.__dict__ for klass in type(obj).__mro__) - - -if PY3: - def get_unbound_function(unbound): - return unbound - - create_bound_method = types.MethodType - - Iterator = object -else: - def get_unbound_function(unbound): - return unbound.im_func - - def create_bound_method(func, obj): - return types.MethodType(func, obj, obj.__class__) - - class Iterator(object): - - def next(self): - return type(self).__next__(self) - - callable = callable -_add_doc(get_unbound_function, - """Get the function out of a possibly unbound function""") - - -get_method_function = operator.attrgetter(_meth_func) -get_method_self = operator.attrgetter(_meth_self) -get_function_closure = operator.attrgetter(_func_closure) -get_function_code = operator.attrgetter(_func_code) -get_function_defaults = operator.attrgetter(_func_defaults) -get_function_globals = operator.attrgetter(_func_globals) - - -if PY3: - def iterkeys(d, **kw): - return iter(d.keys(**kw)) - - def itervalues(d, **kw): - return iter(d.values(**kw)) - - def iteritems(d, **kw): - return iter(d.items(**kw)) - - def iterlists(d, **kw): - return iter(d.lists(**kw)) - - viewkeys = operator.methodcaller("keys") - - viewvalues = operator.methodcaller("values") - - viewitems = operator.methodcaller("items") -else: - def iterkeys(d, **kw): - return iter(d.iterkeys(**kw)) - - def itervalues(d, **kw): - return iter(d.itervalues(**kw)) - - def iteritems(d, **kw): - return iter(d.iteritems(**kw)) - - def iterlists(d, **kw): - return iter(d.iterlists(**kw)) - - viewkeys = operator.methodcaller("viewkeys") - - viewvalues = operator.methodcaller("viewvalues") - - viewitems = operator.methodcaller("viewitems") - -_add_doc(iterkeys, "Return an iterator over the keys of a dictionary.") -_add_doc(itervalues, "Return an iterator over the values of a dictionary.") -_add_doc(iteritems, - "Return an iterator over the (key, value) pairs of a dictionary.") -_add_doc(iterlists, - "Return an iterator over the (key, [values]) pairs of a dictionary.") - - -if PY3: - def b(s): - return s.encode("latin-1") - - def u(s): - return s - unichr = chr - if sys.version_info[1] <= 1: - def int2byte(i): - return bytes((i,)) - else: - # This is about 2x faster than the implementation above on 3.2+ - int2byte = operator.methodcaller("to_bytes", 1, "big") - byte2int = operator.itemgetter(0) - indexbytes = operator.getitem - iterbytes = iter - import io - StringIO = io.StringIO - BytesIO = io.BytesIO - _assertCountEqual = "assertCountEqual" - _assertRaisesRegex = "assertRaisesRegex" - _assertRegex = "assertRegex" -else: - def b(s): - return s - # Workaround for standalone backslash - - def u(s): - return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape") - unichr = unichr - int2byte = chr - - def byte2int(bs): - return ord(bs[0]) - - def indexbytes(buf, i): - return ord(buf[i]) - iterbytes = functools.partial(itertools.imap, ord) - import StringIO - StringIO = BytesIO = StringIO.StringIO - _assertCountEqual = "assertItemsEqual" - _assertRaisesRegex = "assertRaisesRegexp" - _assertRegex = "assertRegexpMatches" -_add_doc(b, """Byte literal""") -_add_doc(u, """Text literal""") - - -def assertCountEqual(self, *args, **kwargs): - return getattr(self, _assertCountEqual)(*args, **kwargs) - - -def assertRaisesRegex(self, *args, **kwargs): - return getattr(self, _assertRaisesRegex)(*args, **kwargs) - - -def assertRegex(self, *args, **kwargs): - return getattr(self, _assertRegex)(*args, **kwargs) - - -if PY3: - exec_ = getattr(moves.builtins, "exec") - - def reraise(tp, value, tb=None): - if value is None: - value = tp() - if value.__traceback__ is not tb: - raise value.with_traceback(tb) - raise value - -else: - def exec_(_code_, _globs_=None, _locs_=None): - """Execute code in a namespace.""" - if _globs_ is None: - frame = sys._getframe(1) - _globs_ = frame.f_globals - if _locs_ is None: - _locs_ = frame.f_locals - del frame - elif _locs_ is None: - _locs_ = _globs_ - exec("""exec _code_ in _globs_, _locs_""") - - exec_("""def reraise(tp, value, tb=None): - raise tp, value, tb -""") - - -if sys.version_info[:2] == (3, 2): - exec_("""def raise_from(value, from_value): - if from_value is None: - raise value - raise value from from_value -""") -elif sys.version_info[:2] > (3, 2): - exec_("""def raise_from(value, from_value): - raise value from from_value -""") -else: - def raise_from(value, from_value): - raise value - - -print_ = getattr(moves.builtins, "print", None) -if print_ is None: - def print_(*args, **kwargs): - """The new-style print function for Python 2.4 and 2.5.""" - fp = kwargs.pop("file", sys.stdout) - if fp is None: - return - - def write(data): - if not isinstance(data, basestring): - data = str(data) - # If the file has an encoding, encode unicode with it. - if (isinstance(fp, file) and - isinstance(data, unicode) and - fp.encoding is not None): - errors = getattr(fp, "errors", None) - if errors is None: - errors = "strict" - data = data.encode(fp.encoding, errors) - fp.write(data) - want_unicode = False - sep = kwargs.pop("sep", None) - if sep is not None: - if isinstance(sep, unicode): - want_unicode = True - elif not isinstance(sep, str): - raise TypeError("sep must be None or a string") - end = kwargs.pop("end", None) - if end is not None: - if isinstance(end, unicode): - want_unicode = True - elif not isinstance(end, str): - raise TypeError("end must be None or a string") - if kwargs: - raise TypeError("invalid keyword arguments to print()") - if not want_unicode: - for arg in args: - if isinstance(arg, unicode): - want_unicode = True - break - if want_unicode: - newline = unicode("\n") - space = unicode(" ") - else: - newline = "\n" - space = " " - if sep is None: - sep = space - if end is None: - end = newline - for i, arg in enumerate(args): - if i: - write(sep) - write(arg) - write(end) -if sys.version_info[:2] < (3, 3): - _print = print_ - - def print_(*args, **kwargs): - fp = kwargs.get("file", sys.stdout) - flush = kwargs.pop("flush", False) - _print(*args, **kwargs) - if flush and fp is not None: - fp.flush() - -_add_doc(reraise, """Reraise an exception.""") - -if sys.version_info[0:2] < (3, 4): - def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, - updated=functools.WRAPPER_UPDATES): - def wrapper(f): - f = functools.wraps(wrapped, assigned, updated)(f) - f.__wrapped__ = wrapped - return f - return wrapper -else: - wraps = functools.wraps - - -def with_metaclass(meta, *bases): - """Create a base class with a metaclass.""" - # This requires a bit of explanation: the basic idea is to make a dummy - # metaclass for one level of class instantiation that replaces itself with - # the actual metaclass. - class metaclass(meta): - def __new__(cls, name, this_bases, d): - return meta(name, bases, d) - return type.__new__(metaclass, 'temporary_class', (), {}) - - -def add_metaclass(metaclass): - """Class decorator for creating a class with a metaclass.""" - def wrapper(cls): - orig_vars = cls.__dict__.copy() - slots = orig_vars.get('__slots__') - if slots is not None: - if isinstance(slots, str): - slots = [slots] - for slots_var in slots: - orig_vars.pop(slots_var) - orig_vars.pop('__dict__', None) - orig_vars.pop('__weakref__', None) - return metaclass(cls.__name__, cls.__bases__, orig_vars) - return wrapper - - -def python_2_unicode_compatible(klass): - """ - A decorator that defines __unicode__ and __str__ methods under Python 2. - Under Python 3 it does nothing. - - To support Python 2 and 3 with a single code base, define a __str__ method - returning text and apply this decorator to the class. - """ - if PY2: - if '__str__' not in klass.__dict__: - raise ValueError("@python_2_unicode_compatible cannot be applied " - "to %s because it doesn't define __str__()." % - klass.__name__) - klass.__unicode__ = klass.__str__ - klass.__str__ = lambda self: self.__unicode__().encode('utf-8') - return klass - - -# Complete the moves implementation. -# This code is at the end of this module to speed up module loading. -# Turn this module into a package. -__path__ = [] # required for PEP 302 and PEP 451 -__package__ = __name__ # see PEP 366 @ReservedAssignment -if globals().get("__spec__") is not None: - __spec__.submodule_search_locations = [] # PEP 451 @UndefinedVariable -# Remove other six meta path importers, since they cause problems. This can -# happen if six is removed from sys.modules and then reloaded. (Setuptools does -# this for some reason.) -if sys.meta_path: - for i, importer in enumerate(sys.meta_path): - # Here's some real nastiness: Another "instance" of the six module might - # be floating around. Therefore, we can't use isinstance() to check for - # the six meta path importer, since the other six instance will have - # inserted an importer with different class. - if (type(importer).__name__ == "_SixMetaPathImporter" and - importer.name == __name__): - del sys.meta_path[i] - break - del i, importer -# Finally, add the importer to the meta path import hook. -sys.meta_path.append(_importer) diff --git a/pyttsx3/__init__.py b/pyttsx4/__init__.py similarity index 100% rename from pyttsx3/__init__.py rename to pyttsx4/__init__.py diff --git a/pyttsx3/driver.py b/pyttsx4/driver.py similarity index 91% rename from pyttsx3/driver.py rename to pyttsx4/driver.py index 90f915e..b58d920 100644 --- a/pyttsx3/driver.py +++ b/pyttsx4/driver.py @@ -46,7 +46,7 @@ def __init__(self, engine, driverName, debug): else: driverName = 'espeak' # import driver module - name = 'pyttsx3.drivers.%s' % driverName + name = 'pyttsx4.drivers.%s' % driverName self._module = importlib.import_module(name) # build driver instance self._driver = self._module.buildDriver(weakref.proxy(self)) @@ -90,6 +90,7 @@ def _pump(self): cmd[0](*cmd[1]) except Exception as e: self.notify('error', exception=e) + print('ERROR:pyttsx4-driver: _pump ',e) if self._debug: traceback.print_exc() @@ -188,6 +189,14 @@ 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. ''' + # actually there are no setBusy(True) in the old code and it works. + # the error ocurrs when i added an sapi save_to_memory function. + # after that, when the result is saved to memory, the busy is not set to True, so next runAndWait will + # DEAD wait. + # I don't know why it don't work for memory. + # but here add setBusy(True) seem ok for the issue. + # here first setBusy(True), and push the endLoop event, and setBusy(False) in startLoop, + self.setBusy(True) self._push(self._engine.endLoop, tuple()) self._driver.startLoop() diff --git a/pyttsx3/drivers/__init__.py b/pyttsx4/drivers/__init__.py similarity index 97% rename from pyttsx3/drivers/__init__.py rename to pyttsx4/drivers/__init__.py index 687eccf..a85d3fc 100644 --- a/pyttsx3/drivers/__init__.py +++ b/pyttsx4/drivers/__init__.py @@ -2,7 +2,7 @@ ''' Utility functions to help with Python 2/3 compatibility ''' -from .. import six +import six def toUtf8(value): ''' diff --git a/pyttsx3/drivers/_espeak.py b/pyttsx4/drivers/_espeak.py similarity index 98% rename from pyttsx3/drivers/_espeak.py rename to pyttsx4/drivers/_espeak.py index 80f3acb..11ea55c 100644 --- a/pyttsx3/drivers/_espeak.py +++ b/pyttsx4/drivers/_espeak.py @@ -60,6 +60,13 @@ def load_windows_epng3(): import sys sys.exit() +# check if dll is loaded sucessfully +if dll is None: + print("Could not load libespeak-ng.so.1 or libespeak.so.1") + print('to install espeak on linux, run: \nsudo apt-get install -y espeak-ng ffmpeg alsa-utils') + import sys + sys.exit() + # constants and such from speak_lib.h EVENT_LIST_TERMINATED = 0 diff --git a/pyttsx4/drivers/coqui_ai_tts.py b/pyttsx4/drivers/coqui_ai_tts.py new file mode 100644 index 0000000..b3b8114 --- /dev/null +++ b/pyttsx4/drivers/coqui_ai_tts.py @@ -0,0 +1,128 @@ +#coding:utf-8 +import time +import numpy as np +import pyaudio + +try: + from TTS.api import TTS +except ImportError: + import sys, subprocess + print('no TTS package. installing...') + subprocess.run([sys.executable, '-m', 'pip', 'install', 'TTS', '-i','https://mirrors.aliyun.com/pypi/simple/']) + from TTS.api import TTS + +def buildDriver(proxy): + return TTSDriver(proxy) + + +class TTSDriver(object): + def __init__(self, proxy): + + self.model_name = TTS.list_models()[0] + self._tts = TTS(self.model_name) + + self._proxy = proxy + self._looping = False + self._speaking = False + self._stopping = False + + self.speaker_wav = None + + + def destroy(self): + self._tts=None + + def say(self, text): + if self._tts.is_multi_speaker: + wav = self._tts.tts(text, speaker_wav=self.speaker_wav, speaker=self._tts.speakers[0], language=self._tts.languages[0]) + else: + wav = self._tts.tts(text, + speaker_wav=self.speaker_wav) + #speaker=self._tts.speakers[0], language=self._tts.languages[0], + # wav is the raw data of the wav file + #format: sample_rate:16000 self._tts.synthesizer.tts_config.audio["sample_rate"] + #format: sample_width:2 + #format: channels:1 + + # wav是 -1.0-+1.0 转为 -32768-+32767 + #wav = (wav * 32767) + # wav 从list转为np.array + wav = np.array(wav, dtype=np.float32) + # wav 从-1.0-+1.0 转为 -32768-+32767 + wav = (wav * 32767).astype(np.int16) + wav = wav.tobytes() + + # list 转为bytes类型 + #wav = np.array(wav, dtype=np.int16).tobytes() + + # 播放wav + # import wave + p = pyaudio.PyAudio() + stream = p.open(format=p.get_format_from_width(2), + channels=1, + rate=16000, + output=True) + stream.write(wav) + stream.stop_stream() + stream.close() + p.terminate() + + self.endLoop() + + def stop(self): + self.endLoop() + + def save_to_file(self, text, filename): + if self._tts.is_multi_speaker: + self._tts.tts_to_file(text=text, file_path = filename, speaker_wav = self.speaker_wav, speaker=self._tts.speakers[0], language=self._tts.languages[0]) + else: + self._tts.tts_to_file(text=text, file_path = filename, speaker_wav = self.speaker_wav) + + def getProperty(self, name): + if name == 'voices': + return TTS.list_models() + elif name == 'voice': + return self.model_name + elif name == 'rate': + return self._rateWpm + elif name == 'volume': + return self._tts.Volume / 100.0 + elif name == 'pitch': + return self.pitch + #print("Pitch adjustment not supported when using SAPI5") + else: + raise KeyError('unknown property %s' % name) + + def setProperty(self, name, value): + if name == 'voice': + self.model_name = value + self._tts = TTS(model_name=self.model_name) + return + + elif name == 'rate': + pass + elif name == 'volume': + pass + elif name == 'pitch': + pass + elif name == 'speaker_wav': + self.speaker_wav = value + else: + raise KeyError('unknown property %s' % name) + + def startLoop(self): + self._looping = True + first = True + while self._looping: + if first: + self._proxy.setBusy(False) + first = False + time.sleep(0.05) + + def endLoop(self): + self._looping = False + + def iterate(self): + self._proxy.setBusy(False) + while 1: + yield diff --git a/pyttsx3/drivers/dummy.py b/pyttsx4/drivers/dummy.py similarity index 100% rename from pyttsx3/drivers/dummy.py rename to pyttsx4/drivers/dummy.py diff --git a/pyttsx3/drivers/espeak.py b/pyttsx4/drivers/espeak.py similarity index 85% rename from pyttsx3/drivers/espeak.py rename to pyttsx4/drivers/espeak.py index b0a5dcd..ff309ce 100644 --- a/pyttsx3/drivers/espeak.py +++ b/pyttsx4/drivers/espeak.py @@ -37,6 +37,7 @@ def __init__(self, proxy): self._stopping = False self._data_buffer = b'' self._numerise_buffer = [] + self.to_filename = None def numerise(self, data): self._numerise_buffer.append(data) @@ -49,6 +50,7 @@ def destroy(self): _espeak.SetSynthCallback(None) def say(self, text): + self.to_filename=None self._proxy.setBusy(True) self._proxy.notify('started-utterance') _espeak.Synth(toUtf8(text), flags=_espeak.ENDPAUSE | @@ -133,7 +135,13 @@ def startLoop(self): time.sleep(0.01) def save_to_file(self, text, filename): - code = self.numerise(filename) + self._proxy.setBusy(True) + self._proxy.notify('started-utterance') + self.to_filename = filename + if isinstance(filename, io.BytesIO): + code = None + else: + code = self.numerise(self.to_filename) _espeak.Synth(toUtf8(text), flags=_espeak.ENDPAUSE | _espeak.CHARS_UTF8, user_data=code) @@ -163,17 +171,24 @@ def _onSynth(self, wav, numsamples, events): location=event.text_position - 1, length=event.length) elif event.type == _espeak.EVENT_MSG_TERMINATED: - stream = NamedTemporaryFile() - - with wave.open(stream, 'wb') as f: - f.setnchannels(1) - f.setsampwidth(2) - f.setframerate(22050.0) - f.writeframes(self._data_buffer) - if event.user_data: + if isinstance(self.to_filename,io.BytesIO): + self.to_filename.write(self._data_buffer) + elif event.user_data: + stream = NamedTemporaryFile() + with wave.open(stream, 'wb') as f: + f.setnchannels(1) + f.setsampwidth(2) + f.setframerate(22050.0) + f.writeframes(self._data_buffer) os.system('ffmpeg -y -i {} {} -loglevel quiet'.format(stream.name, self.decode_numeric(event.user_data))) else: + stream = NamedTemporaryFile() + with wave.open(stream, 'wb') as f: + f.setnchannels(1) + f.setsampwidth(2) + f.setframerate(22050.0) + f.writeframes(self._data_buffer) os.system('aplay {} -q'.format(stream.name)) # -q for quiet self._data_buffer = b'' diff --git a/pyttsx3/drivers/nsss.py b/pyttsx4/drivers/nsss.py similarity index 74% rename from pyttsx3/drivers/nsss.py rename to pyttsx4/drivers/nsss.py index 6cf5f81..c73f9e1 100644 --- a/pyttsx3/drivers/nsss.py +++ b/pyttsx4/drivers/nsss.py @@ -1,8 +1,9 @@ - +#coding:utf-8 from Foundation import * from AppKit import NSSpeechSynthesizer from PyObjCTools import AppHelper from ..voice import Voice +from objc import super def buildDriver(proxy): @@ -43,10 +44,26 @@ def iterate(self): @objc.python_method def say(self, text): - self._proxy.setBusy(True) - self._completed = True - self._proxy.notify('started-utterance') + import time + #self._proxy.setBusy(True) + #self._completed = True + #self._proxy.notify('started-utterance') + #print('debug:nsss:say start', time.time()) self._tts.startSpeakingString_(text) + # add this delay and call to didFinishSpeaking_ to prevent unfinished dead locks + time.sleep(0.1) + cnt = 0 + # needed so script doesn't end w/o talking + while self._tts.isSpeaking(): + time.sleep(0.1) + cnt+=1 + #if cnt>100: + # print('debug:nsss:say start more than 10seconds. stucked?',cnt) + # break + #self.speechSynthesizer_didFinishSpeaking_(self._tts, True) + #print('debug:nsss:say end', time.time()) + self._proxy.setBusy(False) + def stop(self): if self._proxy.isBusy(): @@ -55,13 +72,10 @@ def stop(self): @objc.python_method def _toVoice(self, attr): - try: - lang = attr['VoiceLocaleIdentifier'] - except KeyError: - lang = attr['VoiceLanguage'] - return Voice(attr['VoiceIdentifier'], attr['VoiceName'], - [lang], attr['VoiceGender'], - attr['VoiceAge']) + + return Voice(attr.get('VoiceIdentifier'), attr.get('VoiceName'), + [attr.get('VoiceLanguage')], attr.get('VoiceGender'), + attr.get('VoiceAge')) @objc.python_method def getProperty(self, name): @@ -101,6 +115,11 @@ def setProperty(self, name, value): def save_to_file(self, text, filename): url = Foundation.NSURL.fileURLWithPath_(filename) self._tts.startSpeakingString_toURL_(text, url) + import time + time.sleep(0.1) + # needed so script doesn't end w/o talking + while self._tts.isSpeaking(): + time.sleep(0.1) def speechSynthesizer_didFinishSpeaking_(self, tts, success): if not self._completed: diff --git a/pyttsx3/drivers/sapi5.py b/pyttsx4/drivers/sapi5.py similarity index 75% rename from pyttsx3/drivers/sapi5.py rename to pyttsx4/drivers/sapi5.py index 6cca7b5..29d9723 100644 --- a/pyttsx3/drivers/sapi5.py +++ b/pyttsx4/drivers/sapi5.py @@ -7,6 +7,7 @@ stream = comtypes.client.CreateObject("SAPI.SpFileStream") from comtypes.gen import SpeechLib +from io import BytesIO import pythoncom import time import math @@ -46,6 +47,10 @@ def __init__(self, proxy): self._rateWpm = 200 self.setProperty('voice', self.getProperty('voice')) + #-10=>+10 + self.pitch= 0 + self.pitch_str='' + def destroy(self): self._tts.EventInterests = 0 @@ -53,7 +58,7 @@ def say(self, text): self._proxy.setBusy(True) self._proxy.notify('started-utterance') self._speaking = True - self._tts.Speak(fromUtf8(toUtf8(text))) + self._tts.Speak(self.pitch_str+fromUtf8(toUtf8(text))) def stop(self): if not self._speaking: @@ -63,16 +68,45 @@ def stop(self): self._tts.Speak('', 3) def save_to_file(self, text, filename): + if isinstance(filename, BytesIO): + self.to_memory(text, filename) + return + cwd = os.getcwd() stream = comtypes.client.CreateObject('SAPI.SPFileStream') stream.Open(filename, SpeechLib.SSFMCreateForWrite) - temp_stream = self._tts.AudioOutputStream + + # in case there is no outputstream, the call to AudioOutputStream will fail + is_stream_stored = False + try: + temp_stream = self._tts.AudioOutputStream + is_stream_stored=True + except Exception as e: + #no audio output stream + pass self._tts.AudioOutputStream = stream - self._tts.Speak(fromUtf8(toUtf8(text))) - self._tts.AudioOutputStream = temp_stream + self._tts.Speak(self.pitch_str+fromUtf8(toUtf8(text))) + if is_stream_stored: + self._tts.AudioOutputStream = temp_stream + else: + try: + self._tts.AudioOutputStream = None + except Exception as e: + print('set None no no-output stream machine:', e) + pass stream.close() os.chdir(cwd) + def to_memory(self, text, olist): + stream = comtypes.client.CreateObject('SAPI.SpMemoryStream') + temp_stream = self._tts.AudioOutputStream + self._tts.AudioOutputStream = stream + self._tts.Speak(self.pitch_str+fromUtf8(toUtf8(text))) + self._tts.AudioOutputStream = temp_stream + data = stream.GetData() + olist.write(bytes(data)) + del stream + def _toVoice(self, attr): return Voice(attr.Id, attr.GetDescription()) @@ -93,7 +127,8 @@ def getProperty(self, name): elif name == 'volume': return self._tts.Volume / 100.0 elif name == 'pitch': - print("Pitch adjustment not supported when using SAPI5") + return self.pitch + #print("Pitch adjustment not supported when using SAPI5") else: raise KeyError('unknown property %s' % name) @@ -107,6 +142,11 @@ def setProperty(self, name, value): id_ = self._tts.Voice.Id a, b = E_REG.get(id_, E_REG[MSMARY]) try: + rate = int(math.log(value / a, b)) + if rate<-10: + rate = -10 + if rate>10: + rate = 10 self._tts.Rate = int(math.log(value / a, b)) except TypeError as e: raise ValueError(str(e)) @@ -117,13 +157,15 @@ def setProperty(self, name, value): except TypeError as e: raise ValueError(str(e)) elif name == 'pitch': - print("Pitch adjustment not supported when using SAPI5") + #-10 ->10 + self.pitch = value + self.pitch_str = '' else: raise KeyError('unknown property %s' % name) def startLoop(self): - first = True self._looping = True + first = True while self._looping: if first: self._proxy.setBusy(False) diff --git a/pyttsx3/engine.py b/pyttsx4/engine.py similarity index 100% rename from pyttsx3/engine.py rename to pyttsx4/engine.py diff --git a/pyttsx3/voice.py b/pyttsx4/voice.py similarity index 100% rename from pyttsx3/voice.py rename to pyttsx4/voice.py diff --git a/pyttsx4_say_test.py b/pyttsx4_say_test.py new file mode 100644 index 0000000..a4ce8b7 --- /dev/null +++ b/pyttsx4_say_test.py @@ -0,0 +1,6 @@ +#coding:utf-8 +import pyttsx4 + +engine = pyttsx4.init() +engine.say("Hello, World!") +engine.runAndWait() \ No newline at end of file diff --git a/pyttsx4_test.py b/pyttsx4_test.py new file mode 100644 index 0000000..43f5e20 --- /dev/null +++ b/pyttsx4_test.py @@ -0,0 +1,191 @@ +# coding:utf-8 + +from io import BytesIO +import time + +import pyttsx4 + +import os + + +def test_save_to_file(): + print('test_save_to_file start') + engine = pyttsx4.init() + ############ + file_path = os.path.dirname(__file__)+'/test.wav' + engine.save_to_file('Hello World', file_path) + engine.runAndWait() + print('test_save_to_file finish.file saved to:', file_path) + + +def test_say(): + # engine = pyttsx4.init('coqui_ai_tts') # object creation + engine = pyttsx4.init() + + ############ + engine.setProperty('pitch', -20) + + while True: + # engine.save_to_file('Hello World', 'test.wav') + engine.say('Hello world') + engine.runAndWait() + time.sleep(4) + + +def test_2(): + engine = pyttsx4.init() + + engine.setProperty('pitch', 0) + # engine.save_to_file('Hello World', 'test.wav') + engine.say('你好,我是一个程序员') + engine.runAndWait() + + engine.setProperty('pitch', 10) + # engine.save_to_file('Hello World', 'test.wav') + engine.say('你好,我是一个程序员') + engine.runAndWait() + + engine.setProperty('pitch', 50) + # engine.save_to_file('Hello World', 'test.wav') + engine.say('你好,我是一个程序员') + engine.runAndWait() + + +def test_3(): + + engine = pyttsx4.init() + + b = BytesIO() + engine.save_to_file('Hello World', b) + engine.runAndWait() + + bs = b.getvalue() + # to save b as a wav file, we have to add a wav file header:44byte + # https://docs.fileformat.com/audio/wav/ + # b= bytes(b'RIFF')+ (len(bs)+38).to_bytes(4, byteorder='little')+b'WAVEfmt\x20' +\ + # (18).to_bytes(4, byteorder='little')+(1).to_bytes(2, byteorder='little') +(1).to_bytes(2, byteorder='little')+ \ + # (22050).to_bytes(4,byteorder='little')+(44100).to_bytes(4,byteorder='little') +(2).to_bytes(2, byteorder='little')+\ + # (16).to_bytes(2,byteorder='little')+b'\x00\x00'+ b'data'+ (len(bs)).to_bytes(4, byteorder='little')+bs + + b = bytes(b'RIFF') + (len(bs)+38).to_bytes(4, byteorder='little')+b'WAVEfmt\x20\x12\x00\x00' \ + b'\x00\x01\x00\x01\x00' \ + b'\x22\x56\x00\x00\x44\xac\x00\x00' +\ + b'\x02\x00\x10\x00\x00\x00data' + \ + (len(bs)).to_bytes(4, byteorder='little')+bs + + f = open('test4.wav', 'wb') + f.write(b) + f.close() + + engine.setProperty( + 'voice', 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0') + + engine.say('Hello World') + engine.say('你好,世界') + engine.runAndWait() + + """ 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 + # setting up volume level between 0 and 1 + engine.setProperty('volume', 1.0) + + """VOICE""" + voices = engine.getProperty('voices') # getting details of current voice + # engine.setProperty('voice', voices[0].id) #changing index, changes voices. o for male + # changing index, changes voices. 1 for female + engine.setProperty('voice', voices[1].id) + + """PITCH""" + pitch = engine.getProperty('pitch') # Get current pitch value + print(pitch) # Print current pitch value + # Set the pitch (default 50) to 75 out of 100 + engine.setProperty('pitch', 75) + + 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() + + +def test_qtts(): + engine = pyttsx4.init('coqui_ai_tts') + engine.say('Hello World.') + engine.runAndWait() + + engine.say('this is an english text to voice test.') + engine.runAndWait() + +def test_qtts2(): + engine = pyttsx4.init('coqui_ai_tts') + engine.setProperty('speaker_wav', './ide-guide.wav') + engine.say('Hello World.') + engine.runAndWait() + + engine.say('this is an english text to voice test.') + engine.runAndWait() + +def test_qtts3(): + engine = pyttsx4.init('coqui_ai_tts') + vs = engine.getProperty('voices') + voice_chinese='tts_models/zh-CN/baker/tacotron2-DDC-GST' + engine.setProperty('voice', voice_chinese) + + # engine.say('this is an english text to voice test.') + # engine.runAndWait() + engine.setProperty('speaker_wav', './ide-guide.wav') + + engine.say('这是一个中文说明 .') + engine.runAndWait() + +def test_qtts_to_file(): + engine = pyttsx4.init('coqui_ai_tts') + engine.save_to_file('Hello World.', 'test1.wav') + engine.runAndWait() + + engine.save_to_file('this is an english text to voice test.', 'test2.wav') + engine.runAndWait() + + + + +def test_qtts_to_file2(): + engine = pyttsx4.init('coqui_ai_tts') + engine.save_to_file('what a crazy man.', 'what_crazy_man.wav') + engine.runAndWait() + engine.save_to_file('this is an apple.', 'this_is_an_apple.wav') + engine.runAndWait() + engine.save_to_file('what a lazy dog.', 'what_a_lazy_dog.wav') + engine.runAndWait() + + +def test1_tts(): + engine = pyttsx4.init('coqui_ai_tts') + engine.setProperty('speaker_wav', './docs/i_have_a_dream_10s.wav') + + engine.save_to_file('this is an english text to voice test, listen it carefully and tell who i am.', 'test_mtk.wav') + engine.runAndWait() + + engine.setProperty('speaker_wav', './docs/the_ballot_or_the_bullet_15s.wav') + + engine.save_to_file('this is an english text to voice test, listen it carefully and tell who i am.','test_mx.wav') + engine.runAndWait() + + +if __name__ == '__main__': + test_save_to_file() + #test1_tts() + #test_qtts_to_file() + #test_qtts_to_file2() diff --git a/requirements.txt b/requirements.txt index aedc347..5de7e5d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,9 @@ +six + +# if use ai tts +#TTS +#pyaudio + # see setup.py # pyttsx3 only requires `espeak` driver/library which is system-dependent diff --git a/setup.py b/setup.py index d9ed9d6..070e4bd 100644 --- a/setup.py +++ b/setup.py @@ -7,26 +7,28 @@ 'comtypes; platform_system == "Windows"', 'pypiwin32; platform_system == "Windows"', 'pywin32; platform_system == "Windows"', - 'pyobjc>=2.4; platform_system == "Darwin"' + 'pyobjc>=2.4; platform_system == "Darwin"', + 'six' ] -with open('README.rst', 'r') as f: +with open('README.md', 'r') as f: long_description = f.read() setup( - name='pyttsx3', - packages=['pyttsx3', 'pyttsx3.drivers'], - version='2.91', + name='pyttsx4', + packages=['pyttsx4', 'pyttsx4.drivers'], + version='3.0.15', description='Text to Speech (TTS) library for Python 3. Works without internet connection or delay. Supports multiple TTS engines, including Sapi5, nsss, and espeak.', long_description=long_description, + long_description_content_type='text/markdown', summary='Offline Text to Speech library with multi-engine support', author='Natesh M Bhat', - url='https://github.com/nateshmbhat/pyttsx3', - author_email='nateshmbhatofficial@gmail.com', + url='https://github.com/Jiangshan00001/pyttsx4', + author_email='710806594@qq.com', install_requires=install_requires , - keywords=['pyttsx' , 'ivona','pyttsx for python3' , 'TTS for python3' , 'pyttsx3' ,'text to speech for python','tts','text to speech','speech','speech synthesis','offline text to speech','offline tts','gtts'], + keywords=['pyttsx' , 'ivona','pyttsx for python3' , 'TTS for python3' , 'pyttsx4' ,'text to speech for python','tts','text to speech','speech','speech synthesis','offline text to speech','offline tts','gtts'], classifiers = [ 'Intended Audience :: End Users/Desktop', 'Intended Audience :: Developers', @@ -39,6 +41,10 @@ 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7' - ], + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + 'Programming Language :: Python :: 3.11', + ], )