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

Crea un sintetizador de voz.

## Sintaxis

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

### Parámetros

*speechSynthesizer*   \_Out\_\
Tipo: XSpeechSynthesizerHandle\*

El identificador del sintetizador de voz creado.

### Valor devuelto

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

Devuelve **S\_OK** si se realiza correctamente; de lo contrario, devuelve un código de error. Para obtener una lista de códigos de error, consulte [Códigos de error](/reference/errorcodes).

## Comentarios

<Note>No es seguro llamar a esta función en un subproceso sensible al tiempo. Para obtener más información, consulte [Subprocesos sensibles al tiempo](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads).</Note>

Use esta función para crear y recibir el identificador de una nueva instancia de sintetizador de voz, que proporciona acceso a la funcionalidad de un motor de síntesis de voz instalado, o *voz*.

De forma predeterminada, una nueva instancia de sintetizador de voz usa la voz actual del sistema. Para enumerar y obtener información sobre las voces instaladas en el dispositivo actual, use la función [XSpeechSynthesizerEnumerateInstalledVoices](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerenumerateinstalledvoices) junto con la función de devolución de llamada [XSpeechSynthesizerInstalledVoicesCallback](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerinstalledvoicescallback). Para cada voz instalada, la estructura [XSpeechSynthesizerVoiceInformation](/reference/system/xspeechsynthesizer/structs/xspeechsynthesizervoiceinformation) proporciona el identificador de la voz, la descripción, el texto para mostrar, el género, el idioma y otra información. Invoque [XSpeechSynthesizerSetCustomVoice](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizersetcustomvoice) para usar otra voz instalada, o invoque [XSpeechSynthesizerSetDefaultVoice](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizersetdefaultvoice) para volver a usar la voz actual del sistema.

Después de crear un identificador de sintetizador de voz y especificar una voz, use la función [XSpeechSynthesizerCreateStreamFromText](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromtext) para crear una secuencia de sintetizador de voz y sintetizar voz a partir de texto sin formato. Use las funciones [XSpeechSynthesizerGetStreamDataSize](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizergetstreamdatasize) y [XSpeechSynthesizerGetStreamData](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizergetstreamdata) para obtener los datos de audio de la voz sintetizada desde la secuencia del sintetizador de voz y, a continuación, use la función [XSpeechSynthesizerCloseStreamHandle](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosestreamhandle) para cerrar la secuencia del sintetizador de voz una vez completadas todas las operaciones asincrónicas pendientes.

Use la función [XSpeechSynthesizerCloseHandle](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle) para cerrar el sintetizador de voz y liberar los recursos del sistema cuando haya terminado de usar el sintetizador de voz.

Para evitar pérdidas de memoria, llame a la función [XSpeechSynthesizerCloseHandle](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle) para cerrar un identificador de sintetizador de voz después de haber completado todas las operaciones que usan el identificador.

El ejemplo siguiente muestra cómo sintetizar voz a partir de texto sin formato mediante un sintetizador de voz y una voz instalada, y cómo sintetizar datos de audio con una secuencia de sintetizador de voz. Las funciones **XSpeechSynthesizerCreate** y **XSpeechSynthesizerSetCustomVoice** crean una instancia de sintetizador de voz y, opcionalmente, le asignan una voz personalizada si se especifica un identificador de voz en *voiceId*. A continuación, la función **XSpeechSynthesizerCreateStreamFromText** crea una secuencia de sintetizador de voz y sintetiza voz a partir del texto sin formato especificado en *textToSpeak*. Una vez creada la secuencia, las funciones **XSpeechSynthesizerGetStreamDataSize** y **XSpeechSynthesizerGetStreamData** recuperan los datos de audio de la voz sintetizada que se reproducirá desde la secuencia. Finalmente, una vez completada la reproducción de los datos de audio, las funciones **XSpeechSynthesizerCloseStreamHandle** y **XSpeechSynthesizerCloseHandle** cierran la secuencia del sintetizador de voz y el sintetizador de voz.

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

## Requisitos

**Encabezado:** XSpeechSynthesizer.h

**Biblioteca:** xgameruntime.lib

**Plataformas compatibles:** Windows, consolas de la familia XBOX One y consolas XBOX Series

## Documentación conceptual

* [Texto a voz](/build/console-features/text-to-speech/text-to-speech)
* [Subprocesos sensibles al tiempo](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## Consulte también

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


## Related topics

- [XSpeechSynthesizerCreateStreamFromSsml](/es/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromssml.md)
- [XSpeechSynthesizerCreateStreamFromText](/es/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromtext.md)
- [XSpeechSynthesizerCloseHandle](/es/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle.md)
- [XSpeechSynthesizer](/es/reference/system/xspeechsynthesizer/xspeechsynthesizer_members.md)
- [XSpeechSynthesizerGetStreamData](/es/reference/system/xspeechsynthesizer/functions/xspeechsynthesizergetstreamdata.md)
