?? unit1.pas
字號:
s:string;//存放待分析的文本內容
d1:string;
z:string;//存放臨時字符
l:integer;//s字符串的長度
i:integer;//
o:integer;
times:integer;//文本轉為音頻文件時估計的用時
begin
p:=100;
if richedit1.Text<>'' then
begin
////////////////////////////////////////////////////////////////////////
if czwbt.Checked then rom:='您確認要以【純中文】模式' else
if cywbt.Checked then rom:='您確認要以【純英文】模式' else
if zyhhbt.Checked then rom:='您確認要以【中英混合】模式';
if radiobutton1.Checked then rom:=rom+'【慢語速】以【'+yinpin+'】輸出音頻文件嗎?' else
if radiobutton2.Checked then rom:=rom+'【中語速】以【'+yinpin+'】輸出音頻文件嗎?' else
if radiobutton3.Checked then rom:=rom+'【快語速】以【'+yinpin+'】輸出音頻文件嗎?';
jieguo:=messageDlg(rom,mtConfirmation,[mbyes,mbcancel],0);
case jieguo of
2: exit;
6://////////////////////////確認轉換///////////////////////////////////////
/////純中文模式開始
if czwbt.Checked then
begin
s:=richedit1.Text;
l:=length(s);
l:=l div 2;//全角字符的長度
times:=(l*6) div 1000;//單位為秒
showmessage('轉換可能要花'+inttostr(times)+'秒(≈'+inttostr(times div 60)+'分)鐘,轉換時請耐心等待!');
//注:此處若不將之轉換為全角,則TTS會讀為"點"
s:=StringReplace(s,'.','。',[rfReplaceAll, rfIgnoreCase]);
savedialog1.Filter:='Wave files(*.wav)|*.wav';
savedialog1.FilterIndex:=1;
if savedialog1.Execute then
begin
spfilestream1.Format.Type_:=flatcombobox2.ItemIndex;
spfilestream1.Open(savedialog1.FileName+'.wav',SSFMCreateForWrite,False);
spvoice1.AllowAudioOutputFormatChangesOnNextSet:=false;
spvoice1.AudioOutputStream:=spfilestream1.DefaultInterface;
spvoice1.Speak('<LANG LANGID="804">'+s,1);
spvoice1.WaitUntilDone(-1);
spfilestream1.Close;
showmessage('文本已成功轉換為聲音文件,并存放在'+savedialog1.FileName);
end;
end
else
/////純中文模式結束
///////////////////////////////////////////////////////////////////////////
///////純英文模式開始
if cywbt.Checked then
begin
savedialog1.Filter:='Wave files(*.wav)|*.wav';
savedialog1.FilterIndex:=1;
if savedialog1.Execute then
begin
spfilestream1.Format.Type_:=flatcombobox2.ItemIndex;
spfilestream1.Open(savedialog1.FileName+'.wav',SSFMCreateForWrite,False);
spvoice1.AllowAudioOutputFormatChangesOnNextSet:=false;
spvoice1.AudioOutputStream:=spfilestream1.DefaultInterface;
spvoice1.Speak('<LANG LANGID="409">'+richedit1.Text,1);
spvoice1.WaitUntilDone(-1);
spfilestream1.Close;
showmessage('文本已成功轉換為聲音文件,并存放在'+savedialog1.FileName);
end;
end
else
///////純英文模式結束
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////中英混合模式開始
if zyhhbt.Checked then
begin
s:=richedit1.Text;
s:=StringReplace(s,'a','a',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'b','b',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'c','c',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'d','d',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'e','e',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'f','f',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'g','g',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'h','h',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'i','i',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'j','j',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'k','k',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'l','l',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'m','m',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'n','n',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'o','o',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'p','p',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'q','q',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'r','r',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'s','s',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'t','t',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'u','u',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'v','v',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'w','w',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'x','x',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'y','y',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'z','z',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,' ',' ',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,',',',',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'.','。',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,';',';',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,':',':',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'''',''',[rfReplaceAll, rfIgnoreCase]);//注意單引號的引用
s:=StringReplace(s,'<','〈',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'>','〉',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'/','/',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'\','\',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'?','?',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'|','‖',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'[','[',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,']',']',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'{','{',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'}','}',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'(','(',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,')',')',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'-','-',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'_','﹎',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'=','=',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'+','+',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'!','!',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'@','@',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'#','#',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'$','$',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'%','%',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'^','ˇ',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'&','&',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'*','※',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'1','1',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'2','2',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'3','3',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'4','4',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'5','5',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'6','6',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'7','7',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'8','8',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'9','9',[rfReplaceAll, rfIgnoreCase]);
s:=StringReplace(s,'0','0',[rfReplaceAll, rfIgnoreCase]);
//注:若加入數字過濾后,將無法朗讀所有數字
//showmessage(s);//以上代碼沒問題
l:=length(s);
i:=1;
d:='';
while i<l do
begin
z:=Copy(s,i,2);
if (z<>'a') and (z<>'b') and (z<>'c') and (z<>'d') and (z<>'e')
and (z<>'f') and (z<>'g') and (z<>'h') and (z<>'i') and (z<>'j')
and (z<>'k') and (z<>'l') and (z<>'m') and (z<>'n') and (z<>'o')
and (z<>'p') and (z<>'q') and (z<>'r') and (z<>'s') and (z<>'t')
and (z<>'u') and (z<>'v') and (z<>'w') and (z<>'x') and (z<>'y')
and (z<>'z') or (z='1') or (z='2') or (z='3') or (z='4') or (z='5')
or (z='6') or (z='7') or (z='8') or (z='9') or (z='0')
{and (z<>'\') and (z<>'?') and (z<>'‖') and (z<>'[') and (z=']')
and (z<>'{') and (z<>'}') and (z<>'(') and (z<>')') and (z<>'-')
and (z<>'﹎') and (z<>'=') and (z<>'+') and (z<>'!') and (z<>'@')
and (z<>'#') and (z<>'$') and (z<>'%') and (z<>'ˇ') and (z<>'&')
and (z<>'※') and (z<>''')}then
begin ////
z:=StringReplace(z,'1','1',[rfReplaceAll, rfIgnoreCase]);
z:=StringReplace(z,'2','2',[rfReplaceAll, rfIgnoreCase]);
z:=StringReplace(z,'3','3',[rfReplaceAll, rfIgnoreCase]);
z:=StringReplace(z,'4','4',[rfReplaceAll, rfIgnoreCase]);
z:=StringReplace(z,'5','5',[rfReplaceAll, rfIgnoreCase]);
z:=StringReplace(z,'6','6',[rfReplaceAll, rfIgnoreCase]);
z:=StringReplace(z,'7','7',[rfReplaceAll, rfIgnoreCase]);
z:=StringReplace(z,'8','8',[rfReplaceAll, rfIgnoreCase]);
z:=StringReplace(z,'9','9',[rfReplaceAll, rfIgnoreCase]);
z:=StringReplace(z,'0','0',[rfReplaceAll, rfIgnoreCase]);
if p=0 then //若上次也是中文
begin
d:=d+z;
p:=0;
i:=i+2;
end
else
begin
d:=d+'<LANG LANGID="804">'+z;
p:=0;
i:=i+2;
end
end ////
///////////////////////////////
else //if z<>' ' then
begin ///
if p=1 then //若上次也是英文
begin
d:=d+z;
p:=1;
i:=i+2;
end
else
begin
d:=d+'<LANG LANGID="409">'+z;
p:=1;
i:=i+2;
end
end;
end;
//////////
begin
savedialog1.Filter:='Wave files(*.wav)|*.wav';
savedialog1.FilterIndex:=1;
if savedialog1.Execute then
begin
spfilestream1.Format.Type_:=flatcombobox2.ItemIndex;
spfilestream1.Open(savedialog1.FileName+'.wav',SSFMCreateForWrite,False);
spvoice1.AllowAudioOutputFormatChangesOnNextSet:=false;
spvoice1.AudioOutputStream:=spfilestream1.DefaultInterface;
showmessage(d);
spvoice1.Speak(d,1);
spvoice1.WaitUntilDone(-1);
spfilestream1.Close;
showmessage('文本已成功轉換為聲音文件,并存放在'+savedialog1.FileName);
end;
////////
end;
end;
end;
end
else
showmessage('待轉換的文本內容不能為空!');
end;
procedure TForm1.SpeedButton12Click(Sender: TObject);
begin
combobox1.Text:='SAFT32kHz8BitMono';
end;
procedure TForm1.SpeedButton11Click(Sender: TObject);
var
it: ISpeechObjectTokens;
begin
it := SpVoice1.GetVoices('', '');
SpVoice1.Voice := it.Item(flatcombobox1.ItemIndex);
yinpin:=flatcombobox2.Text;
panel3.Visible:=false;
spvoice1.Voice := spvoice1.GetVoices('','').Item(flatcombobox1.ItemIndex);
end;
procedure TForm1.SpeedButton10Click(Sender: TObject);
begin
panel3.Visible:=true;
end;
procedure TForm1.SpeedButton13Click(Sender: TObject);
begin
panel3.Visible:=false;
end;
procedure TForm1.FormActivate(Sender: TObject);
var
it: ISpeechObjectTokens;
i: integer;
begin
it := SpVoice1.GetVoices('', '');
for i := 0 to it.Count - 1 do
flatComboBox1.Items.Add(it.Item(i).GetDescription(0));
flatComboBox1.ItemIndex := 0;
AddItemToFmtCB;
flatComboBox2.Text := 'SAFT22kHz16BitMono';
it := SpVoice1.GetAudioOutputs('', '');
for i := 0 to it.Count - 1 do
flatComboBox3.Items.Add(it.Item(i).GetDescription(0));
flatCombobox3.ItemIndex := 0;
{ 'load the format combo box
AddItemToFmtCB
' set rate and volume to the same as the Voice
RateSldr.Value = Voice.Rate
VolumeSldr.Value = Voice.Volume
'set the default format
FormatCB.Text = DefaultFmt
' Load the audio output combo box
If Voice.GetAudioOutputs.Count > 0 Then
For Each Token In Voice.GetAudioOutputs
AudioOutputCB.AddItem (Token.GetDescription)
Next
Else
AudioOutputCB.AddItem NoAudioOutput
AudioOutputCB.Enabled = False
End If
AudioOutputCB.ListIndex = 0
'load image list
LoadMouthImages
MouthImgList.MaskColor = vbMagenta
MouthImgList.BackColor = GetSysColor(COLOR_3DFACE)
Set VisemePicture.Picture = MouthImgList.Overlay("MICFULL',('MICFULL")
' init speak flags and sync flag check boxes
m_speakFlags = SVSFlagsAsync Or SVSFPurgeBeforeSpeak Or SVSFIsXML
chkSpFlagAync.Value = Checked
chkSpFlagPurgeBeforeSpeak.Value = Checked
chkSpFlagIsXML.Value = Checked
SetSpeakingState False, False
Exit Sub
}
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -