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