File size: 917 Bytes
3a99d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io, math, wave, uuid
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.responses import FileResponse

app = FastAPI(title="TTS Space (stub)")

class SpeakIn(BaseModel):
    text: str

def _beep_wav(duration_s=0.4, sr=16000, freq=440):
    n = int(duration_s*sr)
    buf = bytearray()
    for i in range(n):
        val = int(32767*0.2*math.sin(2*math.pi*freq*i/sr))
        buf += val.to_bytes(2, byteorder="little", signed=True)
    path = f"/tmp/{uuid.uuid4().hex}.wav"
    with wave.open(path, "wb") as w:
        w.setnchannels(1)
        w.setsampwidth(2)
        w.setframerate(sr)
        w.writeframes(buf)
    return path

@app.post("/speak")
def speak(inp: SpeakIn):
    # Stub: ignore text, return a short beep so Brain can play audio.
    path = _beep_wav()
    return {"audio_path": path}

@app.get("/file/{name}")
def file(name: str):
    return FileResponse(f"/tmp/{name}")