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


Comments

  • Linker Error On Release Configuartion

    Posted by Legacy on 03/27/2003 12:00am

    Originally posted by: John

    Hallo

    I tried to reproduce this sample and it worked out real fine! But when I tried to set the active Configuration to ... Win32 Release I got the following Error:

    Linking...
    LINK : warning LNK4089: all references to "SHELL32.dll" discarded by /OPT:REF
    LINK : warning LNK4089: all references to "comdlg32.dll" discarded by /OPT:REF
    LINK : error : Internal error during ReadSymbolTable
    ExceptionCode = C0000005
    ExceptionFlags = 00000000
    ExceptionAddress = 00433A5D
    NumberParameters = 00000002
    ExceptionInformation[ 0] = 00000001
    ExceptionInformation[ 1] = 00000002
    CONTEXT:
    Eax = 00000002 Esp = 0012F6D0
    Ebx = 010A0701 Ebp = 002FD670
    Ecx = 008364BE Esi = 01090940
    Edx = 00000000 Edi = 01090E00
    Eip = 00433A5D EFlags = 00010216
    SegCs = 0000001B SegDs = 00000023
    SegSs = 00000023 SegEs = 00000023
    SegFs = 00000038 SegGs = 00000000
    Dr0 = 0012F6D0 Dr3 = 010A0701
    Dr1 = 002FD670 Dr6 = 008364BE
    Dr2 = 00000000 Dr7 = 00000000
    Error executing link.exe.
    Tool execution canceled by user.

    Please advise

    Reply
  • Nice!

    Posted by Legacy on 01/26/2003 12:00am

    Originally posted by: R.V.Anand

    Hi Jaggu,

    Where do i get the speech engine components?.

    Reply
  • AcitveVoice Control

    Posted by Legacy on 01/16/2003 12:00am

    Originally posted by: C. T. Simonetti

    Why doesn't the .Speak member function get exposed to your VC application when using the "insert ActiveX control" facility - from with a dialog? I am using MS VC++ .net and am in a MFC/Dialog application.

    Reply
  • pretty

    Posted by Legacy on 10/04/2002 12:00am

    Originally posted by: idiot

    pretty!!!!!!!!!

    Reply
  • small is beautiful.

    Posted by Legacy on 09/17/2002 12:00am

    Originally posted by: Small is beautiful. But how to do this in Visual C ?

    Respected Sir,
    The article gave me a vary good start. But I already have written an Telephony appliaction in C so there I think would problem of com objects can you guide me.
    Thanks
    Santosh

    Reply
  • More Voices

    Posted by Legacy on 09/17/2002 12:00am

    Originally posted by: sujeet

    I am using Microsoft Speech SDK 5.1 in my project. Its working fine n smooth. Only problem i am facing is restriction of voice as I can use only three default voices provided by Microsoft (Mike, Sam and Marry). Is there any way to incorporate more voices? 
    

    Reply
  • it's seem very good!

    Posted by Legacy on 09/16/2002 12:00am

    Originally posted by: leo fewldge

    here is the example too.
    ufirst.yeah.net

    Reply
  • Woman voice

    Posted by Legacy on 09/14/2002 12:00am

    Originally posted by: Yaron

    Thanks! this is so cool!
    Does anyone know if it has got the option to talk in a woman voice?

    Reply
  • How to play through different devices

    Posted by Legacy on 09/14/2002 12:00am

    Originally posted by: Mike

    The code sample was great! It worked right off the bat. One additional thing I wanted to know how to select the device I want to play through: different sound cards, modem etc.

    Any suggestions?

    Reply
  • Well done ! But is it possible in Frensh ?

    Posted by Legacy on 09/13/2002 12:00am

    Originally posted by: March'

    It is a cool feature ! And it looks simple to create but I'm from France... So is it possible to make my computer speaking "en fran�ais" ?

    March'

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • The advent of compute accelerators, special purpose coprocessors that significantly improve the performance of traditional host-based central processing unit (CPU) computations, represents a major transition in the tools available to achieve supercomputing power. This white paper presents Dell solution offerings and shows how they push the envelope of available solutions that bring immediate added value to HPC users in terms of integration, flexibility, and efficiency of hybrid compute solutions.

Most Popular Programming Stories

More for Developers

RSS Feeds