Search code examples
c++text-to-speechsapi

text to speech SAPI voices


Can anybody help me? I use SAPI to speech text, but I can`t set female voice, here is code, It speaks in male voice, but i want to change it, I want female voice

#include "stdafx.h"

using namespace std;

void speack(HRESULT, ISpVoice*, string);

int main(int argc, char* argv[])
{
ISpVoice * pVoice = NULL;

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

HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if (SUCCEEDED(hr))
{
    hr = pVoice->Speak(L"Hi my friend", 0, NULL);
    string text;
    while (true){
        getline(cin, text);
        speack(hr, pVoice, text);
        if (text == "Goodbye" || text == "goodbye")
            break;
    }

    speack(hr, pVoice, "Have a good day !!");

    pVoice->Release();
    pVoice = NULL;
}
::CoUninitialize();
return TRUE;

}

void speack(HRESULT hr, ISpVoice * pVoice, string text){
hr = pVoice->Speak(CA2CT(text.c_str()), 0, NULL);
}

Please help Thank you


Solution

  • When you call

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

    SAPI will create the default voice object. Since you want a specific voice, you'll need to use one of the SAPI helper functions - SpCreateBestObject.

    hr = SpCreateBestObject(SPCAT_VOICES, L”Gender=Female”, NULL, &pVoice); 
    

    This will create the best object in the VOICES category that has a Female gender (since it's specified as a required attribute).