列举语音[Voices]
翻译说明:由于这个部分涉及到了Delphi编写的简单程序,将其换成C#实现,有些不必要的说明不按照原文翻译,基本上是按照原文复述
本节目标:调整朗读语音,调整语音频率和音量
代码环境设定:两个TrackBar代表语音频率和音量,trackBarRate,trackBarVolume,一个ComboBox选择朗读语音,comboBoxLanguage
PS:朗读语音,可能会不明白朗读语音的意思,就是在控制面板-语音-文字语音转换-"语音选择"中的项,比如Microsoft Mary,Microsoft Sam等等
SpVoiceClass类中GetVoices函数原型如下
public
virtual
ISpeechObjectTokens GetVoices(
string
RequiredAttributes,
string
OptionalAttributes);
该函数返回一个ISpeechObjectToken集合ISpeechObjectTokens,ISpeechObjectToken描述了每个朗读语音
函数两个参数均为对所得到集合的限制,第二参数为对第一参数的补充,比如使用
GetVoices(
'
Gender = male
'
,
''
)
就会得到男声集合
对于这些参数,一般有如下参数:Name,Vendor,Age,Gender,Language[有没有更多的不知道,翻过SAPI的帮助也没找到相关说明]
调用以下语句作以说明
ISpeechObjectToken sot
=
svc.GetVoices(String.Empty, String.Empty).Item(
0
);
System.Diagnostics.Trace.WriteLine(sot.GetAttribute( " Name " ));
System.Diagnostics.Trace.WriteLine(sot.GetAttribute( " Vendor " ));
System.Diagnostics.Trace.WriteLine(sot.GetAttribute( " Age " ));
System.Diagnostics.Trace.WriteLine(sot.GetAttribute( " Gender " ));
System.Diagnostics.Trace.WriteLine(sot.GetAttribute( " Language " ));
System.Diagnostics.Trace.WriteLine(sot.GetAttribute( " Name " ));
System.Diagnostics.Trace.WriteLine(sot.GetAttribute( " Vendor " ));
System.Diagnostics.Trace.WriteLine(sot.GetAttribute( " Age " ));
System.Diagnostics.Trace.WriteLine(sot.GetAttribute( " Gender " ));
System.Diagnostics.Trace.WriteLine(sot.GetAttribute( " Language " ));
输出中会显示
Microsoft Mary
Microsoft
Adult
Female
409 ; 9
Microsoft
Adult
Female
409 ; 9
其中409;9代表该语音支持409[英语]和9[...]语言
[这里原文提到了申请ISpeechObjectToken实例的垃圾处理的问题,由于C#的垃圾处理机制,不翻译了]
必要函数介绍完了,下面是完成目标的代码,很简单,一些没说到的写在了注释里
SpVoiceClass svc
=
new
SpVoiceClass();
private void button1_Click( object sender, EventArgs e)
{
svc.Volume = trackBarVolume.Value;
svc.Rate = trackBarRate.Value;
svc.Voice = svc.GetVoices(string.Empty, string.Empty).Item(comboBoxLanguage.SelectedIndex);
svc.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFDefault);
}
private void Form1_Shown( object sender, EventArgs e)
{
trackBarRate.Minimum = -10;
trackBarRate.Maximum = 10;
trackBarRate.Value = svc.Rate;//Rate取值 -10 10
trackBarVolume.Maximum = 100;
trackBarVolume.Value = svc.Volume;//Volume取值 0100
ISpeechObjectTokens sots = svc.GetVoices(String.Empty, String.Empty);
int i = 0;
foreach (ISpeechObjectToken sot in sots)
{
String str = sot.GetDescription(0);//得到语音名称|相当于sot.GetAttribute("name")
comboBoxLanguage.Items.Add(str);
if (str == svc.Voice.GetDescription(0)) comboBoxLanguage.SelectedIndex = i;
i++;
}
}
private void button1_Click( object sender, EventArgs e)
{
svc.Volume = trackBarVolume.Value;
svc.Rate = trackBarRate.Value;
svc.Voice = svc.GetVoices(string.Empty, string.Empty).Item(comboBoxLanguage.SelectedIndex);
svc.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFDefault);
}
private void Form1_Shown( object sender, EventArgs e)
{
trackBarRate.Minimum = -10;
trackBarRate.Maximum = 10;
trackBarRate.Value = svc.Rate;//Rate取值 -10 10
trackBarVolume.Maximum = 100;
trackBarVolume.Value = svc.Volume;//Volume取值 0100
ISpeechObjectTokens sots = svc.GetVoices(String.Empty, String.Empty);
int i = 0;
foreach (ISpeechObjectToken sot in sots)
{
String str = sot.GetDescription(0);//得到语音名称|相当于sot.GetAttribute("name")
comboBoxLanguage.Items.Add(str);
if (str == svc.Voice.GetDescription(0)) comboBoxLanguage.SelectedIndex = i;
i++;
}
}