Search code examples
c++text-to-speechsapi

Implement Microsoft Speech Platform languages in SAPI 5


I created a little program in C++ where I use the SAPI library. In my code, I list the number of voices installed on my system. When I compile, I get 11, but there are only 8 installed and the only voice speaking is Microsoft Anna. I checked it in the registry (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices). I have several languages installed , especially languages from the Microsoft Speech Platform but none can be used.

Installed languages

Furthermore, when I change the voice ID, I get an unhandled exception error and I think it is because the chosen ID does not exist.

Here is my code

#include "stdafx.h"

int main( int argc, char* argv[] )
{

CComPtr<ISpObjectToken>         cpVoiceToken;
CComPtr<IEnumSpObjectTokens>    cpEnum;
ISpVoice *                      pVoice = NULL;
ULONG                           count = 0;
string                          str;

if( FAILED( ::CoInitialize( NULL ) ) )
return FALSE;

HRESULT hr = CoCreateInstance( CLSID_SpVoice, NULL, CLSCTX_ALL, 
IID_ISpVoice, ( void ** )&pVoice );

if( SUCCEEDED( hr ) )
{
   //Enumerate Voices
   hr = SpEnumTokens( SPCAT_VOICES, NULL /*L"Gender=Female"*/, NULL, &cpEnum);
   printf( "Success\n" );
}
else
{
   printf( "Failed to initialize SAPI" );
}

if( SUCCEEDED( hr ) )
{
  //Get number of voices
  hr = cpEnum->GetCount( &count );
  printf( "TTS voices found: %i\n", count );
}
else
{
   printf( "Failed to enumerate voices" );
   hr = S_OK;
}

if( SUCCEEDED( hr ) )
{
   cpVoiceToken.Release();

   cpEnum->Item( 3, &cpVoiceToken ); //3 represents the ID of the voice

   pVoice->SetVoice( cpVoiceToken );
   hr = pVoice->Speak( L"You have selected Microsoft Server Speech Text to Speech Voice (en-GB, Hazel) as the computer's default voice.", 0, NULL ); //speak sentence
   pVoice->Release();
   pVoice = NULL;
}

::CoUninitialize();
system( "PAUSE" );

}

The only voice working is Microsoft Anna, and I don't understand why. If the other languages were not available, the program won't show me that there are so many(11). I wonder if the Microsoft Speech Platform languages are compatible with SAPI.


Solution

  • After many tries and fails, I managed to find an answer to my problem. I compiled my program in Win32. So I decided to change it to x64 and I recompiled the solution. I changed the voice ID in my program, and the voices from the Microsoft Speech Platform worked. This means that the MS Speech Platform languages are 64 bit voices and Microsoft Anna is a 32 bit voice.

    enter image description here

    The following post inspired me.