File size: 1,372 Bytes
af48578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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);
};