const express = require('express');
const cors = require('cors');
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
const axios = require('axios');
const app = express();
app.use(cors());
app.use(express.json());
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
const pythonProcess = spawn('python', ['tts.py']);
pythonProcess.stdout.on('data', (data) => console.log(`Python: ${data}`));
pythonProcess.stderr.on('data', (data) => console.log(`Python System: ${data}`));
app.get('/api/stream-audio', async (req, res) => {
const text = req.query.text || "Привет";
const speaker = req.query.speaker || "baya";
try {
await axios.post('http://127.0.0.1:5000', { text, speaker });
const filePath = path.join(__dirname, 'test.wav');
if (fs.existsSync(filePath)) {
res.set('Content-Type', 'audio/wav');
const stream = fs.createReadStream(filePath);
stream.pipe(res);
} else {
res.status(404).send('Файл не найден');
}
} catch (error) {
console.error(error);
res.status(500).send('Ошибка генерации');
}
});
app.listen(3000, () => console.log('Сервер запущен на http://localhost:3000'));