-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
91 lines (80 loc) · 2.59 KB
/
index.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const path = require('path');
const { spawn } = require('child_process');
const { loadRVCModel, processRVC } = require('./rvc-model');
const { loadONNXModel, processONNX } = require('./onnx-model');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
let pythonProcess = null;
// WebSocket 서버 설정
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('message', async (message) => {
console.log(`Received message => ${message}`);
const data = JSON.parse(message);
if (data.type === 'audio') {
const { audioBuffer, settings, model } = data;
let processedAudio;
if (model === 'RVC') {
processedAudio = await processRVC(audioBuffer, settings);
} else {
processedAudio = await processONNX(audioBuffer, settings);
}
ws.send(JSON.stringify({ type: 'processedAudio', data: processedAudio }));
} else if (data.type === 'startServer') {
startPythonServer();
} else if (data.type === 'stopServer') {
stopPythonServer();
}
});
ws.on('close', () => {
console.log('Client disconnected');
});
});
// Express 라우트
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.post('/upload-model', (req, res) => {
// 모델 업로드 로직 구현
res.json({ message: 'Model uploaded successfully' });
});
// Python 서버 시작
function startPythonServer() {
if (pythonProcess) {
console.log('Python server is already running');
return;
}
pythonProcess = spawn('python', ['python_server.py']);
pythonProcess.stdout.on('data', (data) => {
console.log(`Python server: ${data}`);
});
pythonProcess.stderr.on('data', (data) => {
console.error(`Python server error: ${data}`);
});
}
// Python 서버 중지
function stopPythonServer() {
if (pythonProcess) {
pythonProcess.kill();
pythonProcess = null;
console.log('Python server stopped');
}
}
// 서버 시작
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server is listening on port ${port}`);
loadRVCModel();
loadONNXModel();
});
// 프로세스 종료 시 정리
process.on('SIGINT', () => {
stopPythonServer();
process.exit();
});