TTS C++程序只能在开发PC上运行
我按照SAPI 5.1“文本转语音教程”给出的说明,编译了教程给出的示例代码(说明和示例代码与 SAPI 5.3 TTS 教程!和SAPI 5.4 TTS 教程!)。编译后的程序在构建它的 XP PC 上运行良好,但在其他 XP 和 Win 7 PC 上则无法运行。另外 2 台 PC 没有安装语音 SDK。但是SAPI 5.1演示程序TTSApp和MS Excel 2003文本转语音功能在这两台PC上运行得很好。
为什么教程程序只能在开发PC上运行,而其他2个程序可以在所有PC上运行?
开发PC安装了XP sp3、Visual Studio 2008和SAPI 5.1。当我构建项目时,我选择了预编译头。在下面的步骤 1 中,stdafx.h 文件中没有“#endif”,因此这些行插入在“#include”之后。
来自 XP PC 的错误消息:此应用程序无法启动,因为应用程序配置不正确。重新安装应用程序可能会解决此问题。
来自 Win7 PC 的错误消息:应用程序无法启动,因为其并行配置不正确。请参阅应用程序事件日志或使用命令行 sxstrace.exe 工具了解更多详细信息
。下面是教程。
微软语音SDK SAPI 5.1
文本转语音教程 本教程介绍一个非常基本的文本转语音 (TTS) 示例。控制台应用程序是最简单的语音演示之一。它相当于 TTS 的“Hello World”。使用事件与 TTS 中提供了使用图形界面(和事件泵)的 Windows 应用程序的等效示例。 该示例从最简单的(尽管非功能性的)COM 框架构建到说出一个句子。为每个新功能提供了步骤。该示例甚至比演示使用 XML 标签修改语音更进一步。完整的示例应用程序位于页面底部。
第 1 步:设置项目 第2步:初始化COM 第 3 步:设置声音 第四步:说话! 步骤 5:修改语音
步骤 1:设置项目 虽然可以从头开始编写应用程序,但从现有项目开始更容易。在这种情况下,使用 Visual Studio 的应用程序向导创建 Win32 控制台应用程序。在向导设置期间询问时,选择“Hello, world”作为示例。生成后,打开 STDAfx.h 文件并将以下代码粘贴到“#include”之后、“#endif”语句之前。这会设置 SAPI 所需的附加依赖项。
#define _ATL_APARTMENT_THREADED
#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override something,
//but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>
代码清单1 接下来添加 SAPI.h 和 SAPI.lib 文件的路径。显示的路径适用于标准 SAPI SDK 安装。如果编译器无法找到任一文件,或者执行了非标准安装,请使用文件的新路径。更改项目设置以反映路径。使用项目->设置。菜单项,设置SAPI.h路径。单击“C/C++”选项卡,然后从“类别”下拉列表中选择“预处理器”。在“其他包含目录”中输入以下内容:C:\Program Files\Microsoft Speech SDK 5.1\Include。 设置 SAPI.lib 路径: 1. 从“相同设置”对话框中选择“链接”选项卡。 2. 从类别下拉列表中选择输入。 3. 在“附加库路径”中添加以下路径: C:\Program Files\Microsoft Speech SDK 5.1\Lib\i386。 4.还将“sapi.lib”添加到“对象/库模块”行。确保名称之间用空格分隔。
第2步:初始化COM SAPI 是基于COM 的应用程序,COM 必须在使用前和SAPI 处于活动状态期间进行初始化。在大多数情况下,这是针对主机应用程序的生命周期。以下代码(来自清单 2)初始化 COM。当然,应用程序不会执行初始化以外的任何操作,但它确实确保 COM 成功启动。
#include <stdafx.h>
#include <sapi.h>
int main(int argc, char* argv[])
{
if (FAILED(::CoInitialize(NULL)))
return FALSE;
::CoUninitialize();
return TRUE;
}
代码清单2 第 3 步:设置声音 COM 运行后,下一步就是创建语音。声音只是一个 COM 对象。此外,SAPI 使用智能默认值。在对象初始化期间,SAPI 自动分配大部分值,以便随后可以立即使用该对象。这代表了对早期版本的重要改进。默认值是从控制面板中的语音属性中检索的,包括语音(如果系统上有多种可用)和语言(英语、日语等)等信息。虽然有些默认设置是显而易见的,但其他默认设置则不然(语速、音调等)。尽管如此,所有默认值都可以通过编程方式或在控制面板的语音属性中进行更改。 将 pVoice 指针设置为 NULL 不是必需的,但对于检查错误很有用;这可以确保无效指针不会被重用,或者作为指针已被分配或释放的提醒
#include <stdafx.h>
#include <sapi.h>
int main(int argc, char* argv[])
{
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL)))
return FALSE;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}
。 代码清单 3. 粗体文本表示此示例的新代码。 第四步:说话! 实际说出该短语是一项同样简单的任务:一行调用 Speak 函数。当不再需要该语音实例时,您可以释放该对象。
#include <stdafx.h>
#include <sapi.h>
int main(int argc, char* argv[])
{
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL)))
return FALSE;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
hr = pVoice->Speak(L"Hello world", 0, NULL);
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}
代码清单 4。粗体文本表示此示例的新代码。 第五步:修改语音 可以使用多种方法来修改声音。最直接的方法是将 XML 命令直接应用于流。这些命令在 XML 架构中进行了概述。在本例中,相对评级 10 会降低语音的音调。
#include <stdafx.h>
#include <sapi.h>
int main(int argc, char* argv[])
{
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL)))
return FALSE;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
hr = pVoice->Speak(L"Hello world", 0, NULL);
// Change pitch
hr = pVoice->Speak(L"This sounds normal <pitch middle = '-10'/> but the pitch drops half way through", SPF_IS_XML, NULL );
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}
代码清单 5。粗体文本表示此示例的新代码。这是完整的代码示例。
I followed the instructions given by the SAPI 5.1 “Text-to-Speech Tutorial”, compiled the sample code given by the tutorial(The instructions and the sample code are the same as those given by SAPI 5.3 TTS Tutorial! and SAPI 5.4 TTS Tutorial!). The compiled program works fine on the XP PC where it was built, but it will not work on other XP and Win 7 PCs. The other 2 PCs have no Speech SDK installed. But the SAPI 5.1 demo program TTSApp and MS Excel 2003 Text-to-Speech function work very well on these 2 PCs.
Why the tutorial program can only run on the development PC and other 2 programs can run on all PCs?
The development PC has XP sp3, Visual Studio 2008 and SAPI 5.1 installed. When I build the project I selected pre-compiled header. In step 1 below, there is no “#endif” in the stdafx.h file so these lines are inserted after "#include ".
Error message from the XP PC: this application failed to start because the application configuration is incorrect. Reinstall the application may fix this problem.
Error message from Win7 PC: The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail
Below is the tutorial.
Microsoft Speech SDK
SAPI 5.1
Text-to-Speech Tutorial
This tutorial covers a very basic text-to-speech (TTS) example. The console application is one of the simplest demonstrations of speech. It is the "Hello World" equivalent for TTS. An equivalent sample for a Windows application using a graphical interface (and event pump) is available in Using Events with TTS.
The sample builds up from the simplest (though nonfunctional) COM framework to speaking a sentence. Steps are provided for each new function. The sample even goes one step beyond demonstrating the use XML tags to modify speech. The Complete Sample Application is at the bottom of the page.
Step 1: Setting Up The Project
Step 2: Initialize COM
Step 3: Setting Up Voices
Step 4: Speak!
Step 5: Modifying Speech
Step 1: Setting up the project
While it is possible to write an application from scratch, it is easier to start from an existing project. In this case, use Visual Studio's application wizard to create a Win32 console application. Choose "Hello, world" as the sample when asked during the wizard set up. After generating it, open the STDAfx.h file and paste the following code after "#include " but before the "#endif" statement. This sets up the additional dependencies SAPI requires.
#define _ATL_APARTMENT_THREADED
#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override something,
//but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>
Code Listing 1
Next add the paths to SAPI.h and SAPI.lib files. The paths shown are for a standard SAPI SDK install. If the compiler is unable to locate either file, or if a nonstandard install was performed, use the new path to the files. Change the project settings to reflect the paths. Using the Project->Settings. menu item, set the SAPI.h path. Click the C/C++ tab and select Preprocessor from the Category drop-down list. Enter the following in the "Additional include directories": C:\Program Files\Microsoft Speech SDK 5.1\Include.
To set the SAPI.lib path:
1. Select the Link tab from the Same Settings dialog box.
2. Choose Input from the Category drop-down list.
3. Add the following path to the "Additional library path":
C:\Program Files\Microsoft Speech SDK 5.1\Lib\i386.
4. Also add "sapi.lib" to the "Object/library modules" line. Be sure that the name is separated by a space.
Step 2: Initialize COM
SAPI is a COM-based application, and COM must be initialized both before use and during the time SAPI is active. In most cases, this is for the lifetime of the host application. The following code (from Listing 2) initializes COM. Of course, the application does not do anything beyond initialization, but it does ensure that COM is successfully started.
#include <stdafx.h>
#include <sapi.h>
int main(int argc, char* argv[])
{
if (FAILED(::CoInitialize(NULL)))
return FALSE;
::CoUninitialize();
return TRUE;
}
Code Listing 2
Step 3: Setting up voices
Once COM is running, the next step is to create the voice. A voice is simply a COM object. Additionally, SAPI uses intelligent defaults. During initialization of the object, SAPI assigns most values automatically so that the object may be used immediately afterward. This represents an important improvement from earlier versions. The defaults are retrieved from Speech properties in Control Panel and include such information as the voice (if more than one is available on your system), and the language (English, Japanese, etc.). While some defaults are obvious, others are not (speaking rate, pitch, etc.). Nevertheless, all defaults may be changed either programmatically or in Speech properties in Control Panel.
Setting the pVoice pointer to NULL is not required but is useful for checking errors; this ensures an invalid pointer is not reused, or as a reminder that the pointer has already been allocated or deallocated
#include <stdafx.h>
#include <sapi.h>
int main(int argc, char* argv[])
{
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL)))
return FALSE;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}
Code Listing 3. Bold text represents new code for this example.
Step 4: Speak!
The actual speaking of the phrase is an equally simple task: one line calling the Speak function. When the instance of the voice is no longer needed, you can release the object.
#include <stdafx.h>
#include <sapi.h>
int main(int argc, char* argv[])
{
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL)))
return FALSE;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
hr = pVoice->Speak(L"Hello world", 0, NULL);
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}
Code Listing 4. Bold text represents new code for this example.
Step 5: Modifying Speech
Voices may be modified using a variety of methods. The most direct way is to apply XML commands directly to the stream. The commands are outlined in XML Schema. In this case, a relative rating of 10 will lower the pitch of the voice.
#include <stdafx.h>
#include <sapi.h>
int main(int argc, char* argv[])
{
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL)))
return FALSE;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
hr = pVoice->Speak(L"Hello world", 0, NULL);
// Change pitch
hr = pVoice->Speak(L"This sounds normal <pitch middle = '-10'/> but the pitch drops half way through", SPF_IS_XML, NULL );
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}
Code Listing 5. Bold text represents new code for this example. This is the complete code sample.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须安装附带安装程序 (redist) 的 C++ 运行时。
VC++ 2008 redist
VC++ 2008 SP1 redist
安装哪一个取决于 VS 服务包 - 如果是 SP1,则安装SP1如果不是就不行。
编辑:第二种方法是静态链接运行时库。这会增加可执行文件的大小,但您不需要对目标计算机有额外的要求。
You have to install the C++ runtime which comes with an installer (redist).
VC++ 2008 redist
VC++ 2008 SP1 redist
Which one to install depends on the VS service pack - if SP1 then SP1 if not the not.
EDIT: A second way is to link the runtime libraries static. This increases the size of the executable but you don't need additional requirements on the target machine.