-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
265 lines (238 loc) · 7.17 KB
/
Program.cs
File metadata and controls
265 lines (238 loc) · 7.17 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using LLama;
using LLama.Common;
using TorchSharp;
using static TorchSharp.torch;
using NAudio.Wave;
using System.Diagnostics;
using LLama.Sampling;
public class Tara
{
StatelessExecutor executor;
Snac model;
Device snac_device;
public Tara()
{
// 1bit
string modelPath = @"C:\Users\Jayakuttan\Downloads\orpheus-3b-0.1-ft-UD-IQ1_S.gguf";
// 2bit
//string modelPath = @"C:\Users\Jayakuttan\Downloads\orpheus-3b-0.1-ft.Q2_K.gguf";
// 4bit
//string modelPath = @"E:\ai\orpheus-tts\orpheus_gguf\orpheus-3b-0.1-ft-q4_k_m.gguf";
ModelParams modelParams = new(modelPath)
{
ContextSize = 10240,
GpuLayerCount = 30,
Threads = 16,
BatchSize = 1024,
};
executor = new(LLamaWeights.LoadFromFile(modelParams), modelParams);
model = Snac.from_pretrained(@"snac\24khz\config.json", @"snac\24khz\pytorch_model_unnormed.bin");
snac_device = device("cuda:0");
model = model.to(snac_device);
audio_buffered_stream = new(wave_format);
player.Init(audio_buffered_stream);
}
static string makeOrpheusPrompt(string text)
{
string voice = "tara";
string start = "<|audio|>";
string end = "<|eot_id|>";
string content = $"{voice}: {text}";
return $"{start}{content}{end}";
}
byte[]? convert_to_audio(int[] multiframe)
{
int[] frames;
if (multiframe.Length < 7) { Console.WriteLine("multiframe return"); return null; }
Tensor codes_0 = tensor(Array.Empty<int>(), device: snac_device, dtype: int32);
Tensor codes_1 = tensor(Array.Empty<int>(), device: snac_device, dtype: int32);
Tensor codes_2 = tensor(Array.Empty<int>(), device: snac_device, dtype: int32);
int num_frames = multiframe.Length / 7;
frames = multiframe.Take(num_frames * 7).ToArray();
for (int j = 0; j < num_frames; j++)
{
int i = 7 * j;
if (codes_0.shape[0] == 0)
{
codes_0 = tensor([frames[i]], device: snac_device, dtype: int32);
}
else
{
codes_0 = cat([codes_0, tensor([frames[i]], device: snac_device, dtype: int32)]);
}
if (codes_1.shape[0] == 0)
{
codes_1 = tensor([frames[i + 1]], device: snac_device, dtype: int32);
codes_1 = cat([codes_1, tensor([frames[i + 4]], device: snac_device, dtype: int32)]);
}
else
{
codes_1 = cat([codes_1, tensor([frames[i + 1]], device: snac_device, dtype: int32)]);
codes_1 = cat([codes_1, tensor([frames[i + 4]], device: snac_device, dtype: int32)]);
}
if (codes_2.shape[0] == 0)
{
codes_2 = tensor([frames[i + 2]], device: snac_device, dtype: int32);
codes_2 = cat([codes_2, tensor([frames[i + 3]], device: snac_device, dtype: int32)]);
codes_2 = cat([codes_2, tensor([frames[i + 5]], device: snac_device, dtype: int32)]);
codes_2 = cat([codes_2, tensor([frames[i + 6]], device: snac_device, dtype: int32)]);
}
else
{
codes_2 = cat([codes_2, tensor([frames[i + 2]], device: snac_device, dtype: int32)]);
codes_2 = cat([codes_2, tensor([frames[i + 3]], device: snac_device, dtype: int32)]);
codes_2 = cat([codes_2, tensor([frames[i + 5]], device: snac_device, dtype: int32)]);
codes_2 = cat([codes_2, tensor([frames[i + 6]], device: snac_device, dtype: int32)]);
}
}
List<Tensor> codes = [codes_0.unsqueeze(0), codes_1.unsqueeze(0), codes_2.unsqueeze(0)];
if (
any(le(codes[0], 0)).ToBoolean() || any(ge(codes[0], 4096)).ToBoolean() ||
any(le(codes[1], 0)).ToBoolean() || any(ge(codes[1], 4096)).ToBoolean() ||
any(le(codes[2], 0)).ToBoolean() || any(ge(codes[2], 4096)).ToBoolean()
) { Console.WriteLine("code over return"); return null; }
short[] audio_int16;
using (NewDisposeScope())
using (_ = inference_mode())
{
Tensor audio_hat = model.decode(codes);
//Console.WriteLine(audio_hat.print());
Tensor audio_slice = audio_hat.narrow(dim: -1, start: 2048, length: 2048);
audio_int16 = (audio_slice * 32767).to(int16).data<short>().ToArray();
}
//Console.WriteLine($"min: {audio_int16.Min()}, max: {audio_int16.Max()}");
byte[] audio_bytes = new byte[audio_int16.Length * sizeof(short)];
Buffer.BlockCopy(audio_int16, 0, audio_bytes, 0, audio_bytes.Length);
return audio_bytes;
}
int turn_token_into_ids(int token, int count)
{
return token - 10 - ((count % 7) * 4096);
}
string TOKEN_PREFIX = "<custom_token_";
public int parse_piece_to_token(string piece)
{
if (!piece.Contains(TOKEN_PREFIX)) { return 0; }
string token_str = piece.Replace(TOKEN_PREFIX, "");
int token = Convert.ToInt32(token_str.Replace(">", ""));
return token;
}
void MeasureTime(Action func, string? funcName = null)
{
Stopwatch sw = new();
sw.Start();
func();
sw.Stop();
Console.WriteLine($"{funcName ?? "unknown"}() took {sw.ElapsedMilliseconds}ms");
}
// <summary>
// streaming infra
// </summary>
WaveFormat wave_format = new(24000, 1);
BufferedWaveProvider audio_buffered_stream;
MemoryStream memory_stream;
WaveOutEvent player = new();
WaveFileWriter wav_writer;
public bool streaming = true;
public async Task speech_gen(string text)
{
InferenceParams inferPrams = new()
{
MaxTokens = -1,
SamplingPipeline = new DefaultSamplingPipeline()
{
RepeatPenalty = 1.1f
},
};
string prompt = makeOrpheusPrompt(text);
List<int> ids = new();
memory_stream = new(8192);
wav_writer = new(memory_stream, wave_format);
IAsyncEnumerable<string> reply = executor.InferAsync(prompt, inferenceParams: inferPrams);
await foreach (string piece in reply)
{
int id = turn_token_into_ids(parse_piece_to_token(piece), ids.Count);
if (id > 0)
{
ids.Add(id);
}
if (ids.Count % 7 == 0 && ids.Count > 27)
{
MeasureTime(() =>
{
Task.Run(async () =>
{
byte[]? bytes = convert_to_audio(ids.TakeLast(28).ToArray());
//if (bytes == null) continue;
if (streaming)
{
audio_buffered_stream.AddSamples(bytes, 0, bytes.Length);
}
else
{
await wav_writer.WriteAsync(bytes, 0, bytes.Length);
}
});
}, "decoder");
}
}
wav_writer.Flush();
Console.WriteLine("wav_writing finished");
}
void TimerCallback(object? sender, EventArgs e)
{
double buffered = audio_buffered_stream.BufferedDuration.TotalMilliseconds;
if (buffered < 10)
{
player.Pause();
//Console.WriteLine($"Thread Sleeping");
Thread.Sleep(10);
player.Play();
}
}
Stopwatch sw;
public async Task talk(string text, string output_file = @"outputs\tara.wav")
{
sw = new();
sw.Start();
System.Timers.Timer timer = new(10);
if (streaming)
{
Task _t = Task.Run(() =>
{
timer.Elapsed += TimerCallback;
timer.Start();
player.Play();
});
}
await speech_gen(text);
timer.Stop();
sw.Stop();
if (!streaming)
{
FileStream wav_file = File.OpenWrite(output_file);
memory_stream.WriteTo(wav_file);
wav_file.Close();
long generation_time = sw.ElapsedMilliseconds;
double audio_duration = new MediaFoundationReader(output_file).TotalTime.TotalMilliseconds;
Console.WriteLine($"Finished, generation_time: {generation_time} ms, rtf: {audio_duration / generation_time}");
}
}
public static async Task Main()
{
Tara tara = new();
while (true)
{
Console.Write("Enter text: ");
string? text = Console.ReadLine();
if (text != ":q")
{
await tara.talk(text);
}
else
{
break;
}
}
}
}