Use of SAPI Speech Recognition in a VBS Script?

Aacini picture Aacini · Jun 4, 2014 · Viewed 7.4k times · Source

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...

Answer

Carl picture Carl · Jul 13, 2020

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>