-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathElevenlabs.cs
More file actions
159 lines (140 loc) · 6.48 KB
/
Elevenlabs.cs
File metadata and controls
159 lines (140 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Text;
namespace Wendigos
{
using NAudio.Wave;
using Newtonsoft.Json;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using TimShaw.VoiceBox.Components;
using TimShaw.VoiceBox.Modding;
using TimShaw.VoiceBox.Data;
using UnityEngine;
using UnityEngine.Networking;
static class ElevenLabs
{
const string baseDir = @".\"; // Base Directory of output file
const string baseURL = "https://api.elevenlabs.io/v1/text-to-speech/"; // Base URL of HTTP request
public static string VOICE_ID;
public static float masked_volume = 0;
public static TTSManager ttsManagerComponent;
public static void Init(string api_key, string voice_id, float volumeBoost)
{
try
{
if (voice_id == "pending")
{
return;
}
if (ttsManagerComponent != null)
{
Object.DestroyImmediate(ttsManagerComponent.gameObject);
ttsManagerComponent = null;
}
Console.WriteLine($"[Wendigos TTS] Creating TTS manager object. Disregard \"Service config is null\" errors.");
// Create a new GameObject and attach a TTSManager component to it
GameObject ttsManager = new GameObject("wendigosTtsManager");
ttsManagerComponent = ttsManager.AddComponent<TTSManager>();
// Create an ElevenlabsServiceConfig object and choose a voiceId
ElevenlabsTTSServiceConfig elevenlabsConfig = ModdingTools.CreateTTSServiceConfig<ElevenlabsTTSServiceConfig>();
elevenlabsConfig.voiceId = voice_id;
elevenlabsConfig.modelID = "eleven_flash_v2_5";
if (api_key.Length == 0)
{
Console.WriteLine($"[Wendigos TTS] No Elevenlabs API key found. Attempting to load from environment variable {elevenlabsConfig.apiKeyJSONString} ...");
// Configure the TTS manager with the elevenlabs config.
// This also creates the TTS manager's TextToSpeechService via the ServiceFactory
ModdingTools.InitTTSManagerObject(ttsManagerComponent, elevenlabsConfig);
}
else
{
// Configure the TTS manager with the elevenlabs config.
// This also creates the TTS manager's TextToSpeechService via the ServiceFactory
ModdingTools.InitTTSManagerObject(ttsManagerComponent, elevenlabsConfig, ttsKey: api_key);
}
VOICE_ID = voice_id;
masked_volume = volumeBoost;
Console.WriteLine($"[Wendigos TTS] Created TTS manager.");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void ConvertMp3ToWav(string _inPath_, string _outPath_)
{
using (Mp3FileReader mp3 = new Mp3FileReader(_inPath_))
{
using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3))
{
WaveFileWriter.CreateWaveFile(_outPath_, pcm);
}
}
}
public static void IncreaseVolume(string inputPath, string outputPath, double db)
{
double linearScalingRatio = Math.Pow(10d, db / 10d);
using (WaveFileReader reader = new WaveFileReader(inputPath))
{
VolumeWaveProvider16 volumeProvider = new VolumeWaveProvider16(reader);
using (WaveFileWriter writer = new WaveFileWriter(outputPath, reader.WaveFormat))
{
while (true)
{
var frame = reader.ReadNextSampleFrame();
if (frame == null)
break;
var sample = frame[0] * (float)linearScalingRatio;
if (sample < -0.6f)
sample = -0.6f;
if (sample > 0.6f)
sample = 0.6f;
writer.WriteSample(frame[0] * (float)linearScalingRatio);
}
}
}
}
// Requests WAV file containing AI Voice saying the prompt and outputs the directory to said file
public static void RequestAudio(string prompt, string voice, string fileName, string dir, int fileNum, Action<string> onSuccess)
{
while (File.Exists(Path.Combine(dir, fileName + fileNum.ToString())))
{
fileNum++;
}
fileName = fileName + fileNum.ToString();
string fullFilePath = null;
ttsManagerComponent.GenerateSpeechFileFromText(prompt, fileName, dir, result => {
onSuccess.Invoke(dir + fileName + ".mp3");
/*
try
{
ConvertMp3ToWav(dir + fileName + ".mp3", dir + fileName + "z.wav");
File.Delete(dir + fileName + ".mp3");
IncreaseVolume(dir + fileName + "z.wav", dir + fileName + ".wav", volume_boost);
File.Delete(dir + fileName + "z.wav");
fullFilePath = dir + fileName + ".wav";
onSuccess.Invoke(fullFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
onSuccess.Invoke("");
}
*/
}, err => Debug.LogError(err));
}
public static void StreamAudio(string prompt, string voiceID, AudioStreamer audioStreamer)
{
//var modifiedConfig = (ttsManagerComponent.textToSpeechConfig as ElevenlabsTTSServiceConfig);
//modifiedConfig.voiceId = voiceID;
//ttsManagerComponent.textToSpeechConfig = modifiedConfig;
audioStreamer.StopStreaming(ttsManagerComponent.TextToSpeechService);
audioStreamer.GetComponent<AudioSource>().volume = masked_volume > 1f ? 1f : masked_volume;
Console.WriteLine((ttsManagerComponent.textToSpeechConfig as ElevenlabsTTSServiceConfig).voiceId);
ttsManagerComponent.RequestAudioAndStream(prompt, audioStreamer);
}
}
}