|
|
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): |
|
|
|
|
|
path = _beep_wav() |
|
|
return {"audio_path": path} |
|
|
|
|
|
@app.get("/file/{name}") |
|
|
def file(name: str): |
|
|
return FileResponse(f"/tmp/{name}") |