|
-
Aug 26th, 2009, 06:19 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] speech SDK grammar HOW TO?
hi guys.
im using a couple of commands
[search name, volume up / down / mute, mode day / night]
i cant seem to get the application to listen correctly.
ive tried adding the
.addpronunciation "volume"
etc line, but it still doesnt work
it runs through, detects speech
but it is nothing like what is being said. if i say hello, it thinks i said hawaii??? ive run through the training sessions, with no luck.
how hard is it to get this working, ive seen most ppl use the version four controls, which i cant find anywhere. i have downloaded and installed the sapi sdk5.1 suite from ms rebooted and everything but no ocx controls show
btw, i am using win xp pro version.
cheers guys.
vbcode Code:
Option Explicit
Dim WithEvents RecoContext As SpSharedRecoContext
Dim Grammar As ISpeechRecoGrammar
Dim m_bRecoRunning As Boolean
Public Sub LoadVoice()
' just a note as we a loading the API
btnStart.Caption = "Loading sAPI..."
DoEvents
' load some custom commands
'Dim addlex As SpUnCompressedLexicon
Dim addlex As SpLexicon
Set addlex = New SpLexicon
'Set addlex = New SpLexicon
Set RecoContext = New SpSharedRecoContext
Set Grammar = RecoContext.CreateGrammar(1)
' load the dictation application
Grammar.DictationLoad
addlex.AddPronunciation "Search", 1 ', SPSNoun
addlex.AddPronunciation "Search Artist", 1 ', SPSNoun
addlex.AddPronunciation "Search Album", 1 ', SPSNoun
addlex.AddPronunciation "Volume Mute", 1 ', SPSNoun
addlex.AddPronunciation "Volume On", 1 ', SPSNoun
'
addlex.AddPronunciation "Volume Up", 1 ', SPSNoun
addlex.AddPronunciation "Volume Down", 1 ', SPSNoun
addlex.AddPronunciation "Volume One Percent", 1 ', SPSNoun
addlex.AddPronunciation "Volume Twenty Percent", 1 ', SPSNoun
addlex.AddPronunciation "Player Play", 1 ', SPSNoun
addlex.AddPronunciation "Player Pause", 1 ', SPSNoun
addlex.AddPronunciation "Player Next", 1 ', SPSNoun
addlex.AddPronunciation "Player Previous", 1 ', SPSNoun
addlex.AddPronunciation "Mode Day", 1 ', SPSNoun
addlex.AddPronunciation "Mode Night", 1 ', SPSNoun
addlex.AddPronunciation "Voice OFF", 1 ', SPSNoun
addlex.AddPronunciation "View rear camera", 1 ', SPSNoun
addlex.AddPronunciation "View front camera", 1 ', SPSNoun
addlex.AddPronunciation "Help", 1 ', SPSNoun
' say the voice commands are now listening
RecoContext.Voice.Speak "Hello. I am " & App.Title & ". " & _
"If you require help just say help. " & _
"Don't forget to press the command button before speaking."
End Sub
Private Sub btnExit_Click()
If m_bRecoRunning = True Then
Grammar.DictationUnload
Set Grammar = Nothing
Set RecoContext = Nothing
End If
Unload Me
End Sub
Private Sub btnListen_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If RecoContext Is Nothing Then LoadVoice
btnListen.BackColor = vbGreen
' Start the dictation.
Grammar.DictationSetState SGDSActive
Debug.Print "mouse down event..."
End Sub
Private Sub btnListen_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Debug.Print "mouse up event..."
' stop it
Grammar.DictationSetState SGDSInactive
btnListen.BackColor = clBack
End Sub
' This function handles Recognition event from the reco context object.
' Recognition event is fired when the speech recognition engines recognizes
' a sequences of words.
Private Sub RecoContext_Recognition(ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal RecognitionType As SpeechRecognitionType, _
ByVal Result As ISpeechRecoResult _
)
Dim strText As String
strText = Result.PhraseInfo.GetText
Debug.Print "Recognition: " & strText & ", " & _
StreamNumber & ", " & StreamPosition
End Sub
like i said, its working... just not how i want it to... i kinda remember playing with the controls in college, and it worked pretty well, but for some reason i dont have the ocx files like i said.. BUT i do have voice recognition - as i said, it is writing out, no errors, just complete dutch... lol
Last edited by wpearsall; Aug 26th, 2009 at 07:25 PM.
Reason: added vbcode
Wayne
-
Aug 27th, 2009, 10:30 AM
#2
Frenzied Member
Re: speech SDK grammar HOW TO?
Okay, you are speaking english, you download the english version and not the version for Japanese and Simplified Chinese then have you tried the speech recognition profile manager tool?
http://www.microsoft.com/downloads/d...displaylang=en
Good Luck
Option Explicit should not be an Option!
-
Aug 27th, 2009, 02:25 PM
#3
Thread Starter
Frenzied Member
Re: speech SDK grammar HOW TO?
yes, english version, i speak english. 
i have run all of the training program etc from xp [not sure if i mentioned that]
also i notice that there is no phrase finish for the code im using... is this some sort of error? ... i tried re installing the SDK software also, with no luck. - i just dont get why i have no OCX controls to add, just the reference....
i will check out what you posted in a bit, im at my friends on my laptop atm, so cant do any coding since its all at home on my tower.
-
Aug 28th, 2009, 08:00 AM
#4
Frenzied Member
Re: speech SDK grammar HOW TO?
It might be MS Agent you are thinking of???...
Option Explicit should not be an Option!
-
Aug 28th, 2009, 02:28 PM
#5
Thread Starter
Frenzied Member
Re: speech SDK grammar HOW TO?
does MS agent have the voice ocx files in system32 directory?... i'm sure i didnt download anything like that before, but its been a while since ive dont any coding in VB [about two years :S - lol i struggled to use the left$ function on my latest venture at first, but it all soon came flying back :P
erm ill d/l the agent and check... i would rather use basic voice commands for some stuff then have to hotwire a load of buttons onto a control [although i have got a keyboard all wired and ready now :P - but voice = cleaner...
-
Aug 29th, 2009, 06:55 AM
#6
Thread Starter
Frenzied Member
Re: speech SDK grammar HOW TO?
OK, ive downloaded and installed the MS agent
the direct, a txtbox, and a command button on the form
code i have:
vbcode Code:
Option Explicit
Private Sub Command1_Click()
DirectSR.GrammarFromFile App.Path & "\commands.txt"
Debug.Print DirectSR.Initialized & " < initialized" ' shows 1
DirectSR.Activate
DirectSR.Listen ' tried with and without this....
Debug.Print DirectSR.Initialized & " < activated initialized" ' shows 1
End Sub
Private Sub DirectSR_PhraseFinish(ByVal flags As Long, ByVal beginhi As Long, ByVal beginlo As Long, ByVal endhi As Long, ByVal endlo As Long, ByVal Phrase As String, ByVal parsed As String, ByVal results As Long)
txtcommand.Text = Phrase
End Sub
Private Sub Form_Unload(Cancel As Integer)
DirectSR.Deactivate
End Sub
commands.txt
Code:
[Grammar]
langid=1031
type=cfg
[<start>]
<start>=Microphone
<start>=Search Name
<start>=Search Artist
<start>=Search Album
<start>=Volume Mute
<start>=Volume On
<start>=Volume Up
<start>=Volume Down
<start>=Player Play
<start>=Player Pause
<start>=Player Stop
<start>=Player Next
<start>=Player Back
<start>=Mode Night
<start>=Mode Day
<start>=Voice Off
<start>=Camera Rear
<start>=Camera Front
<start>=Help
it never runs phrase finish - even though i know VR recognises when i say microphone (I added that as i know its the only word it NEVER misunderstands)
-
Aug 29th, 2009, 09:16 PM
#7
Frenzied Member
Re: speech SDK grammar HOW TO?
Okay, lets start over...
Your speaking english and you have the english SDK but things are not working right when you speak into your microphone. The speech to text engine does not recognize what you are saying correctly. In research of this, I came across a thread that was eventually resolved by a better microphone.
Now, it was not my intention to steer you towards the MSAgent SDK especially since it is another discontinued product by MS, but when you said you remembered playing with the controls I was wondering if you might have been thinking of the MSAgent contols/SDK.
Here is a tutorial on the MSAgent SDK...
http://www.a1vbcode.com/app-2627.asp
and here is a quickie tutorial on the speech sdk... (6 very short pages)
http://www.developerfusion.com/artic...ch-recognition
While this one comes with a link to the version 4.0 Speech Recognition SDK and some sample code (3 very short pages)
http://www.developerfusion.com/artic...text-to-speech
Good Luck
Option Explicit should not be an Option!
-
Aug 30th, 2009, 11:27 AM
#8
Thread Starter
Frenzied Member
-
Aug 31st, 2009, 09:02 AM
#9
Frenzied Member
Re: speech SDK grammar HOW TO?
I don't know if you can use it because all I found was the C/C++ sources for it that you would have to compile, but if you see this page...
http://msdn.microsoft.com/en-us/libr...94(VS.85).aspx
you will see where it says...
Microsoft Speech API 5.3
Using the Visual Basic Code Examples
Prerequisites
To run the code examples contained in this documentation, your computer must have the following installed:
SAPI 5.1
Visual Basic 5.0, Visual Basic 6.0, or Visual Basic.Net
Speakers
A microphone is helpful, but not necessary, for demonstrating speech recognition.
...
So yes it does seem that you will be able to use 5.3 with VB5/6/Net (if this is not another MSPrint (misprint by MS))
(If going fully interactive you might want this...
http://www.microsoft.com/downloads/d...f-3c31c14769b7
a bunch of different voices, but requires a boat load more files to download to use.)
Good Luck
Option Explicit should not be an Option!
-
Aug 31st, 2009, 01:18 PM
#10
Thread Starter
Frenzied Member
Re: speech SDK grammar HOW TO?
-
Aug 31st, 2009, 03:48 PM
#11
Thread Starter
Frenzied Member
Re: speech SDK grammar HOW TO?
ok guys,
commands.xml
Code:
<GRAMMAR LANGID="409">
<DEFINE>
<ID NAME="VID_MainCom" VAL="255"/>
<ID NAME="VID_Search" VAL="256"/>
<ID NAME="VID_Volume" VAL="257"/>
<ID NAME="VID_Player" VAL="258"/>
<ID NAME="VID_Mode" VAL="259"/>
<ID NAME="VID_Camera" VAL="260"/>
<ID NAME="VID_Voice" VAL="261"/>
<ID NAME="VID_Help" VAL="262"/>
<ID NAME="oID_SAlbum" VAL="0"/>
<ID NAME="oID_STrack" VAL="1"/>
<ID NAME="oID_SArtist" VAL="2"/>
<ID NAME="oID_VUP" VAL="3"/>
<ID NAME="oID_VDOWN" VAL="4"/>
<ID NAME="oID_VMAX" VAL="5"/>
<ID NAME="oID_VMUTE" VAL="6"/>
<ID NAME="oID_Off" VAL="7"/>
<ID NAME="oID_On" VAL="8"/>
<ID NAME="oID_CFront" VAL="9"/>
<ID NAME="oID_CRear" VAL="10"/>
<ID NAME="oID_COff" VAL="11"/>
<ID NAME="oID_VOff" VAL="12"/>
<ID NAME="oID_HSearch1" VAL="13"/>
<ID NAME="oID_HSearch2" VAL="14"/>
<ID NAME="oID_HSearch3" VAL="15"/>
<ID NAME="oID_HSearch4" VAL="16"/>
<ID NAME="oID_PPlay" VAL="17"/>
<ID NAME="oID_PPause" VAL="18"/>
<ID NAME="oID_PStop" VAL="19"/>
<ID NAME="oID_PPrev" VAL="20"/>
<ID NAME="oID_PNext" VAL="21"/>
<ID NAME="oID_PTrack" VAL="22"/>
</DEFINE>
<RULE ID="VID_MainCom" TOPLEVEL="ACTIVE">
<P>Computer</P>
<P>Radio</P>
<P>
<RULEREF REFID="VID_Search" />
<RULEREF REFID="VID_Volume" />
<RULEREF REFID="VID_Player" />
<RULEREF REFID="VID_Mode" />
<RULEREF REFID="VID_Camera" />
<RULEREF REFID="VID_Voice" />
<RULEREF REFID="VID_Help" />
</P>
</RULE>
<RULE ID="VID_Search">
<L PROPID="VID_Search">
<P VAL="oID_SArtist">Artist</P>
<P VAL="oID_SAlbum">Album</P>
<P VAL="oID_STrack">Track</P>
</L>
</RULE>
<RULE ID="VID_Volume">
<L PROPID="VID_Volume">
<P VAL="oID_VUP">Up</P>
<P VAL="oID_VDOWN">Down</P>
<P VAL="oID_VMAX">Maximum</P>
<P VAL="oID_VMUTE">Mute</P>
</L>
</RULE>
<RULE ID="VID_Player">
<L PROPID="VID_Player">
<P VAL="oID_PPlay">Play</P>
<P VAL="oID_PPause">Pause</P>
<P VAL="oID_PStop">Stop</P>
<P VAL="oID_PPrev">Previous</P>
<P VAL="oID_PNext">Next</P>
<P VAL="oID_PTrack">Track</P>
</L>
</RULE>
<RULE ID="VID_Mode">
<L PROPID="VID_Mode">
<P VAL="oID_Off">Day</P>
<P VAL="oID_On">Night</P>
</L>
</RULE>
<RULE ID="VID_Camera">
<L PROPID="VID_Camera">
<P VAL="oID_COff">Off</P>
<P VAL="oID_CFront">Front</P>
<P VAL="oID_CRear">Rear</P>
</L>
</RULE>
<RULE ID="VID_Voice">
<L PROPID="VID_Voice">
<P VAL="oID_VOff">Off</P>
</L>
</RULE>
<RULE ID="VID_Help">
<L PROPID="VID_Help">
<P VAL="oID_HSearch1">Search</P>
<P VAL="oID_HSearch2">Search Artist</P>
<P VAL="oID_HSearch3">Search Album</P>
<P VAL="oID_HSearch4">Search Track</P>
</L>
</RULE>
</GRAMMAR>
can anyone see why that wont load as the grammar file? - im lost now? i know its something to do with my grammar xml, as the example xml file will load and works... :S
cheers
-
Aug 31st, 2009, 04:37 PM
#12
Thread Starter
Frenzied Member
Re: speech SDK grammar HOW TO?
you know, ive gone to one by one adding on the grammar
Code:
<GRAMMAR LANGID="409">
<DEFINE>
<ID NAME="VID_CSearch" VAL="201" />
<ID NAME="VID_CVolume" VAL="202" />
<ID NAME="VID_CPlayer" VAL="203" />
<ID NAME="VID_CMode" VAL="204" />
<ID NAME="VID_CCamera" VAL="205" />
<ID NAME="VID_CVoice" VAL="206" />
<ID NAME="VID_CHelp" VAL="207" />
<ID NAME="VID_SArtist" VAL="301" />
<ID NAME="VID_SAlbum" VAL="302" />
<ID NAME="VID_SName" VAL="303" />
<ID NAME="VID_STrack" VAL="304" />
<ID NAME="VID_CmdType" VAL="101"/>
<ID NAME="VID_Commands" VAL="102"/>
<ID NAME="VID_Search" VAL="103"/>
</DEFINE>
<RULE ID="VID_Commands" TOPLEVEL="ACTIVE">
<O>Computer</O>
<RULEREF REFID="VID_CmdType" />
</RULE>
<RULE ID="VID_CmdType" >
<L PROPID="VID_CmdType">
<P VAL="VID_CSearch">Search
<O><RULEREF REFID="VID_Search" /></O>
</p>
<P VAL="VID_CVolume">Volume</P>
<P VAL="VID_CPlayer">Player</P>
<P VAL="VID_CMode">Mode</P>
<P VAL="VID_CCamera">Camera</P>
<P VAL="VID_CVoice">Voice</P>
<P VAL="VID_CHelp">Help</P>
</L>
</RULE>
<RULE ID="VID_Search" >
<L PROPID="VID_Search">
<p VAL="VID_SName">Name</p>
<p VAL="VID_SArtist">Artist</p>
<p VAL="VID_SAlbum">Album</p>
<p VAL="VID_STrack">Track</p>
</L>
</RULE>
</GRAMMAR>
this is now working...
i can Search, Search artist, search track etc, volume, etc
i cannot "Artist"
so i think ive solved it.
IF its working when its all linked i will resolve this thread
-
Aug 31st, 2009, 05:33 PM
#13
Thread Starter
Frenzied Member
Re: speech SDK grammar HOW TO?
ok boys and girls...
this is now solved.... Thanks for help
since i couldnt find a FULL listing on the forums for what i needed to do i will now post my code for how i have it working...
vb Code:
' btnStart: Loads Engine - calling loadvoice, which calls loadgrammar - if loaded it unloads it
' btnlisten: if the engine is loaded - enables / disables VC listening - if engine is unloaded it loads it first
Option Explicit
' the actual control we are using
Dim WithEvents RecoContext As SpSharedRecoContext
' the grammar we are working from
Dim Grammar As ISpeechRecoGrammar
' weather or not the grammar and recognition engine are loaded
Dim m_bRecoRunning As Boolean
' < this will b false if the engine is not loaded, but it if the grammar fails to load also...
Dim bGrammarLoaded As Boolean
' to load the voice rec engine
Public Function LoadVoice() As Boolean
On Error GoTo ErrHand:
' just a note as we a loading the API
btnStart.Caption = "Loading sAPI..."
DoEvents
' create a new engine
Set RecoContext = New SpSharedRecoContext
' make a new grammar file to edit
Set Grammar = RecoContext.CreateGrammar(0)
' Load Dictation but set it to Inactive
Grammar.DictationLoad "", SLOStatic
Grammar.DictationSetState SGDSInactive
' Attempt to load the default .xml file and set the RuleId State to Inactive until
' the user starts recognition.
'bGrammarLoaded = LoadDefaultCnCGrammar(App.Path & "\Grammar.xml")
'bGrammarLoaded = LoadDefaultCnCGrammar("E:\Documents and Settings\Wayne\Desktop\Carstra Media Player\speech rec\sapitutorial00\sapi0.xml")
bGrammarLoaded = LoadDefaultCnCGrammar(App.Path & "\sapi0.xml")
' if the grammar file was loaded - it may fail if the syntax is messed up
If bGrammarLoaded = True Then
' say that we are now running.
LoadVoice = True
' you may wish to inform the user that voice activation is enabled now...
' .speak is SLOW for me on my user machine, so im using it only when necessary
With RecoContext.Voice
'.Speak "Hello. I am " & App.Title & ". "
'.Speak "If you require help just say help. "
'.Speak "Don't forget to press the command button before speaking."
End With
Else
' if the file didnt load raise an error
Err.Raise -1
End If
Exit Function
ErrHand:
On Error Resume Next
' destry the things created
Grammar.DictationUnload
Set Grammar = Nothing
Set RecoContext = Nothing
btnStart.Caption = "Error: Failed loading voice"
' and return that we failed to load the engine
LoadVoice = False
Err.Clear
End Function
Private Function LoadDefaultCnCGrammar(sFilepath As String) As Boolean
On Error Resume Next
' First load attempt
Grammar.CmdLoadFromFile sFilepath, SLODynamic
' Second load attempt
If Err Then
On Error GoTo Err_CFGLoad
Grammar.CmdLoadFromFile sFilepath, SLODynamic
End If
' Set rule state to inactive until user clicks Recognition button
Grammar.CmdSetRuleIdState 0, SGDSInactive
LoadDefaultCnCGrammar = True
Exit Function
Err_CFGLoad:
LoadDefaultCnCGrammar = False
Exit Function
End Function
' i use a frame control to enable and disable recognition - using mouse down and up
Private Sub btnListen_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If RecoContext Is Nothing Then if not LoadVoice then beep: Exit
btnListen.BackColor = vbGreen
' Start the dictation. - this is if your using normal dictation and not a grammar XML
'Grammar.DictationSetState SGDSActive
' listen for the rules in XML file - this is what we want...
Grammar.CmdSetRuleIdState 0, SGDSActive
Debug.Print "mouse down event..."
End Sub
Private Sub btnListen_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Debug.Print "mouse up event..."
' stop it
'Grammar.DictationSetState SGDSInactive
Grammar.CmdSetRuleIdState 0, SGDSInactive
btnListen.BackColor = BackColor
End Sub
Private Sub btnStart_Click()
' check if voice recognition is running.
Select Case m_bRecoRunning
' if it is running
Case True
Grammar.DictationUnload
Set Grammar = Nothing
Set RecoContext = Nothing
m_bRecoRunning = False
' if not started
Case False:
' load the speech engine
m_bRecoRunning = LoadVoice
If Not m_bRecoRunning Then btnStart.Caption = "Cannot Start!": Exit Sub
End Select
' make a note of voice command state
btnStart.Caption = IIf(m_bRecoRunning, "*** ON ***", "- OFF -")
End Sub
' This function handles Recognition event from the reco context object.
' Recognition event is fired when the speech recognition engines recognizes
' a sequences of words.
Private Sub RecoContext_Recognition(ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal RecognitionType As SpeechRecognitionType, _
ByVal Result As ISpeechRecoResult _
)
Dim strText As String
strText = Result.PhraseInfo.GetText
Debug.Print "Recognition: " & strText & ", " & _
StreamNumber & ", " & StreamPosition
' i plan to handle like:
' shandletext() = split(strtext," ")
' select case shandletext(0) -> case "Search" ETC - im sure you get the idea...
End Sub
Private Sub Form_Unload(Cancel As Integer)
' i try to always clean up after myself... frees the resources incase its not done for me
If m_bRecoRunning = True Then
Grammar.DictationUnload
Set Grammar = Nothing
Set RecoContext = Nothing
End If
End Sub
The start of My Grammar XML is the following
Code:
<GRAMMAR LANGID="409">
<DEFINE>
<ID NAME="VID_CSearch" VAL="201" />
<ID NAME="VID_CVolume" VAL="202" />
<ID NAME="VID_CPlayer" VAL="203" />
<ID NAME="VID_CMode" VAL="204" />
<ID NAME="VID_CCamera" VAL="205" />
<ID NAME="VID_CVoice" VAL="206" />
<ID NAME="VID_CHelp" VAL="207" />
<ID NAME="VID_SArtist" VAL="301" />
<ID NAME="VID_SAlbum" VAL="302" />
<ID NAME="VID_SName" VAL="303" />
<ID NAME="VID_STrack" VAL="304" />
<ID NAME="VID_CmdType" VAL="101"/>
<ID NAME="VID_Commands" VAL="102"/>
<ID NAME="VID_Search" VAL="103"/>
</DEFINE>
<RULE ID="VID_Commands" TOPLEVEL="ACTIVE">
<O>Computer</O>
<RULEREF REFID="VID_CmdType" />
</RULE>
<RULE ID="VID_CmdType" >
<L PROPID="VID_CmdType">
<P VAL="VID_CSearch">Search
<O><RULEREF REFID="VID_Search" /></O>
</p>
<P VAL="VID_CVolume">Volume</P>
<P VAL="VID_CPlayer">Player</P>
<P VAL="VID_CMode">Mode</P>
<P VAL="VID_CCamera">Camera</P>
<P VAL="VID_CVoice">Voice</P>
<P VAL="VID_CHelp">Help</P>
</L>
</RULE>
<RULE ID="VID_Search" >
<L PROPID="VID_Search">
<p VAL="VID_SName">Name</p>
<p VAL="VID_SArtist">Artist</p>
<p VAL="VID_SAlbum">Album</p>
<p VAL="VID_STrack">Track</p>
</L>
</RULE>
</GRAMMAR>
ive made it a bit more of a layered system... but its all solved now... NOTE: Grammar ID is set to 409 - THIS IS NOT AN ERROR.... for some reason thats what it needs to be set to... 
oh just a note : if the grammar file doesnt load, then pressing the btnListen control will throw an error, since there is no rule set to listen out for....
Last edited by wpearsall; Aug 31st, 2009 at 05:40 PM.
Wayne
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|