File size: 1,310 Bytes
ec17e66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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