xr-ai-accelerator

IXrAiTextToSpeech

The IXrAiTextToSpeech interface defines the contract for AI models that convert text into spoken audio. This interface generates audio clips from text input.

Interface Declaration

public interface IXrAiTextToSpeech

Methods

Execute

Converts text to speech and generates an audio clip asynchronously.

public Task<XrAiResult<AudioClip>> Execute(string text, Dictionary<string, string> options = null)

Parameters:

Returns:

Usage Example

// Load the model
IXrAiTextToSpeech textToSpeech = XrAiFactory.LoadTextToSpeech("OpenAI", new Dictionary<string, string>
{
    { "apiKey", "your-openai-api-key" }
});

// Convert text to speech
string textToSpeak = "Hello, welcome to the XR AI Library!";
var result = await textToSpeech.Execute(textToSpeak, new Dictionary<string, string>
{
    { "voice", "alloy" },
    { "model", "tts-1" },
    { "speed", "1.0" }
});

// Handle the result
if (result.IsSuccess)
{
    AudioClip audioClip = result.Data;
    
    // Play the audio clip
    AudioSource audioSource = GetComponent<AudioSource>();
    audioSource.clip = audioClip;
    audioSource.Play();
}
else
{
    Debug.LogError($"Text-to-speech failed: {result.ErrorMessage}");
}

Model-Specific Options

Different providers support different configuration options:

OpenAI

Audio Integration

The returned AudioClip can be used with Unity’s audio system:

// Play immediately
AudioSource.PlayClipAtPoint(audioClip, transform.position);

// Assign to AudioSource component
audioSource.clip = audioClip;
audioSource.volume = 0.8f;
audioSource.pitch = 1.0f;
audioSource.Play();

// Use with 3D spatial audio
audioSource.spatialBlend = 1.0f; // Full 3D
audioSource.rolloffMode = AudioRolloffMode.Logarithmic;

Quality Considerations

Implementation Notes

Error Handling

if (!result.IsSuccess)
{
    Debug.LogError($"TTS generation failed: {result.ErrorMessage}");
    // Handle specific error cases:
    // - Invalid API key
    // - Text too long
    // - Unsupported voice
    // - Network connectivity issues
    // - Service rate limits
}

Best Practices