文本转语音

TTS 文本转语音包SpeechSDK51 SpeechSDK51LangPack
地址https://www.microsoft.com/en-us/download/details.aspx?id=10121#

tts.cpp

代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SDK下载地址 https://www.microsoft.com/en-us/download/details.aspx?id=10121
// TTS.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "TTS.h"

#include <sapi.h>

#pragma comment(lib, "ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll

#pragma comment(lib, "sapi.lib") //sapi.lib在SDK的lib目录,必需正确配置

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

HMODULE hModule = ::GetModuleHandle(NULL);

if (hModule != NULL)
{
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
ISpVoice * pVoice = NULL;
//COM初始化:
if (FAILED(::CoInitialize(NULL)))
return FALSE;
//获取ISpVoice接口:
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
hr = pVoice->SetVolume(50);
if (FAILED(hr))
{
return FALSE;
}
hr = pVoice->Speak(L"你好?你好。你好!你好;你好哪", 0, NULL);
if (FAILED(hr))
{
return FALSE;
}
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}
}
else
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: GetModuleHandle 失败\n"));
nRetCode = 1;
}

return nRetCode;
}

TextToSpeech.h

代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include <sapi.h>

class CTextToSpeech
{
public:
CTextToSpeech();
virtual ~CTextToSpeech();
BOOL Init();
void Release();
BOOL SetVolume(USHORT vol);

BOOL Speak(
/* [string][in][annotation] */
LPCWSTR pwcs,
/* [in] */ DWORD dwFlags,
/* [out][annotation] */
ULONG *pulStreamNumber);

private:
ISpVoice* _pVoice;
BOOL _init;
};

TextToSpeech.cpp

代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "stdafx.h"

#include "TextToSpeech.h"

#include <sapi.h>
#pragma comment(lib, "ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll
#pragma comment(lib, "sapi.lib") //sapi.lib在SDK的lib目录,必需正确配置

CTextToSpeech::CTextToSpeech()
: _pVoice(NULL)
, _init(FALSE)
{

}


CTextToSpeech::~CTextToSpeech()
{

}
BOOL CTextToSpeech::Init()
{
//COM初始化:
if (FAILED(::CoInitialize(NULL)))
return FALSE;
//获取ISpVoice接口:
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&_pVoice);
if (FAILED(hr))
return FALSE;

hr = _pVoice->SetVolume(50);
if (FAILED(hr))
{
_pVoice->Release();
_pVoice = NULL;
return FALSE;
}
_init = TRUE;
return TRUE;
}

void CTextToSpeech::Release()
{
if (!_init)
{
return ;
}
_pVoice->Release();
_pVoice = NULL;
::CoUninitialize();
_init = FALSE;
return ;
}

BOOL CTextToSpeech::SetVolume(USHORT vol)
{
if (!_init)
{
return FALSE;
}
if (FAILED(_pVoice->SetVolume(vol)))
{
return FALSE;
}
return TRUE;
}

BOOL CTextToSpeech::Speak(
/* [string][in][annotation] */
LPCWSTR pwcs,
/* [in] */ DWORD dwFlags,
/* [out][annotation] */
ULONG *pulStreamNumber)
{
// ISpVoice * _pVoice = NULL;
// //COM初始化:
// if (FAILED(::CoInitialize(NULL)))
// return FALSE;
// //获取ISpVoice接口:
// HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&_pVoice);
// if (SUCCEEDED(hr))
// {
// hr = _pVoice->SetVolume(50);
// if (FAILED(hr))
// {
// return FALSE;
// }
// hr = _pVoice->Speak(L"你好?你好。你好!你好;你好哪", 0, NULL);
// if (FAILED(hr))
// {
// return FALSE;
// }
// _pVoice->Release();
// _pVoice = NULL;
// }
// ::CoUninitialize();
// return TRUE;
if (!_init)
{
return FALSE;
}
if (FAILED(_pVoice->Speak(pwcs, dwFlags, pulStreamNumber)))
return FALSE;
return TRUE;
}

最后更新时间 2016-07-16 13:18:23