DEV Community

SupermanSpace
SupermanSpace

Posted on • Edited on

4

Elevenlabs text to speech unity

Get Voice ID

using System;
using System.Net.Http;
using System.Threading.Tasks;
using UnityEngine;
using Newtonsoft.Json.Linq; // Make sure you add the Newtonsoft.Json package via Unity Package Manager

public class VoiceFetcher : MonoBehaviour
{
    private const string XiApiKey = "<xi-api-key>"; // Your API key

    // URL for the API endpoint
    private static readonly string Url = "https://api.elevenlabs.io/v1/voices";

    // Start is called before the first frame update
    private async void Start()
    {
        await FetchAndPrintVoices();
    }

    private static async Task FetchAndPrintVoices()
    {
        using (var client = new HttpClient())
        {
            // Set up headers for the API request, including the API key for authentication
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            client.DefaultRequestHeaders.Add("xi-api-key", XiApiKey);
            client.DefaultRequestHeaders.Add("Content-Type", "application/json");

            try
            {
                // Send GET request to the API endpoint
                var response = await client.GetAsync(Url);

                // Check if the request was successful
                if (response.IsSuccessStatusCode)
                {
                    // Read and parse the JSON response
                    var jsonResponse = await response.Content.ReadAsStringAsync();
                    var data = JObject.Parse(jsonResponse);

                    // Loop through each voice and print the name and voice_id
                    foreach (var voice in data["voices"])
                    {
                        string name = voice["name"].ToString();
                        string voiceId = voice["voice_id"].ToString();
                        Debug.Log($"{name}; {voiceId}");
                    }
                }
                else
                {
                    // Print error message if the request was not successful
                    Debug.LogError($"Error fetching voices: {await response.Content.ReadAsStringAsync()}");
                }
            }
            catch (Exception e)
            {
                // Print any exceptions that occur
                Debug.LogError($"Exception occurred: {e.Message}");
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Text to speech

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using UnityEngine;

public class TextToSpeech : MonoBehaviour
{
    // Constants for the script
    private const int ChunkSize = 1024; // Size of chunks to read/write at a time
    private const string XiApiKey = "<xi-api-key>"; // Your API key for authentication
    private const string VoiceId = "<voice-id>"; // ID of the voice model to use
    private const string TextToSpeak = "<text>"; // Text you want to convert to speech
    private const string OutputPath = "output.mp3"; // Path to save the output audio file

    // URL for the Text-to-Speech API request
    private static readonly string TtsUrl = $"https://api.elevenlabs.io/v1/text-to-speech/{VoiceId}/stream";

    // Start is called before the first frame update
    private async void Start()
    {
        await FetchAndSaveAudio();
    }

    private static async Task FetchAndSaveAudio()
    {
        using (var client = new HttpClient())
        {
            // Set up headers for the API request, including the API key for authentication
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            client.DefaultRequestHeaders.Add("xi-api-key", XiApiKey);

            // Set up the data payload for the API request, including the text and voice settings
            var data = new
            {
                text = TextToSpeak,
                model_id = "eleven_multilingual_v2",
                voice_settings = new
                {
                    stability = 0.5,
                    similarity_boost = 0.8,
                    style = 0.0,
                    use_speaker_boost = true
                }
            };

            // Serialize the data payload to JSON
            var content = new StringContent(JsonUtility.ToJson(data), System.Text.Encoding.UTF8, "application/json");

            // Make the POST request to the TTS API with headers and data, enabling streaming response
            using (var response = await client.PostAsync(TtsUrl, content, HttpCompletionOption.ResponseHeadersRead))
            {
                if (response.IsSuccessStatusCode)
                {
                    // Open the output file in write-binary mode
                    using (var fileStream = new FileStream(OutputPath, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        // Read the response in chunks and write to the file
                        using (var responseStream = await response.Content.ReadAsStreamAsync())
                        {
                            var buffer = new byte[ChunkSize];
                            int bytesRead;
                            while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
                            {
                                fileStream.Write(buffer, 0, bytesRead);
                            }
                        }
                    }
                    // Inform the user of success
                    Debug.Log("Audio stream saved successfully.");
                }
                else
                {
                    // Print the error message if the request was not successful
                    Debug.LogError(await response.Content.ReadAsStringAsync());
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This is a affiliate link, if you like you can contribute for free https://try.elevenlabs.io/7okdawkpiqom

If you want you can sponsor our writing partners:
https://buymeacoffee.com/clubwritter

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay