File size: 803 Bytes
3aa87fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import edge_tts
import asyncio


def get_voices():
    voices = asyncio.run(edge_tts.list_voices())
    return voices


async def _iterate_chunks(audio):
    async for chunk in audio.stream():
        if chunk["type"] == "audio":
            yield chunk["data"]


async def _async_generator_to_list(async_gen):
    result = []
    async for item in async_gen:
        result.append(item)
    return result


def generate_audio(text: str, voice: str, rate: int) -> bytes:
    sign = '+' if rate > 0 else '-'
    rate = f'{sign}{abs(rate)}%'
    audio = edge_tts.Communicate(text=text, voice=voice, rate=rate)
    chunks = asyncio.run(_async_generator_to_list(_iterate_chunks(audio)))
    buffer = io.BytesIO()

    for chunk in chunks:
        buffer.write(chunk)

    return buffer.getvalue()