-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
97 lines (80 loc) · 2.54 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
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const events = require('events');
class Speech extends events.EventEmitter {
constructor(data) {
super();
// ********************* TTS ********************* //
let ttsLib = require('./tts');
this.tts = new ttsLib();
// ********************* STT ********************* //
let sttLib = require('./stt');
this.stt = new sttLib();
this.stt.on('sttInit', (err, data) => {
this.emit('sttInit', err, data);
});
this.stt.on('sttOn', (data) => {
this.emit('sttOn', data);
});
}
// ********************* TTS ********************* //
textToSpeech(data) {
if ( (typeof data.text != 'string') && (typeof data.text != 'number') ) {
return data.cb('tts Incorrect text format');
}
if (!data.text) {
return data.cb('tts Not found text');
}
if (!data.file) {
return data.cb('tts Missing file name');
}
if (!data.key) {
return data.cb('tts Missing key');
}
let obj = {
text: data.text,
key: data.key,
file: data.file,
speaker: data.speaker || '',
quality: data.quality || '',
platform: data.platform || '',
application: data.application || '',
lang: data.lang || '',
format: 'wav'
};
if (data.voice) obj.speaker = data.voice;
let request = this.tts.init(obj);
this.tts.request(request, obj.file, data.cb);
}
// ********************* STT ********************* //
isReadyStt() {
return this.stt.isReady();
}
isConnectingStt() {
return this.stt.isConnecting();
}
sttStop() {
this.stt.stop();
}
sttInit(options) {
if ( !this.stt.isReady() ) {
if ( !this.stt.isConnecting() ) {
if (!options || !options.developer_key) {
let err = 'sttInit not found options or developer_key'
return this.emit('sttInit', err);
}
let settings = {
developer_key: options.developer_key,
model: options.model || 'general'
};
this.stt.init(settings);
}
}
}
speechToText(payload) {
if ( this.stt.isReady() ) {
this.stt.send(payload);
}
}
}
module.exports = Speech;