SAPI 5.1: Voice - Enabled Applications

P

PC User

I'm trying to apply some code that I've found on Text Reading and
Speech Recongnition. I don't know what version of VB that it was
develop. I've posted my project that I'm working on and I was hoping
someone could help me get it to work. It can be found at:
http://www.access-programmers.co.uk/forums/attachment.php?attachmentid=11527

The code is from:
SAPI 5.1: Voice - Enabled Applications With VB
By Peter A. Bromberg, Ph.D.
http://www.eggheadcafe.com/articles/20011124.asp

Thanks for your time.
PC
 
D

Douglas J Steele

I'm not about to download and open the attachment, but the code outlined in
the eggheadcafe article worked fine for me in Access 97, so I'd assume it'll
work in newer versions as well.

What problem are you having?
 
T

Tom Wickerath

Hi Doug,

I downloaded PC User's sample last night and tried it in Access 2002. I
could not get it to work properly. I found something really strange. I could
get the btnReadToMe_Click() and btnSpeakToMe_Click() procedures to work, in
Access, only in break mode (ie. setting a break point and then executing each
line with the F8 key). When I removed the break point, I didn't hear anything:

Private Sub btnReadToMe_Click()
Dim Voice As SpVoice
Set Voice = New SpVoice
Voice.Speak Nz(Me.Text0, ""), SVSFlagsAsync
End Sub

Private Sub btnSpeakToMe_Click()
Dim Voice As SpVoice
Set Voice = New SpVoice
Voice.Speak "Hello. My name is Peter.", SVSFlagsAsync
End Sub

I converted the sample to Access 97. The same thing is happening....no voice
heard unless I run the code in break mode. In addition, I received an
ambiguous name detected error in Access 97 when I attempted to compile the
code. This occurred on the line of code that reads:

Dim WithEvents RecoContext As SpSharedRecoContext

which was found in two Private Subs. I commented out these two declarations
and moved this line of code to the top of the module, to declare this at the
module level. When I attempt to compile in Access 97, I'm now receiving
"Automation type not supported in Visual Basic" error on the following:

' the following is the event handler for the recognition event
Private Sub RecoContext_Recognition(ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal RecognitionType As SpeechRecognitionType, _
ByVal Result As ISpeechRecoResult _
)


In short, I'm not having much luck getting this to work in either Access 97
or 2002. I thought last night that perhaps this code only works in VB, not
VBA, but your mention that you can get it to work in Access 97 seems to
indicate that something else is the problem.

Is there any chance that you would be willing to post your working sample in
Access 97 to your web site?

Thanks,

Tom
______________________________________________

:

I'm not about to download and open the attachment, but the code outlined in
the eggheadcafe article worked fine for me in Access 97, so I'd assume it'll
work in newer versions as well.

What problem are you having?
 
D

Douglas J Steele

Actually, I noticed that too, so I removed the svsFlagsAsync parameter from
the invocation of the Voice.Speak method.
 
T

Tom Wickerath

Thanks Doug,

I'm now successful in getting two of the three command buttons on PC User's
sample to function correctly: the buttons labelled "Read" and "RD". These
buttons have the following two click event procedures, respectively:

Private Sub btnReadToMe_Click()
Dim Voice As SpVoice
Set Voice = New SpVoice
Voice.Speak Nz(Me.Text0, "")
End Sub

Private Sub btnSpeakToMe_Click()
Dim Voice As SpVoice
Set Voice = New SpVoice
Voice.Speak "Hello. My name is Peter."
End Sub

I haven't gotten his button labelled "WT" to function. The click event for
this button is apparently intended to write something to a file. However,
this is a great start.

Thank You,

Tom
____________________________________________

:

Actually, I noticed that too, so I removed the svsFlagsAsync parameter from
the invocation of the Voice.Speak method.
 
P

PC User

Thank you for your discussion on my project. I made the demo that you
downloaded based on interpretation of the article by Dr. Bromberg. I
may not have explained the buttons adequately. The Read button is
"Read To Me" from what is typed in the textbox. The RD button is
"Speak To Me" from the text in the onClick event of the RD button.
The WT button is "Write My Speech" for speech recognition to type
in your spoken word into the textbox when you have a microphone
attached to the computer. This is Dr. Bromber's code to convert the
spoken word into written text.
All the issues that Tom brought up are the ones that I experienced and
the questions that he has asked are the ones that I would have asked
also. It looks as though we are making progress on this.
 
D

