Delphi 实现简易语音发音(基于TTS方式)


uses Comobj;

procedure TForm1.Button1Click(Sender: TObject);
var
voice: OLEVariant;
begin
voice := CreateOLEObject('SAPI.SpVoice');
voice.Speak('Hello World!', 0);
end;

 

 

Delphi_TTS_1

unit Unit1; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, ComCtrls, Menus, OleCtrls, ACTIVEVOICEPROJECTLib_TLB; 
 
type 
  TForm1 = class(TForm) 
    MainMenu1: TMainMenu; 
    Go1: TMenuItem; 
    Alpha1: TMenuItem; 
    N201: TMenuItem; 
    N501: TMenuItem; 
    N1001: TMenuItem; 
    RT: TRichEdit; 
    ss: TDirectSS; 
    procedure RS(Sender: TObject); 
    procedure N201Click(Sender: TObject); 
    procedure N501Click(Sender: TObject); 
    procedure N1001Click(Sender: TObject); 
    procedure Go1Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 
 
var 
  Form1: TForm1; 
 
implementation 
 
{$R *.dfm} 
 
procedure TForm1.RS(Sender: TObject); 
begin 
rt.Width :=form1.Width -10; 
rt.Height :=form1.Height -48; 
end; 
 
procedure TForm1.N201Click(Sender: TObject); 
begin 
form1.AlphaBlendValue := 51; 
end; 
 
procedure TForm1.N501Click(Sender: TObject); 
begin 
form1.AlphaBlendValue := 130; 
end; 
 
procedure TForm1.N1001Click(Sender: TObject); 
begin 
form1.AlphaBlendValue := 255; 
end; 
 
procedure TForm1.Go1Click(Sender: TObject); 
begin 
ss.Speak(rt.Text); 
end; 
 
end. 
View Code

SpeechLib_TLB控件效能

SpeechLib_TLB控件功能
我在Delphi编写的软件中用到SpeechLib_TLB,声音软件XPSpeech.msi,可以发出的声音不好听,而且声音很小,想请问是哪方面的原因,是XPSpeech.msi软件的原因吗?需要购买发声收费软件吗?
------解决方案--------------------------------------------------------


unit Ifly_Stts;



interface



implementation



// 各种声音格式

Const

 TTS_ADF_DEFAULT = 0;

 TTS_ADF_PCM8K8B1C = 1;

 TTS_ADF_PCM16K8B1C = 2;

 TTS_ADF_PCM8K16B1C = 3;

 TTS_ADF_PCM16K16B1C = 4;

 TTS_ADF_PCM11K8B1C = 5;

 TTS_ADF_PCM11K16B1C = 6;

 TTS_ADF_ALAW16K1C = 9;

 TTS_ADF_ULAW16K1C = 10;

 TTS_ADF_ALAW8K1C = 11;

 TTS_ADF_ULAW8K1C = 12;

 TTS_ADF_ALAW11K1C = 13;

 TTS_ADF_ULAW11K1C = 14;

 TTS_ADF_ADPCMG7218K4B1C = 17;

 TTS_ADF_ADPCMG7216K4B1C = 18;

 TTS_ADF_ADPCMG7233B1C = 19;

 TTS_ADF_ADPCMG7235B1C = 20;

 TTS_ADF_VOX6K1C = 21;

 TTS_ADF_VOX8K1C = 22;

//文本内码

 TTS_CP_GB2312 = 1

 TTS_CP_GBK = 2

 TTS_CP_UNICODE = 4



//'参数类型

 TTS_PARAM_LOCAL_BASE = &H0;

 TTS_PARAM_INBUFSIZE = &H1;

 TTS_PARAM_OUTBUFSIZE = &H2;

 TTS_PARAM_VID = &H3;

 TTS_PARAM_CODEPAGE = &H4;

 TTS_PARAM_AUDIODATAFMT = &H5;

 TTS_PARAM_SPEED = &H6;

 TTS_PARAM_AUDIOHEADFMT = &H7;

 TTS_PARAM_VOLUME = &H8;

 TTS_PARAM_PITCH = &H9;



type

//'================================================================

//' STTS Api 声明

//'================================================================

//' 初始化

function STTSInit():boolean;stdcall;

external 'STTSApi.dll';

//' 逆初始化

function STTSDeInit():boolean;stdcall;

external 'STTSApi.dll';



//' 建立与TTS服务器的连接

function  STTSConnect(sSerialNumber:string;sServerIP:string):Integer;stdcall; 

external 'STTSApi.dll';

//' 断开与TTS服务器的连接

Function STTSDisconnect(hTTSInstance:Integer):boolean;stdcall; 

external 'STTSApi.dll';

//' 设置本次连接的合成参数

Function STTSSetParam(hTTSInstance:integer;lngType:integer;lngParam:integer):Boolean;stdcall; 

external 'STTSApi.dll';

