-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTSPlayer.js
184 lines (152 loc) · 4.37 KB
/
TTSPlayer.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const googleTTS = require('google-tts-api');
const axios = require('axios');
const logger = require('@greencoast/logger');
const languages = require('./languages.json');
const { TTS_ENGINES, AEIOU_API_URL } = require('./constants');
//const { prefix } = require('../common/settings');
class TTSPlayer {
constructor(guild) {
this.guild = guild;
this.queue = [];
this.speaking = false;
this.lang = 'de';
this.slow = false;
}
say(phrase) {
const urls = googleTTS.getAllAudioUrls(phrase, {
lang: this.lang,
slow: this.slow
});
urls.forEach(({ url, shortText }) => {
this.addToQueue(url, shortText, TTS_ENGINES.google);
});
if (!this.speaking) {
this.playTTS();
}
}
aeiou(phrase) {
this.addToQueue(phrase, phrase, TTS_ENGINES.aeiou);
if (!this.speaking) {
this.playTTS();
}
}
addToQueue(source, phrase, engine) {
const parsedEntry = {
source,
phrase,
lang: this.lang,
slow: this.slow,
engine
};
this.queue.push(parsedEntry);
}
playTTS() {
const [firstInQueue] = this.queue;
if (!firstInQueue) {
return;
}
switch (firstInQueue.engine) {
case TTS_ENGINES.google:
this.playGoogle(firstInQueue);
break;
case TTS_ENGINES.aeiou:
this.playAeiou(firstInQueue);
break;
default:
throw new Error('Invalid TTS engine!');
}
}
async playGoogle(firstInQueue) {
const { source, phrase, lang, slow } = firstInQueue;
logger.info(`(TTS): Received googleTTS for ${phrase} with language '${lang}' with ${slow ? 'slow' : 'normal'} speed in guild ${this.guild.name}.`);
this.speaking = true;
const { connection } = this.guild.voice;
const dispatcher = await connection.play(source);
dispatcher.on('speaking', (speaking) => {
if (!speaking) {
this.queue.shift();
this.speaking = false;
this.playTTS();
}
});
dispatcher.on('error', (error) => {
logger.error(error);
this.queue.shift();
this.speaking = false;
this.playTTS();
});
}
playAeiou(firstInQueue) {
const { phrase } = firstInQueue;
axios.get(`${AEIOU_API_URL}/tts`, {
params: {
text: phrase
}
})
.then(async(response) => {
const url = `${AEIOU_API_URL}${response.request.path}`;
logger.info(`(TTS): Received aeiou for ${phrase} in guild ${this.guild.name}.`);
this.speaking = true;
const { connection } = this.guild.voice;
const dispatcher = await connection.play(url);
dispatcher.on('speaking', (speaking) => {
if (!speaking) {
this.queue.shift();
this.speaking = false;
this.playTTS();
}
});
dispatcher.on('error', (error) => {
logger.error(error);
this.queue.shift();
this.speaking = false;
this.playTTS();
});
})
.catch((error) => {
logger.error(error.message);
});
}
stop() {
return new Promise((resolve, reject) => {
try {
const { channel } = this.guild.voice;
this.queue = [];
this.speaking = false;
channel.leave();
logger.info(`Successfully left the voice channel ${channel.name} from guild ${this.guild.name}.`);
resolve();
} catch (error) {
reject(error);
}
});
}
setLang(newLang) {
return new Promise((resolve, reject) => {
if (!languages[newLang]) {
reject(`invalid language. Type **${prefix}langs** for a list of available languages.`);
return;
}
if (this.lang === newLang) {
reject(`language is already set to **${languages[newLang]}**.`);
return;
}
this.lang = newLang;
logger.info(`Guild ${this.guild.name} has changed its language to ${languages[this.lang]}.`);
resolve(languages[newLang]);
});
}
setSpeed(value) {
return new Promise((resolve, reject) => {
if (value !== 'normal' && value !== 'slow') {
reject('invalid speed, it must be either *normal* or *slow*.');
return;
}
this.slow = value === 'slow';
const setSpeed = this.slow ? 'slow' : 'normal';
logger.info(`Guild ${this.guild.name} has changed its speed to ${setSpeed}.`);
resolve(setSpeed);
});
}
}
module.exports = TTSPlayer;