COM組件使用speech:
public class Speach
{
private static Speach _Instance = null ;
private SpeechLib.SpVoiceClass voice =null; //SAPI5.1
private SpeechLib.SpVoice voice = null;//SAPI 5.4
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) ;
}
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 ;
}
}
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()
{
voice.Resume() ;
}
}//end class
使用.NET類庫和系統API:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Synthesis;
using System.Speech;
namespace StudyBeta
{
public class SRead
{
public SpeechSynthesizer synth; //語音合成對象
public SRead()
{
synth = new SpeechSynthesizer();
}
public SRead(int m, int n)
{
//使用 synth 設置朗讀音量 [范圍 0 ~ 100]
synth.Volume = m;
//使用 synth 設置朗讀頻率 [范圍 -10 ~ 10]
synth.Rate = n;
}
public void SpeakChina(string ggg)
{
//SpVoice Voice = new SpVoice();
synth.SelectVoice("Microsoft Lili");
//Voice.Speak(ggg, SpFlags);
synth.SpeakAsync(ggg);
//String speechPeople = synth.Voice;
//使用 synth 設置朗讀音量 [范圍 0 ~ 100]
// synth.Volume = 80;
//使用 synth 設置朗讀頻率 [范圍 -10 ~ 10]
// synth.Rate = 0;
//使用synth 合成 wav 音頻文件:
//synth.SetOutputToWaveFile(string path);
}
public void SpeakEnglish(string ggg)
{
//SpVoice Voice = new SpVoice();
synth.SelectVoice("VW Julie");
synth.Speak(ggg); //ggg為要合成的內容
}
public int m
{
get
{
return synth.Volume;
}
set
{
synth.Volume = value;
}
}
public int n
{
get
{
return synth.Rate;
}
set
{
synth.Rate = value;
}
}
}
TTS朗讀軟件調用Microsoft Speech Platform
1.支持Microsoft Speech Platform的TTS朗讀軟件
介紹過的Balabolka和TextToWav都支持Microsoft Speech Platform,可以直接使用。
- Balabolka和TextToWav都是32位程序,所以Microsoft Speech Platform Runtime需要安裝32位的,無論系統是否是64位;
- TextToWav官方說明是支持Microsoft Speech Platform 10,但測試Version 11也完美支持。
2.不支持Microsoft Speech Platform的TTS朗讀軟件
也就是早期只支持SAPI4和SAPI5的TTS朗讀軟件,比如DSpeech和朗讀女。這類軟件就只能更改Microsoft Speech Platform語音庫的注冊信息了。
以Windows 7安裝Microsoft Huihui為例
- 執行完整“Microsoft Speech Platform 11安裝”步驟(Dspeech和朗讀女也是32位);
- 64位Windows7打開注冊表,找到
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Speech
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Speech Server分別導出(32為應該在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\);
- 編輯Speech Server.reg
- 將全部“Speech Server\v11.0”替換為“Speech”;
- 將全部“Speech Server\\v11.0”替換為“Speech”。
- 依次導入Speech Server.reg和Speech.reg,中間提示錯誤信息不用理會。
這樣在Dspeech和朗讀女中已經可以正確顯示和調用Microsoft Huihui朗讀文本了。
同時,這樣修改后,支持Microsoft Speech Platform的TTS朗讀軟件並不受影響。
以上兩種方法取決於TTS朗讀軟件對Microsoft Speech Platform的支持情況,但系統的控制面板並不會顯示有新的語音庫可選,只能使用TTS朗讀軟件。
其他收費TTS朗讀軟件沒做測試。
如果你使用的是Neospeech等高級語音庫,沒有必要用微軟的,微軟的只是一個簡化版語音庫,但是是完全免費。