不废话,直接上代码,例子是读取日语文本进行朗读。LANGUAGE ID可以改成其他语言的。
#include "pch.h"
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
//read speech text
ifstream input("./speech.txt", ios::in);
stringstream buffer;
buffer << input.rdbuf();
string contents(buffer.str());
/*
language id
411 jpanese
809 chinese
409 english
speech speed
-10 ~ 10
*/
string speech= " " + contents;
/*
#define CP_ACP 0 // default to ANSI code page
#define CP_OEMCP 1 // default to OEM code page
#define CP_MACCP 2 // default to MAC code page
#define CP_THREAD_ACP 3 // current thread's ANSI code page
#define CP_SYMBOL 42 // SYMBOL translations
#define CP_UTF7 65000 // UTF-7 translation
#define CP_UTF8 65001 // UTF-8 translation
*/
int nLen = MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, speech.c_str(), -1, NULL, 0);
wchar_t * wc = new wchar_t[nLen];
MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, speech.c_str(), -1, wc, nLen);
ISpVoice * pVoice = NULL;
//COM initial
if (FAILED(::CoInitialize(NULL)))
return FALSE;
//get ISpVoice interface
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if (SUCCEEDED(hr))
{
hr = pVoice->Speak(wc, SPF_DEFAULT | SPF_IS_XML, NULL);
}
pVoice->Release();
pVoice = NULL;
return 0;
}
使用MSSPEECH需要安装其相关sdk和环境:
INSTALLATION
step 1:Install MicrosoftSpeechPlatformSDK_x64.msi
step 2: Install MSSpeech_TTS_ja-JP_Haruka.msi (For jpanese language)
step 3: Install runtime witch path like X:\Program Files\Microsoft SDKs\Speech\v11.0\Redist\SpeechPlatformRuntime.msi
step 4: Press win + r to run 'ms-settings:easeofaccess-narrator' to select 'Narrator' for your windows