- Switch speakText() to call /api/text-to-speech backend (ElevenLabs) with fallback to native speechSynthesis - Upgrade ElevenLabs voice from Rachel to Adam (pNInz6obpgDQGcFmaJgB) — warmer, more professional - Upgrade model from eleven_monolingual_v1 to eleven_turbo_v2_5 for better quality and lower latency
23 lines
950 B
JavaScript
23 lines
950 B
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const axios = require('axios');
|
|
const { authMiddleware } = require('../middleware/auth');
|
|
|
|
router.post('/text-to-speech', authMiddleware, async (req, res) => {
|
|
try {
|
|
if (!process.env.ELEVENLABS_API_KEY) return res.status(400).json({ error: 'Not configured' });
|
|
const response = await axios({
|
|
method: 'POST',
|
|
url: 'https://api.elevenlabs.io/v1/text-to-speech/pNInz6obpgDQGcFmaJgB',
|
|
headers: { 'xi-api-key': process.env.ELEVENLABS_API_KEY, 'Content-Type': 'application/json' },
|
|
data: { text: req.body.text.substring(0, 5000), model_id: 'eleven_turbo_v2_5', voice_settings: { stability: 0.5, similarity_boost: 0.75 } },
|
|
responseType: 'arraybuffer'
|
|
});
|
|
res.set('Content-Type', 'audio/mpeg');
|
|
res.send(Buffer.from(response.data));
|
|
} catch (err) {
|
|
res.status(500).json({ error: 'TTS failed' });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|