gpt4 book ai didi

c++ - 文字转语音 SAPI 语音

转载 作者:行者123 更新时间:2023-11-30 03:57:30 33 4
gpt4 key购买 nike

有人能帮帮我吗?我使用 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);
}

请帮忙谢谢

最佳答案

// 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";
}
}

关于c++ - 文字转语音 SAPI 语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27922113/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com