The implementation method of C voice function
- 2020-05-10 18:39:09
- OfStack
The SpeechSDK5.1 development kit and SpeechSDK5.1 Langague Pack (English and Chinese) language kit should be installed first, but VS2010 comes with SpeechSDK5.0 com component, which can also be used.
briefly describes 1 of 4 methods:
When reading aloud, use
voice.Speak(string,SpeechVoiceSpeakFlags.SVSFlagsAsync);
Pause, use
voice.Pause();
Continue the reading from the pause, use
voice.Resume();
Stop function
voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
In this way, the functions of "reading aloud", "pause", "continue" and "stop" can be realized completely.
The example code is given directly below:
private void button1_Click(object sender, EventArgs e)
{
Analyse(this.textBox1.Text);
}
public void Analyse(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 (Convert.ToInt32(chr) <= 122 && Convert.ToInt32(chr) >= 65)
{
int iLen = i - iCbeg;
string strValue =
strSpeak.Substring(iCbeg, iLen);
SpeakChina(strValue);
iEbeg = i;
IsChina = false;
}
}
else
{
if (Convert.ToInt32(chr) > 122 || Convert.ToInt32(chr) < 65)
{
int iLen = i - iEbeg;
string strValue =
strSpeak.Substring(iEbeg, iLen);
this.SpeakEnglishi(strValue);
iCbeg = i;
IsChina = true;
}
}
}
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);
}
}
// Chinese
private void SpeakChina(string speak)
{
voice = new SpVoice();
voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(3);// Among them 3 As a Chinese, 024 Is English,
voice.Speak(speak, SpeechVoiceSpeakFlags.SVSFDefault);
}
// English
private void SpeakEnglishi(string speak)
{
voice = new SpVoice();
voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);// Among them 3 As a Chinese, 024 Is English,
voice.Speak(speak, SpeechVoiceSpeakFlags.SVSFDefault);
}
// Save the voice
private void button2_Click(object sender, EventArgs e)
{
try
{
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice Voice = new SpVoice();
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
sfd.Title = "Save to a wave file";
sfd.FilterIndex = 2;
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream SpFileStream = new SpFileStream();
SpFileStream.Open(sfd.FileName, SpFileMode, false);
Voice.AudioOutputStream = SpFileStream;
Voice.Speak(this.textBox1.Text, SpFlags);
Voice.WaitUntilDone(100);
SpFileStream.Close();
}
}
catch (Exception er)
{
MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}