Douglas J. Steele

Afraid I don't have a microphone handy, so I can't do much with the
speech-to-text functionality.

In the routine InitializeGrammar, reference is made to App.Path &
"\newnums.xml". This is supposed to be the XML file mentioned in the
article. You'll need to replace App.Path with a reference to the path to
where the file is. If want to point to the same folder where the MDB file
is, and you're using Access 2000 or newer, you can use
Application.CurrentProject.Path & "\". (If you're using Access 97, you can
use Left$(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir$(CurrentDb.Name))))

In addition to moving the line Dim WithEvents RecoContext As
SpSharedRecoContext to the top of the module (so that it applies to all
modules), I believe it's also necessary to move Dim Grammar As
ISpeechRecoGrammar

It looks as though there might be no way around the "Automation type not
supported" message in Access 97 for the RecoContext_Recognition sub. That
error doesn't occur in Access 2003, though, so if you're using Access 2000
or newer, I think you might be okay.

If I can steal my daughter's microphone sometime this weekend, I'll see
whether I can get any further.
 
D

Douglas J Steele

To determine what voices have been defined, you can use the following code:

Dim objVoice As SpeechLib.SpVoice
Dim objToken As SpeechLib.ISpeechObjectToken

Set objVoice = New SpVoice

' Get each token in the collection returned by GetVoices

For Each objToken In objVoice.GetVoices
Debug.Print objToken.GetDescription
Next objToken

Once you know which voice you want (the number of the voice, not the name),
you can use

Dim objVoice As SpeechLib.SpVoice

Set objVoice = New SpVoice
Set objVoice.Voice = objVoice.GetVoices().Item(intLoop)
objVoice.Speak "Hello, Doug"


Here's some code I wrote to speek the same phrase in all of the voices
available on the machine:

Sub IdentifyTheVoices()

Dim objVoice As SpeechLib.SpVoice
Dim objToken As SpeechLib.ISpeechObjectToken
Dim intLoop As Integer
Dim strVoice As String
Dim varVoices As Variant

Set objVoice = New SpVoice

' Get each token in the collection returned by GetVoices
' and concatentate its description into a string variable

For Each objToken In objVoice.GetVoices
strVoice = strVoice & objToken.GetDescription & ";"
Next

If Len(strVoice) > 0 Then

' Remove the extraneous semi-colon from the end
strVoice = Left$(strVoice, Len(strVoice) - 1)

' Split the concatenated string into an array of voice names.
varVoices = Split(strVoice, ";")

' Check that we actually have an array of voice descriptions
If IsNull(varVoices) = False Then

' Loop through the array of voice descriptions, speaking
' a phrase using each voice.

For intLoop = LBound(varVoices) To UBound(varVoices)
Set objVoice.Voice = objVoice.GetVoices().Item(intLoop)
objVoice.Speak "I'm currently speaking using voice " &
varVoices(intLoop)
Next intLoop

End If

End If

End Sub


Sorry: I didn't have a chance to look at speech to text over the weekend.
 
P

PC User

Thanks much Doug. I've been applying the examples given in the
documentation. I'm getting a little further along on this, but I'm
having trouble with the volume control and the speed rate. Also I can't
get the write to file or read from file to work, probably because the
original code is in a different version of visual basic. I'm thinking
that I should use the Common Dialog ActiveX for the read/write actions.
Any help on this is appreciated. The current file is located at:
http://www.access-programmers.co.uk/forums/attachment.php?attachmentid=11559
 
D

Douglas J Steele

Volume is a numeric value between 0 and 100:

Set objVoice = New SpVoice

objVoice.Volume = 75
objVoice.Speak "How do you do?"

There is no "Speed" property, but there is a Rate property. It's a numeric
property between -10 and 10:

Set objVoice = New SpVoice

For intLoop = -10 To 10 Step 2
objVoice.Rate = intLoop
objVoice.Speak "I'm speaking using rate " & intLoop
Next intLoop
 
P

PC User

Doug,

I get an "Error# -214720096: Automation error" when I try the
speech recognition, even with my microphone connected. Do you have any
idea what that means?

Thanks,
PC
 
T

Tom Wickerath

Hi PC,

I've still been following this thread, because I have some interest in this
SAPI functionality. Thanks for posting your latest sample too.

Here is a KB article that you might find helpful:

INFO: Translating Automation Errors for VB/VBA (Long)
http://support.microsoft.com/?id=186063


