在 VBS 脚本中使用 SAPI 语音识别? [英] Use of SAPI Speech Recognition in a VBS Script?
问题描述
我发现了这个允许在 VBScript 中使用 Windows SAPI 文本到语音功能的单行示例:
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")
我想知道 SAPI 语音识别是否可以以同样简单的方式在 VBScript 程序中使用.当我寻找此类信息时,出现的大量 SAPI 信息与 C++ 相关,例如 Microsoft SAPI 站点,或到 VBS 中的 Text-to-Speech.我试图找到有关可以在 VBScript 中使用的 SAPI COM 对象语音识别部分的文档,但没有找到.
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.
您知道是否存在这样的文档吗?TIA
Do you know if such a documentation exists? TIA
编辑:在收到第一个答案后添加的附加请求
尽管下面的第一个答案提供了指向 SAPI COM 对象文档的链接,但我想吸引您注意我问题中的一点:我想知道 SAPI 语音识别是否可以在 VBScript 程序中使用 IN同样简单的方法".SAPI 文档巨大!我读了好几页,完全迷失了……我的目标是仅识别几个单词,比如 8 或 10 个,并且每次识别出其中一个单词时在屏幕上显示不同的消息;这就对了!(该程序应该是一个通过 cscript
启动的控制台应用程序).是否有一个简单的 VBS 代码示例来实现这样的功能?如果编写此解决方案所需的代码需要有几页,那么这不是我要寻找的答案...
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...
推荐答案
这是一个 vbscript 监听 wav 文件的工作示例:
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"
代码的神奇部分是这一行(rc_"前缀 参数允许事件被 subs 捕获):
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_")
我用于测试的 t.wav 文件中的录制文本是使用 SAPI.SpVoice::Speak 和 MS-David voice 生成的 ;-)
The recorded text in the t.wav file I used for testing has been generated with SAPI.SpVoice::Speak and MS-David voice ;-)
我花了 10 天的时间来研究如何编写这个脚本.Microsoft 正在删除有关自动化、COM、旧式脚本等的文档.很遗憾.
I spent 10 days figuring out how to write this script. Microsoft is removing documentation about automation, COM, old style scripts, etc. A shame.
因此,这可以在听写模式下读取 wav 文件.但我无法纠正它以使其在实时听写模式下工作(即使用麦克风作为直接输入).任何帮助表示赞赏.谢谢.
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.
直接/实时听写模式已解决.如果有兴趣,我分享 vbscript 代码.
direct/live dictation mode solved. If interested I share the vbscript code.
wav 中所说的文本示例:Hello world.这是关于使用圆渐开线的齿轮齿廓.
vbscript 的控制台输出
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>
这篇关于在 VBS 脚本中使用 SAPI 语音识别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!