다음을 통해 공유


XSpeechSynthesizerCreate

음성 신시사이저를 만듭니다.

구문

HRESULT XSpeechSynthesizerCreate(  
         XSpeechSynthesizerHandle* speechSynthesizer  
)  

매개 변수

speechSynthesizer _Out_
형식: XSpeechSynthesizerHandle*

생성된 음성 신시사이저에 대한 핸들입니다.

반환 값

형식: HRESULT

성공한 경우 S_OK를 반환하고, 그렇지 않으면 오류 코드를 반환합니다. 오류 코드 목록은 오류 코드를 참조하세요.

설명

참고 항목

이 함수는 시간에 민감한 스레드에서 호출하는 것이 안전하지 않습니다. 자세한 내용은 시간에 민감한 스레드를 참조하세요.

이 기능을 사용하면 설치된 음성 합성 엔진 또는 음성의 기능에 액세스할 수 있는 새 음성 신시사이저 인스턴스에 대한 핸들을 만들고 수신할 수 있습니다.

기본적으로 새 음성 신시사이저 인스턴스는 현재 시스템 음성을 사용 합니다. 현재 장치에 설치된 음성에 대해 열거하고 정보를 얻으려면 XSpeechSynthesizerEnumerateInstalledVoices 함수를 XSpeechSynthesizerInstalledVoicesCallback 콜백 함수와 함께 사용합니다. 설치된 각 음성에 대해 XSpeechSynthesizerVoiceInformation 구조체는 음성 ID, 설명, 표시 텍스트, 성별, 언어 및 기타 정보를 제공합니다. XSpeechSynthesizerSetCustomVoice를 호출하여 설치된 다른 음성을 사용하거나 XSpeechSynthesizerSetDefaultVoice를 호출하여 현재 시스템 음성을 다시 사용합니다.

음성 신시사이저 핸들을 만들고 음성을 지정한 후 XSpeechSynthesizerCreateStreamFromText 함수를 사용하여 음성 신시사이저 스트림을 만들고 일반 텍스트에서 음성을 합성할 수 있습니다. XSpeechSynthesizerGetStreamDataSizeXSpeechSynthesizerGetStreamData 함수를 사용하여 음성 신시사이저 스트림에서 합성된 음성에 대한 오디오 데이터를 가져온 다음 XSpeechSynthesizerCloseStreamHandle 함수를 사용하여 모든 미해결 비동기 작업이 완료된 후 음성 신시사이저 스트림을 닫습니다.

XSpeechSynthesizerCloseHandle 함수를 사용하여 음성 신시사이저를 닫고 음성 신시사이저를 사용한 후 시스템 리소스를 릴리스합니다.

메모리 누수를 방지하려면 XSpeechSynthesizerCloseHandle 함수를 호출하여 핸들을 사용하는 모든 작업을 완료한 후 음성 신시사이저 스트림 핸들을 닫습니다.

다음 예제에서는 음성 신시사이저 및 설치된 음성을 사용하여 일반 텍스트에서 음성을 합성하는 방법과 음성 신시사이저 스트림으로 오디오 데이터를 합성하는 방법을 보여줍니다. XSpeechSynthesizerCreate 1 및 XSpeechSynthesizerSetCustomVoice 함수는 음성 신시사이저 인스턴스를 만들고, 음성 ID가 voiceId에 지정된 경우에는 사용자 지정 음성을 할당합니다(선택 사항). 그런 다음 XSpeechSynthesizerCreateStreamFromText 함수는 음성 신시사이저 스트림을 만들고 textToSpeak에 지정된 일반 텍스트에서 음성을 합성합니다. 스트림이 생성된 후 XSpeechSynthesizerGetStreamDataSizeXSpeechSynthesizerGetStreamData 함수는 합성 된 음성이 스트림에서 재생될 오디오 데이터를 검색합니다. 마지막으로 오디오 데이터 재생이 완료된 후 XSpeechSynthesizerCloseStreamHandleXSpeechSynthesizerCloseHandle 함수는 음성 신시사이저 스트림 및 음성 신시사이저를 닫습니다.

HRESULT Game::SynthesizeSpeech(
    const char* textToSpeak,
    const char* voiceId)
{
    // Create a new speech synthesizer.
    XSpeechSynthesizerHandle ssHandle = nullptr;
    if (FAILED(XSpeechSynthesizerCreate(&ssHandle))) { return E_FAIL; }

    // If a voice ID was specified, attempt to set the speech synthesizer to
    // use the specified voice. Note that voiceId has a default value of nullptr, 
    // as specified in its function declaration.
    if (voiceId != nullptr) 
    {
        if (FAILED(XSpeechSynthesizerSetCustomVoice(ssHandle, voiceId))) { return E_FAIL; }
    }

    // Create a new speech synthesizer stream from the specified text.
    XSpeechSynthesizerStreamHandle ssStreamHandle = nullptr;
    if (FAILED(XSpeechSynthesizerCreateStreamFromText(ssHandle, textToSpeak, &ssStreamHandle))) { return E_FAIL; }

    // Get the size of the buffer needed for the audio data from our stream.
    size_t bufferSize;
    if (FAILED(XSpeechSynthesizerGetStreamDataSize(ssStreamHandle, &bufferSize))) { return E_FAIL; }

    // Define the buffer, then retrieve the audio data from our stream.
    std::vector<char> streamData;
    streamData.resize(bufferSize);
    if (FAILED(XSpeechSynthesizerGetStreamData(ssStreamHandle, bufferSize, streamData.data(), &bufferSize))) { return E_FAIL; }

    // We now have audio data from the speech synthesizer stream, so let's play it. 
    // For the purposes of this example, the sound is played synchronously, so that we don't
    // risk having an outstanding asynchronous operation when we close the stream.
    PlaySoundW(reinterpret_cast<LPCWSTR>(streamData.data()), nullptr, SND_MEMORY);

    // We're done with the speech synthesizer stream, so let's close it.
    if (FAILED(XSpeechSynthesizerCloseStreamHandle(ssStreamHandle))) { return E_FAIL; }

    // We're done with the speech synthesizer, so let's close that, too.
    if (FAILED(XSpeechSynthesizerCloseHandle(ssHandle))) { return E_FAIL; }

    return S_OK;
}

요구 사항

헤더: XSpeechSynthesizer.h

라이브러리: xgameruntime.lib

지원되는 플랫폼: Windows, Xbox One 패밀리 콘솔 및 Xbox Series 콘솔

참고 항목

XAccessibility
XSpeechSynthesizerCloseHandle
XSpeechSynthesizer