Skip to content

Commit

Permalink
Fixes from various PRs in the pyttsx33 repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Akul2010 committed Dec 23, 2023
1 parent d6f77bf commit 8738b2c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
16 changes: 16 additions & 0 deletions rlvoice/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ def save_to_file(self, text, filename, name):
'''
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 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._push(self._driver.to_memory, (text, olist), name)

def getProperty(self, name):
'''
Called by the engine to get a driver property value.
Expand Down
35 changes: 31 additions & 4 deletions rlvoice/drivers/nsss.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@
from ..voice import Voice
from objc import super
import objc
from Foundation import NSObject, NSTimer, NSURL
from Foundation import NSObject, NSTimer, NSURL, NSDefaultRunLoopMode, NSRunLoop
from PyObjCTools.AppHelper import PyObjCAppHelperRunLoopStopper

def buildDriver(proxy):
return NSSpeechDriver.alloc().initWithProxy(proxy)

class RunLoopStopper(PyObjCAppHelperRunLoopStopper):

def init(self):
self = super(RunLoopStopper, self).init()
self.shouldStop = False
return self

def stop(self):
self.shouldStop = True
# this should go away when/if runEventLoop uses
# runLoop iteration
# if NSApp() is not None:
# NSApp().terminate_(self)

class NSSpeechDriver(NSObject):
@objc.python_method
Expand All @@ -33,7 +47,20 @@ def onPumpFirst_(self, timer):
def startLoop(self):
NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(
0.0, self, 'onPumpFirst:', None, False)
AppHelper.runConsoleEventLoop()
runLoop = NSRunLoop.currentRunLoop()
stopper = RunLoopStopper.alloc().init()
PyObjCAppHelperRunLoopStopper.addRunLoopStopper_toRunLoop_(stopper, runLoop)
try:

while stopper.shouldRun():
nextfire = runLoop.limitDateForMode_(NSDefaultRunLoopMode)
if not stopper.shouldRun():
break
if not runLoop.runMode_beforeDate_(NSDefaultRunLoopMode, nextfire):
stopper.stop()

finally:
PyObjCAppHelperRunLoopStopper.removeRunLoopStopperFromRunLoop_(runLoop)

def endLoop(self):
AppHelper.stopEventLoop()
Expand Down Expand Up @@ -114,9 +141,9 @@ def setProperty(self, name, value):
@objc.python_method
def save_to_file(self, text, filename):
url = NSURL.fileURLWithPath_(filename)
self._tts.startSpeakingString_toURL_(text, url)
import time
time.sleep(0.1)
self._tts.startSpeakingString_toURL_(text, url)
time.sleep(max(1, len(text) * 0.01))
# needed so script doesn't end w/o talking
while self._tts.isSpeaking():
time.sleep(0.1)
Expand Down
11 changes: 11 additions & 0 deletions rlvoice/drivers/sapi5.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import comtypes.client # Importing comtypes.client will make the gen subpackage
from ctypes import c_int16
try:
from comtypes.gen import SpeechLib # comtypes
except ImportError:
Expand Down Expand Up @@ -87,6 +88,16 @@ def save_to_file(self, text, filename):
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()

def _toVoice(self, attr):
return Voice(attr.Id, attr.GetDescription())

Expand Down
16 changes: 16 additions & 0 deletions rlvoice/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ def save_to_file(self, text, filename, name=None):
'''
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
Expand Down

0 comments on commit 8738b2c

Please sign in to comment.