Imports System.io
Imports SpeechLib

Public Class SKSpeechEventArgs
    Inherits System.EventArgs
    '認識した単語数
    Public RecoCount As Integer = 0
    'ルール名
    Public RuleName As String = ""
    '値データ
    Public values() As Integer
    '文字列データ
    Public Words() As String
    'プロパティ名
    Public ItemName() As String
    'コンストラクタ（認識した単語数を指定）
    Public Sub New(ByVal Count As Integer)
        ReDim values(Count)
        ReDim Words(Count)
        ReDim ItemName(Count)
        RecoCount = Count
    End Sub
End Class


Public Class MiraiSpeech
    '音声認識オブジェクト
    Private objRecoContext As SpeechLib.SpSharedRecoContext
    '認識文法
    Private grammar As ISpeechRecoGrammar
    '認識結果を返すイベントを起こすかどうかのフラグ
    Private RecoEnabled As Boolean = True
    '認識結果を返すフラグ
    Public Event Recognition(ByVal sender As Object, ByVal e As SKSpeechEventArgs)

    'RecoEnabled用プロパティ
    Public Property Enabled() As Boolean
        Get
            Return RecoEnabled
        End Get
        Set(ByVal Value As Boolean)
            RecoEnabled = Value
            If RecoEnabled Then
                grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSActive)
            Else
                grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSInactive)
            End If
        End Set
    End Property


    Public Sub New(ByVal FNM As String)
        'Grammerファイルがあるかどうか調べる
        If File.Exists(FNM) = False Then
            Throw New ApplicationException("Grammerファイルが見つかりません。")
            Exit Sub
        End If

        '認識エンジンのイニシャライズ
        Try
            '認識オブジェクトをインスタンス化
            objRecoContext = New SpSharedRecoContext
            '各イベントを宣言
            AddHandler objRecoContext.Hypothesis, AddressOf RecoContext_Hypothesis
            AddHandler objRecoContext.Recognition, AddressOf RecoContext_Recognition
            AddHandler objRecoContext.FalseRecognition, AddressOf RecoContext_FalseReco
            'Grammar（文法）オブジェクト作成
            grammar = objRecoContext.CreateGrammar(1)

            'Dictaion（文章認識）は使わないのでInActiveに設定
            grammar.DictationLoad("", SpeechLoadOption.SLOStatic)
            grammar.DictationSetState(SpeechRuleState.SGDSInactive)
            '単語認識のグラマーファイルを読み込む
            grammar.CmdLoadFromFile(FNM, SpeechLoadOption.SLODynamic)
            grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSInactive)
            '単語認識を開始
            grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSActive)
            '文章認識も開始
            grammar.DictationSetState(SpeechRuleState.SGDSActive)

        Catch ex As Exception
            Throw New ApplicationException("InitSpeech" & Chr(10) & ex.ToString)
            Exit Sub
        End Try


    End Sub


    Public Sub RecoContext_Hypothesis(ByVal streamNumber As Integer, _
                                  ByVal streamPosition As Object, _
                                  ByVal Result As ISpeechRecoResult)

    End Sub


    Public Sub RecoContext_FalseReco(ByVal StreamNumber As Integer, _
                                     ByVal StreamPosition As Object, _
                                     ByVal Result As ISpeechRecoResult)

    End Sub

    Public Sub RecoContext_Recognition(ByVal StreamNumber As Integer, _
                      ByVal StreamPosition As Object, _
                      ByVal RecognitionType As SpeechRecognitionType, _
                      ByVal Result As ISpeechRecoResult)

        If RecoEnabled = False Then Exit Sub

        Dim recoword As String = Result.PhraseInfo.GetText(0, -1, True)

        Dim Val As Integer = Result.PhraseInfo.Properties.Item(0).Value
        Dim PNM As String = Result.PhraseInfo.Properties.Item(0).Name
        Dim CRNM As String = Result.PhraseInfo.Rule.Children.Item(0).Name
        Dim RecoCount As Integer = Result.PhraseInfo.Properties.Count

        Dim args As New SKSpeechEventArgs(RecoCount)
        args.RuleName = Result.PhraseInfo.Rule.Children.Item(0).Name

        Debug.WriteLine("1")
        Dim intloop As Integer
        For intloop = 0 To RecoCount - 1
            args.values(intloop) = Result.PhraseInfo.Properties.Item(intloop).Value
            args.ItemName(intloop) = Result.PhraseInfo.Properties.Item(intloop).Name
            args.Words(intloop) = Result.PhraseInfo.GetText(intloop, 1, True)
        Next

        RaiseEvent Recognition(Me, args)
        Debug.WriteLine("2")

    End Sub

End Class
