Using Text to Voice Interfaces






3.67/5 (15 votes)
Sep 14, 2002
1 min read

154878

5239
Using Microsoft Text to Voice Interfaces in MFC
Introduction
This article guides you to create a simple COM client using MFC, and following the same method we can create clients for any Component when a type library file (.tlb) is available.
Note: On any machines with Windows 2000 Installed check for a "Speech" folder under C:\WINNT. This folder contains all the necessary components to call speech engine.
So to start, fire up the VC++ 6.0 IDE and then select File->New, select the MFC AppWizard [exe] and Enter "SpeakText" as the Project name and save it in the required location. Select "Dialog Base" option and uncheck the "Document/View Architecture".
Design the user interface dialog as shown in the figure above using resource editor and name your dialog and its controls accordingly.
Note: In edit control properties, on the Styles tab select "Multiline" check box for typing multiple lines of text.
Add the following in "stdafx.h"
to import the Voice Engine Component
// ... #include // MFC support for Windows Common Controls #endif // _AFX_NO_AFXCMN_SUPPORT #import "C:\WINNT\Speech\vtxtauto.tlb" no_namespace //{{AFX_INSERT_LOCATION}} ... //
Compile "stdafx.cpp"
file, which results in creation of two new files (1) vtxtauto.tlh,
(2) vtxtauto.tli
in the Debug/Release folder . These files give interface IVTxtAuto
wrapper method implementations.
Browse through SPEAKFLAGS
structure present in vtxtauto.tlh
and the other methods.
Now is the time to call the engine through the IVTxtAuto
Interface.
Add these variables in "SpeakTextDlg.h"
// char sText[1000]; //a char array of required length CLSID clsid; //class id HRESULT hr; //for holding results IVTxtAuto *voicePtr; //a pointer to IVTxtAuto inteface //
Add the following piece of code in OnInitDialog()
function
// CSpeakTextDlg::OnInitDialog() { ... // Initializing COM hr = CoInitialize(NULL); //Getting the CLSID from Program name hr = CLSIDFromProgID(OLESTR("Speech.VoiceText.1"), &clsid); //Creating the instance of the Component hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, __uuidof(IVTxtAuto), (LPVOID*)&voicePtr); if (FAILED(hr)) { AfxMessageBox("Cannot Initialize Speech Engine"); return FALSE; } else { _bstr_t pszSite; //Reqister function should be called first prior calling Speak function //so that the application is registered for using voice engine voicePtr->Register(pszSite,"Radio"); voicePtr->Speak("Welcome to Text to Voice Convertion Engine",vtxtsp_NORMAL); voicePtr->Speak("Type your Text and ask me to speak",vtxtsp_NORMAL); } ... } //
Add the following onclick handler for the "Speak" button
// CSpeakTextDlg::OnSpeak() { ... GetDlgItemText(IDC_TEXT, sText, 200); if (0 == strcmp(sText,"\0") ) voicePtr->Speak("What can i speak for you",vtxtsp_NORMAL); else voicePtr->Speak(sText,vtxtsp_NORMAL); ... } //
Change the second parameter of Speak Function according to the value of
the SPEAKFLAGS
structure present in vtxtauto.tlh
. Add the following code for respective buttons
// void CSpeakTextDlg::OnPause() { voicePtr->AudioPause(); } void CSpeakTextDlg::OnResume() { voicePtr->AudioResume(); } CSpeakTextDlg::OnStop() { voicePtr->StopSpeaking(); } //
That's all. Build and Run the application.