-
Notifications
You must be signed in to change notification settings - Fork 1
/
soundboard.js
154 lines (126 loc) · 4.32 KB
/
soundboard.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
var async = require('async')
var exec = require('child_process').exec;
var fs = require('fs');
var path = require('path');
var walk = require('walk');
var watch = require('node-watch');
var lwip = require('lwip');
var schedule = require('node-schedule');
var configuration = require('./configuration');
var imageTypes = [".jpg", ".gif", ".png"];
// Set up express and io
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app).listen(configuration.listenPort),
io = require('socket.io').listen(server);
String.prototype.endsWith = function (suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
function getSoundFileNames(done) {
var files = [];
var walker = walk.walk(configuration.dataDir, {
followLinks: false
});
walker.on('file', function (root, stat, next) {
if (stat.name.endsWith(".wav")) {
files.push(stat.name);
}
next();
});
walker.on('end', function () {
done(files);
});
}
function getFileFullData(file, done) {
var imgBasePath = configuration.dataDir;
var imgBaseName = file.replace(/\.[^/.]+$/, "");
var potentialImageFiles = imageTypes.map(function (type) {
return imgBaseName + type;
});
async.filter(
potentialImageFiles,
function (potentialFile, callback) {
fs.exists(path.join(imgBasePath, potentialFile), callback);
},
function (existingImageFiles) {
result = {
name: file
};
result.imgName = existingImageFiles[0] || '';
done(null, result);
}
)
}
function playFile(file) {
var command = path.join(__dirname, '/scripts/play.sh') + ' ' + configuration.dataDir + "/" + file;
exec(command)
}
function getFilesFullData(callback) {
getSoundFileNames(function (filesNames) {
async.map(
filesNames,
getFileFullData,
callback);
});
}
var oneDay = 86400000;
app.get('/play/*', function (req, res) {
var soundfile = path.basename(req.path);
playFile(soundfile);
res.send("Playing " + soundfile);
});
app.use('/data', express.static(configuration.dataDir, { maxAge: oneDay }));
app.use('/', express.static(path.join(__dirname, './www')));
var rescaled = [];
app.get('/rescaled/*', function (req, res) {
var soundfile = path.basename(req.path);
console.log(soundfile);
if (soundfile in rescaled) {
res.send(rescaled[soundfile]);
return;
}
lwip.open(path.join(configuration.dataDir, soundfile), function (err, image) {
image.batch()
.cover(100, 100)
.toBuffer("jpg", {quality: 80}, function (err, buffer) {
rescaled[soundfile] = buffer;
res.send(buffer);
});
});
});
io.on('connection', function (socket) {
socket.on('playBroadcast', function (soundfile) {
console.log("Broadcasting " + soundfile + " to " + io.engine.clientsCount + " clients");
// Broadcast play command to clients
io.sockets.emit('play', soundfile);
// Play on server as well
playFile(soundfile);
});
socket.on('playRemote', function (soundfile) {
console.log("Playing remote " + soundfile);
playFile(soundfile);
});
socket.on('getFiles', function () {
console.log("Requesting files!");
getFilesFullData(function (error, files) { socket.emit('files', files); });
});
socket.on('getTitle', function () {
console.log("Requesting title!");
getSoundFileNames(function (files) { socket.emit('title', configuration.pageTitle); });
});
socket.on('pointer', function (movement) {
console.log("Mouse movement " + movement.x + " " + movement.y);
io.sockets.emit('pointer', movement);
});
});
watch(configuration.dataDir, function (filename) {
if (filename.indexOf(".stats", this.length - ".stats".length) === -1) {
console.log('Data dir updated, pushing sound files!');
getFilesFullData(function (error, files) { io.emit('files', files); });
}
});
var j = schedule.scheduleJob('0 5 8 * * 0-5', function() {
playFile("bandcamp.wav");
});
console.log('Soundboard is starting on port http://localhost:' + configuration.listenPort + '...');