//' 获得本次连接的合成参数

Function STTSGetParam(hTTSInstance:Integer;lngType:integer;lngParam:integer):Boolean;stdcall; 

external 'STTSApi.dll';

//' 从字符串合成到音频文件

Function STTSString2AudioFile(hTTSInstance:Integer;sString:string;sWaveFile:string):Boolean;stdcall; 

external 'STTSApi.dll';





//' 从文本文件合成到音频文件

Function STTSFile2AudioFile(hTTSInstance:Integer;sTextFile:string;sWaveFile:string):Boolean;stdcall; 

external 'STTSApi.dll';



//'播放字符串

Function STTSPlayString(hTTSInstance:Integer;sTextFile:string;bAsynch:Boolean):Boolean;stdcall; 

external 'STTSApi.dll';



//'播放文本文件

Function SSTTSPlayTextFile(hTTSInstance:Integer;sTextFile:string;bAsynch:Boolean):Boolean;stdcall; 

external 'STTSApi.dll';



//'播放停止

Function STTSPlayStop():Boolean;stdcall; 

external 'STTSApi.dll';

//'查询播放状态

Function STTSQueryPlayStatus(lngStatus:Integer):Boolean;stdcall; 

external 'STTSApi.dll';



//'TTS版本信息

Function STTSAbout(sAboutInfo:string;ninfosize:Integer):Boolean;stdcall; 

external 'STTSApi.dll';
//' 用来获取错误代码的Windows API函数
//Public Declare Function GetLastError Lib "kernel32" () As Long
end;

end.








unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Memo1: TMemo;

    Button1: TButton;

    Button2: TButton;

    Button3: TButton;

    Button4: TButton;

    OpenDialog1: TOpenDialog;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }



  end;



var

  Form1: TForm1;



implementation





{$R *.dfm}





//'================================================================

//' STTS Api 声明

//'================================================================

//' 初始化

function STTSInit():boolean;stdcall; external 'STTSApi.dll';

//' 逆初始化

function STTSDeinit():boolean;stdcall;

external 'STTSApi.dll';



//' 建立与TTS服务器的连接

function  STTSConnect(sSerialNumber:string;sServerIP:string):Integer;stdcall;

external 'STTSApi.dll';

//' 断开与TTS服务器的连接

Function STTSDisconnect(hTTSInstance:Integer):boolean;stdcall;

external 'STTSApi.dll';

//' 设置本次连接的合成参数

Function STTSSetParam(hTTSInstance:integer;lngType:integer;lngParam:integer):Boolean;stdcall;

external 'STTSApi.dll';

//' 获得本次连接的合成参数

Function STTSGetParam(hTTSInstance:Integer;lngType:integer;lngParam:integer):Boolean;stdcall;

external 'STTSApi.dll';

//' 从字符串合成到音频文件

Function STTSString2AudioFile(hTTSInstance:Integer;sString:string;sWaveFile:string):Boolean;stdcall;

external 'STTSApi.dll';





//' 从文本文件合成到音频文件

Function STTSFile2AudioFile(hTTSInstance:Integer;sTextFile:string;sWaveFile:string):Boolean;stdcall;

external 'STTSApi.dll';



//'播放字符串

Function STTSPlayString(hTTSInstance:Integer;sString:string;bAsynch:Boolean):Boolean;stdcall;

external 'STTSApi.dll';



//'播放文本文件

Function STTSPlayTextFile(hTTSInstance:Integer;sTextFile:string;bAsynch:Boolean):Boolean;stdcall;

external 'STTSApi.dll';



//'播放停止

Function STTSPlayStop():Boolean;stdcall;

external 'STTSApi.dll';

//'查询播放状态

Function STTSQueryPlayStatus(lngStatus:Integer):Boolean;stdcall;

external 'STTSApi.dll';



//'TTS版本信息

Function STTSAbout(sAboutInfo:string;ninfosize:Integer):Boolean;stdcall;

external 'STTSApi.dll';



//' 用来获取错误代码的Windows API函数

//Public Declare Function GetLastError Lib "kernel32" () As Long





procedure TForm1.Button1Click(Sender: TObject);

var

myhandle:integer;

begin

STTSInit();

myhandle:=STTSconnect('80','127.0.0.1');

STTSPlayString(myhandle,self.Memo1.Text,true) ;

STTSDisconnect(myhandle);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
STTSPlayStop();
end;

procedure TForm1.Button3Click(Sender: TObject);
var
strls:string;
myhandle:integer;
begin
if not self.OpenDialog1.Execute then exit;
strls:=self.OpenDialog1.FileName;
STTSInit();
myhandle:=STTSconnect('80','127.0.0.1');
STTSPlayTextFile(myhandle,strls,true);
STTSDisconnect(myhandle);


end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
STTSDeInit();
end;

end.