You can copy the first block of code shown, under "Use FormatMessage", and
paste it into a new module in Access. Change the Private Function to a Public
Function, so that you can call it from the Immediate window:

Change this:
Private Function MessageText(lCode As Long) As String

to this:
Public Function MessageText(lCode As Long) As String

However, when I do this, and then call the function from the immediate
window, I get the following result:

?messagetext(214720096)
Error not found.

Can you verify that you reported the correct error number?

Thanks,
Tom

________________________________________________

:

Doug,

I get an "Error# -214720096: Automation error" when I try the
speech recognition, even with my microphone connected. Do you have any
idea what that means?

Thanks,
PC
 
T

Tom Wickerath

PS. Including the minus sign yields the same message:

?MessageText(-214720096)
Error not found.

________________________________________________

:

Hi PC,

I've still been following this thread, because I have some interest in this
SAPI functionality. Thanks for posting your latest sample too.

Here is a KB article that you might find helpful:

INFO: Translating Automation Errors for VB/VBA (Long)
http://support.microsoft.com/?id=186063


You can copy the first block of code shown, under "Use FormatMessage", and
paste it into a new module in Access. Change the Private Function to a Public
Function, so that you can call it from the Immediate window:

Change this:
Private Function MessageText(lCode As Long) As String

to this:
Public Function MessageText(lCode As Long) As String

However, when I do this, and then call the function from the immediate
window, I get the following result:

?messagetext(214720096)
Error not found.

Can you verify that you reported the correct error number?

Thanks,
Tom

________________________________________________

:

Doug,

I get an "Error# -214720096: Automation error" when I try the
speech recognition, even with my microphone connected. Do you have any
idea what that means?

Thanks,
PC
 
P

PC User

Hi Tom,

It seems that I've gotten almost everything working, but the
speech recognition and the read/write functions. When I try the speech
recognition, I get the "Error# -214720096: Automation error". I tried
it several times and it seems unusual to get a negative error number.
I'm thinking it might have something with the network security at the
company where I work, but I'm not sure. Have you tried the read/write
function? The documentation file has some sample code that I'm trying.
Have you see it?

PC
 
T

Tom Wickerath

Hi PC,
...it seems unusual to get a negative error number.
It isn't unusual at all for the automation errors to be negative; in fact
most of them are negative. Scroll down the page of the KB article that I
provided a link to:

INFO: Translating Automation Errors for VB/VBA (Long)
http://support.microsoft.com/?id=186063

You should see lots of negative error numbers.
Have you tried the read/write function?
No, not yet. I also do not have a microphone currently connected to my PC,
so I cannot test out any speech recognition functionality.
The documentation file has some sample code that I'm trying.
Have you see it?

Yes, I downloaded the compiled help file Sapi.chm (thanks for that link
too), but I can't say that I've put any significant time into reading it at
this point.

Tom
_________________________________________

:

Hi Tom,

It seems that I've gotten almost everything working, but the
speech recognition and the read/write functions. When I try the speech
recognition, I get the "Error# -214720096: Automation error". I tried
it several times and it seems unusual to get a negative error number.
I'm thinking it might have something with the network security at the
company where I work, but I'm not sure. Have you tried the read/write
function? The documentation file has some sample code that I'm trying.
Have you see it?

PC
 
D

Douglas J Steele

Unfortunately, I only have Access 97 installed (plus no mike) here, and as
discussed previously, the code won't work in Access97 for speech
recognition: there's an Automation error message when you compile the code.

What line of code is causing the error?

Have you downloaded the entire SAPI SDK? When you install it, there are a
number of VB samples that install, including one called VB SimpleDict. Does
it work for you? (the code behind that app is essentially identical to
what's in the Egghead article)
 
P

PC User

In the btnWriteMySpeech command button's onclick event the line: "Set
RecoContext = New SpSharedRecoContext" creates the error "Error#
-214720096: Automation error". I don't know whether the subsequent
lines are good, because I haven't gotten past the first error. Also I
don't see any calls for the Initialize() function; so I don't know
where it should be called from.
Later today, I'll check out the "VB SimpleDict" to see if it works for
A2K.

Thanks,
PC
 
D

Douglas J. Steele

I finally got around to testing in Access 2003 (in Access 2000 format) using
a mike, and speech recognition worked fine for me.

Not sure what advice I can give you: I didn't need to make any changes to
the code at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Getting Text from Richtext 3
Listbox Popup Menu 0
problems publishing 5

Top