Initial commit
Browse files
audio.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import numpy as np
|
3 |
+
from elevenlabs import voices, generate, set_api_key, UnauthenticatedRateLimitError
|
4 |
+
|
5 |
+
def pad_buffer(audio):
|
6 |
+
# Pad buffer to multiple of 2 bytes
|
7 |
+
buffer_size = len(audio)
|
8 |
+
element_size = np.dtype(np.int16).itemsize
|
9 |
+
if buffer_size % element_size != 0:
|
10 |
+
audio = audio + b'\0' * (element_size - (buffer_size % element_size))
|
11 |
+
return audio
|
12 |
+
|
13 |
+
def generate_voice(text, voice_name, model_name):
|
14 |
+
try:
|
15 |
+
audio = generate(
|
16 |
+
text[:250], # Limit to 250 characters
|
17 |
+
voice=voice_name,
|
18 |
+
model=model_name
|
19 |
+
)
|
20 |
+
return (44100, np.frombuffer(pad_buffer(audio), dtype=np.int16))
|
21 |
+
except UnauthenticatedRateLimitError as e:
|
22 |
+
raise gr.Error("Thanks for trying out ElevenLabs TTS! You've reached the free tier limit. Please provide an API key to continue.")
|
23 |
+
except Exception as e:
|
24 |
+
raise gr.Error(e)
|