Click here to Skip to main content
15,501,148 members
Articles / Desktop Programming / MFC
Posted 12 Nov 2001


77 bookmarked

Simple Program for Text to Speech Using SAPI (English Version)

Rate me:
Please Sign up or sign in to vote.
3.61/5 (31 votes)
12 Nov 20012 min read
Make a program for Text to Speech conversion using SAPI


In this project I made an application program for Text-to-Speech Conversion. To build this application, we must install the SDK speech from Microsoft on our computer. You can download Speech SDK (it's free) from

The SAPI API provides a high-level interface between an application and speech engines. SAPI implements all the low-level details needed to control and manage the real-time operations of various speech engines.

Applications can control text-to-speech (TTS) using the ISpVoice Component Object Model (COM) interface. Once an application has created an ISpVoice object, the application only needs to call ISpVoice::Speak to generate speech output from some text data. In addition, the IspVoice interface also provides several methods for changing voice and synthesis properties such as speaking rate ISpVoice::SetRate, output volume ISpVoice::SetVolume and changing the current speaking voice ISpVoice::SetVoice

The project devided into five steps :

  1. Creating New Project
  2. Setting Project
  3. Building GUI 
  4. Coding

1. Creating New Project

First you will create the initial ATL project using the MFC AppWizard.

  1. In the Visual C++ environment, click New on the File menu, then choose the Projects tab.

  2. Select the MFC AppWizard (exe).

  3. Type TxtToSpeech as the project name.

Your dialog box should look like this:

Image 1

Figure 1: New Project

Click OK and the MFC AppWizard presents a dialog box offering several choices to configure the type of MFC project (figure 2), choose Dialog based. After that, click Finish button

Image 2

Figure 2: MFC AppWizard Step 1, choose Dialog based

2. Setting Project

To use SAPI (Speech Application Interface) in our application, we must set our project. In file StdAfx.h, Add code like this (after "

" but before the "#endif" statement) :

#include <atlbase.h>
extern CComModule _Module;
#include <atlcom.h>

Change the project settings to reflect the paths. Using the Project->Settings. menu item, set the SAPI.h path. Click the C/C++ tab and select Preprocessor from the Category drop-down list. Enter the following in the "Additional include directories": with directory that Speech SDK available , such as D:\Program Files\Microsoft Speech SDK 5.1\Include. (see figure 3)

Image 3

Figure 3: Setting path

To set the SAPI.lib path (see figure 4):

  1. Select the Link tab from the Same Settings dialog box.
  2. Choose Input from the Category drop-down list.
  3. Add the following path to the "Additional library path" (directory that Speech SDK available), example :
    D:\Program Files\Microsoft Speech SDK 5.1\Lib\i386.
  4. Also add "sapi.lib" to the "Object/library modules" line. 

Image 4

Figure 4: Add library module Sapi.lib and set path

3. Building GUI

Model of GUI in this project like figure 5 :

Image 5

Figure 5: GUI project

4. Coding

In GUI, double click Button, type OnSpeak as name of method. This code:

ISpVoice * pVoice = NULL;

if (FAILED(CoInitialize(NULL)))
    AfxMessageBox("Error to intiliaze COM");

HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL,
    CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
    hr = pVoice->Speak(m_sText.AllocSysString(), 0, NULL);
    pVoice = NULL;


Note: m_sText is variable of Edit Box

After that, you can compile and run this project.


Speech SDK 5.1


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Founder PE College
Indonesia Indonesia
He gradueted from Sepuluh Nopember Institute of Technology (ITS) in Department of Electrical Engineering, Indonesia. His programming interest is VC++, C#, VB, VB.NET, .NET, VBScript, Delphi, C++ Builder, Assembly,and ASP/ASP.NET. He's founder for PE College(, free video tutorial about programming, infrastructure, and computer science. He's currently based in Depok, Indonesia. His blog is and

Comments and Discussions

GeneralText to Speech on Win CE Pin
henrytaihk30-Dec-03 23:46
Memberhenrytaihk30-Dec-03 23:46 
GeneralRe: Text to Speech on Win CE Pin
Anonymous16-Feb-04 10:50
MemberAnonymous16-Feb-04 10:50 
GeneralPure SDK Based SAPI Project Pin
R. Balaji Iyer4-Sep-03 21:27
MemberR. Balaji Iyer4-Sep-03 21:27 
Generalwill do? Pin
arrowind28-Feb-04 3:49
Memberarrowind28-Feb-04 3:49 
Generalatl Pin
tomato ketchup8-Aug-03 3:27
susstomato ketchup8-Aug-03 3:27 
Questioncan i get rid of the speech sdk?? Pin
FARTal_error4-Aug-03 11:21
MemberFARTal_error4-Aug-03 11:21 
Generalchange voice Pin
werter119-Jul-03 20:50
Memberwerter119-Jul-03 20:50 
GeneralRe: change voice Pin
John T3-Mar-05 6:28
MemberJohn T3-Mar-05 6:28 
You can find that in the SDK documentation.
Do an enumeration.
If it is you intentation to change language
you can also enumerate installed languages.

SAPI 4 contains, for now, more languages than SAPI 5,
and it is still downloadable for free at microsoft,
and also has very good documentation!

Hope this helps you.

GeneralRe: change voice Pin
John T3-Mar-05 7:30
MemberJohn T3-Mar-05 7:30 
Generallanguage available Pin
Sabrina5-May-03 16:03
MemberSabrina5-May-03 16:03 
Generalurgent please help.... Pin
jasonymk24-Apr-03 6:08
Memberjasonymk24-Apr-03 6:08 
Generaltext to save wav file Pin
maohj168824-Mar-03 15:38
Membermaohj168824-Mar-03 15:38 
GeneralRe: text to save wav file Pin
sonalg24-Jun-04 19:42
Membersonalg24-Jun-04 19:42 
GeneralRe: text to save wav file Pin
rhdunn16-Jan-05 0:39
Memberrhdunn16-Jan-05 0:39 
GeneralI need some help....SOS Pin
jasonymk5-Feb-03 4:51
Memberjasonymk5-Feb-03 4:51 
GeneralRe: I need some help....SOS Pin
JoeSox9-Mar-03 11:55
MemberJoeSox9-Mar-03 11:55 
GeneralNeed some help please Pin
Anonymous24-Dec-02 9:10
MemberAnonymous24-Dec-02 9:10 
Generalneed Sapi 5.1 Pin
Anonymous13-Nov-02 14:17
MemberAnonymous13-Nov-02 14:17 
GeneralRe: need Sapi 5.1 Pin
Anonymous12-Feb-03 16:19
MemberAnonymous12-Feb-03 16:19 
GeneralISpVoice::Speak() fails in Win32 Release setting Pin
Anonymous11-Oct-02 11:45
MemberAnonymous11-Oct-02 11:45 
GeneralPerfect!!! Pin
Vignier27-Aug-02 23:22
MemberVignier27-Aug-02 23:22 
GeneralSorry, again me Pin
Georgi Petrov23-Aug-02 0:57
MemberGeorgi Petrov23-Aug-02 0:57 
GeneralRe: Sorry, again me Pin
sxl-etr12-Oct-09 19:42
Membersxl-etr12-Oct-09 19:42 
GeneralYou Are great man!!! Pin
Georgi Petrov23-Aug-02 0:51
MemberGeorgi Petrov23-Aug-02 0:51 
Questionhow to make this application in VB Pin
25-Jun-02 10:26
suss25-Jun-02 10:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.