> ## 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

Crée un synthétiseur vocal.

## Syntaxe

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

### Paramètres

*speechSynthesizer*   \_Out\_\
Type : XSpeechSynthesizerHandle\*

Le handle du synthétiseur vocal créé.

### Valeur de retour

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

Retourne **S\_OK** en cas de réussite; sinon, retourne un code d'erreur. Pour obtenir la liste des codes d'erreur, consultez [Codes d'erreur](/fr-CA/reference/errorcodes).

## Remarques

<Note>Il n'est pas sécuritaire d'appeler cette fonction sur un thread sensible au temps. Pour plus d'informations, consultez [Threads sensibles au temps](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads).</Note>

Utilisez cette fonction pour créer une nouvelle instance de synthétiseur vocal et en recevoir le handle; cette instance donne accès aux fonctionnalités d'un moteur de synthèse vocale installé, ou *voix*.

Par défaut, une nouvelle instance de synthétiseur vocal utilise la voix système actuelle. Pour énumérer les voix installées sur l'appareil actuel et obtenir des informations à leur sujet, utilisez la fonction [XSpeechSynthesizerEnumerateInstalledVoices](/fr-CA/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerenumerateinstalledvoices) avec la fonction de rappel [XSpeechSynthesizerInstalledVoicesCallback](/fr-CA/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerinstalledvoicescallback). Pour chaque voix installée, la structure [XSpeechSynthesizerVoiceInformation](/fr-CA/reference/system/xspeechsynthesizer/structs/xspeechsynthesizervoiceinformation) fournit l'ID de la voix, sa description, son texte d'affichage, son genre, sa langue et d'autres informations. Appelez [XSpeechSynthesizerSetCustomVoice](/fr-CA/reference/system/xspeechsynthesizer/functions/xspeechsynthesizersetcustomvoice) pour utiliser une autre voix installée, ou appelez [XSpeechSynthesizerSetDefaultVoice](/fr-CA/reference/system/xspeechsynthesizer/functions/xspeechsynthesizersetdefaultvoice) pour utiliser de nouveau la voix système actuelle.

Après avoir créé un handle de synthétiseur vocal et spécifié une voix, utilisez la fonction [XSpeechSynthesizerCreateStreamFromText](/fr-CA/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromtext) pour créer un flux de synthétiseur vocal et synthétiser la parole à partir de texte brut. Utilisez les fonctions [XSpeechSynthesizerGetStreamDataSize](/fr-CA/reference/system/xspeechsynthesizer/functions/xspeechsynthesizergetstreamdatasize) et [XSpeechSynthesizerGetStreamData](/fr-CA/reference/system/xspeechsynthesizer/functions/xspeechsynthesizergetstreamdata) pour obtenir les données audio de la parole synthétisée à partir du flux du synthétiseur vocal, puis utilisez la fonction [XSpeechSynthesizerCloseStreamHandle](/fr-CA/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosestreamhandle) pour fermer le flux du synthétiseur vocal une fois toutes les opérations asynchrones en attente terminées.

Utilisez la fonction [XSpeechSynthesizerCloseHandle](/fr-CA/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle) pour fermer le synthétiseur vocal et libérer les ressources système lorsque vous avez fini de l'utiliser.

Pour éviter les fuites de mémoire, appelez la fonction [XSpeechSynthesizerCloseHandle](/fr-CA/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle) pour fermer un handle de synthétiseur vocal une fois que vous avez terminé toutes les opérations qui utilisent ce handle.

L'exemple suivant montre comment synthétiser la parole à partir de texte brut à l'aide d'un synthétiseur vocal et d'une voix installée, et comment synthétiser des données audio avec un flux de synthétiseur vocal. Les fonctions **XSpeechSynthesizerCreate** et **XSpeechSynthesizerSetCustomVoice** créent une instance de synthétiseur vocal et, facultativement, lui attribuent une voix personnalisée si un ID de voix est spécifié dans *voiceId*. Ensuite, la fonction **XSpeechSynthesizerCreateStreamFromText** crée un flux de synthétiseur vocal et synthétise la parole à partir du texte brut spécifié dans *textToSpeak*. Une fois le flux créé, les fonctions **XSpeechSynthesizerGetStreamDataSize** et **XSpeechSynthesizerGetStreamData** récupèrent à partir du flux les données audio de la parole synthétisée à lire. Enfin, une fois la lecture des données audio terminée, les fonctions **XSpeechSynthesizerCloseStreamHandle** et **XSpeechSynthesizerCloseHandle** ferment le flux du synthétiseur vocal et le synthétiseur vocal.

```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;
}
```

## Configuration requise

**En-tête :** XSpeechSynthesizer.h

**Bibliothèque :** xgameruntime.lib

**Plateformes prises en charge :** Windows, consoles de la famille XBOX One et consoles XBOX Series

## Documentation conceptuelle

* [Synthèse vocale](/fr-CA/build/console-features/text-to-speech/text-to-speech)
* [Threads sensibles au temps](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## Voir aussi

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


## Related topics

- [XSpeechSynthesizerCreate](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreate.md)
- [XSpeechSynthesizerCreateStreamFromSsml](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromssml.md)
- [XSpeechSynthesizerCreateStreamFromText](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromtext.md)
