【转】[语音合成技术第一讲]web 页面中使用语音合成技术

原文:http://blog.csdn.net/artlife/article/details/1449162

 

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");
 
上面的代码是创建一个" 朗读人"对象,我们要将这个写在js中(有点废话,呵呵)。
 
 
下面的代码将告诉我们" 朗读人"是如何工作的:
 
VoiceObj.Speak(“hello world”);
 
 
下面的代码告诉了我们如何销毁我们的" 朗读人"
 
// Clean up voice object
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  )
    
{
        VoiceObj.Rate 
=  VoiceObj.Rate  +   1 ;
    }

}


//  DecRate() function:
//
        This function decreases the speaking rate by -1 down to a minimum
//
        of -10.
function  DecRate()  {
    
if ( VoiceObj.Rate  >   - 10  )
    
{
        VoiceObj.Rate 
=  VoiceObj.Rate  -   1 ;
    }

}


//  IncVol() function:
//
        This function increases the speaking volume by 10 up to a maximum
//
        of 100.
function  IncVol()  {
    
if ( VoiceObj.Volume  <   100  )
    
{
        VoiceObj.Volume 
=  VoiceObj.Volume  +   10 ;
    }

}


//  DecVol() function:
//
        This function decreases the speaking volume by -10 down to a minimum
//
        of 0.
function  DecVol()  {
    
if ( VoiceObj.Volume  >   9  )
    
{
        VoiceObj.Volume 
=  VoiceObj.Volume  -   10 ;
    }

}


//  SpeakText() function:
//
        This function gets the text from the textbox and sends it to the
//
        Voice object's Speak() function. The value "1" for the second
//
              parameter corresponds to the SVSFlagsAsync value in the SpeechVoiceSpeakFlags
//
        enumerated type.
function  SpeakText()  {
    
if ( idbSpeakText.value  ==   " SpeakText "  )
    
{
        
//  Speak the string in the edit box
         try
        
{
            VoiceObj.Speak( idTextBox.value );
        }

        
catch (exception)
        
{
            alert(
" Speak error " );
        }

    }

    
else   if ( idbSpeakText.value  ==   " Stop "  )
    
{
        
//  Speak empty string to Stop current speaking. The value "2" for 
         //  the second parameter corresponds to the SVSFPurgeBeforeSpeak
         //  value in the SpeechVoiceSpeakFlags enumerated type.
        VoiceObj.Speak(  "" );
    }

}


</ SCRIPT >

< SCRIPT  FOR ="window"  EVENT ="OnQuit()"  LANGUAGE ="JavaScript" >
    
//  Clean up voice object
     delete  VoiceObj;
</ SCRIPT >

</ HEAD >





< BODY >
< H1  align =center > Simple TTS (DHTML) </ H1 >
< H1  align =center >< FONT  size =3 > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </ FONT >
< IMG  alt =""  border =2  hspace =0  id =idImage  src ="mouthclo.bmp" > &nbsp;   </ H1 >
< H1  align =center >   
< TEXTAREA  ID =idTextBox  COLS =50  ROWS =10  WRAP =VIRTUAL > Enter text you wish spoken here </ TEXTAREA >
</ H1 >

< align =center >< STRONG >< STRONG >  
Rate
&nbsp; < STRONG >  
< INPUT  id =idbIncRate  name =button1  type =button  onclick =IncRate()  value ="    +    " ></ STRONG > &nbsp;
< INPUT  id =idbDecRate  name =button2  type =button  onclick =DecRate()  value ="    -    "  style ="LEFT: 237px; TOP: 292px" > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </ STRONG > &nbsp;  

Volume
&nbsp; < STRONG >< STRONG >  
< INPUT  id =idbIncVol  name =button3  onclick =IncVol()  style ="LEFT: 67px; TOP: 318px"  type =button  value ="    +    " > &nbsp;
< INPUT  id =idbDecVol  name =button4  onclick =DecVol()  type =button  value ="    -    "  style ="LEFT: 134px; TOP: 377px" >
</ STRONG ></ STRONG ></ STRONG ></ P >
 
