> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XSpeechSynthesizerCreate

> XSpeechSynthesizerCreate

# XSpeechSynthesizerCreate

音声合成器を作成します。

## Syntax

```cpp theme={null}
HRESULT XSpeechSynthesizerCreate(  
         XSpeechSynthesizerHandle* speechSynthesizer  
)  
```

### Parameters

*speechSynthesizer*   \_Out\_\
型: XSpeechSynthesizerHandle\*

作成された音声合成器のハンドル。

### Return value

型: [HRESULT](https://learn.microsoft.com/openspecs/windows_protocols/ms-erref/0642cb2f-2075-4469-918c-4441e69c548a)

成功した場合は **S\_OK** を返します。そうでない場合はエラー コードを返します。エラー コードの一覧については、[エラー コード](/reference/errorcodes) を参照してください。

## Remarks

<Note>この関数はタイム センシティブ スレッドで呼び出すのは安全ではありません。詳細については、[タイム センシティブ スレッド](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads) を参照してください。</Note>

この関数を使用して、新しい音声合成器インスタンスへのハンドルを作成して受け取ります。これにより、インストールされている音声合成エンジン (*音声*) の機能へのアクセスが提供されます。

既定では、新しい音声合成器インスタンスは現在のシステム音声を使用します。現在のデバイスにインストールされている音声を列挙し情報を取得するには、[XSpeechSynthesizerEnumerateInstalledVoices](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerenumerateinstalledvoices) 関数を [XSpeechSynthesizerInstalledVoicesCallback](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerinstalledvoicescallback) コールバック関数と共に使用します。インストールされている各音声について、[XSpeechSynthesizerVoiceInformation](/reference/system/xspeechsynthesizer/structs/xspeechsynthesizervoiceinformation) 構造体は音声 ID、説明、表示テキスト、性別、言語、その他の情報を提供します。別のインストール済み音声を使用するには [XSpeechSynthesizerSetCustomVoice](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizersetcustomvoice) を呼び出し、現在のシステム音声を再び使用するには [XSpeechSynthesizerSetDefaultVoice](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizersetdefaultvoice) を呼び出します。

音声合成器ハンドルを作成して音声を指定した後、[XSpeechSynthesizerCreateStreamFromText](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromtext) 関数を使用して音声合成器ストリームを作成し、プレーン テキストから音声を合成します。[XSpeechSynthesizerGetStreamDataSize](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizergetstreamdatasize) および [XSpeechSynthesizerGetStreamData](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizergetstreamdata) 関数を使用して、音声合成器ストリームから合成された音声のオーディオ データを取得し、その後、未完了の非同期操作がすべて完了した後に [XSpeechSynthesizerCloseStreamHandle](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosestreamhandle) 関数を使用して音声合成器ストリームを閉じます。

音声合成器の使用が完了したら、[XSpeechSynthesizerCloseHandle](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle) 関数を使用して音声合成器を閉じ、システム リソースを解放します。

メモリ リークを防ぐため、ハンドルを使用するすべての操作が完了した後、[XSpeechSynthesizerCloseHandle](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle) 関数を呼び出して音声合成器ハンドルを閉じてください。

次の例は、音声合成器とインストール済み音声を使用してプレーン テキストから音声を合成する方法と、音声合成器ストリームでオーディオ データを合成する方法を示しています。**XSpeechSynthesizerCreate** および **XSpeechSynthesizerSetCustomVoice** 関数は、音声合成器インスタンスを作成し、*voiceId* に音声 ID が指定されている場合は必要に応じてカスタム音声を割り当てます。次に、**XSpeechSynthesizerCreateStreamFromText** 関数は音声合成器ストリームを作成し、*textToSpeak* で指定されたプレーン テキストから音声を合成します。ストリームが作成された後、**XSpeechSynthesizerGetStreamDataSize** および **XSpeechSynthesizerGetStreamData** 関数は、ストリームから再生する合成音声のオーディオ データを取得します。最後に、オーディオ データの再生が完了した後、**XSpeechSynthesizerCloseStreamHandle** および **XSpeechSynthesizerCloseHandle** 関数が音声合成器ストリームと音声合成器を閉じます。

```cpp theme={null}
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;
}
```

## Requirements

**ヘッダー:** XSpeechSynthesizer.h

**ライブラリ:** xgameruntime.lib

**サポートされているプラットフォーム:** Windows、XBOX One ファミリー本体および XBOX Series 本体

## Conceptual documentation

* [テキスト読み上げ](/build/console-features/text-to-speech/text-to-speech)
* [タイム センシティブ スレッド](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## See also

[XAccessibility](/reference/system/xaccessibility/xaccessibility_members)\
[XSpeechSynthesizerCloseHandle](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle)\
[XSpeechSynthesizer](/reference/system/xspeechsynthesizer/xspeechsynthesizer_members)


## Related topics

- [XSpeechSynthesizerCreateStreamFromSsml](/ja-jp/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromssml.md)
- [XSpeechSynthesizerCreateStreamFromText](/ja-jp/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromtext.md)
- [XSpeechSynthesizerCloseHandle](/ja-jp/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle.md)
- [XSpeechSynthesizer](/ja-jp/reference/system/xspeechsynthesizer/xspeechsynthesizer_members.md)
- [テキスト読み上げ](/ja-jp/build/console-features/text-to-speech/text-to-speech.md)
