import numpy as np from elevenlabs import save from elevenlabs.client import ElevenLabs client = ElevenLabs() class ElevenLabsHelper: @staticmethod def pad_buffer(audio): # Pad buffer to multiple of 2 bytes buffer_size = len(audio) element_size = np.dtype(np.int16).itemsize if buffer_size % element_size != 0: audio = audio + b"\0" * (element_size - (buffer_size % element_size)) return audio @staticmethod def generate_voice(text, voice_name, audio_output_path): try: audio = client.generate( text=text[:300], voice=voice_name, model="eleven_multilingual_v2" # Limit to 300 characters ) save(audio, audio_output_path) except Exception as e: raise RuntimeError(e) @staticmethod def get_voices(): return client.voices.get_all() @staticmethod def select_voices(voices, labels): result = [] for voice in voices: has_required_labels = True for key, value in labels.items(): has_required_labels = has_required_labels and key in voice.labels.keys() and voice.labels[key] == value if has_required_labels: result.append(voice.name) return result