语音识别WAV To String

由于项目需要在网上找了好多,修改下,下面是个样例,大家看下

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using DotNetSpeech;
 5 using System.Threading;
 6 
 7 namespace TestSpRecognize
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             WavToCaption obj = new WavToCaption();
14             obj.openWAV();
15         }
16     }
17     class WavToCaption
18     {
19         private SpInProcRecoContext m_wavRecoContext;
20         private ISpeechRecoGrammar m_Grammar;
21         private SpFileStream m_infile;
22 
23         public WavToCaption()
24         {
25             SpInprocRecognizer recognizer = new SpInprocRecognizer();
26             m_wavRecoContext = (SpInProcRecoContext)recognizer.CreateRecoContext();
27             m_wavRecoContext.RetainedAudio = SpeechRetainedAudioOptions.SRAORetainAudio;
28             m_infile = new SpFileStreamClass();
29             m_infile.Format.GetWaveFormatEx();
30         }
31 
32         public void openWAV()
33         {
34             m_Grammar = m_wavRecoContext.CreateGrammar(0);
35             m_Grammar.DictationLoad("", SpeechLoadOption.SLOStatic);
36 
37             //register an event handler everytime the engine recognizes something from teh file
38             m_wavRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition);
39 
40 
41             //register an event handler when the engine is done reading the  file
42             m_wavRecoContext.EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(RecoContext_EndRecognition);
43 
44             //try to open the file
45             try
46             {
47                 m_infile.Open(@"c:\1.wav",
48                 SpeechStreamFileMode.SSFMOpenForRead, false);
49                 Console.Out.WriteLine("Succesfully opened file");
50             }
51             catch (Exception e)
52             {
53                 Console.Out.Write("Could not find file");
54                 return;
55             }
56 
57             //this makes it so the engine recognizes we're reading in from a  wav, vs. a microphone
58             m_wavRecoContext.Recognizer.AudioInputStream = m_infile;
59 
60             //starts reading the file here
61             m_Grammar.DictationSetState(SpeechRuleState.SGDSActive);
62             Console.ReadKey();
63 
64         }
65 
66         void RecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
67         {
68             //Result.PhraseInfo.GetText(0, -1, true);
69             Console.Out.Write("recognized something");
70             Console.ReadKey();
71         }
72 
73         void RecoContext_EndRecognition(int StreamNumber, object StreamPosition, bool f)
74         {
75             m_infile.Close();
76             m_Grammar.DictationSetState(SpeechRuleState.SGDSInactive);
77 
78         }
79 
80 
81     }

这个是测试可用的

原文地址:https://www.cnblogs.com/bfyx/p/2855015.html