Click here to Skip to main content
12,299,763 members (60,867 online)
Click here to Skip to main content
Add your own
alternative version


77 bookmarked

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

, 12 Nov 2001
Rate this:
Please Sign up or sign in to vote.
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:

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

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 "#include <stdio.h>" 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)

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. 

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

3. Building GUI

Model of GUI in this project like figure 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


About the Author

Agus Kurniawan
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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
osirisgothra18-Jan-12 15:31
memberosirisgothra18-Jan-12 15:31 
GeneralMy vote of 5 Pin
FreeAsABird28-Jun-11 0:40
memberFreeAsABird28-Jun-11 0:40 
Dennis Ray3-May-11 4:53
memberDennis Ray3-May-11 4:53 
GeneralYes,Very Good! Pin
chinesedragon11-May-10 3:17
groupchinesedragon11-May-10 3:17 
Generalis it possibl to viceversa of this program ie speech to text Pin
eswar pothula26-Nov-09 21:56
membereswar pothula26-Nov-09 21:56 
GeneralRe: is it possibl to viceversa of this program ie speech to text Pin
alawy875-Oct-10 13:39
memberalawy875-Oct-10 13:39 
GeneralRe: is it possibl to viceversa of this program ie speech to text Pin
HuangQihua26-Feb-11 20:46
memberHuangQihua26-Feb-11 20:46 
GeneralText to Speech -Audio File German Version Pin
smilechill21-Sep-09 4:25
membersmilechill21-Sep-09 4:25 
GeneralGetTranscript Pin
Danilo Queiroz13-Apr-09 15:30
memberDanilo Queiroz13-Apr-09 15:30 
Generalcreating wav file in TTS Pin
Member 282845212-Apr-09 23:21
memberMember 282845212-Apr-09 23:21 
Generalproblem getting software for speech to text recognition Pin 7:07 7:07 
QuestionSAPI text to speech looping problem! Pin
cesarpachon9-Apr-08 15:31
membercesarpachon9-Apr-08 15:31 
GeneralSTDAfx.h HELP! Pin
Sebcity5-Feb-07 6:30
memberSebcity5-Feb-07 6:30 
GeneralRe: STDAfx.h HELP! Pin
cristitomi23-Sep-07 5:27
membercristitomi23-Sep-07 5:27 
QuestionHow to generate music from text characters? Pin
akks_38-Nov-06 8:11
memberakks_38-Nov-06 8:11 
Generalurgent : automation of engine Pin
nehasingh20-Jan-06 7:50
membernehasingh20-Jan-06 7:50 
Generalspeech recognition Pin
manikandanek29-Oct-05 0:55
membermanikandanek29-Oct-05 0:55 
GeneralText To Speech Convertor Pin
manikandanek29-Oct-05 0:49
membermanikandanek29-Oct-05 0:49 
GeneralRe: Text To Speech Convertor Pin
sen_raj2230-Jan-06 17:53
membersen_raj2230-Jan-06 17:53 
GeneralChanging voice ! Pin
spirit19836-Aug-05 13:43
memberspirit19836-Aug-05 13:43 
GeneralGerman Output?! Pin
Hansa4Ever24-Jun-05 2:23
memberHansa4Ever24-Jun-05 2:23 
Generalis it possible not to use sdk Pin
kaamsha8-Apr-05 10:41
memberkaamsha8-Apr-05 10:41 
GeneralIntergrating TTS with direct sound Pin
wtatters5-Apr-05 11:48
memberwtatters5-Apr-05 11:48 
Generalnice but too simple. Pin
Anonymous30-Mar-05 3:18
sussAnonymous30-Mar-05 3:18 
GeneralRe: nice but too simple. Pin
cristitomi23-Sep-07 5:33
membercristitomi23-Sep-07 5:33 
Questionhow to do speech to text using VB Pin
COOLBHUSHU29-Jan-05 18:51
memberCOOLBHUSHU29-Jan-05 18:51 
GeneralTTS to read web page Pin
Anonymous3-Nov-04 5:08
sussAnonymous3-Nov-04 5:08 
GeneralRe: TTS to read web page Pin
Anonymous30-Mar-05 4:06
sussAnonymous30-Mar-05 4:06 
GeneralTTS application that accept inpur frm command prompt and generate .wav file Pin
sunil choudhary17-Sep-04 0:28
membersunil choudhary17-Sep-04 0:28 
GeneralRe: TTS application that accept inpur frm command prompt and generate .wav file Pin
Anonymous30-Mar-05 4:03
sussAnonymous30-Mar-05 4:03 
Generalabout speech events in speech sdk 5.1 Pin
xunan31-Aug-04 1:53
memberxunan31-Aug-04 1:53 
GeneralDeciding download Pin
RJSoft20-Jul-04 6:54
memberRJSoft20-Jul-04 6:54 
GeneralURGENT: Pronunce numbers as phone numbers with TTS Pin
lxxxl9-Jun-04 10:21
memberlxxxl9-Jun-04 10:21 
GeneralRe: URGENT: Pronunce numbers as phone numbers with TTS Pin
Anonymous30-Mar-05 4:08
sussAnonymous30-Mar-05 4:08 
GeneralRe: URGENT: Pronunce numbers as phone numbers with TTS Pin
Anonymous30-Mar-05 4:08
sussAnonymous30-Mar-05 4:08 
GeneralAgain Pin
sinmorn24-May-04 14:40
membersinmorn24-May-04 14:40 
GeneralRe: Again Pin
cristitomi23-Sep-07 5:37
membercristitomi23-Sep-07 5:37 
GeneralIt's great!!! *but a little question here... Pin
caprice198222-Apr-04 18:43
membercaprice198222-Apr-04 18:43 
GeneralRe: It's great!!! *but a little question here... Pin
Anonymous30-Mar-05 4:04
sussAnonymous30-Mar-05 4:04 
GeneralHelp Pin
sinmorn18-Apr-04 15:57
membersinmorn18-Apr-04 15:57 
GeneralHi,Thanks the access and sos Pin
sinmorn5-Mar-04 15:23
membersinmorn5-Mar-04 15:23 
Generalhi Pin
Anonymous23-Feb-04 21:26
sussAnonymous23-Feb-04 21:26 
GeneralRe: hi Pin
cristitomi23-Sep-07 6:56
membercristitomi23-Sep-07 6:56 
GeneralText to Speech on Win CE Pin
henrytaihk30-Dec-03 22:46
memberhenrytaihk30-Dec-03 22:46 
GeneralRe: Text to Speech on Win CE Pin
Anonymous16-Feb-04 9:50
sussAnonymous16-Feb-04 9:50 
GeneralPure SDK Based SAPI Project Pin
R. Balaji Iyer4-Sep-03 20:27
memberR. Balaji Iyer4-Sep-03 20:27 
Generalwill do? Pin
Arrowind28-Feb-04 2:49
sussArrowind28-Feb-04 2:49 
Generalatl Pin
tomato ketchup8-Aug-03 2:27
susstomato ketchup8-Aug-03 2:27 
Questioncan i get rid of the speech sdk?? Pin
FARTal_error4-Aug-03 10:21
memberFARTal_error4-Aug-03 10:21 
Generalchange voice Pin
werter119-Jul-03 19:50
memberwerter119-Jul-03 19:50 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 13 Nov 2001
Article Copyright 2001 by Agus Kurniawan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid