⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 中英文混合朗读软件发布 TTS Reader1.5
💻 PAS
📖 第 1 页 / 共 3 页
字号:
 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 + -