forked from ajay-gandhi/light-tts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (90 loc) · 2.53 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
var request = require('request'),
fs = require('fs'),
Lame = require('lame'),
Speaker = require('speaker'),
qs = require('querystring');
module.exports = (function () {
function LightTTS() {
this.apis = {
google: 'http://translate.google.com/translate_tts?',
tts_api: 'http://tts-api.com/tts.mp3?'
}
// Default api is Google
this.api_url = apis.google;
// Default langauage is english
this.args = {
text: '',
tl: 'en'
}
}
/**
* Set TTS options
*/
module.exports.set_opts = function (opts) {
if (opts.api_name && this.apis[opts.api_name])
this.api_url = this.pis[opts.api_name];
// Only Google supports languages
if (opts.lang) {
if (this.api_url === this.apis.google) this.args.tl = opts.lang;
else console.log('Only Google TTS supports additional languages');
}
}
/**
* Converts provided text to speech and saves as the given file
*/
LightTTS.prototype.save = function (text, filename, callback) {
// Ensure args
if (!text) return console.log('Missing "text" parameter');
if (!filename) return console.log('Missing "filename" parameter');
this.args.text = text;
// Form the URL
var options = {
url: api_url + qs.stringify(this.args)
}
// File to save audio to
var mp3File = filename + '.mp3';
var mp3_file = fs.createWriteStream(mp3File);
// Make API request
request
.get(options)
.on('error', function (err) {
console.log(err);
})
.on('data', function (data) {
mp3_file.write(data);
})
.on('end', function(){
mp3_file.end();
if (callback) callback();
});
}
/**
* Converts the given text to speech and plays it
*/
LightTTS.prototype.say = function (text, callback) {
// Ensure args
if (!text) return console.log('Missing "text" parameter');
this.args.text = text;
// Form the URL
var options = {
url: api_url + qs.stringify(this.args)
}
// Pipe to Lame to convert to PCM, then pipe to speakers
request
.get(options)
.on('error', function (err) {
console.log(err);
})
.pipe(new Lame.Decoder())
.on('format', function (format) {
var playing = this.pipe(new Speaker(format));
playing.on('error', function (err) {
console.log(err);
});
playing.on('finish', function () {
if (callback) callback();
});
});
}
return new LightTTS();
})();