Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

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 501.9K   10.3K   77   105
Make a program for Text to Speech conversion using SAPI

Introduction

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 http://www.microsoft.comm/speech

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 "

#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)

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:

UpdateData();
ISpVoice * pVoice = NULL;

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

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

CoUninitialize();

Note: m_sText is variable of Edit Box

After that, you can compile and run this project.

Reference

Speech SDK 5.1

License

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(www.pecollege.net), free video tutorial about programming, infrastructure, and computer science. He's currently based in Depok, Indonesia. His blog is http://geeks.netindonesia.net/blogs/agus and http://blog.aguskurniawan.net

Comments and Discussions

 
Generalneed Sapi 5.1 Pin
Anonymous13-Nov-02 13:17
Anonymous13-Nov-02 13:17 
GeneralRe: need Sapi 5.1 Pin
Anonymous12-Feb-03 15:19
Anonymous12-Feb-03 15:19 
GeneralISpVoice::Speak() fails in Win32 Release setting Pin
Anonymous11-Oct-02 10:45
Anonymous11-Oct-02 10:45 
GeneralPerfect!!! Pin
Vignier27-Aug-02 22:22
Vignier27-Aug-02 22:22 
GeneralSorry, again me Pin
Georgi Petrov22-Aug-02 23:57
Georgi Petrov22-Aug-02 23:57 
GeneralRe: Sorry, again me Pin
sxl-etr12-Oct-09 18:42
sxl-etr12-Oct-09 18:42 
GeneralYou Are great man!!! Pin
Georgi Petrov22-Aug-02 23:51
Georgi Petrov22-Aug-02 23:51 
Questionhow to make this application in VB Pin
25-Jun-02 9:26
suss25-Jun-02 9:26 
how to associate the library files and the header files in making this application in VB 6 ?Unsure | :~
QuestionAm I missing something here ?? Pin
21-May-02 15:35
suss21-May-02 15:35 
AnswerRe: Am I missing something here ?? Pin
Georgi Petrov25-Aug-02 22:00
Georgi Petrov25-Aug-02 22:00 
GeneralRe: Am I missing something here ?? Pin
Anonymous23-Feb-04 16:39
Anonymous23-Feb-04 16:39 
GeneralError Pin
24-Apr-02 5:24
suss24-Apr-02 5:24 
GeneralRe: Error Pin
11-May-02 3:40
suss11-May-02 3:40 
GeneralRe: Error Pin
11-May-02 3:41
suss11-May-02 3:41 
GeneralRe: Error Pin
sxl-etr10-Oct-09 5:01
sxl-etr10-Oct-09 5:01 
GeneralI dislike plagiarism Pin
5-Mar-02 1:47
suss5-Mar-02 1:47 
GeneralRe: I dislike plagiarism Pin
26-Mar-02 2:57
suss26-Mar-02 2:57 
GeneralRe: I dislike plagiarism Pin
26-Mar-02 2:58
suss26-Mar-02 2:58 
GeneralRe: I dislike plagiarism Pin
Anonymous13-Nov-02 15:21
Anonymous13-Nov-02 15:21 
GeneralRe: I dislike plagiarism Pin
Anonymous13-Nov-02 15:30
Anonymous13-Nov-02 15:30 
GeneralRe: I dislike plagiarism Pin
simou13-Nov-02 15:30
simou13-Nov-02 15:30 
GeneralRe: I dislike plagiarism Pin
eponk25-Feb-03 17:27
eponk25-Feb-03 17:27 
GeneralExtra Languages support Pin
8-Feb-02 8:28
suss8-Feb-02 8:28 
GeneralRe: Extra Languages support Pin
Agus Kurniawan8-Feb-02 19:13
Agus Kurniawan8-Feb-02 19:13 
GeneralRe: Extra Languages support Pin
9-Feb-02 4:52
suss9-Feb-02 4:52 

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.