diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..2645003 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 1eb6fff..1f78373 100644 --- a/.gitignore +++ b/.gitignore @@ -105,3 +105,4 @@ docs/make.bat # vscode .vscode/ + diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..615aafb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "/usr/bin/python3" +} \ No newline at end of file diff --git a/README.md b/README.md index dabf772..9ea230c 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,6 @@ Feel free to wrap another text-to-speech engine for use with ``pyttsx3``. ### Project Links : -* PyPI (https://pypi.python.org) +* PyPI (https://pypi.org/project/pyttsx3/) * GitHub (https://github.com/nateshmbhat/pyttsx3) * Full Documentation (https://pyttsx3.readthedocs.org) diff --git a/example/main.py b/example/main.py index b7329bb..957b358 100644 --- a/example/main.py +++ b/example/main.py @@ -14,6 +14,7 @@ """VOICE""" voices = engine.getProperty('voices') #getting details of current voice + #engine.setProperty('voice', voices[0].id) #changing index, changes voices. 0 for male engine.setProperty('voice', voices[1].id) #changing index, changes voices. 1 for female @@ -28,7 +29,19 @@ 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() + +"""To memory """ +if sys.platform == 'win32': + # Windows-specific code + audio_array = [] + engine.to_memory("Hello!", audio_array) + engine.to_memory("Hello again!", audio_array) + engine.runAndWait() + print(len(audio_array)) +elif sys.platform == 'linux': + # Linux-specific code + engine.save_to_file('Hello World', 'test.mp3') + engine.runAndWait() +else: + print("Unsupported platform") + diff --git a/makefile b/makefile index 0bf7b97..6a61c81 100644 --- a/makefile +++ b/makefile @@ -2,7 +2,7 @@ clean: rm -rf dist/ rm -rf build/ build: - pip3 install wheel + pip3 install wheel --user python3 setup.py bdist_wheel upload: pip3 install twine diff --git a/pyttsx3/driver.py b/pyttsx3/driver.py index 90f915e..b141fe7 100644 --- a/pyttsx3/driver.py +++ b/pyttsx3/driver.py @@ -160,6 +160,23 @@ def save_to_file(self, text, filename, name): @type name: str ''' self._push(self._driver.save_to_file, (text, filename), name) + + def to_memory(self, text, olist, 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 this utterance. Included in + notifications about this utterance. + @param ostream: The opened output stream + + >>> import pyttsx3 + >>> engine = pyttsx3.init() + >>> ret = [] + >>> engine.to_memory("hello", ret) + ''' + self._push(self._driver.to_memory, (text, olist), name) def getProperty(self, name): ''' diff --git a/pyttsx3/drivers/sapi5.py b/pyttsx3/drivers/sapi5.py index ba5f3bd..edee527 100644 --- a/pyttsx3/drivers/sapi5.py +++ b/pyttsx3/drivers/sapi5.py @@ -15,9 +15,8 @@ import os import time import weakref - +from ctypes import c_int16 import pythoncom - from ..voice import Voice from . import fromUtf8, toUtf8 @@ -80,6 +79,17 @@ def save_to_file(self, text, filename): self._tts.AudioOutputStream = temp_stream 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(fromUtf8(toUtf8(text))) + self._tts.AudioOutputStream = temp_stream + data = stream.GetData() + olist.append([c_int16((data[i])|data[i+1]<<8).value for i in range(0,len(data),2)]) + stream.close() + @staticmethod def _toVoice(attr): diff --git a/pyttsx3/engine.py b/pyttsx3/engine.py index 3f76552..1efd8e1 100644 --- a/pyttsx3/engine.py +++ b/pyttsx3/engine.py @@ -120,7 +120,25 @@ def save_to_file(self, text, filename, name=None): @type name: str ''' self.proxy.save_to_file(text, filename, name) + + def to_memory(self, text, olist, name=None): + ''' + Adds an utterance to speak to the event queue. + @param text: Text to sepak + @type text: unicode + @param name: Name to associate with this utterance. Included in + notifications about this utterance. + @param ostream: The opened output stream + + >>> import pyttsx3 + >>> engine = pyttsx3.init() + >>> ret = [] + >>> engine.to_memory("hello", ret) + + ''' + self.proxy.to_memory(text,olist, name) + def isBusy(self): """ @return: True if an utterance is currently being spoken, false if not diff --git a/setup.py b/setup.py index 588501d..71551f0 100644 --- a/setup.py +++ b/setup.py @@ -3,6 +3,7 @@ # Ubuntu: sudo apt install espeak ffmpeg + install_requires = [ 'comtypes; platform_system == "Windows"', 'pypiwin32; platform_system == "Windows"', @@ -11,6 +12,7 @@ ] + with open('README.rst', 'r') as f: long_description = f.read()