zoukankan      html  css  js  c++  java
  • 语音识别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     }

    这个是测试可用的

  • 相关阅读:
    Unable to load the specified metadata resource
    Web开发人员速查卡
    vs 中大括号之间垂直虚线显示
    第4届华为编程大赛决赛试题解答(棋盘覆盖)
    assert()函数用法总结
    Win7安装VC++6.0已知的兼容性问题的解决方法
    VC6打开一个文件或工程的时候,会导致VC6崩溃而关闭
    浮点数取整.
    1.4 VC6.0在win7下安装的兼容性问题以及解决办法
    华为编程大赛_将字符数组内的数字排序
  • 原文地址:https://www.cnblogs.com/bfyx/p/2855015.html
Copyright © 2011-2022 走看看