-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextra-tts-ipc.js
99 lines (94 loc) · 3.07 KB
/
extra-tts-ipc.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
(function () {
var ipcRenderer = null;
try {
ipcRenderer = window.ipcRenderer || require('electron').ipcRenderer;
} catch(e) { }
var extra_tts = null;
var tts = {
callbacks: {},
clean_callbacks: function() {
var now = (new Date()).getTime();
var new_callbacks = {};
for(var idx in tts.callbacks) {
if(tts.callbacks[idx] && tts.callbacks[idx].timestamp && tts.callbacks[idx].timestamp > now - (30 * 60 * 1000)) {
new_callbacks[idx] = tts.callbacks[idx];
}
}
tts.callbacks = new_callbacks;
},
add_callback: function(callback, type) {
tts.clean_callbacks();
var now = (new Date()).getTime();
var id = null;
if(callback) {
id = (Math.random() * 9999) + "-" + now;
tts.callbacks[id] = {
timestamp: now,
type: type,
callback: callback,
id: id
};
}
return id;
},
trigger_callback: function (object) {
if(object.callback_id && tts.callbacks[object.callback_id]) {
tts.callbacks[object.callback_id].callback(object.result);
}
},
run: function(method, a, b, c) {
if(tts.ipc) {
var success_id = tts.add_callback(a && a.success, 'success');
var progress_id = tts.add_callback(a && a.progress, 'progress');
var error_id = tts.add_callback(a && a.error, 'error');
if(a) {
delete a['success'];
delete a['error'];
delete a['progress'];
}
if(a) {
a.base_dir = tts.base_dir;
}
ipcRenderer.send('extra-tts-exec', JSON.stringify({
method: method,
success_id: success_id,
progress_id: progress_id,
error_id: error_id,
args: [a, b, c]
}));
} else {
console.log("extra-tts will run in the current process");
extra_tts = extra_tts || (window.node_extras && window.node_extras.extra_tts) || require('extra-tts');
extra_tts[method].apply(extra_tts, [a, b, c]);
}
},
enabled: false
};
var keys = ['init', 'status', 'reload', 'getAvailableVoices', 'downloadVoice', 'deleteVoice', 'speakText', 'stopSpeakingText'];
for (var idx = 0; idx < keys.length; idx++) {
(function (method) {
tts[method] = function (a, b, c) {
tts.run(method, a, b, c);
}
})(keys[idx]);
}
if(ipcRenderer && ipcRenderer.send) {
ipcRenderer.on('extra-tts-ready', function(event, message) {
if(message == 'ready') {
console.log("extra-tts will run in the main electron process");
tts.ipc = true;
}
});
setTimeout(function() {
ipcRenderer.send('extra-tts-ready');
}, 100);
ipcRenderer.on('extra-tts-exec-result', function (event, message) {
console.log("RESULT");
var json = JSON.parse(message);
if(json.callback_id) {
tts.trigger_callback(json);
}
});
}
module.exports = tts;
})();