-
Notifications
You must be signed in to change notification settings - Fork 0
/
tts.py
346 lines (288 loc) · 12 KB
/
tts.py
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import os
import shutil
import asyncio
import subprocess
import requests
import time
import torch
import torchaudio
import subprocess
import numpy as np
import threading
import re
import sys
# Add .\venv\Lib\site-packages\f5_tts
if not os.path.exists(os.path.join(os.path.dirname(__file__), 'F5-TTS')):
print("Cloning F5-TTS repository...")
subprocess.run(
[
"git", "clone", "--depth", "1",
"https://github.com/SWivid/F5-TTS",
"F5-TTS"
],
cwd=os.path.dirname(__file__)
)
sys.path.append(os.path.join(os.path.dirname(__file__), 'F5-TTS', 'src'))
from f5_tts.model import DiT
from f5_tts.infer.utils_infer import (
load_model,
preprocess_ref_audio_text,
infer_process,
load_vocoder,
chunk_text,
)
if not os.path.exists(os.path.join(os.path.dirname(__file__), 'models/F5TTS_Base_bigvgan')):
# Download model file
print("Downloading F5-TTS model file...")
os.makedirs(os.path.join(os.path.dirname(__file__), 'models/F5TTS_Base'), exist_ok=True)
url = 'https://huggingface.co/SWivid/F5-TTS/resolve/main/F5TTS_Base_bigvgan/model_1250000.pt?download=true'
response = requests.get(url)
with open(os.path.join(os.path.dirname(__file__), 'models/F5TTS_Base_bigvgan/model_1250000.pt'), 'wb') as f:
f.write(response.content)
from pydub.playback import play
from pydub import AudioSegment
from dotenv import load_dotenv
import edge_tts
load_dotenv()
device = "cuda" if torch.cuda.is_available() else "cpu"
class TextToSpeech:
language = 'en'
tts_class = None
voice = None
voice_ref_audio = None
voice_ref_text = None
model = None
audio_queue = asyncio.Queue() # Add queue for audio segments
is_playing = False
play_task = None
def __init__(self, model=None, voice=None):
tts_mapping = {
'deepgram': self.deepgramTTS,
'edgeTTS': self.edgeTTS,
'f5TTS': self.f5TTS
}
# Select the TTS model
if (model is not None):
self.tts_class = tts_mapping.get(model)
else:
print("Select the TTS model:")
for i, tts in enumerate(tts_mapping.keys(), start=1):
print(f"{i}. {tts}")
tts_index = int(input("Enter the number of your choice: ")) - 1
tts_type = list(tts_mapping.keys())[tts_index]
self.tts_class = tts_mapping.get(tts_type)
if self.tts_class is None:
raise ValueError(f'Invalid tts model: {tts_type}')
# Select the voice
if (voice is not None):
self.voice = voice
elif self.tts_class == self.deepgramTTS:
asyncio.run(self.select_deepgramTTS_voice())
elif self.tts_class == self.edgeTTS:
asyncio.run(self.select_edgeTTS_voice())
elif self.tts_class == self.f5TTS:
asyncio.run(self.select_f5TTS_voice())
async def select_edgeTTS_voice(self):
voices = await edge_tts.list_voices()
print("Select the voice:")
english_voices = [voice for voice in voices if voice['Locale'].startswith('en-')]
for i, voice in enumerate(english_voices, start=1):
print(f"{i}. f'{voice['FriendlyName']} ({voice['ShortName']})'")
while True:
try:
voice_index = int(input("Enter the number of your choice: ")) - 1
if voice_index < 0 or voice_index >= len(english_voices):
print("Invalid choice. Please enter a number corresponding to the list of voices.")
else:
break
except ValueError:
print("Invalid input. Please enter a number.")
self.voice = english_voices[voice_index]['ShortName']
async def select_deepgramTTS_voice(self):
voices = [
"aura-asteria-en",
"aura-luna-en",
"aura-stella-en",
"aura-athena-en",
"aura-hera-en",
"aura-orion-en",
"aura-arcas-en",
"aura-perseus-en",
"aura-angus-en",
"aura-orpheus-en",
"aura-helios-en",
"aura-zeus-en"
]
print("Select the voice:")
for i, voice in enumerate(voices, start=1):
print(f"{i}. {voice}")
while True:
try:
voice_index = int(input("Enter the number of your choice: ")) - 1
if voice_index < 0 or voice_index >= len(voices):
print("Invalid choice. Please enter a number corresponding to the list of voices.")
else:
break
except ValueError:
print("Invalid input. Please enter a number.")
self.voice = voices[voice_index]
async def select_f5TTS_voice(self):
# Select the voice
self.voice = self.select_local_voice()
# Ensure preprocess_ref_audio_text returns exactly two elements
ref_audio, ref_text = preprocess_ref_audio_text(
'./voices/' + self.voice,
"",
device=device
)
print(f"Ref audio: {ref_audio}")
print(f"Ref text: {ref_text}")
# Assign the correct values
self.voice_ref_audio = ref_audio
self.voice_ref_text = ref_text
# Load F5TTS model
model_cls = DiT
model_cfg = dict(
dim=1024, depth=22, heads=16,
ff_mult=2, text_dim=512, conv_layers=4
)
ckpt_file = str('./models/F5TTS_Base_bigvgan/model_1250000.pt')
vocab_file = os.path.join('./F5-TTS/data/Emilia_ZH_EN_pinyin/vocab.txt')
# Ensure the model is loaded correctly
self.vocoder = load_vocoder(vocoder_name="bigvgan", device=device)
self.model = load_model(model_cls, model_cfg, ckpt_file, mel_spec_type="bigvgan", vocab_file=vocab_file)
if self.model is None:
raise ValueError("Failed to load the F5TTS model.")
async def process(self, text):
await self.tts_class(text)
@staticmethod
def is_installed(lib_name: str) -> bool:
lib = shutil.which(lib_name)
return lib is not None
def select_local_voice(self):
# Get a list of .wav files in the ./voices directory
voices = [f for f in os.listdir('./voices') if f.endswith('.wav')]
if not voices:
raise ValueError("No .wav files found in the ./voices directory.")
print("Select the voice:")
for i, voice in enumerate(voices, start=1):
print(f"{i}. {voice}")
while True:
try:
voice_index = int(input("Enter the number of your choice: ")) - 1
if voice_index < 0 or voice_index >= len(voices):
print("Invalid choice. Please enter a number corresponding to the list of voices.")
else:
break
except ValueError:
print("Invalid input. Please enter a number.")
print(f"Selected voice: {voices[voice_index]}")
return voices[voice_index]
async def deepgramTTS(self, text):
if not self.is_installed("ffplay"):
raise ValueError("ffplay not found, necessary to stream audio.")
DEEPGRAM_URL = f"https://api.deepgram.com/v1/speak?model={self.voice}&encoding=linear16&sample_rate=24000"
headers = {
"Authorization": f"Token {os.getenv('DEEPGRAM_API_KEY')}",
"Content-Type": "application/json"
}
payload = {
"text": text,
}
player_command = ["ffplay", "-autoexit", "-", "-nodisp"]
player_process = subprocess.Popen(
player_command,
stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
self.player_process = player_process # Store the process for stopping
start_time = time.time() # Record the time before sending the request
with requests.post(DEEPGRAM_URL, stream=True, headers=headers, json=payload) as r:
if r.status_code != 200:
raise ValueError(f"Request to Deepgram API failed with status code {r.status_code}. \n\n{r.text}")
for chunk in r.iter_content(chunk_size=1024):
if chunk:
player_process.stdin.write(chunk)
player_process.stdin.flush()
if player_process.stdin:
player_process.stdin.close()
player_process.wait()
end_time = time.time()
elapsed_time = int((end_time - start_time) * 1000)
print(f">> TTS ({elapsed_time}ms)")
async def edgeTTS(self, text):
if not self.is_installed("ffplay"):
raise ValueError("ffplay not found, necessary to stream audio.")
start_time = time.time()
player_command = ["ffplay", "-autoexit", "-", "-nodisp"]
with self.audio_lock: # Ensure only one audio plays at a time
player_process = subprocess.Popen(
player_command,
stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
self.player_process = player_process # Store the process for stopping
communicate = edge_tts.Communicate(text, self.voice).stream()
async for chunk in communicate:
if chunk['type'] == 'audio':
player_process.stdin.write(chunk['data'])
player_process.stdin.flush()
if player_process.stdin:
player_process.stdin.close()
player_process.wait()
end_time = time.time()
elapsed_time = int((end_time - start_time) * 1000)
print(f">> TTS ({elapsed_time}ms)")
async def play_queue(self):
while True:
try:
# Wait for the next audio segment
audio_segment = await self.audio_queue.get()
self.is_playing = True
# Remove the lock since we're already handling synchronization via the queue
play(audio_segment)
self.is_playing = False
# Signal that we've finished processing this queue item
self.audio_queue.task_done()
except Exception as e:
print(f"Error in play_queue: {e}")
async def process_chunk(self, text):
"""Process a single chunk of text into audio"""
if not text.strip(): # Skip empty chunks
return
audio, final_sample_rate, _ = infer_process(
self.voice_ref_audio, self.voice_ref_text, text, self.model, self.vocoder, device=device, mel_spec_type="bigvgan",
)
# Process audio
final_wave = audio
max_val = np.max(np.abs(final_wave))
if max_val > 0:
final_wave = final_wave / max_val
final_wave = (final_wave * 32767).astype(np.int16)
sample_width = final_wave.dtype.itemsize
# Convert to AudioSegment
audio_segment = AudioSegment(
final_wave.tobytes(),
frame_rate=final_sample_rate,
sample_width=sample_width,
channels=1
)
audio_segment = audio_segment - 10 # Reduce volume
return audio_segment
async def f5TTS(self, text):
# Start the queue player if not already running
if not self.play_task or self.play_task.done():
self.play_task = asyncio.create_task(self.play_queue())
# Split the input text into batches
audio, sr = torchaudio.load(self.voice_ref_audio)
max_chars = int(len(self.voice_ref_text.encode("utf-8")) / (audio.shape[-1] / sr) * (25 - audio.shape[-1] / sr))
chunks = chunk_text(text, max_chars=max_chars)
# Process chunks sequentially to maintain order
for chunk in chunks:
if chunk:
# Process each chunk and play it immediately
audio_data = await self.process_chunk(chunk)
if audio_data:
await self.audio_queue.put(audio_data)