运行目录下必须有四个DLL
AUCodec.dll
iFlySAPI.dll
iFlyTTS.dll
STTSApi.dll
View Code

应用 Microsoft TTS 语音引擎的 Delphi 语言实例

建立一个单窗体工程,添加一个 Edit 和 2 个 Button 控件,分别命名为 Edit1、btnSpeak、btnStop。然后安装引用 TTS 的 vtxtauto.tlb 类库文件,方法是点击菜单“工程-Import Type Library”,点“add”按钮,然后选中 Windows/speech 下的 Vtxtauto.tlb 文件,放入安装包后,再将其添加成 Delphi 的一个 Unit,自动产生的名字为“VTxtAuto_TLB”。然后,在主 Unit 的 Uses 里引用 VTxtAuto_TLB,即在 Uses 后添加字串“VTxtAuto_TLB”。最后参考以下代码:

procedure TfrmSpeech.FormCreate(Sender: TObject);

    begin

      vtxt:=CoVTxtAuto_.Create;

      vtxt.Register(‘’,‘Speech’);

      Edit1.Text:=‘I am a chinese, I love my homeland very much, and you?’

    end;



    procedure TfrmSpeech.btnSpeakClick(Sender: TObject);

    begin

      vtxt.Speed:=100;

      vtxt.Speak(trim(Edit1.Text),10);

    end;



    procedure TfrmSpeech.btnStopClick(Sender: TObject);

    begin

      vtxt.StopSpeaking;

    end;

    end.
View Code

delphi7如何调用系统语音库

(一)要使用系统的语音库,你需要先安装MicrosoftSpeechSDK5.1及其语言包,下载地址: SpeechSDK5.1: http://www.newhua.com/soft/38264.htmSpeechSDK5.1语言包(中英文): http://www.onlinedown.net/soft/38265.htm

(二)安装完成后,需要在D7里导入类库以便生成SAPI组件于IDE组件面板. 启动D7,Project->ImportTypeLibrary->找到MicrosoftSpeechObjectLibrary(Version5.0) 点Install.. 成功后会在D7的组件面板里出现SAPI5.1控件页.

(三)OK,开始来个简单的工程. 启动D7新建个工程,往窗口里扔进一个TMemo,两个TButton,再扔进一个TSpVoice(SAPI5.1控件页),写下下边的代码:

unitUnit1; interface uses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms, Dialogs,OleServer,SpeechLib_TLB,StdCtrls; type TForm1=class(TForm) Button1:TButton; Memo1:TMemo; SpVoice1:TSpVoice; Button2:TButton; Button3:TButton; procedureButton1Click(Sender:TObject); procedureButton3Click(Sender:TObject); private {Privatedeclarations} public procedurePlayVoxEx; end; var Form1:TForm1; implementation {$R*.dfm} {TForm1} procedureTForm1.PlayVoxEx;var i:Integer; s,t:String;begin s:=Memo1.Text; fori:=1toLength(s)do begin ifnot(s[i]in[#10,#13])then begin ifByteType(s,i)=mbSingleBytethen begin SpVoice1.Voice:=SpVoice1.GetVoices('','').Item(0);//设置报读的语种,0为英文,3为中文 t:=s[i]; end elseifByteType(s,i)=mbLeadBytethen begin SpVoice1.Voice:=SpVoice1.GetVoices('','').Item(3); t:=s[i]; t:=s[i] s[i 1]; end else continue; SpVoice1.Speak(t,SVSFlagsAsync);//读出Text end; end;end; procedureTForm1.Button1Click(Sender:TObject);begin SpVoice1.Voice:=SpVoice1.GetVoices('','').Item(3); SpVoice1.Speak(Memo1.Text,SVSFlagsAsync);end; procedureTForm1.Button3Click(Sender:TObject);begin SpVoice1.Voice:=SpVoice1.GetVoices('','').Item(0); SpVoice1.Speak(Memo1.Text,SVSFlagsAsync);end; end. Button1是直接读中文语音的,Button2可以读中英文混合(效果相当差),Button3直接读英文用的
View Code

 

 我知道的就这么多了,希望对你有点用... 这边的代码直接复制编译会报错,需要按CTRL H打开替换框,复制" "双引号内的字符替换成两个空格(全部替换),再编译就行了. procedureTForm1.Button3Click(Sender:TObject);begin SpVoice1.Voice:=SpVoice1.GetVoices('','').Item(0); SpVoice1.Speak(Memo1.Text,SVSFlagsAsync);end; 改成: procedureTForm1.Button3Click(Sender:TObject);begin PlayVoxEx;end; dclusr.dpk是缺省的用户安装控件包,多出来的应该是你之前安装到dclusr.dpk包里的一些别的控件...在导入ActiveX那个界面里点Install后,你可以选Intonewpackage,然后浏览一个目录填个包名再返回安装就不会有这些东西了.

追问:

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM