hit-cxf
2026-05-13 bf683066ff6e7d75ab2c6dd896ad675ba31f52b5
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
import os
from typing import Optional
 
 
def speed_to_rate(speed: Optional[float]) -> str:
    value = 1.0 if speed is None else float(speed)
    percent = int(round((value - 1.0) * 100))
    sign = "+" if percent >= 0 else ""
    return f"{sign}{percent}%"
 
 
async def generate_edge_tts(
    text: str,
    output_path: str,
    voice: str = "zh-CN-YunjianNeural",
    speed: float = 1.2,
) -> str:
    try:
        import edge_tts
    except ImportError as exc:
        raise RuntimeError("edge-tts is required for audio generation. Install backend dependencies again.") from exc
 
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    communicate = edge_tts.Communicate(text=text, voice=voice, rate=speed_to_rate(speed))
    await communicate.save(output_path)
    return output_path