德尔福SAPI文字转语音 [英] Delphi SAPI Text-To-Speech
问题描述
首先:这不是 Delphi和SAPI 的重复。我在Delphi中的SAPI主题有一个具体的问题。
First of all: this is not a duplicate of Delphi and SAPI. I have a specific problem with the "SAPI in Delphi" subject.
我在Delphi 2009中使用了优秀的Import Type-Library指南,以获得组件中的TSpVoice组件调色板这很好。使用
I have used the excellent Import Type-Library guide in Delphi 2009 to get a TSpVoice component in the component palette. This works great. With
var
SpVoice: TSpVoice;
我可以写
SpVoice.Speak('This is an example.', 1);
获取异步音频输出。
第一个问题
First question
根据文档,我可以写
SpVoice.Speak('This is an example.', 0);
获取同步音频输出,而是得到一个EZeroDivide异常。为什么?
to get synchronous audio output, but instead I get an EZeroDivide exception. Why's that?
第二个问题
Second question
但是更重要的是,我想能够动态创建SpVoice对象(我认为这称为晚期绑定SpVoice对象),部分原因是我的应用程序的所有会话中只有很小一部分将使用它,部分原因是我不想假定在最终用户的系统上存在SAPI服务器。
But more importantly, I would like to be able to create the SpVoice object dynamically (I think this is called to "late-bind" the SpVoice object), partly because only a very small fraction of all sessions of my app will use it, and partly because I do not want to assume the existance of the SAPI server on the end-user's system.
为此,我尝试了
procedure TForm1.FormClick(Sender: TObject);
var
SpVoice: Variant;
begin
SpVoice := CreateOleObject('SAPI.SpVoice');
SpVoice.Speak('this is a test', 0);
end;
这显然什么都不做! (用1替换0给出了EZeroDivide异常。)
which apparently does nothing at all! (Replacing the 0 with 1 gives me the EZeroDivide exception.)
我对COM / OLE自动化颇为新奇。对于我在这篇文章中显示的任何无知或愚蠢,我很抱歉...
I am rather new to COM/OLE automation. I am sorry for any ignorance or stupidity shown by me in this post...
每个人遇到同样问题的好处,François解释说,SAPI / Windows中有一个错误(某些不兼容的地方),这使得以下代码提高了EZeroDivide异常:
For the benefit of everyone encountering the same problem as I did, the video by François explained there is a bug in SAPI/Windows (some incompatibility somewhere), which makes the following code raise the EZeroDivide exception:
procedure TForm1.FormClick(Sender: TObject);
var
SpVoice: variant;
begin
SpVoice := CreateOleObject('SAPI.SpVoice');
SpVoice.Speak('This is a text.');
end;
视频提供的解决方案是更改FPU控制字:
The solution, as presented by the video, is to alter the FPU control word:
procedure TForm1.FormClick(Sender: TObject);
var
SpVoice: variant;
SavedCW: Word;
begin
SpVoice := CreateOleObject('SAPI.SpVoice');
SavedCW := Get8087CW;
Set8087CW(SavedCW or $4);
SpVoice.Speak('This is a text.');
Set8087CW(SavedCW);
end;
此外,如果要异步播放声音,则必须确保玩家不会超出范围!
And, in addition, if you want to play a sound asynchronously, then you have to make sure that the player doesn't go out of scope!
推荐答案
您可能会发现有趣的是看到这个CodeRage 4会话的会议.embarcadero.com/coderage/sessions =nofollow noreferrer>语音启用Delphi应用程序(zip)
你会得到你想要的how-to...
(我猜你在Vista或+上,零分差在XP上没有发生)
You may find interesting to see this CodeRage 4 session on "Speech Enabling Delphi Applications (zip)" You'll get the "how-to" you're looking for... (and I guess you are on Vista or + as the the zero divide did not happend on XP)
这篇关于德尔福SAPI文字转语音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!