C++ builder TTS5.1实现语音播放 生成wav文件

1.将sapi.dll导入到C++Builder:
Component->Import Component->Import a Type Library->Add添加Sapi.dll语音识别引擎库文件。
2.Install完后会自动生成.cpp | .h文件。

3.将.cpp|.h文件添加如工程,引用,可以进行编程了。


直接播放声音:

void TTSSpeaker(UnicodeString AText) {

	SpVoice * pVoice;
	if (SUCCEEDED(::CoInitialize(NULL))) {
		if (::CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpeechVoice,
			(void **)&pVoice) == S_OK)
			try {
			 pVoice->SetRate(0); // 语速 0 -> 20 
			 pVoice->SetVolume(100); // 音量 0 -> 100
			 pVoice->Speak(AText.c_str(), 0, NULL); 
			}
		__finally {
			pVoice->Release();
		}
		::CoUninitialize();
	}


}

生成wav文件

void TTSSaveAsWav(UnicodeString AText,UnicodeString filename) {

	SpVoice * pVoice;
	if (SUCCEEDED(::CoInitialize(NULL))) {
		if (::CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpeechVoice,
			(void **)&pVoice) == S_OK)
			try {

			SpeechVoiceSpeakFlags spFlags =
				SpeechVoiceSpeakFlags::SVSFlagsAsync;
			SpeechStreamFileMode spFileMode =
				SpeechStreamFileMode::SSFMCreateForWrite;
			SpFileStream *spFileStream;
			if (::CoCreateInstance(CLSID_SpFileStream, NULL, CLSCTX_ALL,
				IID_ISpeechFileStream, (void **)&spFileStream) == S_OK) {
				spFileStream->Open(filename.c_str(), spFileMode, false);
				pVoice->_set_AudioOutputStream(spFileStream);
				pVoice->Speak(AText.c_str(), spFlags);
				pVoice->WaitUntilDone(1000);
				spFileStream->Close();
			}
			}
		__finally {
			pVoice->Release();
		}
		::CoUninitialize();
	}


}


以下是TTS5.1的下载链接:

点击打开链接


你可能感兴趣的:(C++ builder TTS5.1实现语音播放 生成wav文件)