Share via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

Microsoft Speech Platform

Generate Speech from Text

Using the text-to-speech (TTS) functionality in the Microsoft Speech Platform, you can generate synthesized speech output from a text string or from the text in a file.

The following code snippet in C++ demonstrates how to generate speech from a text string and from text in a file using a specific voice. The example uses a helper method (SpEnumTokens) to enumerate the available voice tokens located under the key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech Server\v11.0\Voices. SpEnumTokens returns a token enumerator containing all tokens that meet a set of required and optional attributes. The enumerator returns tokens in the order in which they best match the specified attributes. In the following example, the required voice attribute is L"language=409", and there are no optional attributes. SpEnumTokens will return all of the voice tokens with the attribute L"language=409".

Using the IEnumSpObjectTokens::Next() method, you can find the best matching voice token and then set it as a current voice by calling ISpVoice::SetVoice() method.

When passing a file name instead of a text string, the example sets the speech flag SPF_IS_FILENAME in the call to ISpVoice::Speak. Please note, you may choose to use ISpVoice::SpeakStream to speak a file. In that case, you need to call SPBindToFile, a helper function, to bind the text file to an ISpStream object, and then call ISpVoice::SpeakStream.

`

HRESULT                       hr = S_OK;
CComPtr <ISpVoice>            cpVoice;
CComPtr <ISpObjectToken>      cpToken;
CComPtr <IEnumSpObjectTokens> cpEnum;

// Create a voice. hr = cpVoice.CoCreateInstance( CLSID_SpVoice );

// Enumerate voice tokens with attribute L"language=409". if(SUCCEEDED(hr)) { hr = SpEnumTokens(SPCAT_VOICES, L"language=409", NULL, &cpEnum;); }

// Get the best matching token. if(SUCCEEDED(hr)) { hr = cpEnum ->Next(1, &cpToken;, NULL); }

// Set the voice. if(SUCCEEDED(hr)) { hr = cpVoice->SetVoice( cpToken); }

// Set the output to the default audio device. if(SUCCEEDED(hr)) { hr = cpVoice->SetOutput( NULL, TRUE ); }

// Speak a string directly. if (SUCCEEDED(hr)) { hr = cpVoice->Speak(L"Hello, world!", SPF_Default, 0); }

// Speak the text in a file (assumed to exist). if(SUCCEEDED(hr)) { hr = cpVoice->Speak( L"c:\ttstemp.txt", SPF_IS_FILENAME, NULL ); }

// Release objects. cpVoice.Release(); cpEnum.Release(); cpToken.Release();

`

See Also