In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech.
在本教程中,我将向您展示如何在Delphi中使用Windows Speech API。 我将仅介绍一些基本功能,例如文本到语音以及控制语音速度。
SAPI Installation
SAPI安装
First you need to install the SAPI type library, the type library is attached to this tutorial, into Delphi.
首先,您需要将SAPI类型库(此教程附带的类型库)安装到Delphi中。
In the Delphi IDE install a new component via the menu:
在Delphi IDE中,通过菜单安装新组件:
Component > Install Component... (See Step 1 Image)
组件>安装组件...(请参阅第1步映像)
Select the 'Into new package' Tab, then click on the 'Browse' button, Select the '.PAS' file included in the type library attachment. (See Step 2 Image)
选择“放入新软件包”选项卡,然后单击“浏览”按钮,选择类型库附件中包含的“ .PAS”文件。 (请参阅第2步图片)
Create a new package file name such as 'Speech.dpk'. Enter a package description such as 'Windows Speech API'. (See Step 2 Image)
创建一个新的程序包文件名,如“ Speech.dpk”。 输入程序包描述,例如“ Windows Speech API”。 (请参阅第2步图片)
Click on 'OK', when prompted to build then install the package, click on the 'Yes' button. (See Step 3 Image)
单击“确定”,当系统提示您进行构建,然后安装软件包时,单击“是”按钮。 (请参阅第3步图片)
You should get a prompt saying that new components have been installed. (See Step 4 Image)
您应该得到一个提示,说明已经安装了新组件。 (请参阅第4步图片)
Simple Text To Speech
简单的文字转语音
Create a new application.
创建一个新的应用程序。
Find the 'SpVoice' component, and drag it onto your form. (See Step 5 Image)
找到“ SpVoice”组件,然后将其拖动到表单上。 (请参阅第5步图片)
Add an edit box and a button to the form and, on the button onclick event handler, enter the following code:
在表单中添加一个编辑框和一个按钮,然后在按钮onclick事件处理程序上输入以下代码:
-
procedure TForm1.Button1Click(Sender: TObject);
-
begin
-
if not (Edit1.Text = '') then
-
SpVoice1.Speak(Edit1.Text, SVSFDefault);
-
end;
When the user enters something into the textbox the SAPI says the text outloud. (See Step 6 Image).
当用户在文本框中输入内容时,SAPI会大声说出文本。 (请参阅第6步图片)。
Change Voice Speed
更改语音速度
Add a TrackBar component to your form (TrackBar is found in the win32 pallette). On the trackbar onchange event handler add the code below:
将TrackBar组件添加到窗体中(在Win32调色板中找到TrackBar)。 在轨迹栏onchange事件处理程序上,添加以下代码:
-
procedure TForm1.TrackBar1Change(Sender: TObject);
-
begin
-
SpVoice1.Rate := TrackBar1.Position;
-
end;
This trackbar will change the speed of the text to speech engine depending on where the trackbar is. It will make the voice either slower or faster; this may help the user understand the speech.
该跟踪栏将根据跟踪栏的位置来更改文本到语音引擎的速度。 它将使声音变慢或变快。 这可以帮助用户理解语音。
Run the project and you have a simple text to speech implementation in Delphi. This could be used to enable your application to speak to the user.
运行该项目,您将在Delphi中实现一个简单的文本到语音的实现。 这可用于使您的应用程序与用户对话。
I have attached the project files so you can use them for reference.
我已经附加了项目文件,因此您可以将其用作参考。
The tyle libraries cannot be attached, however they are here:
tyle库无法附加,但是它们在这里:
http://rapidshare.com/files/416314381/Speech_Type_Library.zip.html http://rapidshare.com/files/416314381/Speech_Type_Library.zip.html speech-tutorial.zip speech-tutorial.zip
翻译自: Using Windows Text to Speech with Delphi | Experts Exchange
delphi语音