web 页面中使用语音合成技术
前言:
语音合成技术其实并没有什么神秘的,也不像想象中的那么繁杂。今天我就与大家一起来看一下,怎么让我们网页为我们朗读文本。怎样做到在
web
中进行语音合成
。我也将使用最短的代码,最通俗话语来完成这篇文章。
环境要求:
首先我们需要一个微软的
Speech SDK 5.1
的安装包(当然你的机器的操作系统版本要在
windows2000
以上的这个范畴)
,
来使得我们的机器具有语音识别的功能。安装包,您可以在这里找到:
安装说明:
·
If you want to download sample code, documentation, SAPI, and the U.S. English Speech engines for development purposes, download the Speech SDK 5.1 file (SpeechSDK51.exe).
·
If you want to use the Japanese and Simplified Chinese engines for development purposes, download the Speech SDK 5.1 Language Pack file (SpeechSDK51LangPack.exe) in addition to the Speech SDK 5.1 file.
·
If you want to redistribute the Speech API and/or the Speech engines to integrate and ship as a part of your product, download the Speech 5.1 SDK Redistributables file (SpeechSDK51MSM.exe).
·
If you want to get only the Mike and Mary voices redistributable for Windows XP, download Mike and Mary redistributables (Sp5TTIntXP.exe).
·
If you only want the documentation, download the Documentation file (sapi.chm).
其实上面这些可以不看,请您下载并安装
SpeechSDK51.exe
和
SpeechSDK51LangPack.exe
就可以了。
让我们开始:
环境已经准备好了
,那就让我们正式开始吧。
首先我们需要一个能够"发声"的对象,暂时我们就称他为" 朗读人"。在不同的语音合成的程序中,他所出现的形式也是不同的,当然这是后话,以后我再告诉你(嘿嘿,不是卖关子,这是第一讲,咱们先让它能说话了先)。
在web 应用程序的 html 代码中创建" 朗读人"对象:
// Create the Sapi SpVoice object
var VoiceObj = new ActiveXObject("Sapi.SpVoice");
var VoiceObj = new ActiveXObject("Sapi.SpVoice");
上面的代码是创建一个" 朗读人"对象,我们要将这个写在js中(有点废话,呵呵)。
下面的代码将告诉我们" 朗读人"是如何工作的:
VoiceObj.Speak(“hello world”);
下面的代码告诉了我们如何销毁我们的" 朗读人"
// Clean up voice object
delete VoiceObj;
delete VoiceObj;
当您如果读到了这里,我首先要感谢您的耐心。与此同时我也要恭喜你了,如果您是一个敏感的程序员。这个时候您可能已经开始编写您自己的语音合成代码了。因为我们知道了,如何创建对象,如何使用对象的方法,和如何delete它。
当然这些还远远不够,让我们再做的更好些:
控制声音的属性
控制音量(1~100):
VoiceObj.Volume = 80 ;
控制语速(-10~10)
VoiceObj.Rate = 0;
控制朗读人的声音
VoiceObj.Voice = "Microsoft Anna";
控制朗读人的硬件设备输出
VoiceObj.AudioOutput = "SoundMax Integrated";
好了 该知道的 我们都已经知道了 , 再让我们看一个完整的例子来结束我们这一次的语音合成的学习。
完整的例子:
<!--
Copyright @ 2001 Microsoft Corporation All Rights Reserved.
-->
< HTML >
< HEAD >
< META HTTP-EQUIV ="Content-Type" content ="text/html; charset=UTF-8" >
< TITLE > TTS Demo </ TITLE >
< SCRIPT LANGUAGE ="JavaScript" > ...
// Create the Sapi SpVoice object
var VoiceObj = new ActiveXObject("Sapi.SpVoice");
// ChangeVoice() function:
// This function sets the newly selected voice choice from the Voice
// Select box on the Voice object.
function ChangeVoice() ...{
var i = parseInt( idsVoices.value );
VoiceObj.Voice = VoiceObj.GetVoices().Item(i);
}
// ChangeAudioOutput() function:
// This function sets the newly selected audio output choice from the
// Audio Output Select box on the Voice object.
function ChangeAudioOutput() ...{
var i = parseInt( idsAudioOutputs.value );
VoiceObj.AudioOutput = VoiceObj.GetAudioOutputs().Item(i);
}
// IncRate() function:
// This function increases the speaking rate by 1 up to a maximum
// of 10.
function IncRate() ...{
if( VoiceObj.Rate < 10
< HTML >
< HEAD >
< META HTTP-EQUIV ="Content-Type" content ="text/html; charset=UTF-8" >
< TITLE > TTS Demo </ TITLE >
< SCRIPT LANGUAGE ="JavaScript" > ...
// Create the Sapi SpVoice object
var VoiceObj = new ActiveXObject("Sapi.SpVoice");
// ChangeVoice() function:
// This function sets the newly selected voice choice from the Voice
// Select box on the Voice object.
function ChangeVoice() ...{
var i = parseInt( idsVoices.value );
VoiceObj.Voice = VoiceObj.GetVoices().Item(i);
}
// ChangeAudioOutput() function:
// This function sets the newly selected audio output choice from the
// Audio Output Select box on the Voice object.
function ChangeAudioOutput() ...{
var i = parseInt( idsAudioOutputs.value );
VoiceObj.AudioOutput = VoiceObj.GetAudioOutputs().Item(i);
}
// IncRate() function:
// This function increases the speaking rate by 1 up to a maximum
// of 10.
function IncRate() ...{
if( VoiceObj.Rate < 10