3
votes

I found this one-line example that allows to use the Windows SAPI Text-to-Speech feature in VBScript:

CreateObject("SAPI.SpVoice").Speak("This is a test")

I wonder if the SAPI Speech Recognition could be used in a VBScript program in the same easy way. When I seek for such information the tons of SAPI information that appear are related to C++, like the Microsoft SAPI site, or to Text-to-Speech in VBS. I tried to find documentation about the SAPI COM object Speech Recognition part that could be used in a VBScript, but found none.

Do you know if such a documentation exists? TIA

EDIT: Additional request added after the first answer was recevied

Although the first answer below provide a link to the SAPI COM object documentation, I want to attract your attention to a point in my question: "I wonder if the SAPI Speech Recognition could be used in a VBScript program IN THE SAME EASY WAY". The SAPI documentation is huge! I read several pages of it and I am completely lost... My goal is to recognize just a few single words, say 8 or 10, and show a different message in the screen each time that one of they was recognized; that is it! (The program should be a console application started via cscript). Is there a simple example of VBS code that achieve such thing? If the required code to program this solution needs to have several pages, then it is not the answer I am looking for...

2
Did you take a look at the C&C recognition code sample?Ansgar Wiechers
@AnsgarWiechers: I copy-pasted the example at that link in test.vbs file, executed it via cscript //nologo test.vbs and got this (equivalent) error: "test.vbs(1, 19) Microsoft VBScript compiler error: End of instruction expected". I use Windows 8.1 Spanish version and cscript Windows Script Host version 5.8Aacini
I think you need to make some adjustments to the code, since it's VB, not VBScript. The latter doesn't support things like On Error Goto MARK, and you'd have to replace object creation of the form Set RC = New SpSharedRecoContext with CreateObject() and the full component name: Set RC = CreateObject("SAPI.SpSharedRecoContext").Ansgar Wiechers
Did you find yourself anything new since then?Wolf
@Wolf: No, sorry. I tried to write a working example using all the compiled data, but the program did not worked in VBS, that is, in a program compiled by cscript.exe. There are a series of changes intended to adjust the program to the VBS environment, but they never worked. It is much simpler to write a C++ program for this...Aacini

2 Answers

1
votes

Here is a working example of vbscript listening a wav file:

scriptRunning = true

Sub rc_Recognition(StreamNumber, StreamPosition, RecognitionType, Result)
  Wscript.Echo "Reco: ", Result.PhraseInfo.GetText, ".", RecognitionType
End Sub

Sub rc_StartStream(StreamNumber, StreamPosition)
  Wscript.Echo "Start: ", StreamNumber, StreamPosition
End Sub

Sub rc_EndStream(StreamNumber, StreamPosition, StreamReleased)
  Wscript.Echo "End: ", StreamNumber, StreamPosition, StreamReleased
  scriptRunning = false
End Sub


outwav = "C:\SOFT\projects\af2t\t.wav"
Const SAFT22kHz16BitMono = 22
Const SSFMOpenForRead = 0

set sapiFStream = CreateObject("SAPI.SpFileStream")
sapiFStream.Format.Type = SAFT16kHz16BitMono
sapiFStream.Open outwav, SSFMOpenForRead


MsgBox "A SpeechLib::ISpRecoContext object will be created"

Const SGDSActive = 1

Set rct = WScript.CreateObject("SAPI.SpInProcRecoContext", "rc_")
Set rgnz = rct.Recognizer
Set rgnz.AudioInputStream = sapiFStream
Set rcGrammar = rct.CreateGrammar
'rcGrammar.DictationLoad
rcGrammar.DictationSetState SGDSActive
i = 0
while scriptRunning and i < 100
  WScript.Sleep(50)
  i = i + 1
wend

MsgBox "A SpeechLib::ISpRecoContext object has been created"

The magical part of the code is this line (the "rc_" prefix param allows events to be caught by the subs):

Set rct = WScript.CreateObject("SAPI.SpInProcRecoContext", "rc_")

The recorded text in the t.wav file I used for testing has been generated with SAPI.SpVoice::Speak and MS-David voice ;-)

I spent 10 days figuring out how to write this script. Microsoft is removing documentation about automation, COM, old style scripts, etc. A shame.

So, this works in dictation mode reading a wav file. But I couldn't correct it to make it work in live dictation mode (i.e. using the microphone as direct input). Any help appreciated for this. Thanks.

EDIT: direct/live dictation mode solved. If interested I share the vbscript code.

EDIT2: text sample spoken in the wav: Hello world. This is a talk about gear tooth profile using a circle involute. Console output from the vbscript

C:\SOFT\projects\af2t>cscript r.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. Tous droits réservés.

Start:  1 0
Reco:  Hello world . 0
Reco:  this is a talk about gear to the profile using a circle invalid . 0
End:  1 195040 -1

C:\SOFT\projects\af2t>
0
votes

Yes. Look at the SAPI Automation Overview; it will tell you all about the late-bound COM interfaces and objects available to VBScript.