Creating a Text-to-Voice Convention

Environment: VC6, Win2k

Prerequisites: On any Windows 2000-installed machine, check for a "Speech" folder under C:\WINNT. This folder contains all the necessary components to call the speech engine.

This article guides you through the steps necessary to create a simple COM client using MFC with the available type library file (.tlb). At the end, you will know how to create clients for any component when a type library file (.tlb) is available.

So, let's start creating a dialog-based MFC client application by firing the VC IDE.

Select File->New, select MFC AppWizard[exe], and Enter "SpeakText" as the Project name. Save it in the required location. Select the "Dialog Base" option and uncheck "Document/View Arch."

Use the resource editor to design a user interface dialog, as shown in the following figure.

Name your dialog and its controls accordingly.

Note: On the Styles tab of the edit control properties sheet, select the "Multiline" check box for typing multiple lines of text.

Add the following in "stdafx.h" to import the Voice Engine Component:

...
#include <afxcmn.h>      // MFC support for Windows Common Controls
#endif                   // _AFX_NO_AFXCMN_SUPPORT

#import "C:\WINNT\Speech\vtxtauto.tlb" no_namespace
//{{AFX_INSERT_LOCATION}}
...

Compile the "stdafx.cpp" file and its results to create two new files, vtxtauto.tlh and vtxtauto.tli, in the Debug/Release folder. These files give the interface the IVTxtAuto wrapper method implementations.

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 or holding results
HRESULT hr;
IVTxtAuto *voicePtr;       //a pointer to IVTxtAuto interface

Add the following piece of code in the OnInitDialog() function.

CSpeakTextDlg::OnInitDialog()
{
...
// Initializing COM
hr = CoInitialize(NULL);
//Getting the CLSID from the 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;

   //The Register function should be called first prior to
   //calling the Speak function so that the application is
   //registered for using the voice engine.
   voicePtr->Register(pszSite,"Radio");
   voicePtr->Speak("Welcome to Text to Voice Conversion Engine",
                   vtxtsp_NORMAL);
   voicePtr->Speak("Type your Text and ask me to speak",
                   vtxtsp_NORMAL);
}
...
}

Add the following on the click of 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);
...
}

Add the following code on the click of the Stop button

CSpeakTextDlg::OnStop()
{
   voicePtr->StopSpeaking();
}

That's all. Build and run the application. Wherever there is a need to give spoken information, call the speak function through the dialog.

Downloads

Download demo project - 6 Kb