-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
179 lines (178 loc) · 6.09 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
// dependencies
var mplayer = require('mplayer'),
restify = require('restify'),
fs = require('fs'),
_ = require('underscore'),
config = require('config'),
dir = require('node-dir'),
changeCase = require('change-case'),
httpreq = require('httpreq');
// the soundbeard object
var soundbeard = {
name: 'soundbeard v1',
player: null,
server: null,
snippet_list: null,
init: function() {
this.checkConfig();
this.player = new mplayer();
this.player.volume(100);
var that = this;
this.server = restify.createServer({
name: that.name
});
this.server.use(restify.bodyParser());
this.bindRoutes();
this.launchServer();
},
checkConfig: function() {
var keys = ['ip', 'port', 'sounds', 'allowedFormats'];
_.each(keys, function(key) {
if (!config.has(key)) {
console.log('WARNING: Config property "'+key+'" does not exists. Exiting...');
process.exit(1);
}
});
},
bindRoutes: function() {
var that = this;
this.server.get('/play/:folder/:snippet', that.play);
this.server.get('/play/:snippet', that.play);
this.server.get('/stop', that.stop);
this.server.post('/say', that.say);
this.server.get('/list', that.snippets);
this.server.get(/.*/, restify.serveStatic({
directory: 'sites',
default: 'index.html'
}));
},
launchServer: function() {
var that = this;
this.server.listen(config.get('port'), config.get('ip'), function() {
console.log('%s listening at %s', that.server.name, that.server.url);
});
},
play: function(req, res, next) {
var path = config.get('sounds') + (_.has(req.params, 'folder') ? req.params.folder + '/' : '') + req.params.snippet;
fs.exists(path, function(exists) {
if (exists) {
soundbeard.player.stop();
soundbeard.player.openFile(path);
soundbeard.player.play();
res.send({
playing: req.params.snippet
});
} else {
res.status(404);
res.send({
error: 'could not find soundfile: ' + req.params.snippet
});
}
})
},
stop: function(req, res) {
soundbeard.player.stop();
res.send();
},
say: function(req, res) {
soundbeard.speech(req, res);
},
speech: function(req, res) {
if (undefined == req.params.text) {
res.status(400);
res.send({
error: 'no text is given'
});
}
soundbeard.speakToGoogle(req.params.text, function() {
res.send({
speaking: req.params.text
});
});
},
snippets: function(req, res) {
if(soundbeard.snippet_list == null) {
var path = config.get('sounds');
dir.paths(path, function(err, paths) {
if (err) throw err;
var filteredFiles = [];
var list = {};
var files = fs.readdirSync(path);
_.each(files, function(file) {
if (soundbeard.isSoundFile(file)) {
filteredFiles.push(file);
}
});
_.each(paths.dirs, function(subdir) {
var subdirName = subdir.split('/').pop();
var files = fs.readdirSync(subdir);
_.each(files, function(file) {
if (soundbeard.isSoundFile(file)) {
filteredFiles.push(subdirName + '/' + file);
}
});
});
filteredFiles.sort();
_.each(filteredFiles, function(file) {
list[soundbeard.getReadableName(file)] = soundbeard.buildSoundHref(file);
});
soundbeard.snippet_list = list;
res.send(list);
});
} else {
res.send(soundbeard.snippet_list);
}
},
getReadableName: function(file) {
var pathSplit = file.split('/');
var label = '';
var fileName = file;
if (pathSplit.length > 1) {
var label = pathSplit[0] + '/';
var fileName = pathSplit[1];
}
ext = file.split('.').pop();
fileName = changeCase.titleCase(fileName.replace('.' + ext, ''));
return label + fileName
},
buildSoundHref: function(file) {
return 'http://' + config.get('ip') + ':' + config.get('port') + '/play/' + file;
},
isSoundFile: function(file) {
if (file[0] == '.' || file == '.' || file == '..') {
return false;
}
var extension = file.split('.').pop();
return (-1 !== _.indexOf(config.get('allowedFormats'), extension));
},
speakToGoogle: function(text, callback) {
var that = this;
var savePath = __dirname + '/'+this.getRandomString()+'.mp3';
var lang = config.has('tts_lang') ? config.get('tts_lang') : 'de';
var url = 'http://translate.google.com/translate_tts?tl='+lang+'&q='+text;
httpreq.get(url, {
binary: true,
headers: {
// this is a stupid fix for the picky Google TTS API
"User-Agent": "Chrome/43.0.2357.130 Safari/537.36"
}
}, function(err, res) {
fs.writeFile(savePath, res.body, function(fileErr) {
console.log(savePath)
that.player.openFile(savePath);
that.player.play();
setTimeout(function() {
fs.unlink(savePath);
}, 10000);
if (typeof(callback) == 'function') {
callback();
}
});
});
},
getRandomString: function() {
return 'snippet_'+Math.random().toString(36).substring(7);
}
};
// startup
soundbeard.init();