249 lines
8.6 KiB
JavaScript
249 lines
8.6 KiB
JavaScript
"use strict";
|
|
var common_vendor = require("../../common/vendor.js");
|
|
var config_index = require("../../config/index.js");
|
|
var utils_wsRequest = require("../../utils/wsRequest.js");
|
|
var utils_audioUtil = require("../../utils/audioUtil.js");
|
|
var api_index = require("../../api/index.js");
|
|
require("../../utils/request.js");
|
|
const _sfc_main = {
|
|
setup(__props) {
|
|
const innerAudioContext = wx.createInnerAudioContext({
|
|
useWebAudioImplement: false
|
|
});
|
|
common_vendor.reactive([]);
|
|
innerAudioContext.playbackRate = 1.3;
|
|
const recorderManager = wx.getRecorderManager();
|
|
const ws = common_vendor.ref(null);
|
|
const params = {
|
|
audio: "",
|
|
meta_info: {
|
|
session_id: "",
|
|
encoding: "raw"
|
|
},
|
|
is_close: false
|
|
};
|
|
const options = {
|
|
duration: 6e5,
|
|
sampleRate: 16e3,
|
|
numberOfChannels: 1,
|
|
encodeBitRate: 96e3,
|
|
format: "PCM",
|
|
frameSize: 12
|
|
};
|
|
innerAudioContext.onEnded(() => {
|
|
playNext();
|
|
});
|
|
const startRecord = () => {
|
|
isRecording.value = true;
|
|
console.log("\u5F00\u59CB\u5F55\u97F3");
|
|
recorderManager.start(options);
|
|
};
|
|
const stopRecord = () => {
|
|
if (!isRecording.value) {
|
|
console.log("\u6CA1\u6709\u5728\u5F55\u97F3...");
|
|
return;
|
|
}
|
|
recorderManager.stop();
|
|
isRecording.value = false;
|
|
console.log("\u505C\u6B62\u5F55\u97F3");
|
|
};
|
|
const playNext = () => {
|
|
if (aiVoices.length > 0) {
|
|
isPlaying.value = true;
|
|
let first = aiVoices.shift(0);
|
|
if (first) {
|
|
recorderManager.pause();
|
|
innerAudioContext.src = first;
|
|
innerAudioContext.play();
|
|
}
|
|
} else {
|
|
isPlaying.value = false;
|
|
recorderManager.resume();
|
|
}
|
|
};
|
|
let aiVoices = common_vendor.reactive([]);
|
|
let roleName = common_vendor.ref("");
|
|
let currentRoleId = common_vendor.ref("");
|
|
let status = common_vendor.ref("\u7B49\u5F85\u5BF9\u65B9\u63A5\u6536\u9080\u8BF7...");
|
|
let isRecording = common_vendor.ref(false);
|
|
let isPlaying = common_vendor.ref(false);
|
|
let isLoudSpeaker = common_vendor.ref(true);
|
|
let aiAvatarId = common_vendor.ref("");
|
|
let backGroundList = common_vendor.reactive([]);
|
|
const currentBackGroundImage = common_vendor.computed$1(() => {
|
|
return backGroundList[0];
|
|
});
|
|
common_vendor.onShow(() => {
|
|
common_vendor.index.getStorage({
|
|
key: "chatInfo",
|
|
success: ({
|
|
data
|
|
}) => {
|
|
const {
|
|
session_id,
|
|
background_ids = "",
|
|
role_name,
|
|
avatar_id,
|
|
role_id
|
|
} = data;
|
|
roleName.value = role_name;
|
|
currentRoleId.value = role_id;
|
|
params.meta_info.session_id = session_id;
|
|
aiAvatarId.value = avatar_id;
|
|
backGroundList.push(...background_ids.split(","));
|
|
}
|
|
});
|
|
const pcmToArrayBuffer = (pcmData, sampleRate, channels, sampleBits) => {
|
|
var _a;
|
|
const chunkSize = 36 + pcmData.byteLength;
|
|
const byteRate = sampleRate * channels * (sampleBits / 8);
|
|
const blockAlign = channels * (sampleBits / 8);
|
|
const wavHeader = new ArrayBuffer(44);
|
|
const view = new DataView(wavHeader);
|
|
view.setUint32(0, 1380533830, false);
|
|
view.setUint32(4, chunkSize, true);
|
|
view.setUint32(8, 1463899717, false);
|
|
view.setUint32(12, 1718449184, false);
|
|
view.setUint32(16, 16, true);
|
|
view.setUint16(20, 1, true);
|
|
view.setUint16(22, channels, true);
|
|
view.setUint32(24, sampleRate, true);
|
|
view.setUint32(28, byteRate, true);
|
|
view.setUint16(32, blockAlign, true);
|
|
view.setUint16(34, sampleBits, true);
|
|
view.setUint32(36, 1684108385, false);
|
|
view.setUint32(40, pcmData.byteLength, true);
|
|
const wavArrayBuffer = new Uint8Array(wavHeader);
|
|
const pcmArrayBuffer = new Uint8Array(pcmData);
|
|
const combinedArrayBuffer = new Uint8Array(wavArrayBuffer.length + pcmArrayBuffer.length);
|
|
combinedArrayBuffer.set(wavArrayBuffer, 0);
|
|
combinedArrayBuffer.set(pcmArrayBuffer, wavArrayBuffer.length);
|
|
const base64 = wx.arrayBufferToBase64(combinedArrayBuffer);
|
|
const data = Object.assign({}, params);
|
|
data.audio = base64;
|
|
(_a = ws.value) == null ? void 0 : _a.sendMsg(data);
|
|
console.log(data);
|
|
};
|
|
recorderManager.onFrameRecorded((res) => {
|
|
const {
|
|
frameBuffer,
|
|
isLastFrame
|
|
} = res;
|
|
pcmToArrayBuffer(frameBuffer, 16e3, 1, 16);
|
|
});
|
|
});
|
|
const usermessageIsEnd = common_vendor.ref(false);
|
|
const llmmessageIsEnd = common_vendor.ref(false);
|
|
const usermsg = common_vendor.reactive([]);
|
|
const llmmsg = common_vendor.reactive([]);
|
|
common_vendor.onMounted(() => {
|
|
var _a;
|
|
try {
|
|
wx.setKeepScreenOn({
|
|
keepScreenOn: true
|
|
});
|
|
ws.value = new utils_wsRequest.webSocketClass(config_index.wsVoiceCallUrl, 60);
|
|
(_a = ws.value) == null ? void 0 : _a.initSocket().then((res) => {
|
|
status.value = "\u5DF2\u63A5\u901A";
|
|
setTimeout(() => {
|
|
status.value = "";
|
|
}, 2e3);
|
|
common_vendor.index.$on("message", ({
|
|
type,
|
|
data
|
|
}) => {
|
|
status.value = "";
|
|
console.log("\u6536\u5230\u56DE\u590D", data);
|
|
if (data instanceof ArrayBuffer) {
|
|
utils_audioUtil.savePCMBuffer(data, aiVoices);
|
|
setTimeout(() => {
|
|
if (!isPlaying.value) {
|
|
playNext();
|
|
}
|
|
}, 400);
|
|
} else {
|
|
const message = JSON.parse(data);
|
|
if (message.type === "user_text") {
|
|
usermsg.push(message.msg);
|
|
} else if (message.type === "llm_text") {
|
|
llmmsg.push(message.msg);
|
|
} else if (message.type === "end") {
|
|
usermessageIsEnd.value = true;
|
|
llmmessageIsEnd.value = true;
|
|
api_index.saveMessage({
|
|
role_id: currentRoleId.value,
|
|
direction: 0,
|
|
message: usermsg.join("")
|
|
}).then(({ status: status2 }) => {
|
|
usermsg.length = 0;
|
|
api_index.saveMessage({
|
|
role_id: currentRoleId.value,
|
|
direction: 1,
|
|
message: llmmsg.join("")
|
|
}).then(({ status: status3 }) => {
|
|
llmmsg.length = 0;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
});
|
|
startRecord();
|
|
});
|
|
} catch (e) {
|
|
}
|
|
});
|
|
const finlizer = () => {
|
|
var _a, _b;
|
|
wx.setKeepScreenOn({
|
|
keepScreenOn: false
|
|
});
|
|
innerAudioContext.destroy();
|
|
stopRecord();
|
|
const data = Object.assign({}, params);
|
|
data.audio = "";
|
|
data.is_close = true;
|
|
(_a = ws.value) == null ? void 0 : _a.sendMsg(data);
|
|
(_b = ws.value) == null ? void 0 : _b.closeSocket();
|
|
};
|
|
const endCall = () => {
|
|
common_vendor.index.navigateBack({
|
|
delta: 1
|
|
});
|
|
};
|
|
const goToChat = () => {
|
|
common_vendor.index.redirectTo({
|
|
url: "/pages/chatOnline/index"
|
|
});
|
|
};
|
|
common_vendor.onUnmounted(() => {
|
|
finlizer();
|
|
});
|
|
const changeLoudSpeaker = () => {
|
|
innerAudioContext.pause();
|
|
wx.setInnerAudioOption({
|
|
speakerOn: !isLoudSpeaker.value
|
|
});
|
|
innerAudioContext.play();
|
|
isLoudSpeaker.value = !isLoudSpeaker.value;
|
|
};
|
|
return (_ctx, _cache) => {
|
|
return {
|
|
a: `${common_vendor.unref(config_index.filePrefix)}${common_vendor.unref(currentBackGroundImage)}`,
|
|
b: `${common_vendor.unref(config_index.filePrefix)}${common_vendor.unref(aiAvatarId)}`,
|
|
c: common_vendor.t(common_vendor.unref(roleName)),
|
|
d: common_vendor.t(common_vendor.unref(status)),
|
|
e: common_vendor.unref(isRecording),
|
|
f: common_vendor.o(($event) => goToChat()),
|
|
g: common_vendor.o(($event) => endCall()),
|
|
h: common_vendor.o(($event) => changeLoudSpeaker()),
|
|
i: common_vendor.o(($event) => goToChat()),
|
|
j: common_vendor.o(($event) => endCall()),
|
|
k: common_vendor.t(common_vendor.unref(isLoudSpeaker) ? "\u626C\u58F0\u5668\u5DF2\u5F00" : "\u626C\u58F0\u5668\u5DF2\u5173"),
|
|
l: common_vendor.o(($event) => changeLoudSpeaker())
|
|
};
|
|
};
|
|
}
|
|
};
|
|
var MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-07f20a9a"], ["__file", "C:/Users/bzm15/Desktop/ix/mini_web-develop/pages/call/index.vue"]]);
|
|
wx.createPage(MiniProgramPage);
|