文本转语音
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
|
#include "stdafx.h" #include "TTS.h"
#include <sapi.h>
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "sapi.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) { if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0)) { _tprintf(_T("错误: MFC 初始化失败\n")); nRetCode = 1; } else { 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->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 { _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( LPCWSTR pwcs, DWORD dwFlags, 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") #pragma comment(lib, "sapi.lib")
CTextToSpeech::CTextToSpeech() : _pVoice(NULL) , _init(FALSE) {
}
CTextToSpeech::~CTextToSpeech() {
} BOOL CTextToSpeech::Init() { if (FAILED(::CoInitialize(NULL))) return FALSE; 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( LPCWSTR pwcs, DWORD dwFlags, ULONG *pulStreamNumber) {
if (!_init) { return FALSE; } if (FAILED(_pVoice->Speak(pwcs, dwFlags, pulStreamNumber))) return FALSE; return TRUE; }
|
最后更新时间 2016-07-16 13:18:23