import { v4 as uuid4 } from 'uuid' import _ from 'lodash' export async function registerVoiceprint({ name, userId, organizeId, file }) { // 检查文件的 MIME 类型是否为 WAV 文件 if (file.type !== 'audio/wav') { throw new Error('文件类型必须是 WAV'); } const formData = new FormData(); formData.append('file', file, file.name); const registerRes = await fetch('https://conference.voiceaitech.com/api/convoice/feature/extract', { // 替换为实际的上传URL method: 'POST', body: formData, }).then(response => response.json()) if (!_.get(registerRes, ['result', 'featureData'])) { throw new Error("声音文件不达标,请注意文件时长大于1分钟和音量"); } const featureData = registerRes.result.featureData const insertRes = await fetch('https://gpt4.cocorobo.cn/insert_sy_voiceprint', { method: 'POST', body: JSON.stringify({ name, featureData, user_id: userId, organize_id: organizeId, }), headers: { 'Content-Type': 'application/json', hwMac: organizeId } }).then(res => res.json()) if (!_.get(insertRes, ['FunctionResponse'])) { throw new Error('上传失败') } return featureData } export async function getVoiceprints({ userId, organizeId }) { const res = await fetch('https://gpt4.cocorobo.cn/get_sy_voiceprint', { method: 'POST', body: JSON.stringify({ user_id: userId, }), headers: { 'Content-Type': 'application/json', hwMac: organizeId } }).then(res => res.json()) if (!_.get(res, ['FunctionResponse'])) { throw new Error('获取声纹列表失败') } return JSON.parse(res.FunctionResponse) } export async function createWs({ enroll_infos }) { // 创建一个WebSocket实例 const ws = new WebSocket( 'wss://conference.voiceaitech.com/api/convoice/streaming' ); let _resolve // const p = new Promise(resolve => _resolve = resolve) ws.onopen = () => { const data = { eventName: 'start', sessionId: uuid4(), timestamp: new Date().getTime(), convoiceInfo: { options: 2, hotwords: [ { weight: 5, hotword: '上课', }, // TODO ], enroll_infos, }, }; ws.send(JSON.stringify(data)); // setTimeout(() => { // _resolve() // }, 3000) }; // await p // 处理来自服务器的消息 // ws.onmessage = (e) => { // const res = JSON.parse(e.data); // if (res?.userdata?.results?.length > 0) { // this.role_arr = res.userdata.results; // } // if (res?.userdata?.last_block) { // ws.close(); // } // }; // 处理错误 // ws.onerror = (error) => { // console.error('WebSocket error:', error); // }; return ws }