Use of SAPI Speech Recognition in a VBS Script?

无人久伴 提交于 2020-07-19 04:29:32

问题


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


回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/24027773/use-of-sapi-speech-recognition-in-a-vbs-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!