I should have stated in the original thread that SAPI5 defaults to synchronous mode whereas SAPI4 defaults to asynchronous. Sorry Vince..
Please see NOTES at end.
This code demos setting:
asynchronous mode
synchronous mode (default)
Volume
Rate (how fast it speaks..)
Pausing and Resuming asynchronous speech
and how to speak the contents of a file (please spec a REAL FILE!)
Code: Select all
#define UNICODE
'
#define SPF_DEFAULT 0 'not asynchronous
#define SPF_ASYNC 1 'mmmm... asynchronous
#define SPF_PURGEBEFORESPEAK 2 'Purges all pending speak requests prior to this speak call.
#define SPF_IS_FILENAME 4 'The string passed is a file name, and the file text should be spoken.
#define SPF_IS_XML 8 'The input text will be parsed for XML markup.
#define SPF_IS_NOT_XML 16
'
#include once "disphelper/disphelper.bi"
#include once "windows.bi"
'
dim as HRESULT hr
dim myt as wstring * 512
dim as string tempstr
dim shared as IDispatch ptr tts=NULL 'DISPATCH_OBJ(tts)
declare sub imouttahere() destructor
'
dhInitialize(TRUE)
dhToggleExceptions(TRUE)'FALSE) 'set this TRUE to get error codes
hr=dhCreateObject("Sapi.SpVoice",NULL,@tts)
'
'test speaking
myt="Now is the time for all good men to come to the aid of the party"
dhPutValue(tts, ".Volume = %d", 80) 'Volume range is 0 to 100
dhCallMethod(tts,".Speak(%S,%d)", @myt,0)'synchronous
'
sleep 1000
'
'test speaking a text file, have it long enough for
' the pause and resume tests that follow..
myt="c:\whatever\long\file.txt" 'PLEASE change to something real..
tempstr=myt
if dir(myt)="" then
beep
print "Abandoning further tests, ";myt;" file doesn't exist"
print
print "Exit in 5 seconds.."
sleep 5000
end
end if
dhPutValue(tts,".Volume = %d",100)
dhCallMethod(tts,".Speak(%S,%d)",_
@myt,_
SPF_IS_NOT_XML or SPF_IS_FILENAME or SPF_ASYNC)'async from file
'
'pause..
sleep 4000
print "pausing.."
dhCallMethod(tts,".Pause")
'
'resume
sleep 4000
print "resuming.."
dhPutValue(tts,".Rate = %d",2) 'Rate range is -10 to 10
dhCallMethod(tts,".Resume")
'
print
print "Sleeping to exit.."
sleep
end
'
sub imouttahere() destructor
SAFE_RELEASE(tts)
end sub
NOTES:
This code is specific to SAPI5 which in theory will run on Win98. The core elements (see other thread) were/are a part of WinXP. The speech engine et al are not installed with XP, however installing any Office prod that has speech capabilities does. Without Office installed (I'm not sure what flavors) you must install the speech engine etc (again see other thread) separately.
Kristopher reports that all works on Vista without additional installations.
There is a method to wait for the asynchronous speech to complete, say before exiting your program, - if I have time I'll finger that out later.
ETA "destructor" on sub.. won't work without it..