Interop.SpeechLib.dll




본 프로젝트에서 사용될 음성인식 부분이다.

-음성인식은 Speach엔진을 사용하였으며, Speach엔진을 사용하는 방법이다.

-DLL로 구성하였다.

-참조된 SpeachDLL을 포함하고 있어야한다.

-MS에서 재공하는 SpeechSDK51.exe 를 다운받아야 사용할 수 있다.


public class Speach

    {

        SpeechLib.SpSharedRecoContext objRecoContext = null;

        SpeechLib.ISpeechRecoGrammar grammar = null;

        SpeechLib.ISpeechGrammarRule menuRule = null;

        public event SpeachResultReturn SpeachResultReturnEventHandler = null;

        public string result = string.Empty;

        public string Result

        {

            get

            {

                return result;

            }

        }

        public void StartSpeach()

        {

            objRecoContext = new SpeechLib.SpSharedRecoContext();

            objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(objRecoContext_Hypothesis);

 

            objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(objRecoContext_Recognition);

           

            grammar = objRecoContext.CreateGrammar(0);

            menuRule = grammar.Rules.Add("MenuCommands"SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);

            object PropValue = "";

 

            menuRule.InitialState.AddWordTransition(null"End"" "SpeechGrammarWordType.SGLexical, "End"1ref PropValue, 1.0F);

            menuRule.InitialState.AddWordTransition(null"Start"" "SpeechGrammarWordType.SGLexical,"Start"2ref PropValue, 1.0F);

            menuRule.InitialState.AddWordTransition(null"Aircon"" "SpeechGrammarWordType.SGLexical,"Aircon"3ref PropValue, 1.0F);

            menuRule.InitialState.AddWordTransition(null"Audio"" "SpeechGrammarWordType.SGLexical,"Audio"4ref PropValue, 1.0F);

            menuRule.InitialState.AddWordTransition(null"Option"" "SpeechGrammarWordType.SGLexical,"Option"4ref PropValue, 1.0F);

 

            grammar.Rules.Commit();

            grammar.CmdSetRuleState("MenuCommands"SpeechRuleState.SGDSActive);

        }

 

        void objRecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)

        {

            string txtReco = string.Empty;

            txtReco = Result.PhraseInfo.GetText(0, -1true);

            switch (txtReco)

            {

                case "End": result = "End"break;

                case "Start": result = "Start"break;

                case "Aircon": result = "Aircon"break;

                case "Audio": result = "Audio"break;

                case "Option": result = "Option"break;

            }

            if (txtReco != null)

            {

                if (SpeachResultReturnEventHandler != null)

                {

                    SpeachResultReturnEventHandler();

                }

            }

        }

 

        void objRecoContext_Hypothesis(int StreamNumber, object StreamPosition, ISpeechRecoResult Result)

        {

            string txtHyp = string.Empty;

            txtHyp = Result.PhraseInfo.GetText(0, -1true);           

        }

    }


 

-사용법

-WinFrom 응용프로그램 이용하여 Test프로그램을 만들어 보았다.

public partial class Form1 : Form

    {

        SpeachDll.Speach myspeach = new SpeachDll.Speach();

        public Form1()

        {

            InitializeComponent();          

        }

 

        private void button1_Click(object sender, EventArgs e)

        { 

            myspeach.StartSpeach();

            myspeach.SpeachResultReturnEventHandler += new SpeachDll.SpeachResultReturn(myspeach_SpeachResultReturnEventHandler);

        }

 

        void myspeach_SpeachResultReturnEventHandler()

        {

            MessageBox.Show(myspeach.Result);

        }

    }


'Programing > C#&.Net' 카테고리의 다른 글

계산기 예제  (0) 2016.11.30
노트북 블루투스 + 안드로이드 폰 연동  (0) 2016.11.30
회원관리 연습  (0) 2016.11.30
.NET 리모팅  (0) 2016.11.30
직렬화(Serializable) 예제  (0) 2016.11.30

+ Recent posts