您的位置:首页 > 其它

语音识别WAV To String

2013-01-10 16:17 579 查看
由于项目需要在网上找了好多,修改下,下面是个样例,大家看下

using System;
using System.Collections.Generic;
using System.Text;
using DotNetSpeech;
using System.Threading;

namespace TestSpRecognize
{
class Program
{
static void Main(string[] args)
{
WavToCaption obj = new WavToCaption();
obj.openWAV();
}
}
class WavToCaption
{
private SpInProcRecoContext m_wavRecoContext;
private ISpeechRecoGrammar m_Grammar;
private SpFileStream m_infile;

public WavToCaption()
{
SpInprocRecognizer recognizer = new SpInprocRecognizer();
m_wavRecoContext = (SpInProcRecoContext)recognizer.CreateRecoContext();
m_wavRecoContext.RetainedAudio = SpeechRetainedAudioOptions.SRAORetainAudio;
m_infile = new SpFileStreamClass();
m_infile.Format.GetWaveFormatEx();
}

public void openWAV()
{
m_Grammar = m_wavRecoContext.CreateGrammar(0);
m_Grammar.DictationLoad("", SpeechLoadOption.SLOStatic);

//register an event handler everytime the engine recognizes something from teh file
m_wavRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition);

//register an event handler when the engine is done reading the  file
m_wavRecoContext.EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(RecoContext_EndRecognition);

//try to open the file
try
{
m_infile.Open(@"c:\1.wav",
SpeechStreamFileMode.SSFMOpenForRead, false);
Console.Out.WriteLine("Succesfully opened file");
}
catch (Exception e)
{
Console.Out.Write("Could not find file");
return;
}

//this makes it so the engine recognizes we're reading in from a  wav, vs. a microphone
m_wavRecoContext.Recognizer.AudioInputStream = m_infile;

//starts reading the file here
m_Grammar.DictationSetState(SpeechRuleState.SGDSActive);
Console.ReadKey();

}

void RecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
{
//Result.PhraseInfo.GetText(0, -1, true);
Console.Out.Write("recognized something");
Console.ReadKey();
}

void RecoContext_EndRecognition(int StreamNumber, object StreamPosition, bool f)
{
m_infile.Close();
m_Grammar.DictationSetState(SpeechRuleState.SGDSInactive);

}

}


这个是测试可用的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