-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
308 lines (262 loc) · 8.45 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const fs = require('fs');
const path = require('path');
const { Worker } = require('worker_threads');
const browser = 'firefox'; // ADD .ENV FILE FOR USER BROWSER, OR AUTODETECT
// Create a Timer worker for precise timing
const timerWorker = new Worker(`
const { parentPort } = require('worker_threads');
function preciseTick() {
parentPort.postMessage({ type: 'tick', time: process.hrtime.bigint() });
setTimeout(preciseTick, 50); // 20 ticks per second
}
preciseTick();
`, { eval: true });
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
// Store client connections and their states
const clients = new Map();
let globalStartTime = process.hrtime.bigint();
let isPlaying = false;
let currentTrackTime = 0;
let currentSong = null;
// Song library management
const SONGS_DIR = path.join(__dirname, 'songs');
const songLibrary = new Map();
// Create songs directory if it doesn't exist
if (!fs.existsSync(SONGS_DIR)) {
fs.mkdirSync(SONGS_DIR);
}
// Load song library
function loadSongLibrary() {
const files = fs.readdirSync(SONGS_DIR);
songLibrary.clear();
files.forEach(file => {
if (file.endsWith('.mp3')) {
const filePath = path.join(SONGS_DIR, file);
const stats = fs.statSync(filePath);
const id = file.replace('.mp3', '');
try {
// Try to load metadata from companion JSON file
const metadataPath = path.join(SONGS_DIR, `${id}.json`);
let metadata = {};
if (fs.existsSync(metadataPath)) {
metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
}
songLibrary.set(id, {
id,
title: metadata.title || file.replace('.mp3', ''),
artist: metadata.artist || 'Unknown',
duration: metadata.duration || 0,
filename: file,
size: stats.size
});
} catch (err) {
console.error(`Error loading metadata for ${file}:`, err);
}
}
});
}
// Initial library load
loadSongLibrary();
app.use(express.static('public'));
app.use(express.json());
// API endpoints for song management
app.get('/api/songs', (res) => {
const songs = Array.from(songLibrary.values());
res.json(songs);
});
// Stream audio file
app.get('/api/songs/:id/stream', (req, res) => {
const song = songLibrary.get(req.params.id);
if (!song) {
return res.status(404).json({ error: 'Song not found' });
}
const audioPath = path.join(SONGS_DIR, song.filename);
const stat = fs.statSync(audioPath);
res.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': stat.size
});
const stream = fs.createReadStream(audioPath);
stream.pipe(res);
});
app.post('/api/download', async (req, res) => {
const { url } = req.body;
const audioPath = path.join(SONGS_DIR);
// Check if url is a spotify link
if (url.includes('spotify.com')) {
console.log('Using spotDL for Spotify download');
const spotDL = path.join(__dirname, 'spotdl-4.2.11-win32.exe');
const spotDLProcess = require('child_process').spawn(spotDL, [url, '--bitrate', '192k', '--output', audioPath]);
spotDLProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
spotDLProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
spotDLProcess.on('close', (code) => {
console.log(`spotDL process exited with code ${code}`);
});
res.json({ message: 'Spotify Download started' });
} else {
// Use yt-dlp for other links
console.log('Using yt-dlp for download');
const ytDlp = path.join(__dirname, 'yt-dlp_x86.exe');
const ytDlpProcess = require('child_process').spawn(ytDlp, ['--extract-audio', '--audio-format', 'mp3', '--audio-quality', '0', '--cookies-from-browser', browser, '--output', path.join(audioPath, '%(title)s.%(ext)s'), url]);
ytDlpProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ytDlpProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ytDlpProcess.on('close', (code) => {
console.log(`yt-dlp process exited with code ${code}`);
});
res.json({ message: 'yt-dlp Download started' });
}
});
// Handle precise timing updates from worker
timerWorker.on('message', ({ type, time }) => {
if (type === 'tick' && isPlaying) {
const elapsed = Number(time - globalStartTime) / 1e9; // Convert to seconds
currentTrackTime = elapsed;
for (const [client, { latency }] of clients.entries()) {
if (client.readyState === WebSocket.OPEN) {
const clientMessage = JSON.stringify({
type: 'sync',
serverTime: Date.now(),
trackTime: currentTrackTime,
isPlaying,
currentSong,
latency
});
setImmediate(() => {
try {
client.send(clientMessage);
} catch (err) {
console.error('Failed to send to client:', err);
}
});
}
}
}
});
wss.on('connection', (ws) => {
clients.set(ws, {
latency: 0,
lastPing: Date.now()
});
ws.send(JSON.stringify({
type: 'init',
serverTime: Date.now(),
trackTime: currentTrackTime,
isPlaying,
currentSong,
songs: Array.from(songLibrary.values())
}));
measureLatency(ws);
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
switch (data.type) {
case 'play':
if (!isPlaying) {
isPlaying = true;
globalStartTime = process.hrtime.bigint() - BigInt(Math.floor(data.time * 1e9));
broadcastState();
}
break;
case 'pause':
if (isPlaying) {
isPlaying = false;
currentTrackTime = data.time;
broadcastState();
}
break;
case 'seek':
currentTrackTime = data.time;
if (isPlaying) {
globalStartTime = process.hrtime.bigint() - BigInt(Math.floor(data.time * 1e9));
}
broadcastState();
break;
case 'select_song':
const song = songLibrary.get(data.songId);
if (song) {
currentSong = song;
currentTrackTime = 0;
isPlaying = false;
broadcastState();
}
break;
case 'pong':
const clientState = clients.get(ws);
if (clientState) {
clientState.latency = (Date.now() - clientState.lastPing) / 2;
}
break;
}
} catch (err) {
console.error('Error processing message:', err);
}
});
ws.on('close', () => {
clients.delete(ws);
});
});
function broadcastState() {
const message = JSON.stringify({
type: 'state',
serverTime: Date.now(),
trackTime: currentTrackTime,
isPlaying,
currentSong
});
for (const [client, { latency }] of clients.entries()) {
if (client.readyState === WebSocket.OPEN) {
setImmediate(() => {
try {
client.send(message);
} catch (err) {
console.error('Failed to broadcast to client:', err);
}
});
}
}
}
function measureLatency(ws) {
const pingInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
const clientState = clients.get(ws);
if (clientState) {
clientState.lastPing = Date.now();
ws.send(JSON.stringify({ type: 'ping' }));
}
} else {
clearInterval(pingInterval);
}
}, 5000);
}
// Watch for changes in the songs directory
fs.watch(SONGS_DIR, (eventType, filename) => {
if (filename && (filename.endsWith('.mp3') || filename.endsWith('.json'))) {
console.log('Song library change detected, reloading...');
loadSongLibrary();
// Notify all clients of the updated song list
const songs = Array.from(songLibrary.values());
const message = JSON.stringify({ type: 'library_update', songs });
for (const [client] of clients.entries()) {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
}
}
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});