-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSegZunAudio.js
268 lines (222 loc) · 6.52 KB
/
SegZunAudio.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
class SegZunAudio {
/*
Constructor
*/
constructor(text) {
this.AUDIO_LIMIT = 6;
this.HEAD_AUDIO_LIMIT = 4;
this.shouldPause = false;
this.canPlay = false;
this.playNext = 0;
this.isPlaying = false;
this.audios = new Array(this.AUDIO_LIMIT);
for (let i=0;i<this.AUDIO_LIMIT;i++) {
this.audios[i] = new Object();
this.audios[i].id = -1;
this.audios[i].duration = 100;
this.audios[i].status = "empty";
this.audios[i].audio = null;
}
this.headAudios = new Array(this.HEAD_AUDIO_LIMIT);
for (let i=0;i<this.HEAD_AUDIO_LIMIT;i++) {
this.headAudios[i] = new Object();
this.headAudios[i].id = -1;
this.headAudios[i].duration = 100;
this.headAudios[i].status = "empty";
this.headAudios[i].audio = null;
}
this.text = text + "。VOICEVOX:ずんだもん";
this.mainRequest();
}
/*
Method
Send a HTTP request to api.tts.quest and get the audioStatusUrl.
*/
mainRequest() {
// The main stuff.
// Created a function so I can call from setTimeout.
// SZA stands for SegZunAudio.
function main(SZA) {
let req = new XMLHttpRequest();
req.open("GET", "https://api.tts.quest/v1/zun/?text="+encodeURI(SZA.text));
req.send();
req.onload = (e) => {
let jres = JSON.parse(req.response);
// When 429 (Too many request), Try again later.
if (jres.success==false && jres.errorMessage == 429) {
setTimeout(main, 1000*jres.retryAfter, SZA);
return;
}
// audioStatusUrl is the url of audioStatusPage.
// In audioStatusPage, you can find the list of audios.
SZA.audioStatusUrl = jres.audioStatusUrl + "?retry=";
// Once received the audioStatusUrl,
// call a function to get the audioUrls.
SZA.audioStatusRequest();
};
}
// Giving "this" to use as SZA.
main(this);
}
/*
Method
Send a HTTP request to api.tts.quest and get the audioUrls.
*/
audioStatusRequest() {
function main(SZA) {
let req = new XMLHttpRequest();
req.open("GET", SZA.audioStatusUrl);
req.send();
req.onload = (e) => {
let jres = JSON.parse(req.response);
// When audioUrls isn't ready, Try again later.
if (jres.audioCount == -1) {
SZA.audioStatusUrl += "0";
setTimeout(main, 1000, SZA);
return;
}
// At this point, the audio url may return error 404.
// This is because the audios may not have been created yet.
SZA.audioCount = jres.audioCount;
SZA.audioUrls = new Array(SZA.audioCount);
for (let i=0;i<jres.audioCount;i++) {
SZA.audioUrls[i] = jres.audios[i].url;
}
let iEnd = Math.min(SZA.audioCount, SZA.HEAD_AUDIO_LIMIT);
for (let i=0;i<iEnd;i++) {
SZA.genAudio(i);
}
};
}
// Wait 1 sec incase URL isn't created yet.
setTimeout(main, 1000, this);
}
/*
Method
Send HTTP request to tts.quest to get Audio data.
*/
genAudio(id) {
function main(id, address, audios, SZA) {
// It means someone has allready created.
if (audios[address].id == id) {
return;
}
audios[address].status = "creating"
audios[address].id = id;
audios[address].audio = new Audio(SZA.audioUrls[id]);
audios[address].audio.load();
// It takes time to load.
audios[address].audio.addEventListener("loadedmetadata",function(e) {
audios[address].duration = 1000*audios[address].audio.duration;
audios[address].id = id;
audios[address].status = "ready";
if (id==0) SZA.canPlay = true;
});
// When still creating, the server returns 404...
audios[address].audio.addEventListener("error",function(e) {
audios[address] = new Object();
audios[address].id = -1;
audios[address].duration = 100;
audios[address].status = "empty";
audios[address].audio = null;
setTimeout(main, 1000, id, address, audios, SZA);
});
}
// Since you can play from the begining,
// the frist X audios won't be deleted.
// For the rest of the audio, it will only use Y.
// This is because chromium limits the number of Audio.
// Y[ X] is defined in [HEAD_]AUDIO_LIMIT.
if (id<this.HEAD_AUDIO_LIMIT) {
main(
id,
id,
this.headAudios,
this
);
}
else {
main(
id,
id%this.AUDIO_LIMIT,
this.audios,
this
);
}
}
/*
Method
Starts playing the audio from id. RECURSIVE.
*/
startPlayingById(id) {
function main(SZA, id) {
if (!SZA.canPlay) {
setTimeout(main, 1000, SZA, id);
return;
}
if (SZA.shouldPause) {
SZA.playNext = id;
return;
}
SZA.playNext = id+1;
let audio = {};
if (id<SZA.HEAD_AUDIO_LIMIT) {
audio = SZA.headAudios[id];
}
else {
audio = SZA.audios[id%SZA.AUDIO_LIMIT];
}
if (audio.id != id) {
audio.status = "wrongId";
SZA.genAudio(id);
}
if (audio.status!="ready") {
setTimeout(main, 1000, SZA, id)
return;
}
if (id+SZA.HEAD_AUDIO_LIMIT<SZA.audioCount) {
SZA.genAudio(id+SZA.HEAD_AUDIO_LIMIT);
}
audio.audio.play();
if (id+1<SZA.audioCount) {
let playNextAudioOnEvent = (e) => {
audio.audio.removeEventListener('playing', playNextAudioOnEvent);
// The most difficult bit. How long should you wait.
let delay = Math.max(audio.duration -120, audio.duration *.80);
setTimeout(main, delay, SZA, id+1)
};
audio.audio.addEventListener('playing', playNextAudioOnEvent);
}
else {
SZA.isPlaying = false;
}
}
if (this.isPlaying) return;
if (id==this.audioCount) id = 0;
this.shouldPause = false;
this.isPlaying = true;
main(this, id);
}
/*
Method
Starts playing the audio from playNext.
*/
resume() {
this.startPlayingById(this.playNext);
}
/*
Method
Pauses the audio. play() to resume.
*/
pause() {
this.shouldPause = true;
this.isPlaying = false;
}
/*
Method
Plays audio from the begining
*/
play() {
this.startPlayingById(0);
}
}