Search code examples
c++windowstext-to-speechsapi

Cannot correctly speak non-english text using SAPI5.4


For example:


#include <sapi.h>
#include <atlbase.h>
#include <atlstr.h>
#pragma warning (suppress: 4996)
#include <sphelper.h>
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);
    CComPtr<ISpObjectToken>        cpVoiceToken;
    SpGetTokenFromId(L"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens\\TTS_MS_ZH-CN_HUIHUI_11.0", &cpVoiceToken);
    hr = pVoice->SetVoice(cpVoiceToken);
    if (SUCCEEDED(hr))
    {
        hr = pVoice->Speak(L"你好 Hello", 0, NULL);
    }

    return 0;
}

Can speak "hello" but will skip "你好" Cannot speak Japanese correctly.

My IDE is Visual Studio 2022.


Solution

  • Solution: Change the source files into UTF-16LE With BOM!! Don't use utf-8.