shengyang.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { v4 as uuid4 } from 'uuid'
  2. import _ from 'lodash'
  3. export async function registerVoiceprint({ name, userId, organizeId, file }) {
  4. // 检查文件的 MIME 类型是否为 WAV 文件
  5. if (file.type !== 'audio/wav') {
  6. throw new Error('文件类型必须是 WAV');
  7. }
  8. const formData = new FormData();
  9. formData.append('file', file, file.name);
  10. const registerRes = await fetch('https://conference.voiceaitech.com/api/convoice/feature/extract', { // 替换为实际的上传URL
  11. method: 'POST',
  12. body: formData,
  13. }).then(response => response.json())
  14. if (!_.get(registerRes, ['result', 'featureData'])) {
  15. throw new Error("声音文件不达标,请注意文件时长大于1分钟和音量");
  16. }
  17. const featureData = registerRes.result.featureData
  18. const insertRes = await fetch('https://gpt4.cocorobo.cn/insert_sy_voiceprint', {
  19. method: 'POST',
  20. body: JSON.stringify({
  21. name,
  22. featureData,
  23. user_id: userId,
  24. organize_id: organizeId,
  25. }),
  26. headers: {
  27. 'Content-Type': 'application/json',
  28. hwMac: organizeId
  29. }
  30. }).then(res => res.json())
  31. if (!_.get(insertRes, ['FunctionResponse'])) {
  32. throw new Error('上传失败')
  33. }
  34. return featureData
  35. }
  36. export async function getVoiceprints({ userId, organizeId }) {
  37. const res = await fetch('https://gpt4.cocorobo.cn/get_sy_voiceprint', {
  38. method: 'POST',
  39. body: JSON.stringify({
  40. user_id: userId,
  41. }),
  42. headers: {
  43. 'Content-Type': 'application/json',
  44. hwMac: organizeId
  45. }
  46. }).then(res => res.json())
  47. if (!_.get(res, ['FunctionResponse'])) {
  48. throw new Error('获取声纹列表失败')
  49. }
  50. return JSON.parse(res.FunctionResponse)
  51. }
  52. export async function createWs({ enroll_infos }) {
  53. // 创建一个WebSocket实例
  54. const ws = new WebSocket(
  55. 'wss://conference.voiceaitech.com/api/convoice/streaming'
  56. );
  57. let _resolve
  58. // const p = new Promise(resolve => _resolve = resolve)
  59. ws.onopen = () => {
  60. const data = {
  61. eventName: 'start',
  62. sessionId: uuid4(),
  63. timestamp: new Date().getTime(),
  64. convoiceInfo: {
  65. options: 2,
  66. hotwords: [
  67. {
  68. weight: 5,
  69. hotword: '上课',
  70. },
  71. // TODO
  72. ],
  73. enroll_infos,
  74. },
  75. };
  76. ws.send(JSON.stringify(data));
  77. // setTimeout(() => {
  78. // _resolve()
  79. // }, 3000)
  80. };
  81. // await p
  82. // 处理来自服务器的消息
  83. // ws.onmessage = (e) => {
  84. // const res = JSON.parse(e.data);
  85. // if (res?.userdata?.results?.length > 0) {
  86. // this.role_arr = res.userdata.results;
  87. // }
  88. // if (res?.userdata?.last_block) {
  89. // ws.close();
  90. // }
  91. // };
  92. // 处理错误
  93. // ws.onerror = (error) => {
  94. // console.error('WebSocket error:', error);
  95. // };
  96. return ws
  97. }