Speech Recognition Command and Control SAPI 5.1

Share your advanced PureBasic knowledge/code with the community.
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Speech Recognition Command and Control SAPI 5.1

Post by Nico »

Here is a voice recognition code, it works normally for window XP and more

Tested on Window Seven

You need ComatePlus (thank you SROD).

Speech recognition is not available for all languages, then verify that your computer have the speech recognition.

It will use the tutorials window speech recognition before testing for best results.

Do the test with a headset is preferable!

Important:
change LANGID in the file "sol.xml" , example:
409 English (United States)
809 English (United Kingdom)
040C French (Standard)
407 German (Standard)

See the different LANGID Decimal (change to hexa) here: http://msdn.microsoft.com/en-us/library ... 0).%20Aspx

Compile to unicode!
The file sol.xml must be save in UTF-8 format.

See the xml language here: http://msdn.microsoft.com/en-us/library ... 2(v=vs.85)

In my example, a messagerequester appears if you say for the same result: (see the file sol.xml)
show window
show the window
show the window now
show application
show the application
show the application now

Another messagerequester appears if you say
hide window
hide the window
hide the window now
hide application
hide the application
hide the application now



Main code (compile to unicode)
Think to put xml file path of "sol.xml" in the source , line 293

Code: Select all

XIncludeFile "COMatePLUS.pbi"

;{- Enumerations / DataSections
;{ Windows
Enumeration
  #Window_0
EndEnumeration
;} 
;{ Gadgets
Enumeration
  #Text_Message
  #String
  #Text_2
  #Text_3
  #Editor
  #Button_Desactive
  #Button_Reactive
EndEnumeration
;} 
;{ Fonts
Enumeration
  #Font_Text_Message
  #Font_String_1
EndEnumeration
;} 
;} 

Structure Parametres
  SpRecoContext.i
  SpeechRecoGrammar.i
EndStructure

