index.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { useCallback, useEffect, useRef, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useParams, usePathname } from 'next/navigation'
  4. import {
  5. RiCloseLine,
  6. RiLoader2Line,
  7. } from '@remixicon/react'
  8. import Recorder from 'js-audio-recorder'
  9. import { useRafInterval } from 'ahooks'
  10. import { convertToMp3 } from './utils'
  11. import s from './index.module.css'
  12. import cn from '@/utils/classnames'
  13. import { StopCircle } from '@/app/components/base/icons/src/vender/solid/mediaAndDevices'
  14. import { audioToText } from '@/service/share'
  15. type VoiceInputTypes = {
  16. onConverted: (text: string) => void
  17. onCancel: () => void
  18. wordTimestamps?: string
  19. }
  20. const VoiceInput = ({
  21. onCancel,
  22. onConverted,
  23. wordTimestamps,
  24. }: VoiceInputTypes) => {
  25. const { t } = useTranslation()
  26. const recorder = useRef(new Recorder({
  27. sampleBits: 16,
  28. sampleRate: 16000,
  29. numChannels: 1,
  30. compiling: false,
  31. }))
  32. const canvasRef = useRef<HTMLCanvasElement | null>(null)
  33. const ctxRef = useRef<CanvasRenderingContext2D | null>(null)
  34. const drawRecordId = useRef<number | null>(null)
  35. const [originDuration, setOriginDuration] = useState(0)
  36. const [startRecord, setStartRecord] = useState(false)
  37. const [startConvert, setStartConvert] = useState(false)
  38. const pathname = usePathname()
  39. const params = useParams()
  40. const clearInterval = useRafInterval(() => {
  41. setOriginDuration(originDuration + 1)
  42. }, 1000)
  43. const drawRecord = useCallback(() => {
  44. drawRecordId.current = requestAnimationFrame(drawRecord)
  45. const canvas = canvasRef.current!
  46. const ctx = ctxRef.current!
  47. const dataUnit8Array = recorder.current.getRecordAnalyseData()
  48. const dataArray = [].slice.call(dataUnit8Array)
  49. const lineLength = parseInt(`${canvas.width / 3}`)
  50. const gap = parseInt(`${1024 / lineLength}`)
  51. ctx.clearRect(0, 0, canvas.width, canvas.height)
  52. ctx.beginPath()
  53. let x = 0
  54. for (let i = 0; i < lineLength; i++) {
  55. let v = dataArray.slice(i * gap, i * gap + gap).reduce((prev: number, next: number) => {
  56. return prev + next
  57. }, 0) / gap
  58. if (v < 128)
  59. v = 128
  60. if (v > 178)
  61. v = 178
  62. const y = (v - 128) / 50 * canvas.height
  63. ctx.moveTo(x, 16)
  64. if (ctx.roundRect)
  65. ctx.roundRect(x, 16 - y, 2, y, [1, 1, 0, 0])
  66. else
  67. ctx.rect(x, 16 - y, 2, y)
  68. ctx.fill()
  69. x += 3
  70. }
  71. ctx.closePath()
  72. }, [])
  73. const handleStopRecorder = useCallback(async () => {
  74. clearInterval()
  75. setStartRecord(false)
  76. setStartConvert(true)
  77. recorder.current.stop()
  78. drawRecordId.current && cancelAnimationFrame(drawRecordId.current)
  79. drawRecordId.current = null
  80. const canvas = canvasRef.current!
  81. const ctx = ctxRef.current!
  82. ctx.clearRect(0, 0, canvas.width, canvas.height)
  83. const mp3Blob = convertToMp3(recorder.current)
  84. const mp3File = new File([mp3Blob], 'temp.mp3', { type: 'audio/mp3' })
  85. const formData = new FormData()
  86. formData.append('file', mp3File)
  87. formData.append('word_timestamps', wordTimestamps || 'disabled')
  88. let url = ''
  89. let isPublic = false
  90. if (params.token) {
  91. url = '/audio-to-text'
  92. isPublic = true
  93. }
  94. else if (params.appId) {
  95. if (pathname.search('explore/installed') > -1)
  96. url = `/installed-apps/${params.appId}/audio-to-text`
  97. else
  98. url = `/apps/${params.appId}/audio-to-text`
  99. }
  100. try {
  101. const audioResponse = await audioToText(url, isPublic, formData)
  102. onConverted(audioResponse.text)
  103. onCancel()
  104. }
  105. catch (e) {
  106. onConverted('')
  107. onCancel()
  108. }
  109. }, [clearInterval, onCancel, onConverted, params.appId, params.token, pathname, wordTimestamps])
  110. const handleStartRecord = async () => {
  111. try {
  112. await recorder.current.start()
  113. setStartRecord(true)
  114. setStartConvert(false)
  115. if (canvasRef.current && ctxRef.current)
  116. drawRecord()
  117. }
  118. catch (e) {
  119. onCancel()
  120. }
  121. }
  122. const initCanvas = () => {
  123. const dpr = window.devicePixelRatio || 1
  124. const canvas = document.getElementById('voice-input-record') as HTMLCanvasElement
  125. if (canvas) {
  126. const { width: cssWidth, height: cssHeight } = canvas.getBoundingClientRect()
  127. canvas.width = dpr * cssWidth
  128. canvas.height = dpr * cssHeight
  129. canvasRef.current = canvas
  130. const ctx = canvas.getContext('2d')
  131. if (ctx) {
  132. ctx.scale(dpr, dpr)
  133. ctx.fillStyle = 'rgba(209, 224, 255, 1)'
  134. ctxRef.current = ctx
  135. }
  136. }
  137. }
  138. if (originDuration >= 600 && startRecord)
  139. handleStopRecorder()
  140. useEffect(() => {
  141. initCanvas()
  142. handleStartRecord()
  143. const recorderRef = recorder?.current
  144. return () => {
  145. recorderRef?.stop()
  146. }
  147. }, [])
  148. const minutes = parseInt(`${parseInt(`${originDuration}`) / 60}`)
  149. const seconds = parseInt(`${originDuration}`) % 60
  150. return (
  151. <div className={cn(s.wrapper, 'absolute inset-0 rounded-xl')}>
  152. <div className='absolute inset-[1.5px] flex items-center pl-[14.5px] pr-[6.5px] py-[14px] bg-primary-25 rounded-[10.5px] overflow-hidden'>
  153. <canvas id='voice-input-record' className='absolute left-0 bottom-0 w-full h-4' />
  154. {
  155. startConvert && <RiLoader2Line className='animate-spin mr-2 w-4 h-4 text-primary-700' />
  156. }
  157. <div className='grow'>
  158. {
  159. startRecord && (
  160. <div className='text-sm text-gray-500'>
  161. {t('common.voiceInput.speaking')}
  162. </div>
  163. )
  164. }
  165. {
  166. startConvert && (
  167. <div className={cn(s.convert, 'text-sm')}>
  168. {t('common.voiceInput.converting')}
  169. </div>
  170. )
  171. }
  172. </div>
  173. {
  174. startRecord && (
  175. <div
  176. className='flex justify-center items-center mr-1 w-8 h-8 hover:bg-primary-100 rounded-lg cursor-pointer'
  177. onClick={handleStopRecorder}
  178. >
  179. <StopCircle className='w-5 h-5 text-primary-600' />
  180. </div>
  181. )
  182. }
  183. {
  184. startConvert && (
  185. <div
  186. className='flex justify-center items-center mr-1 w-8 h-8 hover:bg-gray-200 rounded-lg cursor-pointer'
  187. onClick={onCancel}
  188. >
  189. <RiCloseLine className='w-4 h-4 text-gray-500' />
  190. </div>
  191. )
  192. }
  193. <div className={`w-[45px] pl-1 text-xs font-medium ${originDuration > 500 ? 'text-[#F04438]' : 'text-gray-700'}`}>{`0${minutes.toFixed(0)}:${seconds >= 10 ? seconds : `0${seconds}`}`}</div>
  194. </div>
  195. </div>
  196. )
  197. }
  198. export default VoiceInput