< align =center >< STRONG >< BUTTON  id =idbSpeakText  onclick =SpeakText(); 
   
style ="HEIGHT: 24px; LEFT: 363px; TOP: 332px; WIDTH: 178px" > SpeakText </ BUTTON ></ STRONG ></ P >
 
< align =center >< STRONG > Voice &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   
< STRONG > Audio Output &nbsp; </ STRONG ></ STRONG ></ P >
< align =center >  

< SELECT  id =idsVoices  name =Voices  onchange =ChangeVoice()  style ="FONT-FAMILY: serif; HEIGHT: 21px; WIDTH: 179px" >   </ SELECT >
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

< SELECT  id =idsAudioOutputs  name =AudioOutputs  onchange =ChangeAudioOutput()  style ="HEIGHT: 22px; WIDTH: 179px" >    </ SELECT >
 

< SCRIPT  LANGUAGE ="JavaScript" >
//  Code in the BODY of the webpage is used to initialize controls and
//
 to handle SAPI events

/* **** Initializer code **** */
InitializeControls();

function  InitializeControls()
{
    
//  Initialize the Voices and AudioOutput Select boxes
     var  VoicesToken  =  VoiceObj.GetVoices();
    
var  AudioOutputsToken  =  VoiceObj.GetAudioOutputs();

    
//  Add correct strings to Voice Select box
     for var  i = 0 ; i < VoicesToken.Count; i ++  )
    
{
        
var  oOption  =  document.createElement( " OPTION " );
        idsVoices.options.add(oOption);
        oOption.innerText 
=  VoicesToken.Item(i).GetDescription();
        oOption.value 
=  i;
    }


    
//  Add correct strings to Audio Output Select box
     for var  i = 0 ; i < AudioOutputsToken.Count; i ++  )
    
{
        
var  oOption  =  document.createElement( " OPTION " );
        idsAudioOutputs.options.add(oOption);
        oOption.innerText 
=  AudioOutputsToken.Item(i).GetDescription();
        oOption.value 
=  i;
    }
    
}


/* **** Event handling code **** */
//  These functions are used to handle the SAPI events

//  Handle StartStream event    
function  VoiceObj::StartStream()  {
    idbSpeakText.value 
=   " Stop " ;
}


//  Handle EndStream event    
function  VoiceObj::EndStream()  {
    idbSpeakText.value 
=   " SpeakText " ;
    idImage.src 
=   " mouthclo.bmp " ;
}


//  Handle Viseme event    
function  VoiceObj::Viseme(StreamNum, StreamPos, Duration, VisemeType, Feature, VisemeId)  {
    
//  Map the VisemeId to the appropriate .bmp
     if ( VisemeId  ==   15   ||  VisemeId  ==   17   ||  VisemeId  ==   18   ||  VisemeId  == 21  )
    
{
        idImage.src 
=   " mouthop1.bmp " ;
    }

    
else   if ( VisemeId  ==   14   ||  VisemeId  ==   16   ||  VisemeId  ==   19   ||  VisemeId  ==   20  )
    
{
        idImage.src 
=   " mouthop2.bmp " ;
    }

    
else   if ( VisemeId  ==   4   ||  VisemeId  ==   6   ||  VisemeId  ==   9   ||  VisemeId  ==   12  )
    
{
        idImage.src 
=   " mouthop3.bmp " ;
    }

    
else   if ( VisemeId  ==   1   ||  VisemeId  ==   2   ||  VisemeId  ==   3   ||  VisemeId  ==   11  )
    
{
        idImage.src 
=   " mouthop4.bmp " ;
    }

    
else   if ( VisemeId  ==   7   ||  VisemeId  ==   8  )
    
{
        idImage.src 
=   " mouthnar.bmp " ;
    }

    
else   if ( VisemeId  ==   5   ||  VisemeId  ==   10   ||  VisemeId  ==   13  )
    
{
        idImage.src 
=   " mouthmed.bmp " ;
    }

    
else
    
{
        idImage.src 
=   " mouthclo.bmp " ;
    }

}

</ SCRIPT >



< STRONG >
< HR ></ STRONG >
< P ></ P >

</ BODY >
</ HTML >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值