微软语音朗读引擎MSSPEECH打造自己的TTS应用

不废话,直接上代码,例子是读取日语文本进行朗读。LANGUAGE ID可以改成其他语言的。

#include "pch.h"
#include <Windows.h>
#include <string>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iostream>
#include <sapi.h>
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= "<VOICE REQUIRED='LANGUAGE=411' /><rate absspeed='-2' />" + 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值