3

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

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • 1
    Did you take a look at the [C&C recognition code sample](http://msdn.microsoft.com/en-us/library/ee125183%28v=vs.85%29.aspx)? – Ansgar Wiechers Jun 04 '14 at 11:19
  • @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.8 – Aacini Jun 05 '14 at 01:50
  • 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 Jun 05 '14 at 08:51
  • Did you find yourself anything new since then? – Wolf Dec 07 '16 at 09:34
  • @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 Dec 07 '16 at 16:58
  • @Aacini I see, thanks for the short report. – Wolf Dec 07 '16 at 21:03
  • @Wolf, Aacini: I just posted an answer with a working vbscript example. – Carl Jul 13 '20 at 17:06

2 Answers2

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>
Carl
  • 139
  • 7
  • Great to read it works for you. If you as well add the text-to-wav part, there is a big chance to collect positive replies for this answer. Thanks in advance. – Wolf Jul 14 '20 at 08:08
  • 1
    @Wolf : You mean text-to-speech saved in wav file ? If you share the link with the question I'd be glad to answer. The OP Aacini's question is about speech-to-text, so I just answered to that ;-) – Carl Jul 14 '20 at 08:55
  • I see. BTW (finding or asking the question will take some time): did you ever encounter problems with unicode in the text source? I failed with this when trying to output Polish numbers (Polish language was installed successfully, but VB seemed to have problems with utf-8 input), I really have little experience with VB Script... – Wolf Jul 14 '20 at 09:28
  • 1
    @Wolf : here is an answered *text-to-speech saved to wav file* question: https://stackoverflow.com/questions/20498004/how-to-save-sapi-text-to-speech-to-an-audio-file-in-vbscript . I'm used to english and latin languages, I didn't encountered encoding issues (yet...). – Carl Jul 14 '20 at 09:59
  • Thanks :-) Perhaps I should have added that I was not interested in getting told the reverse part of above (which I already tried since it's a lot easier), but in the self-containment of this example. (IOW, `Debug.Assert(text = codec(text))`) – Wolf Jul 14 '20 at 10:45
0

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

Eric Brown
  • 13,774
  • 7
  • 30
  • 71
  • Thanks for your answer. Please, see the edit in my question above. If I don't receive an answer with additional information in a reasonable lapse, I will select this answer... – Aacini Jun 04 '14 at 08:31
  • You could take http://msdn.microsoft.com/en-us/library/ee125181(v=vs.85).aspx as a starting point. Still contains stuff you don´t want to know, but...compact enough to create you own experiments. – TheBlastOne Jun 04 '14 at 10:02
  • @ansgar-wiechers comment is a pretty good starting point. SR is simply more difficult than TTS; you have to define what you're listening for, and then do something with the results. – Eric Brown Jun 04 '14 at 16:03