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     }

    这个是测试可用的

  • 相关阅读:
    nginx安装
    win7 mount到Linux下无法分配内存的问题(Cannot allocate memory)
    [转]linux时间同步
    关于文件缓冲的问题
    【转】VNC配置
    yum代理设置
    mysql开启日志
    [译]rabbitmq 2.5 Where’s my message? Durability and you
    [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation
    [译]rabbitmq 2.2 Building from the bottom: queues
  • 原文地址:https://www.cnblogs.com/bfyx/p/2855015.html
Copyright © 2011-2022 走看看