Results 1 to 13 of 13

Thread: [RESOLVED] speech SDK grammar HOW TO?

  1. #1

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    Resolved [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:
    1. Option Explicit
    2.  
    3. Dim WithEvents RecoContext As SpSharedRecoContext
    4. Dim Grammar As ISpeechRecoGrammar
    5.  
    6. Dim m_bRecoRunning As Boolean
    7.  
    8. Public Sub LoadVoice()
    9.  
    10.         ' just a note as we a loading the API
    11.         btnStart.Caption = "Loading sAPI..."
    12.         DoEvents
    13.        
    14.        
    15.         ' load some custom commands
    16.         'Dim addlex As SpUnCompressedLexicon
    17.         Dim addlex As SpLexicon
    18.        
    19.         Set addlex = New SpLexicon
    20.        
    21.         'Set addlex = New SpLexicon
    22.         Set RecoContext = New SpSharedRecoContext
    23.         Set Grammar = RecoContext.CreateGrammar(1)
    24.        
    25.         ' load the dictation application
    26.         Grammar.DictationLoad
    27.        
    28.         addlex.AddPronunciation "Search", 1 ', SPSNoun
    29.         addlex.AddPronunciation "Search Artist", 1 ', SPSNoun
    30.         addlex.AddPronunciation "Search Album", 1 ', SPSNoun
    31.        
    32.         addlex.AddPronunciation "Volume Mute", 1 ', SPSNoun
    33.         addlex.AddPronunciation "Volume On", 1 ', SPSNoun
    34.         '
    35.         addlex.AddPronunciation "Volume Up", 1 ', SPSNoun
    36.         addlex.AddPronunciation "Volume Down", 1 ', SPSNoun
    37.         addlex.AddPronunciation "Volume One Percent", 1 ', SPSNoun
    38.         addlex.AddPronunciation "Volume Twenty Percent", 1 ', SPSNoun
    39.        
    40.         addlex.AddPronunciation "Player Play", 1 ', SPSNoun
    41.         addlex.AddPronunciation "Player Pause", 1 ', SPSNoun
    42.         addlex.AddPronunciation "Player Next", 1 ', SPSNoun
    43.         addlex.AddPronunciation "Player Previous", 1 ', SPSNoun
    44.        
    45.         addlex.AddPronunciation "Mode Day", 1 ', SPSNoun
    46.         addlex.AddPronunciation "Mode Night", 1 ', SPSNoun
    47.         addlex.AddPronunciation "Voice OFF", 1 ', SPSNoun
    48.        
    49.         addlex.AddPronunciation "View rear camera", 1 ', SPSNoun
    50.         addlex.AddPronunciation "View front camera", 1 ', SPSNoun
    51.        
    52.         addlex.AddPronunciation "Help", 1 ', SPSNoun
    53.        
    54.         ' say the voice commands are now listening
    55.         RecoContext.Voice.Speak "Hello.  I am " & App.Title & ". " & _
    56.                                 "If you require help just say help. " & _
    57.                                 "Don't forget to press the command button before speaking."
    58.                                
    59.  
    60. End Sub
    61.  
    62. Private Sub btnExit_Click()
    63.  
    64.    
    65.     If m_bRecoRunning = True Then
    66.    
    67.         Grammar.DictationUnload
    68.         Set Grammar = Nothing
    69.         Set RecoContext = Nothing
    70.  
    71.     End If
    72.    
    73.     Unload Me
    74.  
    75.  
    76. End Sub
    77.  
    78. Private Sub btnListen_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    79.  
    80.     If RecoContext Is Nothing Then LoadVoice
    81.    
    82.     btnListen.BackColor = vbGreen
    83.        
    84.     ' Start the dictation.
    85.     Grammar.DictationSetState SGDSActive
    86.  
    87.     Debug.Print "mouse down event..."
    88.  
    89. End Sub
    90.  
    91. Private Sub btnListen_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    92.    
    93.    
    94.     Debug.Print "mouse up event..."
    95.    
    96.     ' stop it
    97.     Grammar.DictationSetState SGDSInactive
    98.    
    99.     btnListen.BackColor = clBack
    100.  
    101. End Sub
    102.  
    103. ' This function handles Recognition event from the reco context object.
    104. ' Recognition event is fired when the speech recognition engines recognizes
    105. ' a sequences of words.
    106. Private Sub RecoContext_Recognition(ByVal StreamNumber As Long, _
    107.                                     ByVal StreamPosition As Variant, _
    108.                                     ByVal RecognitionType As SpeechRecognitionType, _
    109.                                     ByVal Result As ISpeechRecoResult _
    110.                                     )
    111.                                    
    112.     Dim strText As String
    113.     strText = Result.PhraseInfo.GetText
    114.    
    115.     Debug.Print "Recognition: " & strText & ", " & _
    116.         StreamNumber & ", " & StreamPosition
    117.  
    118. 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

  2. #2
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    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!

  3. #3

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

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

  4. #4
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: speech SDK grammar HOW TO?

    It might be MS Agent you are thinking of???...
    Option Explicit should not be an Option!

  5. #5

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

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

  6. #6

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    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:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.  
    5.  
    6. DirectSR.GrammarFromFile App.Path & "\commands.txt"
    7.  
    8. Debug.Print DirectSR.Initialized & " < initialized" ' shows 1
    9.  
    10. DirectSR.Activate
    11.  
    12. DirectSR.Listen ' tried with and without this....
    13.  
    14. Debug.Print DirectSR.Initialized & " < activated initialized" ' shows 1
    15.  
    16. End Sub
    17.  
    18. 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)
    19.  
    20.     txtcommand.Text = Phrase
    21.  
    22. End Sub
    23.  
    24. Private Sub Form_Unload(Cancel As Integer)
    25.     DirectSR.Deactivate
    26.    
    27. 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)
    Wayne

  7. #7
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    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!

  8. #8

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    Re: speech SDK grammar HOW TO?

    hi ya... thanks for your input, but as you can see from above i have actually got all this code sorted...

    im just wondering if the fact i have SAPI5.1 its stopping the 4.0 working?

    i have tried different microphones to no luck. - i believe it may be my voice thats the problem, some words DO come out correct on the 5.1 code, as i said - microphone is one of them - hence me adding this to the command list to check on the 4.0 code. - but then again - i have no problem using the VC in my ex's car for the stereo... (Ford Fiesta Zetec) - but thats like a layered system expecting single inputs like
    "Track" [TRACK NUMBER?] "One"
    but does accept things like
    >"CD TRACK ONE" and
    >"Phone Dial Number 0 1 2 1 5 5 5 6 6 6 6 DIAL" [dial number?] "YES"


    i would rather use the 5.1 code since the ms agent is being discontinued BUT... if the only way i can get this working is with the MS agent im cool with that [the system im running this app on wont be updated all that often]

    is there the equivilant to the phrase finish on the 5.1 SDK? and can i load the grammar from a file onto the 5.1 SDK? (Being a later version im sure it should work pretty much the same???)

    if so could somebody post some sample code for that please? - this one is really doing my head in now..

    thanks!

    ALSO... i notice from the MSDN library that 5.3 SDK is available, is that for VB6 or like .net? would that be better - can i use that?

    Cheers
    Wayne

  9. #9
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    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!

  10. #10

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    Re: speech SDK grammar HOW TO?

    hi ya... i think i have actually got this thing working now, except im not sure how to write the XML file to actually program the thing...

    i've looked all through the code for the sample "RecoVB" application included, - and that is actually working for me now, recognises what it expects to hear = FANTASTIC!

    so i have started up my application, used all the same commands etc in my voice rec, tried my XML file - but no go...

    im guessing its because i need to set up the commands it expects to hear so maybe you can explain how i layer the xml file?

    basically my commands will be:

    Code:
    Search Artist Spell ???
    Search Artist Wild End Spell ???
    Search Artist Wild Start Spell ???
    Search Artist Wild Both Spell ???
    
    Search Track Name Spell ???
    Search Album  Spell ???
    
    Player Track Number ??? [Again i will alter code to detect 1 - however many tracks are in the list]
    Player Pause
    Player Play
    Player Next
    Player Back
    
    Volume Up
    Volume Down
    Volume Mute
    Volume Maximum
    
    Help
    Help Player
    Help Volume
    Help Search
    Help Search Artist
    Help Search Artist Wild
    at the ??? point im guessing i will have to load a new input with different grammar list for A-Z ETC / 0-9

    - im sure that shouldnt be too difficault when i come to it though

    oh FYI : i have switched back to the SAPI5.1 - was up most of the night fiddling with it - and im guessing now the XML is the easy part - i hope lol

    cheers in advance
    Wayne

  11. #11

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    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
    Wayne

  12. #12

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    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
    Wayne

  13. #13

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    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:
    1. ' btnStart: Loads Engine - calling loadvoice, which calls loadgrammar - if loaded it unloads it
    2. ' btnlisten: if the engine is loaded - enables / disables VC listening - if engine is unloaded it loads it first
    3.  
    4. Option Explicit
    5.  
    6. ' the actual control we are using
    7. Dim WithEvents RecoContext As SpSharedRecoContext
    8. ' the grammar we are working from
    9. Dim Grammar As ISpeechRecoGrammar
    10. ' weather or not the grammar and recognition engine are loaded
    11. Dim m_bRecoRunning As Boolean
    12. ' < this will b false if the engine is not loaded, but it if the grammar fails to load also...
    13. Dim bGrammarLoaded As Boolean
    14.  
    15.  
    16. ' to load the voice rec engine
    17. Public Function LoadVoice() As Boolean
    18. On Error GoTo ErrHand:
    19.  
    20.     ' just a note as we a loading the API
    21.     btnStart.Caption = "Loading sAPI..."
    22.     DoEvents
    23.    
    24.     ' create a new engine
    25.     Set RecoContext = New SpSharedRecoContext
    26.     ' make a new     grammar file to edit
    27.     Set Grammar = RecoContext.CreateGrammar(0)
    28.    
    29.     '   Load Dictation but set it to Inactive
    30.     Grammar.DictationLoad "", SLOStatic
    31.     Grammar.DictationSetState SGDSInactive
    32.    
    33.     ' Attempt to load the default .xml file and set the RuleId State to Inactive until
    34.     ' the user starts recognition.
    35.     'bGrammarLoaded = LoadDefaultCnCGrammar(App.Path & "\Grammar.xml")
    36.     'bGrammarLoaded = LoadDefaultCnCGrammar("E:\Documents and Settings\Wayne\Desktop\Carstra Media Player\speech rec\sapitutorial00\sapi0.xml")
    37.    
    38.     bGrammarLoaded = LoadDefaultCnCGrammar(App.Path & "\sapi0.xml")
    39.    
    40.    
    41.     ' if the grammar file was loaded - it may fail if the syntax is messed up
    42.     If bGrammarLoaded = True Then
    43.    
    44.         ' say that we are now running.
    45.         LoadVoice = True
    46.        
    47.         ' you may wish to inform the user that voice activation is enabled now...
    48.         ' .speak is SLOW for me on my user machine, so im using it only when necessary
    49.         With RecoContext.Voice
    50.             '.Speak "Hello.  I am " & App.Title & ". "
    51.             '.Speak "If you require help just say help. "
    52.             '.Speak "Don't forget to press the command button before speaking."
    53.         End With
    54.        
    55.     Else
    56.         ' if the file didnt load raise an error
    57.         Err.Raise -1
    58.        
    59.     End If
    60.    
    61.  
    62. Exit Function
    63. ErrHand:
    64. On Error Resume Next
    65.     ' destry the things created
    66.     Grammar.DictationUnload
    67.     Set Grammar = Nothing
    68.     Set RecoContext = Nothing
    69.  
    70.     btnStart.Caption = "Error: Failed loading voice"
    71.    
    72.     ' and return that we failed to load the engine
    73.     LoadVoice = False
    74.     Err.Clear
    75.  
    76. End Function
    77.  
    78.  
    79. Private Function LoadDefaultCnCGrammar(sFilepath As String) As Boolean
    80. On Error Resume Next
    81.  
    82.     ' First load attempt
    83.     Grammar.CmdLoadFromFile sFilepath, SLODynamic
    84.    
    85.     ' Second load attempt
    86.     If Err Then
    87.         On Error GoTo Err_CFGLoad
    88.         Grammar.CmdLoadFromFile sFilepath, SLODynamic
    89.     End If
    90.    
    91.     ' Set rule state to inactive until user clicks Recognition button
    92.     Grammar.CmdSetRuleIdState 0, SGDSInactive
    93.    
    94.     LoadDefaultCnCGrammar = True
    95.    
    96. Exit Function
    97. Err_CFGLoad:
    98.     LoadDefaultCnCGrammar = False
    99.     Exit Function
    100. End Function
    101.  
    102.  
    103. ' i use a frame control to enable and disable recognition - using mouse down and up
    104. Private Sub btnListen_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    105.  
    106.     If RecoContext Is Nothing Then if not LoadVoice then beep: Exit
    107.    
    108.     btnListen.BackColor = vbGreen
    109.        
    110.     ' Start the dictation. - this is if your using normal dictation and not a grammar XML
    111.     'Grammar.DictationSetState SGDSActive
    112.    
    113.     ' listen for the rules in XML file - this is what we want...
    114.     Grammar.CmdSetRuleIdState 0, SGDSActive
    115.    
    116.     Debug.Print "mouse down event..."
    117.  
    118. End Sub
    119.  
    120. Private Sub btnListen_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    121.    
    122.    
    123.     Debug.Print "mouse up event..."
    124.    
    125.     ' stop it
    126.     'Grammar.DictationSetState SGDSInactive
    127.     Grammar.CmdSetRuleIdState 0, SGDSInactive
    128.    
    129.     btnListen.BackColor = BackColor
    130.  
    131. End Sub
    132.  
    133. Private Sub btnStart_Click()
    134.  
    135.     ' check if voice recognition is running.
    136.     Select Case m_bRecoRunning
    137.         ' if it is running
    138.         Case True
    139.            
    140.            
    141.             Grammar.DictationUnload
    142.            
    143.             Set Grammar = Nothing
    144.             Set RecoContext = Nothing
    145.            
    146.             m_bRecoRunning = False
    147.        
    148.         ' if not started
    149.         Case False:
    150.        
    151.             ' load the speech engine
    152.             m_bRecoRunning = LoadVoice
    153.            
    154.             If Not m_bRecoRunning Then btnStart.Caption = "Cannot Start!": Exit Sub
    155.            
    156.            
    157.            
    158.     End Select
    159.    
    160.     ' make a note of voice command state
    161.     btnStart.Caption = IIf(m_bRecoRunning, "*** ON ***", "- OFF -")
    162.  
    163. End Sub
    164.  
    165.  
    166. ' This function handles Recognition event from the reco context object.
    167. ' Recognition event is fired when the speech recognition engines recognizes
    168. ' a sequences of words.
    169. Private Sub RecoContext_Recognition(ByVal StreamNumber As Long, _
    170.                                     ByVal StreamPosition As Variant, _
    171.                                     ByVal RecognitionType As SpeechRecognitionType, _
    172.                                     ByVal Result As ISpeechRecoResult _
    173.                                     )
    174.                                    
    175.     Dim strText As String
    176.     strText = Result.PhraseInfo.GetText
    177.    
    178.     Debug.Print "Recognition: " & strText & ", " & _
    179.         StreamNumber & ", " & StreamPosition
    180.  
    181.      ' i plan to handle like:
    182.     ' shandletext() = split(strtext," ")
    183.     ' select case shandletext(0) -> case "Search" ETC - im sure you get the idea...
    184.    
    185. End Sub
    186.  
    187. Private Sub Form_Unload(Cancel As Integer)
    188.    
    189. ' i try to always clean up after myself... frees the resources incase its not done for me
    190.     If m_bRecoRunning = True Then
    191.    
    192.         Grammar.DictationUnload
    193.         Set Grammar = Nothing
    194.         Set RecoContext = Nothing
    195.  
    196.     End If
    197.  
    198.  
    199. 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
  •  



Click Here to Expand Forum to Full Width