forked from Marak/say.js
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
199 lines (160 loc) · 5.47 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use strict';
var child_process = require('child_process');
var path = require('path');
var say = exports;
var childD;
// use the correct library per platform
if (process.platform === 'darwin') {
say.speaker = 'say';
say.base_speed = 175;
} else if (process.platform === 'linux') {
say.speaker = 'festival';
say.base_speed = 100;
} else if (process.platform === 'win32') {
say.speaker = 'powershell';
}
/**
* Uses system libraries to speak text via the speakers.
*
* @param {string} text Text to be spoken
* @param {string|null} voice Name of voice to be spoken with
* @param {number|null} speed Speed of text (e.g. 1.0 for normal, 0.5 half, 2.0 double)
* @param {Function|null} callback A callback of type function(err) to return.
*/
say.speak = function(text, voice, speed, callback) {
var commands, pipedData;
if (typeof callback !== 'function') {
callback = function() {};
}
if (!text) {
// throw TypeError because API was used incorrectly
throw new TypeError('Must provide text parameter');
}
// tailor command arguments to specific platforms
if (process.platform === 'darwin') {
if (!voice) {
commands = [ text ];
} else {
commands = [ '-v', voice, text];
}
if (speed) {
commands.push('-r', convertSpeed(speed));
}
} else if (process.platform === 'linux') {
commands = ['--pipe'];
if (speed) {
pipedData = '(Parameter.set \'Audio_Command "aplay -q -c 1 -t raw -f s16 -r $(($SR*' + convertSpeed(speed) + '/100)) $FILE") ';
}
if (voice) {
pipedData += '(' + voice + ') ';
}
pipedData += '(SayText \"' + text + '\")';
} else if (process.platform === 'win32') {
pipedData = text;
commands = [ 'Add-Type -AssemblyName System.speech; $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer; $speak.Speak([Console]::In.ReadToEnd())' ];
} else {
// if we don't support the platform, callback with an error (next tick) - don't continue
return process.nextTick(function() {
callback(new Error('say.js speak does not support platform ' + process.platform));
});
}
var options = (process.platform === 'win32') ? { shell: true } : undefined;
childD = child_process.spawn(say.speaker, commands, options);
childD.stdin.setEncoding('ascii');
childD.stderr.setEncoding('ascii');
if (pipedData) {
childD.stdin.end(pipedData);
}
childD.stderr.once('data', function(data) {
// we can't stop execution from this function
callback(new Error(data));
});
childD.addListener('exit', function (code, signal) {
if (code === null || signal !== null) {
return callback(new Error('say.js: could not talk, had an error [code: ' + code + '] [signal: ' + signal + ']'));
}
childD = null;
callback(null);
});
};
say.export = function(text, voice, speed, filename, callback) {
var commands, pipedData;
if (!text) {
// throw TypeError because API was used incorrectly
throw new TypeError('Must provide text parameter');
}
if (!filename) {
// throw TypeError because API was used incorrectly
throw new TypeError('Must provide a filename');
}
if (typeof callback !== 'function') {
callback = function() {};
}
// tailor command arguments to specific platforms
if (process.platform === 'darwin') {
if (!voice) {
commands = [ text ];
} else {
commands = [ '-v', voice, text];
}
if (speed) {
commands.push('-r', convertSpeed(speed));
}
if (filename){
commands.push('-o', filename, '--data-format=LEF32@32000');
}
} else {
// if we don't support the platform, callback with an error (next tick) - don't continue
return process.nextTick(function() {
callback(new Error('say.js export does not support platform ' + process.platform));
});
}
childD = child_process.spawn(say.speaker, commands);
childD.stdin.setEncoding('ascii');
childD.stderr.setEncoding('ascii');
if (pipedData) {
childD.stdin.end(pipedData);
}
childD.stderr.once('data', function(data) {
// we can't stop execution from this function
callback(new Error(data));
});
childD.addListener('exit', function (code, signal) {
if (code === null || signal !== null) {
return callback(new Error('say.js: could not talk, had an error [code: ' + code + '] [signal: ' + signal + ']'));
}
childD = null;
callback(null);
});
};
/**
* Stops currently playing audio. There will be unexpected results if multiple audios are being played at once
*
* TODO: If two messages are being spoken simultaneously, childD points to new instance, no way to kill previous
*/
exports.stop = function(callback) {
if (typeof callback !== 'function') {
callback = function() {};
}
if (!childD) {
return callback(new Error('No speech to kill'));
}
if (process.platform === 'linux') {
// TODO: Need to ensure the following is true for all users, not just me. Danger Zone!
// On my machine, original childD.pid process is completely gone. Instead there is now a
// childD.pid + 1 sh process. Kill it and nothing happens. There's also a childD.pid + 2
// aplay process. Kill that and the audio actually stops.
process.kill(childD.pid + 2);
} else if (process.platform === 'win32') {
childD.stdin.pause();
child_process.exec('taskkill /pid ' + childD.pid + ' /T /F')
} else {
childD.stdin.pause();
childD.kill();
}
childD = null;
callback(null);
};
function convertSpeed(speed) {
return Math.ceil(say.base_speed * speed);
}