Peter Parente schrieb:
> Hi,
> 
> I'm trying to use the SAPI5 SPVoice interface through comtypes, but I
> don't seem to be getting any events.
> 
> I instantiate the interface like so and call ShowEvents
> 
> import comtypes.client
> tts = comtypes.client.CreateObject('SAPI.SPVoice')
> advise = comtypes.client.ShowEvents(tts)
> 
[...]
> 
> When I invoke the speak() method on the tts object, I believe I should
> see some of these events (word, sentence, etc.) once I enter a message
> loop. However, I see nothing printed:
> 
> tts.speak('The quick brown fox jumps over the lazy dog.')
> comtypes.client.PumpEvents(10)
> 
> I also tried passing my own class with callback methods named
> _ISpeechVoiceEvents_StartStream and so forth to GetEvents, but still,
> none of the callbacks get invoked.

I investigated this a little bit.  It seems that SAPI doesn't use the
standard COM mechanism to deliver events:

http://msdn.microsoft.com/en-us/library/ms717961%28VS.85%29.aspx

(The following is what I found out with some quick experiments.  I do
not know if this is the recommended or the best way to do things, I
just wanted to find out how to get SOME events).

First, you call CreateObject() with interface=ISpVoice.  This interface
has a method SetNotifySink() which you are required to call with a
sink interface - a COM pointer to an object that implements the
ISpNotifySink interface.  You have to implement this object yourself.

Then, you have to call SetInterest (a method of ISpVoice) to determine
which events you want to be notified about.

When events are delivered, they call the Notify() method on your event sink,
which in turn now has to call GetEvents() on the ISpVoice interface.
All this is demontstrated in the following code:

"""
import logging
logging.basicConfig(level=logging.INFO)

from comtypes.client import CreateObject
from comtypes.gen.SpeechLib import ISpVoice, ISpNotifySink
from comtypes import COMObject

class EventSink(COMObject):
    _com_interfaces_ = [ISpNotifySink]

    def __init__(self, source):
        self.source = source

    def Notify(self):
        print "NOTIFIED"

s = CreateObject("SAPI.SpVoice", interface=ISpVoice)
s.SetInterest(-1, 0)
s.SetNotifySink(EventSink(s))

s.speak("The quick brown fox jumps over the lazy dog.", 0)
"""

The code does print quite some "NOTIFIED" lines, but it isn't so easy
to retrieve the actual events from the source.  At least, it's a start.

-- 
Thanks,
Thomas


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
comtypes-users mailing list
comtypes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Reply via email to