C++ 文本到语音SAPI语音

C++ 文本到语音SAPI语音,c++,text-to-speech,sapi,C++,Text To Speech,Sapi,有人能帮我吗? 我用SAPI来表达文字,但我不能设置女声,这是代码,它用男声表达,但我想改变它,我想要女声 #include "stdafx.h" using namespace std; void speack(HRESULT, ISpVoice*, string); int main(int argc, char* argv[]) { ISpVoice * pVoice = NULL; if (FAILED(::CoInitialize(NULL))) return FALS

有人能帮我吗? 我用SAPI来表达文字,但我不能设置女声,这是代码,它用男声表达,但我想改变它,我想要女声

#include "stdafx.h"

using namespace std;

void speack(HRESULT, ISpVoice*, string);

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"Hi my friend", 0, NULL);
    string text;
    while (true){
        getline(cin, text);
        speack(hr, pVoice, text);
        if (text == "Goodbye" || text == "goodbye")
            break;
    }

    speack(hr, pVoice, "Have a good day !!");

    pVoice->Release();
    pVoice = NULL;
}
::CoUninitialize();
return TRUE;

}

void speack(HRESULT hr, ISpVoice * pVoice, string text){
hr = pVoice->Speak(CA2CT(text.c_str()), 0, NULL);
}
请帮忙 谢谢你打电话来

CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
SAPI将创建默认的语音对象。由于您需要特定的语音,因此需要使用SAPI助手函数之一-

这将在VOICES类别中创建具有女性性别的最佳对象(因为它被指定为必需属性)

//speak.cpp
#包括“stdafx.h”
#包括“speak.h”
使用名称空间std;
布尔语(wchar_t*文本,wchar_t*pszReqAttribs)
{
ISpVoice*pVoice=NULL;
HRESULT stInitializing=CoCreateInstance(CLSID_SpVoice、NULL、CLSCTX_ALL、IID_ISpVoice、(void**)和pVoice);
如果(成功(初始化))
{
ISpObjectToken*cpToken(空);
HRESULT stTokenFinding=SpFindBestToken(SPCAT_语音、pszReqAttribs、L“”和cpToken);
if(成功(stTokenFinding))
{
HRESULT stVoiceSetting=pVoice->SetVoice(cpToken);
if(成功(stVoiceSetting))
{
HRESULT stspeaked=pVoice->Speak(文本,0,空);
if(成功(stspeaked))
{
cpToken->Release();
cpToken=NULL;
pVoice->Release();
pVoice=NULL;
返回true;
}
其他的
{
cpToken->Release();
cpToken=NULL;
pVoice->Release();
pVoice=NULL;

wcout您所要做的就是添加XML标记。请参阅官方

if(成功(hr))
{
不能说话((L“”+input).c_str(),0,NULL);
pVoice->Release();
pVoice=NULL;

ok,我试过了,但是SpCreateBestObject没有定义?这个函数需要包含吗?HRESULT hr=SpCreateBestObject(SPCAT_VOICES,L“Gender=Female”,NULL,&pVoice);你需要包含sphelper.h才能得到SpCreateBestObject实现。ok。现在定义了函数,但是出现了一个错误c:\program files(x86)\windows kits\8.1\include\um\sphelper.h(1319):错误C4996:“GetVersionExW”:在之前被声明为不推荐使用它35;包括sphelper.h(不过可能最好在以后恢复警告)。我该怎么做?这是我在堆栈中的第一个问题,我找不到如何标记:)请告诉我
hr = SpCreateBestObject(SPCAT_VOICES, L”Gender=Female”, NULL, &pVoice); 
// speak.cpp

#include "stdafx.h"
#include "speak.h"

using namespace std;

bool speak(wchar_t * text, wchar_t * pszReqAttribs)
{
    ISpVoice * pVoice = NULL;
    HRESULT stInitializing = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
    if (SUCCEEDED(stInitializing))
    {
        ISpObjectToken* cpToken(NULL);
        HRESULT stTokenFinding = SpFindBestToken(SPCAT_VOICES, pszReqAttribs, L"", &cpToken);
        if (SUCCEEDED(stTokenFinding))
        {
            HRESULT stVoiceSetting = pVoice->SetVoice(cpToken);
            if (SUCCEEDED(stVoiceSetting))
            {
                HRESULT stSpoken = pVoice->Speak(text, 0, NULL);
                if (SUCCEEDED(stSpoken))
                {
                    cpToken->Release();
                    cpToken = NULL;

                    pVoice->Release();
                    pVoice = NULL;

                    return true;
                }
                else
                {
                    cpToken->Release();
                    cpToken = NULL;

                    pVoice->Release();
                    pVoice = NULL;

                    wcout << "Error, I couldn't play this text " << text << endl;
                    return false;
                }
            }
            else
            {
                cpToken->Release();
                cpToken = NULL;

                pVoice->Release();
                pVoice = NULL;

                wcout << "Error, I can't set this voice " << pszReqAttribs << endl;
                return false;
            }
        }
        else
        {
            pVoice->Release();
            pVoice = NULL;

            wcout << "Error, I can't find this voice " << pszReqAttribs << endl;
            return false;
        }
    }
    else {
        wcout << "Error, I can't create Voice instance" << endl;
        return false;
    }
}


// language.cpp

//#pragma once 
#include "stdafx.h"
#include "language.h"

wchar_t * getLanguage(wchar_t * languageShortcut) throw (wchar_t*)
{   
    if (wcscmp(languageShortcut, L"EN") == 0) {
        return L"Vendor=IVONA Software Sp. z o. o.;Language=809";
    }
    else if(wcscmp(languageShortcut, L"DE") == 0){
        return L"Vendor=IVONA Software Sp. z o. o.;Language=407";
    }
    else if (wcscmp(languageShortcut, L"PL") == 0) {
        return L"Vendor=IVONA Software Sp. z o. o.;Language=415";
    }
    else {
        throw L"I don't uderstand your language";
    }
}
if (SUCCEEDED(hr))
{
    cout << "Enter text:\n";
    getline(wcin, input);
}
hr = pVoice->Speak((L"<voice required='Gender = Female;'>" + input).c_str(), 0, NULL);
pVoice->Release();
pVoice = NULL;