C++ Builder 2010利用microsoft speech api 5.1实现语音朗读

    技术2022-06-23  89

    最近项目中要用到TTS语音转化,下载了speech sdk 5.1,发现都是VC6.0下的例子,自己研究了下,迁移到C++ Builder下面

     

    1.安装speech sdk5.1,对于XP以后的系统,或者安装了office的操作系统,无需安装

    2.在C++ Builder IDE中导入sapi.dll到工程中,生成SpeechLib_TLB.cpp以及SpeechLib_TLB.h

     

     

     

     

    3.在Form1设计器里面,设计以下界面

     

     

    4.Form1 Unit1.cpp代码

    // --------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include "SpeechLib_TLB.h" // --------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1* Form1; ISpVoice* voice; ISpObjectTokenCategory* cpTokenCategory = NULL; IEnumSpObjectTokens* cpEnumTokens = NULL; ISpObjectToken* pSpObjectToken = NULL; // --------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Memo1->Text = ""; // 初始化SpVoice voice = NULL; CoInitialize(NULL); OleCheck(CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (LPVOID*) & voice)); voice->SetVolume(TrackBar1->Position); voice->SetRate(TrackBar2->Position); // 获取系统语音库列表 OleCheck(CoCreateInstance(CLSID_SpObjectTokenCategory, NULL, CLSCTX_ALL, IID_ISpObjectTokenCategory, (LPVOID*) & cpTokenCategory)); cpTokenCategory->SetId((unsigned short*) L"HKEY_LOCAL_MACHINE//SOFTWARE//Microsoft//Speech//Voices", false); // cpTokenCategory->EnumTokens(L"language=804", NULL, &cpEnumTokens); cpTokenCategory->EnumTokens(NULL, NULL, &cpEnumTokens); unsigned long count = 0; cpEnumTokens->GetCount(&count); for (; count > 0; count--) { if (pSpObjectToken != NULL) pSpObjectToken->Release(); if (SUCCEEDED(cpEnumTokens->Next(1, &pSpObjectToken, NULL))) { ISpDataKey * key = NULL; pSpObjectToken->OpenKey((unsigned short*)L"Attributes", &key); WCHAR* strTemp = NULL; key->GetStringValue((unsigned short*)L"Name", (unsigned short**) & strTemp); ComboBoxEx1->AddItem(strTemp, NULL); if (key != NULL) { key->Release(); } } } // 默认采用第一个语音库 ComboBoxEx1->ItemIndex = 0; if (pSpObjectToken != NULL) pSpObjectToken->Release(); cpEnumTokens->Item(0, &pSpObjectToken); voice->SetVoice(pSpObjectToken); } /** * 释放对象 */ void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { if (cpTokenCategory != NULL) cpTokenCategory->Release(); if (cpEnumTokens != NULL) cpEnumTokens->Release(); if (pSpObjectToken != NULL) pSpObjectToken->Release(); if (voice != NULL) voice->Release(); CoUninitialize(); } /** * 设置音量 */ void __fastcall TForm1::TrackBar1Change(TObject *Sender) { voice->SetVolume(TrackBar1->Position); } /** * 设置语速 */ void __fastcall TForm1::TrackBar2Change(TObject *Sender) { voice->SetRate(TrackBar2->Position); } /** * 开始朗读 */ void __fastcall TForm1::Button1Click(TObject *Sender) { // 异步方式朗读 voice->Speak(Memo1->Text.w_str(), 1, NULL); } /** * 暂停朗读 */ void __fastcall TForm1::Button2Click(TObject *Sender) { voice->Pause(); } /** * 继续朗读 */ void __fastcall TForm1::ResumeClick(TObject *Sender) { voice->Resume(); } /** * 停止朗读 */ void __fastcall TForm1::Button3Click(TObject *Sender) { voice->Speak(NULL, 2, NULL); } /** * ComboBox onchange事件,切换语音库 */ void __fastcall TForm1::ComboBoxEx1Change(TObject *Sender) { // 先停止朗读 voice->Speak(NULL, 2, NULL); if (pSpObjectToken != NULL) pSpObjectToken->Release(); cpEnumTokens->Item(ComboBoxEx1->ItemIndex, &pSpObjectToken); // 切换语音库 voice->SetVoice(pSpObjectToken); } // ---------------------------------------------------------------------------

     

    5. 程序运行界面

     

     

     


    最新回复(0)