Procedure SpeechRecoContextEvents(event.COMateObject, event$, parameterCount)
  Protected iParam.iUnknown
  Protected SpeechRecoResult.COMateObject
  Protected SpeechPhraseInfo.COMateObject
  Protected SpeechPhraseElements.COMateObject
  Protected SpeechPhraseElement.COMateObject
  Protected SpeechPhraseRule.COMateObject
  Protected Texte.s, Word.s
  Protected count.l, niveau.l, a.l
  
  ; Debug("EVT " + Str(parameterCount) + " " + event$)
  
  Select event$
    Case "FalseRecognition"
      SetGadgetText(#String, "<Unrecognized word or phrase>")
      
    Case "Recognition"
      Debug "-------------Recognition-----------------"
      iParam = event\GetObjectEventParam(4)
      If iParam
        SpeechRecoResult = COMate_WrapCOMObject(iParam)
        
        If SpeechRecoResult
          SpeechPhraseInfo = SpeechRecoResult\GetObjectProperty("PhraseInfo()")
          COMate_GetLastErrorDescription()
          If SpeechPhraseInfo
            
            Texte = SpeechPhraseInfo\GetStringProperty("GetText(0, -1, 1)")
            SetGadgetText(#String, Texte)
            
            SpeechPhraseElements = SpeechPhraseInfo\GetObjectProperty("Elements")
            If SpeechPhraseElements <> 0
              count = SpeechPhraseElements\GetIntegerProperty("Count")
              For a = 0 To count - 1
                SpeechPhraseElement = SpeechPhraseElements\GetObjectProperty("Item(" + Str(a) + ")")
                If SpeechPhraseElement <> 0
                  niveau = SpeechPhraseElement\GetIntegerProperty("ActualConfidence")
                  ; #SECLowConfidence = -1    ; niveau de reconnaissance basse
                  ; #SECNormalConfidence = 0  ; niveau de reconnaissance normal
                  ; #SECHighConfidence = 1    ; niveau de reconnaissance haute
                  ; Pour chaque mot, j'affiche le niveau de reconnaissance
                  Word = SpeechPhraseElement\getstringproperty("DisplayText")
                  Debug "Word = "+Word+" ; Niveau = " + Str(niveau)
                  SpeechPhraseElement\Release()
                EndIf
              Next a
              
              SpeechPhraseElements\Release()
            EndIf
            
            SpeechPhraseRule = SpeechPhraseInfo\GetObjectProperty("Rule")
            If SpeechPhraseRule <> 0
              rule.s = SpeechPhraseRule\GetStringProperty("Name")
              Debug "Rule = " + rule
              SpeechPhraseRule\Release()
            EndIf
            
            
            Select Rule
              Case "show"
                MessageRequester("Info", "Command = " + Rule)
              Case "hide"
                MessageRequester("Info", "Command = " + Rule)
            EndSelect
            
            SpeechPhraseInfo\Release()
          EndIf
          SpeechRecoResult\Release()
        EndIf
        iParam\Release()
      EndIf
      
    Case "Hypothesis"
      Debug "------------Hypothesis------------------"
      
      SetGadgetText(#Text_Message, "Analysis ongoing...")
      
      iParam = event\GetObjectEventParam(3)
      If iParam
        SpeechRecoResult = COMate_WrapCOMObject(iParam)
        If SpeechRecoResult
          SpeechPhraseInfo = SpeechRecoResult\GetObjectProperty("PhraseInfo()")
          If SpeechPhraseInfo
            
            Texte = SpeechPhraseInfo\GetStringProperty("GetText(0, -1, 1)")
            
            SetGadgetText(#Editor, Texte + Chr(13) + GetGadgetText(#Editor))
            
            SpeechPhraseInfo\Release()
          EndIf
          SpeechRecoResult\Release()
        EndIf
        iParam\Release()
      EndIf
      
    Case "SoundStart"
      Debug "SoundStart"
      SetGadgetText(#String, "")
      
    Case "SoundEnd"
      Debug "SoundEnd"
      SetGadgetText(#Text_Message, "Speak")
      
    Case "StartStream"
      ; Debug "StartStream"
      
    Case "EndStream"
      ; Debug "EndStream"
      
  EndSelect
EndProcedure

Procedure Load_Speech_Recognition(*Speech_Recognition.Parametres, File.s)
  Protected SpRecoContext.COMateObject
  Protected SpRecoGrammar.COMateObject
  Protected Init_Recognition.l = 0
  
  Protected SpRecognizer.COMateObject
  Protected SpeechRecognizer.COMateObject
  Protected SpObjectTokenCategory.COMateObject
  Protected SpObjectToken.COMateObject
  
  
  SpRecoContext = COMate_CreateObject("SAPI.SpInProcRecoContext")
  Debug COMate_GetLastErrorDescription()
  If SpRecoContext = 0 : Goto Clean : EndIf
  
  
  SpRecoGrammar = SpRecoContext\GetObjectProperty("CreateGrammar(0)")
  If SpRecoGrammar = 0 : Goto Clean : EndIf
  
  RecoGrammar.idispatch = SpRecoGrammar\GetCOMObject()
  If RecoGrammar = 0 : Goto Clean : EndIf
  
  RecoGrammar\QueryInterface(?IID_ISpeechRecoGrammar, @SpeechRecoGrammar_)
  If SpeechRecoGrammar_ = 0 : Goto Clean : EndIf
  SpRecoGrammar\Release()
  
  SpeechRecoGrammar.COMateObject = COMate_WrapCOMObject(SpeechRecoGrammar_)
  If SpeechRecoGrammar = 0 : Goto Clean : EndIf
  
  #SLOStatic = 0
  #SLODynamic = 1
  If SpeechRecoGrammar\invoke("CmdLoadFromFile('" + File + "', " + Str(#SLODynamic) + ")") <> 0
    Goto Clean
  EndIf
  
  If SpRecoContext\SetEventHandler(#COMate_CatchAllEvents, @SpeechRecoContextEvents()) <> 0
    Goto Clean
  EndIf
  
  SpRecognizer = SpRecoContext\GetObjectProperty("Recognizer()")
  If SpRecognizer = 0 : Goto Clean : EndIf
  
  Recognizer.idispatch = SpRecognizer\GetCOMObject()
  If Recognizer = 0 : Goto Clean : EndIf
  
  Recognizer\QueryInterface(?IID_ISpeechRecognizer, @SpeechRecognizer_)
  If SpeechRecognizer_ = 0 : Goto Clean : EndIf
  
  SpeechRecognizer.COMateObject = COMate_WrapCOMObject(SpeechRecognizer_)
  If SpeechRecognizer = 0 : Goto Clean : EndIf
  
  SpObjectTokenCategory = COMate_CreateObject("SAPI.SpObjectTokenCategory")
  If SpObjectTokenCategory = 0 : Goto Clean : EndIf
  
  #SpeechCategoryAudioIn = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\AudioInput"
  If SpObjectTokenCategory\invoke("SetId('" + #SpeechCategoryAudioIn + "', 0)") <> 0
    Goto Clean
  EndIf
  
  SpObjectToken = COMate_CreateObject("SAPI.SpObjectToken")
  If SpObjectToken = 0 : Goto Clean : EndIf
  
  Audio$ = SpObjectTokenCategory\GetStringProperty("Default()")
  Debug audio$
  If SpObjectToken\invoke("SetId('" + Audio$ + "')") <> 0
    Goto Clean
  EndIf
  
  If SpeechRecognizer\Setpropertyref("AudioInput(" + Str(SpObjectToken) + " as COMateObject)") <> 0
    Goto Clean
  EndIf
  
  ; La Règle de grammaire est active.
  #SGDSActive = 1
  If SpeechRecoGrammar\invoke("CmdSetRuleIdState(0, " + Str(#SGDSActive) + ")") <> 0
    Goto Clean
  Else
    Init_Recognition = 1
  EndIf
  
  Clean :
  If Init_Recognition = 0
    If SpRecoContext <> 0 : SpRecoContext\Release() : EndIf
    If SpRecoGrammar <> 0 : SpRecoGrammar\Release() : EndIf
    If SpeechRecoGrammar <> 0 : SpeechRecoGrammar\Release() : EndIf
  Else
    *Speech_Recognition\SpRecoContext = SpRecoContext
    *Speech_Recognition\SpeechRecoGrammar = SpeechRecoGrammar
  EndIf
  
  ProcedureReturn Init_Recognition
EndProcedure

Procedure Finish_Speech_Recognition(*Speech_Recognition.Parametres)
  Protected SpRecoContext.COMateObject
  Protected SpeechRecoGrammar.COMateObject
  
  SpRecoContext = *Speech_Recognition\SpRecoContext
  SpeechRecoGrammar = *Speech_Recognition\SpeechRecoGrammar
  
  If SpeechRecoGrammar <> 0
    ; La Règle de grammaire est inactive.
    #SGDSInActive = 0
    SpeechRecoGrammar\invoke("CmdSetRuleIdState(0, " + Str(#SGDSInActive) + ")")
    SpeechRecoGrammar\Release()
  EndIf
  
  If SpRecoContext <> 0
    SpRecoContext\SetEventHandler(#COMate_CatchAllEvents, 0)
    SpRecoContext\Release()
  EndIf
  
EndProcedure

Procedure Desactive_Recognition(*Speech_Recognition.Parametres, Valeur.l)
  Protected SpeechRecoGrammar.COMateObject
  SpeechRecoGrammar = *Speech_Recognition\SpeechRecoGrammar
  
  #SGDSInActive = 0
  #SGDSActive = 1
  If Valeur = 0
    SpeechRecoGrammar\invoke("CmdSetRuleIdState(0, " + Str(#SGDSActive) + ")")
    Debug COMate_GetLastErrorDescription()
  ElseIf Valeur = 1
    SpeechRecoGrammar\invoke("CmdSetRuleIdState(0, " + Str(#SGDSInActive) + ")")
    Debug COMate_GetLastErrorDescription()
  EndIf
  
EndProcedure

Procedure OpenWindow_Window_0()
  If OpenWindow(#Window_0, 732, 260, 600, 400, "Speech Recognition Control and Command", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar | #PB_Window_SizeGadget)
    TextGadget(#Text_Message, 0, 0, 600, 50, "Speak and say : hide or show", #PB_Text_Center)
    StringGadget(#String, 0, 95, 600, 35, "", #PB_String_ReadOnly)
    TextGadget(#Text_2, 0, 75, 600, 20, "  Recognized sentence:")
    TextGadget(#Text_3, 0, 160, 600, 20, "  Hypothetical sentence:")
    EditorGadget(#Editor, 0, 180, 600, 165, #PB_Editor_ReadOnly)
    ButtonGadget(#Button_Desactive, 60, 355, 200, 35, "Deactivate")
    ButtonGadget(#Button_Reactive, 335, 355, 200, 35, "Activate")
    DisableGadget(#Button_Reactive, 1)
    ; Gadget Fonts
    SetGadgetFont(#Text_Message, LoadFont(#Font_Text_Message, "Microsoft Sans Serif", 12, 272))
    SetGadgetFont(#String, LoadFont(#Font_String_1, "Microsoft Sans Serif", 12, 16))
  EndIf
EndProcedure

CoInitialize_(0)

File.s = "           ;put xml file path of "sol.xml" 
If Load_Speech_Recognition(@Speech_Recognition.Parametres, File) = 0
  MessageRequester("Erreur", "Initialization of motor speech recognition failed!")
  End
EndIf

OpenWindow_Window_0()

;{- Event loop
Repeat
  Select WaitWindowEvent()
      ;/ //////////////////
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button_Desactive
          DisableGadget(#Button_Reactive, 0)
          DisableGadget(#Button_Desactive, 1)
          Desactive_Recognition(@Speech_Recognition.Parametres, 1)
          
        Case #Button_Reactive
          DisableGadget(#Button_Reactive, 1)
          DisableGadget(#Button_Desactive, 0)
          Desactive_Recognition(@Speech_Recognition.Parametres, 0)
          
      EndSelect
      ;/ ///////////////////////
    Case #PB_Event_CloseWindow
      Select EventWindow()
        Case #Window_0
          CloseWindow(#Window_0)
          Finish_Speech_Recognition(@Speech_Recognition.Parametres)
          CoUninitialize_()
          Break
      EndSelect
  EndSelect
ForEver

;} 


Macro GUID(iid, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0)
  DataSection
    IID :
    Data.l $a0
    Data.w $a1, $a2
    Data.b $a3, $a4, $a5, $a6, $a7, $a8, $a9, $b0
  EndDataSection
EndMacro

GUID(IID_ISpeechRecoGrammar, B6D6F79F, 2158, 4E50, B5, BC, 9A, 9C, CD, 85, 2A, 09)
GUID(IID_ISpeechRecognizer, 2D5F1C0C, BD75, 4B08, 94, 78, 3B, 11, FE, A2, 58, 6C)
GUID(IID_ISpeechXMLRecoResult, AAEC54AF, 8F85, 4924, 94, 4D, B7, 9D, 39, D7, 2E, 19)
The file "sol.xml"
Put the file "sol.xml" in the same directory of the main code, save it in the UTF-8 format!
Consider changing the LANGID if necessary!

Code: Select all

<GRAMMAR LANGID="409">
    <DEFINE>
   <ID NAME="RID_show" VAL="1"/>
    </DEFINE>

    <DEFINE>
   <ID NAME="RID_hide" VAL="2"/>
    </DEFINE>

   <RULE NAME="afficher" ID="RID_show" TOPLEVEL="ACTIVE">
      <P>show</P>
      <RULEREF NAME="transition" />
      <RULEREF NAME="cible" />
      <RULEREF NAME="now" />
   </RULE>


   <RULE NAME="hide" ID="RID_hide" TOPLEVEL="ACTIVE">
      <p>hide</p>
      <RULEREF NAME="transition" />
      <RULEREF NAME="cible" />
      <RULEREF NAME="now" />
   </RULE>

   <RULE NAME="transition">
      <O>the</O>
   </RULE>

   <RULE NAME="cible">
    <L>
      <P>window</P>
      <P>application</P>
    </L>
   </RULE>

   <RULE NAME="now">
      <O>now</O>
   </RULE>

</GRAMMAR>
hichem
User
User
Posts: 26
Joined: Sun Sep 04, 2005 4:18 pm

Re: Speech Recognition Command and Control SAPI 5.1

Post by hichem »

a very good job thank's nico.
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Re: Speech Recognition Command and Control SAPI 5.1

Post by Nico »

Thanks :D
Liqu
User
User
Posts: 77
Joined: Sun Apr 21, 2013 10:31 am

Re: Speech Recognition Command and Control SAPI 5.1

Post by Liqu »

using pb 5.31 32bit, compile in unicode

Help, I got this error : " Initialization of motor speech recognition failed! "
sol.xml has been created ( UTF-8 )
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Re: Speech Recognition Command and Control SAPI 5.1

Post by Nico »

What is your operating system?
Post Reply