在sapi5 speak函数中存储保存用户输入数据的变量
store a variable that holds user input data in sapi5 speak function
我正在开发一个使用Microsoft SAPI5语音引擎的应用程序。然而,我已经碰壁了。我一直在尝试使用存储用户输入的变量的数据,以便TTS引擎可以重复它。但是sapi5语音功能不允许字符串,wstring或其他类型,除了LPCWSTR,从我的研究是一个指向宽字符串的指针,所以它不应该接受wstring吗?
下面是一些来自msdn的示例代码:
#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;
}
比如说我有这样一段代码:
...
wstring text;
if( SUCCEEDED( hr ) )
{
wcin >> text;
hr = pVoice->Speak(text, SPF_IS_XML, NULL);
pVoice->Release();
pVoice = NULL;
}
...
但这不起作用。如何存储允许LPCWSTR类型的变量?我对c++有点陌生,这是我第一次遇到这种问题,所以它对我来说很新。
我看到关于这个主题的OP有完全相同的问题:https://stackoverflow.com/questions/12292790/how-do-i-use-variables-in-sapi-tts
经过大约2个小时的扎实研究,我在msdn上找到了一篇关于将字符串转换为LPCWSTR格式的文章。代码如下:
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
然后我将这段代码包含到我的项目中,然后为TTS引擎初始化函数创建了一个名为LPCWSTR Repeat的函数参数(以便转换后的字符串可以在TTS函数中使用,并且引擎将显示转换后的字符串的内容)。
static int TTS_Speak_Dialogue(LPCWSTR Repeat)
{
// Set Sapi5 voice properties
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL)))
return FALSE;
// Create new instance of Sapi5 once initialized in COM
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
hr = pVoice->Speak(Repeat, SPF_IS_XML, NULL);
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}
然后我创建了另一个函数来管理转换和管理用户输入,以便TTS引擎可以重复它。
static void convert_string()
{
// Declare a string type variable
string UserInput;
// Now get input from user
cout << "Get the TTS to repeat Input : ";
cin >> UserInput;
// Convert string to LPCWSTR!
std::wstring stemp = s2ws(UserInput);
// UserInput string now converted to result
LPCWSTR result = stemp.c_str();
// Call the TTS engine function and use the converted string
TTS_Speak_Dialogue(result);
}
我希望我的回答能帮助那些和我有同样问题的人。
我本可以更详细地解释我的答案,但我需要一些睡眠,所以请接受我真诚的道歉:-)。
相关文章:
- C++如何通过用户输入删除列表元素
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 用户定义函数中的指针和输入
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 在C++中,我如何接受不同于同一行的用户输入
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 当用户键入分隔符时,停止getline()输入
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- 显示基于用户输入的整数的字符
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 如果用户没有输入任何内容或输入错误,如何重新输入用户的输入?
- 为什么在代码块中执行时会跳过输入用户输入的行?
- 使用 for 循环在数组中输入用户