-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_to_speech.js
62 lines (57 loc) · 1.84 KB
/
text_to_speech.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
var util = require('util');
var Device = require('zetta-device');
var exec = require('child_process').exec;
var TextToSpeech = module.exports = function(availableOptions) {
Device.call(this);
this._availableVoices = availableOptions['voices'];
this._availableDevices = availableOptions['devices'];
};
util.inherits(TextToSpeech, Device);
TextToSpeech.prototype.init = function(config) {
config
.state('silent')
.type('text-to-speech')
.name('Text to Speech')
.when('silent', { allow: ['say']})
.when('speaking', { allow: [] })
.map('say', this.say, [
{ name: 'words', title: 'Words to Speak', type: 'text'},
{ name: 'voice', title: 'Voice Persona', type: 'radio',
value: this._availableVoices},
{ name: 'device', title: 'Audio Output Device', type: 'radio',
value: this._availableDevices},
{ name: 'rate', title: 'Rate of Speech', type: 'range',
min: 90, max: 720, step: 1, units: 'words per minute'}
]);
};
TextToSpeech.prototype.say = function(words, voice, device, rate, cb) {
this.state = 'speaking';
var self = this;
var sayCommand = 'say ';
if (device) {
sayCommand += '-a ' + device + ' ';
}
if (voice) {
sayCommand += '-v ' + voice + ' ';
}
if (rate) {
sayCommand += '-r ' + rate + ' ';
}
sayCommand += '"' + words + '"';
exec(sayCommand, function (error, stdout, stderr) {
if (error === null) {
self.state = 'silent';
cb();
} else {
self.state = 'error';
console.log('stderr: ' + stderr);
console.log('exec error: ' + error);
cb(error);
}
});
};
// TODO this feels like it should be a Zetta platform thing
// Zetta knows from above config that I want an Object not a string
TextToSpeech.prototype._marshal = function(param) {
return (typeof param === 'string') ? JSON.parse(param) : ((param instanceof Array) ? param[0] : param);
}