UNPKG

1.58 kBJavaScriptView Raw
1(function() {
2 module.exports = function($) {
3 var _;
4 ({_} = $);
5 $.say_ = async function(text, option = {}) {
6 var i, lang, len, listCmd, listMessage, msg, name, type, voice;
7 type = $.type(text);
8 listMessage = (function() {
9 switch (type) {
10 case 'array':
11 return text;
12 case 'boolean':
13 case 'number':
14 case 'string':
15 return [text];
16 default:
17 throw new Error(`invalid type '${type}'`);
18 }
19 })();
20 for (i = 0, len = listMessage.length; i < len; i++) {
21 msg = listMessage[i];
22 $.info('say', msg);
23 if ($.os !== 'macos') {
24 continue;
25 }
26 msg = $.parseString(msg).replace(/[#\(\)-]/g, '');
27 msg = _.trim(msg);
28 if (!msg.length) {
29 continue;
30 }
31 listCmd = ['say'];
32 if (option.lang) {
33 lang = option.lang.toLowerCase();
34 if (name = $.say_.mapLang[lang]) {
35 lang = name;
36 }
37 listCmd.push(`--voice=${lang}`);
38 }
39 if (option.voice) {
40 voice = option.voice.toLowerCase();
41 listCmd.push(`--voice=${voice}`);
42 }
43 listCmd.push(msg);
44 await $.exec_(listCmd.join(' '), {
45 silent: true
46 });
47 }
48 return $; // return
49 };
50 $.say_.mapLang = {
51 'ja': 'kyoko',
52 'ja-jp': 'kyoko',
53 'zh': 'ting-ting',
54 'zh-cn': 'ting-ting',
55 'zh-hk': 'sin-ji',
56 'zh-tw': 'mei-jia'
57 };
58 return $.say_; // return
59 };
60
61}).call(this);