-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (60 loc) · 1.96 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
var util = require('util');
var Scout = require('zetta-scout');
var exec = require('child_process').exec;
var async = require('async');
var TextToSpeech = require('./text_to_speech');
var TextToSpeechScout = module.exports = function() {
Scout.call(this);
};
util.inherits(TextToSpeechScout, Scout);
TextToSpeechScout.prototype.init = function(next) {
var self = this;
async.parallel(
{
voices: function(callback) {
exec('say -v ?', function (error, stdout, stderr) {
callback(error, self.parseAvailableVoices(stdout));
});
},
devices: function(callback){
exec('say -a ?', function (error, stdout, stderr) {
callback(error, self.parseAvailableDevices(stdout));
});
}
}, function(error, availableOptions) {
if (typeof error === 'undefined' || error === null) {
self.discover(TextToSpeech, availableOptions);
next();
} else {
console.log('exec error: ' + error);
}
}
);
};
TextToSpeechScout.prototype.parseAvailableVoices = function(stream) {
var regExp = new RegExp(/^(.*)([a-z][a-z]_[A-Z][A-Z])\s+#(.*)$/);
var keys = ['name', 'locale', 'example'];
return this.parseAvailableOptions(stream, regExp, keys);
}
TextToSpeechScout.prototype.parseAvailableDevices = function(stream) {
var regExp = new RegExp(/^\s*(\d+)(.*)$/);
var keys = ['id', 'name'];
return this.parseAvailableOptions(stream, regExp, keys);
}
TextToSpeechScout.prototype.parseAvailableOptions = function(stream, regExp, keys) {
var options = new Array();
var lines = stream.split('\n');
for (var i = 0; i < lines.length; i++) {
match = lines[i].match(regExp);
if (match !== null) {
var option = {};
for (var j=0; j < keys.length; j++) {
option[keys[j]] = match[j+1].trim();
}
option['value'] = match[1].trim();
option['text'] = match[1].trim() + ' | ' + match[2].trim();
options.push(option);
}
}
return options;
}