在sapi5 speak函数中存储保存用户输入数据的变量

store a variable that holds user input data in sapi5 speak function

本文关键字:输入 用户 数据 变量 保存 存储 sapi5 speak 函数      更新时间:2023-10-16

我正在开发一个使用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);
}

我希望我的回答能帮助那些和我有同样问题的人。

我本可以更详细地解释我的答案,但我需要一些睡眠,所以请接受我真诚的道歉:-)。