Initializing an Application for Text-to-Speech

To implement text-to-speech features, you must initialize the OLE libraries and then create instances of the audio-destination object and the text-to-speech engine object. The engine object translates a text buffer into audio output and passes it to the audio-destination object, which plays the speech output on the audio-output device. In addition, you can use the text-to-speech enumerator and engine enumerator to find an engine and mode that meets your application's requirements.

The following example demonstrates how to initialize an application for text-to-speech. It shows how to create the necessary objects for implementing text-to-speech translation. The example accomplishes the following tasks:

· Uses the CoInitialize function to initialize the OLE libraries.

· Creates an instance of the multimedia audio-destination object, which allows the engine to send speech output to the multimedia wave-out driver.

· Uses the text-to-speech enumerator to find an engine and mode that most closely matches the application's requirements.

· Creates an instance of an application-defined class that implements the ITTSBufNotifySink interface. The engine calls this interface to notify the application of buffer-related events.

// FindAndSelectEngine - creates a multimedia audio-destination object,

// finds and selects a text-to-speech engine and mode, and creates

// the buffer notification object.

// Returns TRUE if successful or FALSE if an error occurs.

//

// Global variables:

// g_pIAMM - address of IAudioMultiMediaDevice interface

// g_TTSModeInfo - address of TTSMODEINFO structure that contains

// information about the selected mode

// g_pIBufNotifySink - address of the application's

// ITTSBufNotifySink interface

BOOL FindAndSelectEngine(void)

{

HRESULT hRes; // result code

TTSMODEINFO ttsmiTemp; // information about the desired mode

PITTSFIND pITTSFind; // address of ITTSFind interface

// Initialize OLE.

if (FAILED(CoInitialize(NULL)))

return FALSE;

// Create the multimedia audio-destination object and retrieve the

// address of the object's IAudioMultiMediaDevice interface.

hRes = CoCreateInstance(CLSID_MMAudioDest, NULL, CLSCTX_ALL,

IID_IAudioMultiMediaDevice, (void**) &g_pIAMM);

if (hRes)

return FALSE;

// Set the device identifier for the multimedia wave-out device.

g_pIAMM->DeviceNumSet(WAVE_MAPPER);

// Create the text-to-speech enumerator and retrieve the address of

// the object's ITTSFind interface.

hRes = CoCreateInstance(CLSID_TTSEnumerator, NULL, CLSCTX_ALL,

IID_ITTSFind, (void**) &pITTSFind);

if (FAILED(hRes)) {

g_pIAMM->Release;

return FALSE;

}

// Define the characteristics of the desired text-to-speech mode.

ZeroMemory(&ttsmiTemp, sizeof(ttsmiTemp));

ttsmiTemp.language.LanguageID =

MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);

ttsmiTemp.wGender = GENDER_FEMALE;

ttsmiTemp.wAge = TTSAGE_ADULT;

lstrcpy(ttsmiTemp.szStyle, "Business");

// Find the engine and mode that most closely matches the desired

// characteristics.

hRes = pITTSFind->Find(&ttsmiTemp, NULL, &g_TTSModeInfo);

if (hRes) {

g_pIAMM->Release;

pITTSFind->Release();

return FALSE;

}

// Select the text-to-speech mode and create an engine object to

// represent the mode. Also, retrieve the address of the engine's

// ITTSCentral interface.

hRes = pITTSFind->Select(g_TTSModeInfo.gModeID, &g_pITTSCentral,

(LPUNKNOWN) g_pIAMM);

if (hRes) {

g_pIAMM->Release;

pITTSFind->Release();

return FALSE;

};

// Release the text-to-speech enumerator; it is no longer needed.

pITTSFind->Release();

// Create an instance of the buffer notification object based on the

// application-defined CBufNotify class.

if ((g_pIBufNotifySink = new CBufNotify) == NULL)

return FALSE;

return TRUE;

}