Advertisement
apieceoffruit

ElevenLabs

Mar 12th, 2023 (edited)
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Http;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Newtonsoft.Json;
  8. using UnityEngine;
  9.  
  10. namespace JasonStorey
  11. {
  12.     public class ElevenLabs
  13.     {
  14.         readonly string _apiKey;
  15.  
  16.         HttpClient _client;
  17.  
  18.         const string DEFAULT_KEY = "YOUR_KEY";
  19.         public ElevenLabs() : this(DEFAULT_KEY) { }
  20.         public ElevenLabs(string apiKey)
  21.         {
  22.             _apiKey = apiKey;
  23.             _client = new HttpClient();
  24.             _client.DefaultRequestHeaders.Add("xi-api-key",_apiKey);
  25.             _client.DefaultRequestHeaders.Add("x-api-key",_apiKey);
  26.            
  27.         }
  28.  
  29.         string ToMp3FileSave(HttpWebRequest req,string path)
  30.         {
  31.             HttpWebResponse response = (HttpWebResponse)req.GetResponse();
  32.             // Get the stream associated with the response.
  33.             Stream receiveStream = response.GetResponseStream();
  34.             byte[] buffer = new byte[32768];
  35.             using (FileStream fileStream = File.Create(path))
  36.             {
  37.                 while (true)
  38.                 {
  39.                     int read = receiveStream.Read(buffer, 0, buffer.Length);
  40.                     if (read <= 0)
  41.                         break;
  42.                     fileStream.Write(buffer, 0, read);
  43.                 }
  44.             }
  45.             return path;
  46.         }
  47.  
  48.        
  49.        
  50.         public async Task SaveToFile(string message,string path,Action<string> completed)
  51.         {
  52.             HttpResponseMessage result = (HttpResponseMessage)await Say(message,VOICE_AMY);
  53.  
  54.             var stream = await result.Content.ReadAsStreamAsync();
  55.             byte[] buffer = new byte[32768];
  56.             await using var fileStream = File.Create(path);
  57.             while (true)
  58.             {
  59.                 int read = await stream.ReadAsync(buffer, 0, buffer.Length);
  60.                 if (read <= 0)
  61.                     break;
  62.                 fileStream.Write(buffer, 0, read);
  63.             }
  64.             completed?.Invoke(path);
  65.         }
  66.  
  67.         public async Task<object> Say(string message, string voice)
  68.         {
  69.             var json = JsonConvert.SerializeObject(TextRequest.Create(message));
  70.             Debug.Log(json);
  71.             var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
  72.             return
  73.                 await _client.PostAsync("https://api.elevenlabs.io/v1/text-to-speech/" +
  74.                                         voice, httpContent);
  75.            
  76.         }
  77.  
  78.         const string VOICE_AMY = "NUAlGdat74Sbul1N99HF";
  79.     }
  80.  
  81.     [Serializable]
  82.     public class TextRequest
  83.     {
  84.         public static TextRequest Create(string text) => new() { text = text, voice_settings = new VoiceSettings()};
  85.        
  86.         public string text;
  87.         public VoiceSettings voice_settings;
  88.        
  89.     }
  90.  
  91.     [Serializable]
  92.     public class VoiceSettings
  93.     {
  94.         public float similarity_boost = 0.7f;
  95.         public float stability = 0.7f;
  96.     }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement