Spaces:
Running
Running
| const express = require("express"); | |
| const router = express.Router(); | |
| const { synthesizeAudio, streamAudio } = require("../textToSpeech/textToSpeech"); | |
| // POST /api/tts | |
| router.post("/tts", async (req, res) => { | |
| const { text, speed = 1.0, lengthScale } = req.body; | |
| if (!text || typeof text !== "string") { | |
| return res.status(400).json({ error: "Texto inválido." }); | |
| } | |
| try { | |
| // Usar lengthScale se fornecido, senão usar speed para calcular | |
| const finalLengthScale = lengthScale || (2.0 / speed); | |
| console.log(`API TTS: texto="${text.substring(0, 30)}...", speed=${speed}, lengthScale=${finalLengthScale}`); | |
| const audioBuffer = await synthesizeAudio(text, finalLengthScale); | |
| res.set({ | |
| "Content-Type": "audio/wav", | |
| "Content-Disposition": 'inline; filename="output.wav"', | |
| }); | |
| res.send(audioBuffer); | |
| } catch (err) { | |
| console.error("Erro ao gerar áudio:", err); | |
| res.status(500).json({ error: "Erro interno ao gerar áudio." }); | |
| } | |
| }); | |
| router.post("/tts-stream", (req, res) => { | |
| const { text, speed = 1.0, lengthScale } = req.body; | |
| if (!text || typeof text !== "string") { | |
| return res.status(400).json({ error: "Texto inválido." }); | |
| } | |
| const finalLengthScale = lengthScale || (2.0 / speed); | |
| streamAudio(text, finalLengthScale, res); | |
| }); | |
| module.exports = (app) => { | |
| app.use("/api", router); | |
| }; |