forked from awamper/text-translator
-
Notifications
You must be signed in to change notification settings - Fork 28
/
google_tts.js
35 lines (27 loc) · 991 Bytes
/
google_tts.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
const Gst = imports.gi.Gst;
const URI =
"https://translate.google.com/translate_tts?client=tw-ob&ie=UTF-8&total=1&idx=0&textlen=%d&q=%s&tl=%s";
const MAX_LEN = 100;
var GoogleTTS = class GoogleTTS {
constructor() {
Gst.init(null);
this._player = Gst.ElementFactory.make("playbin", "player");
this._bus = this._player.get_bus();
this._bus.add_signal_watch();
this._bus.connect("message::error", () => this._kill_stream());
this._bus.connect("message::eos", () => this._kill_stream());
}
_kill_stream() {
this._player.set_state(Gst.State.NULL);
}
speak(text, lang) {
let extract = text.substr(0, MAX_LEN - 1);
this._kill_stream();
let uri = URI.format(extract.length, encodeURIComponent(extract), lang);
this._player.set_property("uri", uri);
this._player.set_state(Gst.State.PLAYING);
}
destroy() {
this._player.set_state(Gst.State.NULL);
}
};