File size: 2,006 Bytes
05b9f96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
import os, base64, requests
from .settings import VOICE_GATEWAY_URL, VOICE_ENGINE, VOICE_NAME
def to_prosody(tone,pacing,energy):
    return {"rate":round(0.7+pacing*0.6,2),"pitch":round(0.8+tone*0.4,2),"volume":round(0.7+energy*0.6,2)}
def synthesize(text:str, tone:float, pacing:float, energy:float, voice_name=None, engine=None)->bytes:
    payload={"engine":engine or VOICE_ENGINE,"voice":voice_name or VOICE_NAME,"text":text,
             "prosody":to_prosody(tone,pacing,energy),"require_disclosure":True,
             "jurisdiction":os.getenv("VOICE_JURISDICTION","EU")}
    url=f"{VOICE_GATEWAY_URL}/v1/tts/synthesize"
    r=requests.post(url,json=payload,timeout=60); r.raise_for_status()
    ct=r.headers.get("Content-Type","")
    if "application/json" in ct:
        data=r.json(); b64=data.get("audio_base64"); 
        if not b64: raise RuntimeError("TTS JSON missing audio_base64"); 
        return base64.b64decode(b64)
    return r.content


def synthesize_sing(text:str, voice_name:str, melody:list, tempo:int=90, emotion:str="uplifted",
                    tone:float=0.6, pacing:float=0.5, energy:float=0.6)->bytes:
    """
    Calls the Voice Gateway singing endpoint. Expects a base64 WAV or raw WAV bytes.
    """
    payload = {
        "engine": VOICE_ENGINE,
        "voice": voice_name or VOICE_NAME,
        "mode": "sing",
        "text": text,
        "melody": melody or [],
        "tempo": tempo,
        "emotion": emotion,
        "prosody": to_prosody(tone, pacing, energy),
        "require_disclosure": True,
        "jurisdiction": os.getenv("VOICE_JURISDICTION","EU")
    }
    url = f"{VOICE_GATEWAY_URL}/v1/tts/sing"
    r = requests.post(url, json=payload, timeout=60)
    r.raise_for_status()
    ct = r.headers.get("Content-Type","")
    if "application/json" in ct:
        data = r.json()
        b64 = data.get("audio_base64")
        if not b64: raise RuntimeError("Sing JSON missing audio_base64")
        return base64.b64decode(b64)
    return r.content