index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <div class="flex items-center">
  3. <template v-if="state.isStart">
  4. <el-tooltip content="停止语音输入" placement="top">
  5. <div class="voice __hover" @click="onStop">
  6. <div v-for="item in 4" ref="ref_bars" />
  7. </div>
  8. </el-tooltip>
  9. </template>
  10. <template v-else>
  11. <el-tooltip content="语音输入" placement="top">
  12. <div class="__hover size-6" @click="onStart">
  13. <img class="h-full w-full" src="@/assets/images/chat/audio.png" />
  14. </div>
  15. </el-tooltip>
  16. </template>
  17. <template v-if="state.isStart">
  18. <div class="ml-1.5 text-sm text-[#4f4f4f]">
  19. {{ durationCpt }}
  20. </div>
  21. </template>
  22. </div>
  23. </template>
  24. <script setup lang="ts">
  25. import {
  26. computed,
  27. getCurrentInstance,
  28. onMounted,
  29. reactive,
  30. ref,
  31. watch,
  32. } from 'vue'
  33. import { ElMessage } from 'element-plus'
  34. const emit = defineEmits(['onLoading', 'onAudio'])
  35. const props = defineProps({})
  36. const state: any = reactive({
  37. isStart: false,
  38. duration: 0,
  39. mediaRecorder: null,
  40. audioBlob: null,
  41. timer: null,
  42. analyser: null,
  43. animationId: null,
  44. phase: 0,
  45. })
  46. const ref_bars = ref()
  47. const durationCpt = computed(() => {
  48. const minutes = Math.floor(state.duration / 60)
  49. const seconds = Math.floor(state.duration % 60)
  50. return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`
  51. })
  52. const onStart = async () => {
  53. state.isStart = true
  54. try {
  55. // 请求麦克风权限
  56. const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
  57. const audioContext = new (window.AudioContext ||
  58. window.webkitAudioContext)()
  59. state.analyser = audioContext.createAnalyser()
  60. state.analyser.fftSize = 256
  61. const microphone = audioContext.createMediaStreamSource(stream)
  62. microphone.connect(state.analyser)
  63. updateVolumeBars()
  64. state.mediaRecorder = new MediaRecorder(stream)
  65. const audioChunks: any = []
  66. state.mediaRecorder.ondataavailable = (event) => {
  67. audioChunks.push(event.data)
  68. }
  69. state.mediaRecorder.onstop = async () => {
  70. clearInterval(state.timer)
  71. state.audioBlob = new Blob(audioChunks, { type: 'audio/mp3' })
  72. // this.audioUrl = URL.createObjectURL(this.audioBlob)
  73. stream.getTracks().forEach((track) => track.stop())
  74. if (microphone) {
  75. microphone.disconnect()
  76. }
  77. if (audioContext && audioContext.state !== 'closed') {
  78. audioContext.close()
  79. }
  80. cancelAnimationFrame(state.animationId)
  81. // 重置柱状图
  82. ref_bars.value.forEach((bar) => {
  83. bar.style.height = '4px'
  84. })
  85. if (!state.audioBlob) {
  86. ElMessage.error('没有可上传的录音文件')
  87. return
  88. }
  89. try {
  90. const formData = new FormData()
  91. formData.append('file', state.audioBlob)
  92. // const audioResponse = await audioToText(
  93. // `/installed-apps/${import.meta.env.VITE_DIFY_APPID}/audio-to-text`,
  94. // false,
  95. // formData,
  96. // )
  97. emit(
  98. 'onAudio',
  99. '语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容语音内容',
  100. )
  101. } catch (err) {
  102. emit('onAudio', '')
  103. ElMessage.error('上传错误:' + err)
  104. } finally {
  105. emit('onAudio', '')
  106. }
  107. }
  108. state.mediaRecorder.start()
  109. const startTime = Date.now()
  110. state.duration = 0
  111. // 更新录音时长
  112. state.timer = setInterval(() => {
  113. state.duration = Math.floor((Date.now() - startTime) / 1000)
  114. }, 1000)
  115. } catch (err: any) {
  116. ElMessage.error('无法访问麦克风: ' + err.message)
  117. console.error('录音错误:', err)
  118. }
  119. }
  120. const onStop = async () => {
  121. emit('onLoading')
  122. state.isStart = false
  123. if (state.mediaRecorder) {
  124. state.mediaRecorder.stop()
  125. }
  126. }
  127. const updateVolumeBars = () => {
  128. if (!state.isStart) return
  129. const array = new Uint8Array(state.analyser.frequencyBinCount)
  130. state.analyser.getByteFrequencyData(array)
  131. let sum = 0
  132. for (let i = 0; i < array.length; i++) {
  133. sum += array[i]
  134. }
  135. const average = sum / array.length
  136. const baseVolume = Math.min(1, average / 70)
  137. // 更新相位
  138. state.phase += 0.2
  139. if (state.phase > Math.PI * 2) state.phase -= Math.PI * 2
  140. // 更新每个柱子的高度
  141. ref_bars.value.forEach((bar, index) => {
  142. // 每个柱子有轻微相位差
  143. const barPhase = state.phase + (index * Math.PI) / 3
  144. // 波浪因子 (0.5-1.5范围)
  145. const waveFactor = 0.8 + Math.sin(barPhase) * 0.2
  146. // 基础高度 + 音量影响 * 波浪因子
  147. const height =
  148. 4 +
  149. baseVolume *
  150. (index > 0 && index < ref_bars.value.length - 1 ? 15 : 5) *
  151. waveFactor
  152. bar.style.height = `${height}px`
  153. })
  154. state.animationId = requestAnimationFrame(updateVolumeBars)
  155. }
  156. const calculateBarLevels = (volume) => {
  157. // 根据音量计算4个柱子的高度比例
  158. // 无声音时全部为0,有声音时从低到高依次点亮
  159. const thresholds = [0.25, 0.5, 0.75, 1.0]
  160. return thresholds.map((t) =>
  161. Math.max(0, Math.min(1, (volume - (t - 0.25)) * 4)),
  162. )
  163. }
  164. onMounted(() => {})
  165. </script>
  166. <style lang="scss" scoped>
  167. .voice {
  168. background-color: rgba(var(--czr-main-color-rgb), 0.1);
  169. gap: 2px;
  170. width: 32px;
  171. height: 32px;
  172. border-radius: 8px;
  173. display: flex;
  174. align-items: center;
  175. justify-content: center;
  176. > div {
  177. width: 2px;
  178. height: 4px;
  179. background-color: var(--czr-main-color);
  180. transition: height 0.15s ease-out;
  181. &:nth-child(2) {
  182. margin-right: 1px;
  183. }
  184. }
  185. }
  186. </style>