??xml version="1.0" encoding="utf-8" standalone="yes"?>
使用微YAGENT的要?/strong>
要用该技术,我们必须有下面的lgQ?
微YAgent不是以ActiveX控制DLL的Ş式提供的。要?NET中用它Q可以用由.NET框架SDK提供的AxImp.exe工具Q? C#中的微YAgentQ? 中国3S?3s8.cn 专业?S?3s8.cn 专业?S?3s8.cn
微YAgent API能够提供卡通角色的昄Q另外,它还可以支持语音识别Q因此应用Y件可以对语音命o作出反应Q而卡通角色可以通过合成的语韟뀁录制好的音频信h文字对命令作出反应?/p>
·微YAgent核心lg
·微YAgent中的卡通角艜y━Genie、Merlin、Robby和Peedy
·微YSpeech API 4.0aq行旉?
·微Y语音识别引擎
·Lernout和Hauspie文字-语音引擎
上面的所有这些组仉可以从http://microsoft.com/products/msagent/downloads.htm下蝲?
语音技术简?
文字-语音转换指的是计机文字信息{换ؓ合成语音q行输出Q语韌别是指计机能够识别话者所说的话,接受说话者的命o和输入的数据?
语音识别和文?语音转换都需要用到相关的引擎Q几乎所有的语音识别引擎都是输入的语音数据转换Z特定引擎相关的音素,然后q些音素被{换ؓ应用E序能够使用的文字?
文本-语音转换的二U方式:
1、合成文?语音转换
2、连l文?语音转换
合成文本-语音转换方式Q?
在合成的发音方式中,引擎处理每个单词Qƈ产生该单词的发音音素Q然后这些音素被转入一个复杂的法中,模仿人类的发声方式,生成语音?
q箋文本-语音转换方式Q?
在连l文?语音转换方式中,引擎Ҏ(gu)本信息进行处理,从一个预先录制好的语韛_中找出句子、单词和短语Q在q种方式中,生成的语xq箋的?br />
语音应用E序的编E接?
微Y语音应用E序~程接口在Win32QWindows 95、Windows NTQ下使用了OLElg对象模式QCOMQ架构,微Y的Agent架构在合成语韌Z使用了微软语韛_用程序编E接口(SAPIQ,q用SAPI支持语音输入Q语韌别SR或文?语音转换TTSQ。微软的Agent定义了让应用E序讉K其服务的接口Q得应用程序能够控制角色动甅R支持用戯入事Ӟ指定输出方式?
角色H口
在微软Agent应用E序中,卡通角色是在它们各自的H口中被昄的,q些H口L出现在Z轴顺序的最端。用户可以通过鼠标左键拖动角色Ud角色所在的H口Q角色的囑փ随着指针而移动?
说话汽球圈?/strong>
除了语音输出外,动画角色q支持以卡通类型说话汽球圈形式的文字字q,角色说话Ӟ所说的文字出现在汽球圈儿中,当说完时Q汽球圈也就不见了?
在互联网|页中用微软AGENT
要在互联|网中使用微YAgent服务Q可以在|页中的或元素中使用HTML标记Q指定控制的CLSIDQ类标识W)Q另外,q需要用CODEBASE参数指定微YAgent的安装文件的位置和版本号?
我们可以使用Vbscript、Javascript和Jscript在互联网|页中用微软Agent?
?
?NET架构中用微软AGENT
AxImp -->> ActiveX控制-WinH体l合体生成器
Syntax: AxImp [/? | [[/source] OCXName]]
Aximp agentctl.dll
上面的命令生成二个文ӞAxAgentObjects.dll和AgentObjects.dll。通过使用q二个文Ӟ我们可以在.NET中用Agent了?
要在C#中用微软AgentQ我们必dE序中添加二个DLL文gQAxAgentObjects.dll和AgentObjects.dll。加载动画角色的代码是相当简单的Q?
======================================================================================================================AxAgent.Characters.Load("Genie",(object)"C:/Windows/Msagent/chars/GENIE.acs");
Character = AxAgent.Characters["Genie"];
file://语a讄为美国英?
Character.LanguageID = 0x409;
file://该行代码昄角色
Character.Show(null);
让角色说话的代码如下所C:
Character.Speak ("Welcome you sir VISIT www.onlinecsharpteach.netfirms.com ",null);
下面我们来看一个例子:
The Example:
输出Q?
using System;
using System.Drawing;
using System.WinForms;
using AgentObjects;
public class Hello: Form
{
private System.ComponentModel.Container components;
private System.WinForms.Button button2;
private System.WinForms.Button button1;
private System.WinForms.TextBox textBox1;
private AxAgentObjects.AxAgent AxAgent;
private IAgentCtlCharacterEx Character;
public Hello()
{
InitializeComponent();
}
public static void Main(string[] args)
{
Application.Run(new Hello());
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.WinForms.Button();
this.button2 = new System.WinForms.Button();
this.textBox1 = new System.WinForms.TextBox();
this.AxAgent = new AxAgentObjects.AxAgent();
AxAgent.BeginInit();
button2.Click += new System.EventHandler(button2_Click);
button1.Location = new System.Drawing.Point(88, 208);
button1.BackColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)255, (byte)128, (byte)128);
button1.Size = new System.Drawing.Size(152, 32);
button1.TabIndex = 1;
button1.Text = "Load character";
button2.Location = new System.Drawing.Point(120, 240);
button2.BackColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)255, (byte)128, (byte)128);
button2.Size = new System.Drawing.Size(96, 24);
button2.TabIndex = 2;
button2.Text = "SPEAK";
textBox1.Location = new System.Drawing.Point(48, 8);
textBox1.Text = " ";
textBox1.Multiline = true;
textBox1.TabIndex = 0;
textBox1.Size = new System.Drawing.Size(248, 200);
textBox1.BackColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)255, (byte)128, (byte)128);
this.Text = "MSAGENT DEMO";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.WindowState = System.WinForms.FormWindowState.Maximized;
this.BackColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)255, (byte)192, (byte)192);
this.ClientSize = new System.Drawing.Size(344, 301);
this.Controls.Add(button2);
this.Controls.Add(button1);
this.Controls.Add(textBox1);
this.Controls.Add(AxAgent);
button1.Click += new System.EventHandler(button1_Click);
AxAgent.EndInit();
}
protected void button2_Click(object sender, System.EventArgs e)
{
if(textBox1.Text.Length == 0)
return;
Character.Speak(textBox1.Text, null);
}
protected void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.AddExtension = true;
openFileDialog.Filter = "Microsoft Agent Characters (*.acs)|*.acs";
openFileDialog.FilterIndex = 1 ;
openFileDialog.RestoreDirectory = true ;
if(openFileDialog.ShowDialog() != DialogResult.OK)
return;
try { AxAgent.Characters.Unload("CharacterID"); }
catch { }
AxAgent.Characters.Load("CharacterID", (object)openFileDialog.FileName);
Character = AxAgent.Characters["CharacterID"];
Character.LanguageID = 0x409;
Character.Show(null);
Character.Play ("announce");
Character.Speak ("welcome you sir",null);
} }
?
?
l束语:
微Y的Agent API提供了支持动画角色显C的服务Q被配置为OLE AutomationQCOMQ服务器Ӟ它能够多个被称为客h客户端应用Y件的应用E序同时托管或用其动画、输入、输出服务?
]]>
下面我们设计一个能够朗M英文混合语言的类Q?
我们用单例模式实现该类Q类的代码如下,我们详l解释:
public class Speach
{
private static Speach _Instance = null ;
private SpeechLib.SpVoiceClass voice =null;
private Speach()
{
BuildSpeach() ;
}
public static Speach instance()
{
if (_Instance == null)
_Instance = new Speach() ;
return _Instance ;
}
private void SetChinaVoice()
{
voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0) ; 中国3S?3s8.cn
}
private void SetEnglishVoice()
{
voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(1) ;
}
private void SpeakChina(string strSpeak)
{
SetChinaVoice() ;
Speak(strSpeak) ;
}
private void SpeakEnglishi(string strSpeak)
{
SetEnglishVoice() ;
Speak(strSpeak) ;
}
public void AnalyseSpeak(string strSpeak)
{
int iCbeg = 0 ;
int iEbeg = 0 ;
bool IsChina = true ;
for(int i=0;i<strSpeak.Length;i++)
{
char chr = strSpeak[i] ;
if (IsChina)
{
if (chr<=122&&chr>=65)
{
int iLen = i - iCbeg ;
string strValue = strSpeak.Substring(iCbeg,iLen) ;
SpeakChina(strValue) ;
iEbeg = i ;
IsChina = false ; 中国3S?3s8.cn
}
}
else
{
if (chr>122||chr<65)
{
int iLen = i - iEbeg ;
string strValue = strSpeak.Substring(iEbeg,iLen) ;
this.SpeakEnglishi(strValue) ;
iCbeg = i ;
IsChina = true ;
}
}
}//end for
if (IsChina)
{
int iLen = strSpeak.Length - iCbeg ;
string strValue = strSpeak.Substring(iCbeg,iLen) ;
SpeakChina(strValue) ;
}
else
{
int iLen = strSpeak.Length - iEbeg ;
string strValue = strSpeak.Substring(iEbeg,iLen) ;
SpeakEnglishi(strValue) ;
}
}
private void BuildSpeach()
{
if (voice == null)
voice = new SpVoiceClass() ;
}
public int Volume
{
get
{
return voice.Volume ;
}
set
{
voice.SetVolume((ushort)(value)) ;
}
}
public int Rate
{
get
{
return voice.Rate ;
}
set
{
voice.SetRate(value) ;
}
}
private void Speak(string strSpeack)
{
try
{
voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ;
}
catch(Exception err)
{
throw(new Exception("发生一个错误:"+err.Message)) ;
}
}
public void Stop()
{
voice.Speak(string.Empty,SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak) ;
}
public void Pause()
{
voice.Pause() ;
}
public void Continue()
{ 专业?S?3s8.cn
voice.Resume() ;
}
}//end class
=========================================================================
?private SpeechLib.SpVoiceClass voice =null;q里Q我们定义个一个用来发音的c,q且在第一ơ调用该cLQ对它用BuildSpeachҎ(gu)q行了初始化。?br />
我们q定义了两个属性Volume和RateQ能够设|音量和语速?
我们知道QSpVoiceClass 有一个SpeakҎ(gu)Q我们发音主要就是给他传递一个字W串Q它负责d该字W串Q如下所C?
其中SpeechVoiceSpeakFlags.SVSFlagsAsync表示异步发音?private void Speak(string strSpeack)
{
try
{
voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ;
}
catch(Exception err)
{
throw(new Exception("发生一个错误:"+err.Message)) ;
}
} 但是Q这个方法本wƈ不知道你l的字符串是什么语aQ所以需要我们它q个字符串用什么语ad。SpVoiceClass cȝVoice 属性就是用来设|语U的Q我们可以通过SpVoiceClass 的GetVoicesҎ(gu)得到所有的语种列表Q然后在Ҏ(gu)参数选择相应的语U,比如讄语种为汉语如下所C:
private void SetChinaVoice()
{
voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0) ;
}
0表示是汉用,1234都表C语,是口音不同。?br />
q样Q我们就讄了语U,如果l合发音Ҏ(gu)Q我们就可以设计Z个只发汉语语音的Ҏ(gu) private void SpeakChina(string strSpeak)
{
SetChinaVoice() ;
Speak(strSpeak) ;
}
只发p语音的方法也是类似的Q上面程序里有?
对于一D中英文混合的语aQ我们让E序d混合语音的方法就是:~程把这D语a的中英文分开Q对于中文调用SpeakChinaҎ(gu)Q英文调用SpeakEnglishiҎ(gu)Q至于怎样判断一个字W是英文q是中文Q我采用的是判断asc码的Ҏ(gu)Q具体的cL法是通过AnalyseSpeak实现的。?br />
q样Q对于一D中英文混合文字Q我们只需把它作ؓ参数传递给AnalyseSpeak可以了Q他能够完成中英文的混合发音?
============================================================================================================
当然Q对于发音的暂定、l、停止等操作Q上面也l出了简单的Ҏ(gu)调用Q很Ҏ(gu)明白?
下面单介l一下中文语韌别的Ҏ(gu)Q?
先把该语韌别的cL代码贴在下面Q然后再做说明:
我们定义了ssrContex 和isrg韌别的上下文和语法Q通过讄isrg的DictationSetStateҎ(gu)Q我们可以开始或l束识别Q在上面的程序中是BeginRec和CloseRecҎ(gu)。cDisplay 是我们用来输别结果的地方Qؓ了能够在大部分控件上都可以显C结果,我用了一个Control cL定义它。当Ӟ每次语音识别后都会触发ISpeechRecoContextEvents_RecognitionEventHandler 事gQ我们定义了一个这LҎ(gu)ContexRecognition来响应事Ӟq且在这个方法里输出识别l果?br />public class SpRecognition
{
private static SpRecognition _Instance = null ;
private SpeechLib.ISpeechRecoGrammar isrg ;
private SpeechLib.SpSharedRecoContextClass ssrContex =null;
private System.Windows.Forms.Control cDisplay ;
private SpRecognition()
{
ssrContex = new SpSharedRecoContextClass() ;
isrg = ssrContex.CreateGrammar(1) ;
SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle = new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition) ;
ssrContex.Recognition += recHandle ;
}
public void BeginRec(Control tbResult)
{ 中国3S?3s8.cn
isrg.DictationSetState(SpeechRuleState.SGDSActive) ;
cDisplay = tbResult ;
}
public static SpRecognition instance()
{
if (_Instance == null)
_Instance = new SpRecognition() ;
return _Instance ;
}
public void CloseRec()
{
isrg.DictationSetState(SpeechRuleState.SGDSInactive) ;
}
private void ContexRecognition(int iIndex,object obj,SpeechLib.SpeechRecognitionType type,SpeechLib.ISpeechRecoResult result)
{
cDisplay.Text += result.PhraseInfo.GetText(0,-1,true) ;
}
}
]]>
下面的Speech已对MSTTS作了单封装?
1.安装好MSTTSQ如果你有装金山词霸Q系l就已经安装了),可以在winnt\speech中打到vtxtauto.tlb文gQ?
2.?Net SDK自带的tlbimp工具把vtxtauto.tlb转换?dll格式:
tlbimp vtxtauto.tlb /silent /namespace:mstts /out:mstts.dll
q时的mstts.dll已成?net frameworkq行库的一个类?
3.~写一个封装vtxtauto的简单类:Speech .
//========================Speech.cs======================
using SystemQ?
using msttsQ?//MSTTS名称I间
namespace Bedlang{ //定义名称I间
public class Speech{
private VTxtAuto VTxtAutoExQ?
public SpeechQ){
VTxtAutoEx = new VTxtAutoQ)Q?
VTxtAutoEx.RegisterQ? "," "Q; //注册COMlg
}
public void SpeakQString textQ{
VTxtAutoEx.SpeakQtext, 0Q; //发音
}
}
}
//========================Speech.cs======================
4.~译B(ti)edlang.Speech
csc /target:library /out:Bedlang.dll speech.cs /r:mstts.dll
如果用vs.net开发,可直接生成项目就可以了?
5.发音实现
//========================demo.cs======================
分别加入Label,TextBox,Button控g各一个到windows Form中,修改它们的属性,源代码如下:
using SystemQ?
using System.DrawingQ?
using System.CollectionsQ?
using System.ComponentModelQ?
using System.Windows.FormsQ?
using System.DataQ?
namespace Bedlang
{
///
/// Form1 的摘要说明?
///
public class demo : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1Q?
private System.Windows.Forms.TextBox textBox1Q?
private System.Windows.Forms.Button button1Q?
///
/// 必需的设计器变量?
///
private System.ComponentModel.Container components = nullQ?
public demoQ)
{
//
// Windows H体设计器支持所必需?
//
InitializeComponentQ)Q?
//
// TODO: ?InitializeComponent 调用后添加Q何构造函C?
//
}
///
/// 清理所有正在用的资源?
///
protected override void DisposeQ?bool disposing Q?
{
ifQ?disposing Q?
{
if Qcomponents != nullQ?
{
components.DisposeQ)Q?
}
}
base.DisposeQ?disposing Q;
}
]]>
using System.IO;
using System.Text;
namespace WAV
{
/// <summary>
/// Summary description for Wav.
/// </summary>
public class Wav
{public Wav()
{ //
// TODO: Add constructor logic here
//
}
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
string strpath=@"C:\Documents and Settings\Administrator\桌面\trojan\怀忉|?wav";//=@"F:\Music";
if(args.Length>0)
{ strpath=args[0].Trim();
}
if(File.Exists(strpath))
{ GetWavInfo(strpath);
Console.WriteLine("GetWavInfo Successfully!");
//Console.WriteLine("");
}
else
{ Console.Write("Please Enter the write filepath!\n");
专业?S?3s8.cn
专业?S?3s8.cn