其实这并不是一件困难的事,因为金山词霸实际上也只是直接使用了MS的TTS(Text-to-Speech)技术而已,通过对MS Speech API 的编程,我们完全可以实现比其更强大的功能。下面,让我们在Delphi环境下实际尝试一下。 字串8
首先从金山词霸的光盘上\ ciba目录下运行并安装mstts. Exe(MS TTs engine)和spchapi. Exe (MS Speech API).
字串1
再在Delphi 里新建一个application。 字串3
然后从Delphi的菜单Project/Import Type Library. . .中选击Add. . .,浏览到Windows目录下的Speech子目录里,打开vtxtauto. Tlb,可以看到下面Class names中会出现我们需要的接口的包装类的名字TVTxtAuto,注意对话框底部的Generate Component Wrapper需要被选中,点击Create Unit 就可以打开一个名为VtxtAuTo_TLB的Unit。
至此,我们就可以开始实际编程了。 字串9
首先在Form的OnCreate中对需要使用接口初始化并进行注册:
字串8
...
字串6
Procedure TfrmMain. FormCreate (Sender. Tobject); begin
FISpeech: = CoVTxtAuto_Create; 字串1
FISpeech. Register (“Demo Site’, ‘Demo App’); end; 字串2
...
由于Delphi对接口进行了良好的包装,因此,只需直接建立包装类,其他后台的操作如接口的查询、释放等交给Delphi在后台自动完成。 字串4
注意这里的Register操作是必不可少的,因为可能有多个程序不同设置使用TIS引擎。 字串8
然后在MainForm上放一个Tmemo和若干个Tbutton,并在其OnClick中加入对应的代码。 字串7
Form定义如下: 字串1
Object frmMain: TfrmMain 字串1
Left = 296
字串5
Top = 187 字串1
BorderStyle = bsDialog 字串2
Caption = ‘英文全文朗读演示’ 字串3
ClientHeight = 185
字串5
ClientWidth = 456 字串9
Color = clBtnFace 字串7
Font. Charset = GB2312_CHARSET
字串3
Font. Color = clWindowText
字串8
Font. Height = -12 字串3
Font. Name = ‘宋体’
字串4
Font. Style = [ ]
字串9
OldCreateOrder = False 字串2
Position = poDesktopCenter 字串1
OnCreate = FormCreate 字串9
PixelsPerInch = 96 字串7
TexHeight = 12
字串3
Object memText: Tmemo 字串2
Left =8
字串8
Top = 8 字串4
Width = 361 字串7
Height = 169 字串9
ScrollBars = ssVertical 字串4
TabOrder = 0
End
Object btnRead: Tbutton
Left = 376
字串4
Top = 8
Width = 75 字串4
Height = 25 字串8
Caption = ‘朗读(&R)’ 字串9
TabOrder = 1
字串2
OnClick = btnReadClick
字串4
End
Object btnPause: Tbutton
字串2
Left = 376
Top = 40
Width = 75
Height = 25
字串8
Caption = ‘暂停(&P)’
Enabled = False 字串8
TabOrder = 2
OnClick = btnPauseClick
End
字串5
Object btnStop: Tbutton
Left = 376 字串7
Top = 72
字串7
Width = 75
Height = 25 字串8
Caption = ‘停止(&S)’ 字串2
Enabled = False 字串4
TabOrder = 3 字串8
OnClick = btnStopClick
End
字串9
Object btnForward: Tbrtton 字串2
Left = 376 字串7
Top = 120
字串2
Width = 75 字串5
Height = 25
Caption = ‘下一句(&N)’ 字串7
Enabled = False
字串1
TabOrder =4 字串5
OnClick = btnForwardClick
End 字串9
Object btnRewind: Tbutton 字串8
Left = 376
字串9
Top = 152
Width = 75 字串4
Height = 25 字串2
Caption = ‘上一句(&W)’ 字串3
Enabled = false 字串6
TabOrder = 5
字串4
OnClick = btnRewindClick
字串7
End
字串6
End
关键代码段如下:
1.朗读与停止
... 字串6
procedure TfrmMain. BtnReadClick (Sender: Tobject); begin
FISpeech. Speak (memText. Lines. Text, vtxtst_READING0;
字串9
BtnPause. Enabled : = True; 字串5
BtuStop. Enabled : = True; 字串3
End;
Procedure TfrmMain. BtnStopClicl (Sender: Tobject); begin 字串2
FISpeech. StopSpeaking;
BtnPause. Enabled : = False;
BtnStop. Enabled : = False;
BtnPause. Caption : = ‘暂停(&P)’;end;
字串4
...
字串1
speak成员函数的第一个参数向TTS引擎传递需要朗读的文字,第二个参数由指定朗读时使用的语气和优先级的两个符号合并而成。 字串2
|
符号 字串8 |
值 字串3 |
意义 字串3 | |
|
语气 字串8 字串6 |
vtxtst_STATEMENT 字串9 |
$00000001 字串6 |
平常说话的语气(缺省) |
|
vtxtst_QUESTION 字串9 |
$00000002 字串1 |
提问的语气 | |
|
vtxtst_COMMAND 字串5 |
$00000004 字串1 |
命令的语气 | |
|
vtxtst_WARNING 字串7 |
$00000008 字串5 |
警告的语气 | |
|
vtxtst_READING 字串2 |
$00000010 字串1 |
阅读文档时的语气 | |
|
vtxtst_NUMBERS 字串2 |
$00000020 字串1 |
适合阅读数字的语气 | |
|
vtxtst_SPREADSHEET 字串9 |
$00000040 字串2 |
适合阅读电子表格中元素的语气 | |
|
优先级 字串8 |
vtxtsp_VERYHING 字串3 |
$00000080 字串6 |
立即读出,可打断正在读的内容 |
|
vtxtsp_HIGH 字串9 |
$00000100 字串4 |
尽快读出,加入播放队列开始 | |
|
vtxtsp_NORMAL 字串2 |
$00000200 字串1 |
仅仅加入播放队列末尾(缺省) |
2.暂停为与恢复 字串4
... 字串9
procedure TfrmMain. BtnPauseClick (Sender: Tobject);
begin 字串5
with FISpeech do
if IsSpeaking then 字串2
begin
AudioPause; 字串8
BtnPause. Caption : = ‘恢复(&R)’;
字串6
End
Else 字串5
Begin 字串4
AudioResume;
字串8
BtnPause. Caption : = ‘暂停(&R)’;
字串2
End; 字串3
End; 字串3
... 字串1
这里通过IsSpeaking属性可以取得当前状态,然后通过audioPause和AudioResume暂停和恢复朗读。 字串8
另外用成员函数AudioFastForward和AudioRewind可以向前和后跳过一句朗读,通过读写speed属性还可以取得或调协朗读的语速,单位是字/分,缺省值为170。 字串1
注意speed和Enabled属性缺省设置是只写的,但实际上IVTxtAuto提供了Get_Speed和Get_Enabled函数,所以我们只要把VtxtAuto_TLB单元中的声明修改一下,如:
...
IVTxtAuto = interface (Idispatch)
...
property Speed: Integer read Get_Speed write Set_Speed;
propert Enabled: Integer read Get_Speed write Set_Speed; 字串1
... 字串7
end; 字串2
...
字串5
即可正常使用Speed和enabled属性了。 字串7
至此,一个简单的英文全文朗读器就完成了,有兴趣的朋友可以为其加入监视剪贴板,热键激活等功能。
字串8
本文程序在win NT/98 + Delphi 4/5环境下调试通过。
字串8