-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
180 lines (165 loc) · 5.42 KB
/
main.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
const express = require('express');
const app = express();
const fs = require('fs');
const bodyParser = require('body-parser');
const { response } = require('express');
const config = (() => {
try {
return require('./config.json');
} catch {
fs.writeFileSync('./config.json', '{"port": 1337}');
return {port: 1337};
}
})();
//holly shift
app.use(express.static(__dirname + '/public'));
app.disable('x-powered-by');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
let timeout_music;
if(config.radio) {
console.log("RADIO ON");
const __ffmpeg = require('fluent-ffmpeg');
const lame = require('@suldashi/lame');
const {Readable} = require('stream');
const __GetData = async function(path) {
return new Promise(resolve => {
__ffmpeg.ffprobe(path, (err, data) => {
resolve(err ? Buffer.from('nah') : data);
});
});
}
var song;
var songs = [];
function __randomSort() {
let oldPos = songs.indexOf(song);
let NewPos = Math.floor(Math.random() * songs.length);
while(oldPos > -1 && NewPos == oldPos)
NewPos = Math.floor(Math.random() * songs.length);
song = songs[NewPos];
return song;
}
function __listSort() {
let p = songs.indexOf(song);
if(++p >= songs.length)
p = 0;
song = songs[p];
return song;
}
var methodSort = __listSort;
var listeners = new Set();
const stream = new Readable();
stream._read = function(size) {
if (this.data.length) {
const chunk = this.data.slice(0, size);
this.data = this.data.slice(size, this.data.length);
this.push(chunk);
} else {
this.push(Buffer.from('nah')); // I no have data, because I must do streaming!
}
};
const encoder = lame.Encoder({
channels: 2,
bitDepth: 16,
sampleRate: 44100,
});
async function workerDJ() {
while(1)
{
//re-search songs
songs = [];
for(const path of fs.readdirSync('./public/music'))
{
songs.push({
path,
fdata: await __GetData('./public/music/' + path)
});
}
// player
methodSort()
for(let played = 0; played < Math.pow(songs.length, 1.2); methodSort(), ++played)
{
// const stream = fs.createReadStream('./public/music/' + song.path);
const decoder = lame.Decoder();
decoder.on('format', () => {
decoder.on('data', data => {
encoder.write(data);
});
});
let data = fs.readFileSync('./public/music/' + song.path);
song.buffer = data;
decoder.write(data);
song.started = Date.now();
console.log(song);
await (new Promise(res => timeout_music = setTimeout(res, Math.trunc((song.fdata.format.duration-1)*1000), 0)));
song.started = 0;
decoder.removeAllListeners();
delete song.buffer;
}
}
}
encoder.on('data', chunk => {
for(const stream of listeners)
stream.write(chunk);
});
app.get('/radio', async (req, res) => {
console.log(`open ${req.connection.remoteAddress}`);
response.writeHead(200, { "Content-Type": "audio/mp3", "Connection": "close", "Transfer-Encoding": "identity"});
res.on('close', () => {
console.log(`close ${req.connection.remoteAddress}`);
listeners.delete(res);
});
if(song) {
if(song.started && song.buffer)
{
const decoder = lame.Decoder();
const encoder = lame.Encoder({
channels: 2,
bitDepth: 16,
sampleRate: 44100,
});
encoder.on('data', data => {
res.write(data);
});
decoder.pipe(encoder);
decoder.write(song.buffer.slice( song.fdata.format.bit_rate/8 * (Date.now() - song.started)/1000 ));
await (new Promise(res => setTimeout(res, song.fdata.format.duration*1000-(Date.now()-song.started), 0)))
}
else if(song.started && encoder._transformState.writechunk && encoder._transformState.writechunk.length)
res.write(encoder._transformState.writechunk);
}
listeners.add(res);
});
app.get('/api/changeMethodSort', (req, res) => {
methodSort = methodSort === __listSort ? __randomSort : __listSort;
res.send('ok, dude.');
});
app.get('/api/skip', (req, res) => {
if(timeout_music)
{
let f = timeout_music._onTimeout;
timeout_music.close();
f();
res.send("ok");
} else res.send('what?');
});
workerDJ();
}
app.listen(config.port, () => { console.log('::' + config.port); })
app.get('/:v?', (req, res) => {
let stat;
console.log(req.path);
try {
stat = fs.statSync('./public'+req.path);
} catch (e){
return res.send(JSON.stringify(e, undefined, ' '));
}
if(stat.isDirectory)
res.send(
fs.readdirSync('./public' + req.path)
.map(path => `<a href="http://${req.hostname}:${config.port}${req.path + path}">${req.path + path}</a>`)
.join('<br/>')
);
else
res.sendFile('./public/' + req.path);
});