Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
410 lines (359 loc) · 21.4 KB
/
Copy pathscript.js
File metadata and controls
410 lines (359 loc) · 21.4 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// ==UserScript==
// @name 微软tts 下载按钮
// @namespace http://tampermonkey.net/
// @version 0.2.4
// @description 微软tts 下载按钮 方便下载合成的音频
// @author skygongque
// @match https://azure.microsoft.com/*/services/cognitive-services/text-to-speech/*
// @icon https://www.microsoft.com/favicon.ico
// @grant none
// @run-at document-start
// @license MIT
// ==/UserScript==
(function () {
'use strict';
window.xtw = {}
// hook addEventListener
window.document.addEventListener_ = document.addEventListener;
document.addEventListener = function () {
if (arguments[0] == "DOMContentLoaded" && arguments.length == 2) {
//debugger
window.initializeTTSDemo2 = function (localizedResources) {
$(document).ready(function () {
if (!window.xtw.playdlbut) {
window.xtw.playdlbut = document.createElement("button");
window.xtw.playdlbut.innerHTML = "先点播放";
window.xtw.playdlbut.classList.add('button');
window.xtw.playdlbut.classList.add('button--primary01');
window.xtw.playdlbut.classList.add('svg-button');
window.xtw.li = document.createElement('li');
window.xtw.li.appendChild(window.xtw.playdlbut);
window.xtw.playliparent = document.getElementById('playli').parentElement
window.xtw.playliparent.appendChild(window.xtw.li);
}
var play = document.getElementById('playbtn'),
playli = document.getElementById('playli'),
stop = document.getElementById('stopbtn'),
stopli = document.getElementById('stopli'),
language = document.getElementById('languageselect'),
voice = document.getElementById('voiceselect'),
voicestyle = document.getElementById('voicestyleselect'),
roleplay = document.getElementById('roleplayselect'),
roleplaydiv = document.getElementById('roleplayselectdiv'),
secondarylocale = document.getElementById('secondarylocaleselect'),
secondarylocalediv = document.getElementById('secondarylocaleselectdiv'),
text = document.getElementById('ttstext'),
ssml = document.getElementById('ttsssml'),
status = document.getElementById('ttsstatus'),
speed = document.getElementById('speed'),
speedlabel = document.getElementById('speedlabel'),
pitch = document.getElementById('pitch'),
pitchlabel = document.getElementById('pitchlabel'),
player = null,
SpeechSDK = window.SpeechSDK,
voiceList = {},
styleList = {},
rolePlayList = {},
secondaryLocaleList = {},
defaultText;
SpeechSDK = window.SpeechSDK;
$.ajax({
url: 'https://' + localizedResources.region + '.api.speech.microsoft.com/cognitiveservices/voices/list',
type: 'GET',
success: function textToSpeechVoiceListAjaxSuccess(data) {
// put neural voices in front.
var sorted = data.sort(function (a, b) {
return a.VoiceType.localeCompare(b.VoiceType);
});
$.each(sorted, function (_index, element) {
var displayName = element.DisplayName;
if (element.Status === 'Deprecated') {
// Don't show deprecated voices.
return;
}
if (!voiceList[element.Locale]) {
voiceList[element.Locale] = '';
}
if (element.VoiceType === 'Neural') {
displayName += ' (Neural)';
}
if (element.LocalName !== element.DisplayName) {
displayName += ' - ' + element.LocalName;
}
if (element.Status === 'Preview') {
displayName += ' - ' + localizedResources.ttsPreview;
}
voiceList[element.Locale] += '<option value="' + element.ShortName + '">' + displayName + '</option>';
styleList[element.ShortName] = element.StyleList;
rolePlayList[element.ShortName] = element.RolePlayList;
secondaryLocaleList[element.ShortName] = element.SecondaryLocaleList;
});
language.onchange();
},
error: function textToSpeechVoiceListAjaxError(_jqXHR, _textStatus, error) {
status.innerText = localizedResources.srTryAgain;
global.Core.Util.TrackException('A Text To Speech voice list API Ajax error occurred: ' + error);
}
});
function SpeakOnce() {
var config = SpeechSDK.SpeechTranslationConfig.fromEndpoint(new URL('wss://' + localizedResources.region + '.api.speech.microsoft.com/cognitiveservices/websocket/v1?TrafficType=AzureDemo')),
synthesizer,
audioConfig;
// due to a bug in Chromium (https://bugs.chromium.org/p/chromium/issues/detail?id=1028206)
// mp3 playback has some beeps, using a higher bitrate here as a workaround.
config.speechSynthesisOutputFormat = SpeechSDK.SpeechSynthesisOutputFormat.Audio24Khz96KBitRateMonoMp3;
player = new SpeechSDK.SpeakerAudioDestination();
player.onAudioEnd = function () {
stopli.hidden = true;
playli.hidden = false;
};
audioConfig = SpeechSDK.AudioConfig.fromSpeakerOutput(player);
synthesizer = new SpeechSDK.SpeechSynthesizer(config, audioConfig);
synthesizer.synthesisCompleted = function () {
// 此时已经合成完毕了 arguments[1].privResult.privAudioData 为MP3文件
//debugger;
window.xtw.privAudioData = arguments[1].privResult.privAudioData;
window.xtw.playdlbut.innerHTML = "下载音频";
window.xtw.playdlbut.onclick = function () {
// https://stackoverflow.com/questions/19327749/javascript-blob-filename-without-link
function saveFile(name, type, data) {
if (data !== null && navigator.msSaveBlob) {
return navigator.msSaveBlob(new Blob([data], { type: type }), name);
}
var a = $("<a style='display: none;'/>");
var url = window.URL.createObjectURL(new Blob([data], { type: type }));
a.attr("href", url);
a.attr("download", name);
$("body").append(a);
a[0].click();
window.URL.revokeObjectURL(url);
a.remove();
window.xtw.playdlbut.innerHTML = "先点播放";
};
//debugger;
if (window.xtw.privAudioData) {
saveFile(new Date().getTime(), "audio/mp3", window.xtw.privAudioData);
window.xtw.privAudioData = null;
} else {
alert('先点播放,等按钮变成下载音频再点击')
}
};
synthesizer.close();
synthesizer = null;
};
synthesizer.SynthesisCanceled = function (s, e) {
var details;
stopPlaying();
details = SpeechSDK.CancellationDetails.fromResult(e);
if (details.reason === SpeechSDK.CancellationReason.Error) {
status.innerText = localizedResources.srTryAgain;
}
};
synthesizer.speakSsmlAsync(ssml.value, function () { }, function (error) {
status.innerText = localizedResources.srTryAgain + ' ' + error;
});
}
function stopPlaying() {
playli.hidden = false;
stopli.hidden = true;
if (player !== null) {
player.pause();
}
player = null;
play.focus();
}
stop.onclick = function () {
stopPlaying();
};
play.onclick = function () {
stopli.hidden = false;
playli.hidden = true;
status.innerText = '';
SpeakOnce(function () { });
stop.focus();
};
language.onchange = function () {
defaultText = '';
$.each(localizedResources.ttsDefaultText, function (key, value) {
if (language[language.selectedIndex].value === key) {
defaultText = value;
} else if (!defaultText && language[language.selectedIndex].value.startsWith(key)) {
defaultText = value;
}
});
if (!defaultText) {
defaultText = localizedResources.ttsDefaultText.default;
}
text.value = defaultText;
// Arabic, Hebrew, Urdu, Persian and Pashto use right-to-left text direction
if (['ar', 'he', 'ur', 'fa', 'ps'].includes(language[language.selectedIndex].value.slice(0, 2))) {
text.style.direction = 'rtl';
} else {
text.style.direction = '';
}
voice.innerHTML = voiceList[language[language.selectedIndex].value];
voice.onchange();
BuildAndSetSSML();
};
voice.onchange = function () {
voicestyle.innerHTML = '';
voicestyle.options.add(new Option('General', 'general'));
if (styleList[voice[voice.selectedIndex].value]) {
$.each(styleList[voice[voice.selectedIndex].value], function (_index, element) {
voicestyle.options.add(new Option(
(element[0].toUpperCase() + element.slice(1))
.replace('Customerservice', 'Customer Service')
.replace('Voiceassistant', 'Voice Assistant'), element));
});
voicestyle.disabled = false;
} else {
voicestyle.disabled = true;
}
if (rolePlayList[voice[voice.selectedIndex].value]) {
roleplay.innerHTML = '';
roleplay.options.add(new Option('Default', 'Default'));
$.each(rolePlayList[voice[voice.selectedIndex].value], function (_index, element) {
roleplay.options.add(new Option(element, element));
});
roleplaydiv.hidden = false;
} else {
roleplaydiv.hidden = true;
}
if (secondaryLocaleList[voice[voice.selectedIndex].value]) {
secondarylocale.innerHTML = '';
secondarylocale.options.add(new Option(language[language.selectedIndex].text + ' - Default', language[language.selectedIndex].value));
$(language.options).each(function () {
if (secondaryLocaleList[voice[voice.selectedIndex].value].includes($(this).attr('value'))) {
secondarylocale.options.add(new Option($(this).text(), $(this).attr('value')));
}
});
secondarylocalediv.hidden = false;
} else {
secondarylocalediv.hidden = true;
}
pitch.disabled = false;
speed.disabled = false;
BuildAndSetSSML();
};
voicestyle.onchange = function () {
BuildAndSetSSML();
};
roleplay.onchange = function () {
BuildAndSetSSML();
};
secondarylocale.onchange = function () {
if (secondarylocale.selectedIndex !== 0) {
pitch.disabled = true;
speed.disabled = true;
} else {
pitch.disabled = false;
speed.disabled = false;
}
defaultText = '';
$.each(localizedResources.ttsDefaultText, function (key, value) {
if (secondarylocale[secondarylocale.selectedIndex].value === key) {
defaultText = value;
} else if (!defaultText && secondarylocale[secondarylocale.selectedIndex].value.startsWith(key)) {
defaultText = value;
}
});
if (!defaultText) {
defaultText = localizedResources.ttsDefaultText.default;
}
text.value = defaultText;
BuildAndSetSSML();
};
pitch.oninput = function () {
var normalizedVal = ((pitch.value - pitch.min) / (pitch.max - pitch.min)) * (2),
displayVal;
if (Math.abs(normalizedVal) < 1) {
displayVal = normalizedVal.toPrecision(2);
} else {
displayVal = normalizedVal.toPrecision(3);
}
pitchlabel.innerText = localizedResources.ttsPitch + ': ' + displayVal;
};
pitch.onchange = function () {
BuildAndSetSSML();
};
speed.oninput = function () {
var normalizedVal = ((speed.value - speed.min) / (speed.max - speed.min)) * (3),
displayVal;
if (Math.abs(normalizedVal) < 1) {
displayVal = normalizedVal.toPrecision(2);
} else {
displayVal = normalizedVal.toPrecision(3);
}
speedlabel.innerText = localizedResources.ttsSpeed + ': ' + displayVal;
};
speed.onchange = function () {
BuildAndSetSSML();
};
text.oninput = function () {
BuildAndSetSSML();
};
ssml.oninput = function () {
checkSSMLLength();
};
function BuildAndSetSSML() {
var ssmlHeader = '<speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="en-US">{0}</speak>',
ssmlVoiceName = '<voice name="{VOICE}">{0}</voice>',
ssmlProperties = '<prosody rate="{SPEED}" pitch="{PITCH}">{TEXT}</prosody>',
ssmlExpression = '<mstts:express-as {STYLE_ATTRIBUTE} {ROLE_PLAY_ATTRIBUTE}>{0}</mstts:express-as>',
ssmlSecondaryLocale = '<lang xml:lang="{SECONDARY_LOCALE}">{0}</lang>',
ssmlText = ssmlProperties,
escapedText;
ssmlText = ssmlText.replace('{SPEED}', speed.value + '%');
ssmlText = ssmlText.replace('{PITCH}', pitch.value + '%');
if (voicestyle.selectedIndex !== 0 || (!roleplaydiv.hidden && roleplay.selectedIndex !== 0)) {
ssmlText = ssmlExpression.replace('{0}', ssmlText);
if (voicestyle.selectedIndex !== 0) {
ssmlText = ssmlText.replace('{STYLE_ATTRIBUTE}', 'style="' + voicestyle[voicestyle.selectedIndex].value + '"');
} else {
ssmlText = ssmlText.replace('{STYLE_ATTRIBUTE}', '');
}
if (!roleplaydiv.hidden && roleplay.selectedIndex !== 0) {
ssmlText = ssmlText.replace('{ROLE_PLAY_ATTRIBUTE}', 'role="' + roleplay[roleplay.selectedIndex].value + '"');
} else {
ssmlText = ssmlText.replace('{ROLE_PLAY_ATTRIBUTE}', '');
}
}
ssmlText = ssmlVoiceName.replace('{0}', ssmlText);
ssmlText = ssmlText.replace('{VOICE}', voice[voice.selectedIndex].value);
escapedText = text.value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')
.replace(/'/g, ''');
if (!secondarylocalediv.hidden) {
escapedText = ssmlSecondaryLocale.replace('{0}', escapedText);
escapedText = escapedText.replace('{SECONDARY_LOCALE}', secondarylocale[secondarylocale.selectedIndex].value);
}
ssmlText = ssmlHeader.replace('{0}', ssmlText);
ssmlText = ssmlText.replace('{TEXT}', escapedText);
ssml.value = ssmlText;
checkSSMLLength();
}
function checkSSMLLength() {
var ssmlText = ssml.value,
ssmlLength = ssmlText.length;
if (ssmlLength > 1000) {
status.innerText = localizedResources.ttsTextTooLong;
play.setAttribute('disabled', 'disabled');
} else {
status.innerText = '';
play.removeAttribute('disabled');
}
}
});
};
window.document.addEventListener_("DOMContentLoaded", function (event) {
window.initializeTTSDemo2(localizedResources);
});
} else if (arguments.length == 3) {
window.document.addEventListener_(arguments[0], arguments[1], arguments[2])
} else if (arguments.length == 2) {
window.document.addEventListener_(arguments[0], arguments[1])
}else{
debugger;
}
}
// Your code here...
})();