You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
2.7 KiB
112 lines
2.7 KiB
import QMUtil from "../../util.js";
|
|
|
|
let { config } = QMUtil;
|
|
let btnStatus = "UNDEFINED"; // "UNDEFINED" "CONNECTING" "PLAY" "STOP"
|
|
|
|
const audioPlayer = new AudioPlayer("../");
|
|
audioPlayer.onPlay = () => {
|
|
btnStatus = "PLAY";
|
|
window.changeVideo();
|
|
};
|
|
|
|
audioPlayer.onStop = (audioDatas) => {
|
|
if(btnStatus === "PLAY"){
|
|
btnStatus="STOP";
|
|
window.playOver();
|
|
}
|
|
};
|
|
function getWebSocketUrl(apiKey, apiSecret) {
|
|
var url = "wss://tts-api.xfyun.cn/v2/tts";
|
|
var host = location.host;
|
|
var date = new Date().toGMTString();
|
|
var algorithm = "hmac-sha256";
|
|
var headers = "host date request-line";
|
|
var signatureOrigin = `host: ${host}\ndate: ${date}\nGET /v2/tts HTTP/1.1`;
|
|
var signatureSha = CryptoJS.HmacSHA256(signatureOrigin, apiSecret);
|
|
var signature = CryptoJS.enc.Base64.stringify(signatureSha);
|
|
var authorizationOrigin = `api_key="${apiKey}", algorithm="${algorithm}", headers="${headers}", signature="${signature}"`;
|
|
var authorization = btoa(authorizationOrigin);
|
|
url = `${url}?authorization=${authorization}&date=${date}&host=${host}`;
|
|
return url;
|
|
}
|
|
|
|
let ttsWS;
|
|
function connectWebSocket(str) {
|
|
const url = getWebSocketUrl(config.API_KEY, config.API_SECRET);
|
|
if ("WebSocket" in window) {
|
|
ttsWS = new WebSocket(url);
|
|
} else if ("MozWebSocket" in window) {
|
|
ttsWS = new MozWebSocket(url);
|
|
} else {
|
|
alert("浏览器不支持WebSocket");
|
|
return;
|
|
}
|
|
btnStatus = "CONNECTING";
|
|
|
|
ttsWS.onopen = (e) => {
|
|
audioPlayer.start({
|
|
autoPlay: true,
|
|
sampleRate: 16000,
|
|
resumePlayDuration: 1000
|
|
});
|
|
btnStatus = "PLAY";
|
|
var params = {
|
|
common: {
|
|
app_id: config.APPID,
|
|
},
|
|
business: {
|
|
aue: "raw",
|
|
auf: "audio/L16;rate=16000",
|
|
vcn: "x4_mengmengneutral",
|
|
speed: 50, //语速
|
|
volume: 80, //音量
|
|
pitch: 50, //音高
|
|
bgs: 1,
|
|
tte:"UTF8",
|
|
},
|
|
data: {
|
|
status: 2,
|
|
text: Base64.encode(str),
|
|
},
|
|
};
|
|
|
|
ttsWS.send(JSON.stringify(params));
|
|
};
|
|
ttsWS.onmessage = (e) => {
|
|
let jsonData = JSON.parse(e.data);
|
|
// 合成失败
|
|
if (jsonData.code !== 0) {
|
|
console.error(jsonData);
|
|
btnStatus = "UNDEFINED";
|
|
return;
|
|
}
|
|
audioPlayer.postMessage({
|
|
type: "base64",
|
|
data: jsonData.data.audio,
|
|
isLastData: jsonData.data.status === 2,
|
|
});
|
|
if (jsonData.code === 0 && jsonData.data.status === 2) {
|
|
ttsWS.close();
|
|
}
|
|
};
|
|
ttsWS.onerror = (e) => {
|
|
console.error(e);
|
|
};
|
|
ttsWS.onclose = (e) => {
|
|
// console.log(e);
|
|
};
|
|
}
|
|
|
|
window.stopTTS = function(){
|
|
btnStatus = "UNDEFINED";
|
|
ttsWS?.close();
|
|
audioPlayer.reset();
|
|
audioPlayer.stop();
|
|
}
|
|
|
|
window.startTTS = function(str) {
|
|
stopTTS();
|
|
// 开始合成
|
|
connectWebSocket(str);
|
|
}
|
|
|
|
|