当前位置:   article > 正文

vue+科大讯飞语音听写功能(解决针对vue new Worker报错问题)_vue项目使用讯飞在线语音合成

vue项目使用讯飞在线语音合成

参考1:vue+科大讯飞语音听写功能(解决针对vue new Worker报错问题)_Other world的博客-CSDN博客

参考2:vue中使用web worker - Gerryli - 博客园

 参考3:将PC浏览器、ZOOM等软件正在播放的音频实时转成文字!讯飞语音输入法的妙用 - 知乎

1.查看node版本,本人如下:

本人项目目录,主要用到的画红色圈圈的三个文件

2.添加 package.json 文件版本:

  1. "dependencies": {
  2. enc": "^0.4.0",
  3. "jquery": "^3.4.1",
  4. },
  5. "devDependencies": {
  6. "crypto-js": "^4.0.0",
  7. "vconsole": "^3.3.4",
  8. "vue-template-compiler": "2.6.12",
  9. "worker-loader": "^2.0.0"
  10. }

3. 配置 vue.config.js 文件

 不配置会报错 TypeError: _transcodeWorker.default is not a constructor

 vue.config.js要添加以下配置:

  1. configureWebpack: config => {
  2. config.module.rules.push({
  3. test: /\.worker.js$/,
  4. use: {
  5. loader: 'worker-loader',
  6. options: { inline: true, name: 'workerName.[hash].js' }
  7. }
  8. })
  9. },
  10. 或(因本人配置不是函数模式,采用的是以下对象模式配置):
  11. chainWebpack(config) {
  12. config.output.globalObject('this')
  13. config.module
  14. .rule('worker')
  15. .test(/\.worker.js$/)
  16. .use('worker-loader')
  17. .loader('worker-loader')
  18. .options({ inline: true, name: 'workerName.[hash].js' })
  19. }

在你运行时候,会发现控制台会报错,“window is undefined”,这个是因为worker线程中不存在window对象,因此不能直接使用,要用this代替,要在vue.config.js中添加以下配置

  1. chainWebpack: config => {
  2. config.output.globalObject('this')
  3. }

打包的时候报错就加上:

parallel: false

合成一起就是

  1. module.exports = {
  2. configureWebpack: config => {
  3. config.module.rules.push({
  4. test: /\.worker.js$/,
  5. use: {
  6. loader: 'worker-loader',
  7. options: { inline: true, name: 'workerName.[hash].js' }
  8. }
  9. })
  10. },
  11. parallel: false,
  12. chainWebpack: config => {
  13. config.output.globalObject('this')
  14. }
  15. }

配置完后你会发现不会报错了,然后就可以正常运行了!

3. 创建 transcode.worker.js 文件 ,(在 语音听写流式API demo js语言 讯飞源文件中取)

  1. // (function(){
  2. self.onmessage = function(e){
  3. transAudioData.transcode(e.data)
  4. }
  5. let transAudioData = {
  6. transcode(audioData) {
  7. let output = transAudioData.to16kHz(audioData)
  8. output = transAudioData.to16BitPCM(output)
  9. output = Array.from(new Uint8Array(output.buffer))
  10. self.postMessage(output)
  11. // return output
  12. },
  13. to16kHz(audioData) {
  14. var data = new Float32Array(audioData)
  15. var fitCount = Math.round(data.length * (16000 / 44100))
  16. var newData = new Float32Array(fitCount)
  17. var springFactor = (data.length - 1) / (fitCount - 1)
  18. newData[0] = data[0]
  19. for (let i = 1; i < fitCount - 1; i++) {
  20. var tmp = i * springFactor
  21. var before = Math.floor(tmp).toFixed()
  22. var after = Math.ceil(tmp).toFixed()
  23. var atPoint = tmp - before
  24. newData[i] = data[before] + (data[after] - data[before]) * atPoint
  25. }
  26. newData[fitCount - 1] = data[data.length - 1]
  27. return newData
  28. },
  29. to16BitPCM(input) {
  30. var dataLength = input.length * (16 / 8)
  31. var dataBuffer = new ArrayBuffer(dataLength)
  32. var dataView = new DataView(dataBuffer)
  33. var offset = 0
  34. for (var i = 0; i < input.length; i++, offset += 2) {
  35. var s = Math.max(-1, Math.min(1, input[i]))
  36. dataView.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true)
  37. }
  38. return dataView
  39. },
  40. }
  41. // })()

4. 创建 translation.js 文件 ,(在 语音听写流式API demo js语言 讯飞源文件中取 src\pages\index\index.js)

  1. import CryptoJS from 'crypto-js'
  2. import Enc from 'enc'
  3. import VConsole from 'vconsole'
  4. import $ from 'jquery'
  5. import TransWorker from './js/transcode.worker.js'
  6. import './index.css'
  7. let transWorker = new TransWorker()
  8. //APPID,APISecret,APIKey在控制台-我的应用-语音听写(流式版)页面获取
  9. const APPID = 'd7b51bcb'
  10. const API_SECRET = 'ZTNmZjk3N2FkZTljZjg0YTYzMGZiNmZj'
  11. const API_KEY = '4bc26e6fd3868195919a8c14054eac66'
  12. /**
  13. * 获取websocket url
  14. * 该接口需要后端提供,这里为了方便前端处理
  15. */
  16. function getWebSocketUrl() {
  17. return new Promise((resolve, reject) => {
  18. // 请求地址根据语种不同变化
  19. var url = 'wss://iat-api.xfyun.cn/v2/iat'
  20. var host = 'iat-api.xfyun.cn'
  21. var apiKey = API_KEY
  22. var apiSecret = API_SECRET
  23. var date = new Date().toGMTString()
  24. var algorithm = 'hmac-sha256'
  25. var headers = 'host date request-line'
  26. var signatureOrigin = `host: ${host}\ndate: ${date}\nGET /v2/iat HTTP/1.1`
  27. var signatureSha = CryptoJS.HmacSHA256(signatureOrigin, apiSecret)
  28. var signature = CryptoJS.enc.Base64.stringify(signatureSha)
  29. var authorizationOrigin = `api_key="${apiKey}", algorithm="${algorithm}", headers="${headers}", signature="${signature}"`
  30. var authorization = btoa(authorizationOrigin)
  31. url = `${url}?authorization=${authorization}&date=${date}&host=${host}`
  32. resolve(url)
  33. })
  34. }
  35. class IatRecorder {
  36. constructor({ language, accent, appId } = {}) {
  37. let self = this
  38. this.status = 'null'
  39. this.language = language || 'zh_cn'
  40. this.accent = accent || 'mandarin'
  41. this.appId = appId || APPID
  42. // 记录音频数据
  43. this.audioData = []
  44. // 记录听写结果
  45. this.resultText = ''
  46. // wpgs下的听写结果需要中间状态辅助记录
  47. this.resultTextTemp = ''
  48. transWorker.onmessage = function (event) {
  49. self.audioData.push(...event.data)
  50. }
  51. }
  52. // 修改录音听写状态
  53. setStatus(status) {
  54. this.onWillStatusChange && this.status !== status && this.onWillStatusChange(this.status, status)
  55. this.status = status
  56. }
  57. setResultText({ resultText, resultTextTemp } = {}) {
  58. this.onTextChange && this.onTextChange(resultTextTemp || resultText || '')
  59. resultText !== undefined && (this.resultText = resultText)
  60. resultTextTemp !== undefined && (this.resultTextTemp = resultTextTemp)
  61. }
  62. // 修改听写参数
  63. setParams({ language, accent } = {}) {
  64. language && (this.language = language)
  65. accent && (this.accent = accent)
  66. }
  67. // 连接websocket
  68. connectWebSocket() {
  69. return getWebSocketUrl().then(url => {
  70. console.log(url)
  71. let iatWS
  72. if ('WebSocket' in window) {
  73. iatWS = new WebSocket(url)
  74. } else if ('MozWebSocket' in window) {
  75. iatWS = new MozWebSocket(url)
  76. } else {
  77. alert('浏览器不支持WebSocket')
  78. return
  79. }
  80. this.webSocket = iatWS
  81. this.setStatus('init')
  82. iatWS.onopen = e => {
  83. this.setStatus('ing')
  84. // 重新开始录音
  85. setTimeout(() => {
  86. this.webSocketSend()
  87. }, 100)
  88. }
  89. iatWS.onmessage = e => {
  90. this.result(e.data)
  91. }
  92. iatWS.onerror = e => {
  93. this.recorderStop()
  94. }
  95. iatWS.onclose = e => {
  96. this.recorderStop()
  97. }
  98. })
  99. }
  100. // 初始化浏览器录音
  101. recorderInit() {
  102. navigator.getUserMedia =
  103. navigator.getUserMedia ||
  104. navigator.webkitGetUserMedia ||
  105. navigator.mozGetUserMedia ||
  106. navigator.msGetUserMedia
  107. // 创建音频环境
  108. try {
  109. this.audioContext = new (window.AudioContext || window.webkitAudioContext)()
  110. this.audioContext.resume()
  111. if (!this.audioContext) {
  112. alert('浏览器不支持webAudioApi相关接口')
  113. return
  114. }
  115. } catch (e) {
  116. if (!this.audioContext) {
  117. alert('浏览器不支持webAudioApi相关接口')
  118. return
  119. }
  120. }
  121. // 获取浏览器录音权限
  122. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  123. navigator.mediaDevices
  124. .getUserMedia({
  125. audio: true,
  126. video: false,
  127. })
  128. .then(stream => {
  129. getMediaSuccess(stream)
  130. })
  131. .catch(e => {
  132. getMediaFail(e)
  133. })
  134. } else if (navigator.getUserMedia) {
  135. navigator.getUserMedia(
  136. {
  137. audio: true,
  138. video: false,
  139. },
  140. stream => {
  141. getMediaSuccess(stream)
  142. },
  143. function(e) {
  144. getMediaFail(e)
  145. }
  146. )
  147. } else {
  148. if (navigator.userAgent.toLowerCase().match(/chrome/) && location.origin.indexOf('https://') < 0) {
  149. alert('chrome下获取浏览器录音功能,因为安全性问题,需要在localhost或127.0.0.1或https下才能获取权限')
  150. } else {
  151. alert('无法获取浏览器录音功能,请升级浏览器或使用chrome')
  152. }
  153. this.audioContext && this.audioContext.close()
  154. return
  155. }
  156. // 获取浏览器录音权限成功的回调
  157. let getMediaSuccess = stream => {
  158. console.log('getMediaSuccess')
  159. // 创建一个用于通过JavaScript直接处理音频
  160. this.scriptProcessor = this.audioContext.createScriptProcessor(0, 1, 1)
  161. this.scriptProcessor.onaudioprocess = e => {
  162. // 去处理音频数据
  163. if (this.status === 'ing') {
  164. transWorker.postMessage(e.inputBuffer.getChannelData(0))
  165. }
  166. }
  167. // 创建一个新的MediaStreamAudioSourceNode 对象,使来自MediaStream的音频可以被播放和操作
  168. this.mediaSource = this.audioContext.createMediaStreamSource(stream)
  169. // 连接
  170. this.mediaSource.connect(this.scriptProcessor)
  171. this.scriptProcessor.connect(this.audioContext.destination)
  172. this.connectWebSocket()
  173. }
  174. let getMediaFail = (e) => {
  175. alert('请求麦克风失败')
  176. console.log(e)
  177. this.audioContext && this.audioContext.close()
  178. this.audioContext = undefined
  179. // 关闭websocket
  180. if (this.webSocket && this.webSocket.readyState === 1) {
  181. this.webSocket.close()
  182. }
  183. }
  184. }
  185. recorderStart() {
  186. if (!this.audioContext) {
  187. console.log("11111111")
  188. this.recorderInit()
  189. } else {
  190. console.log("22222222")
  191. this.audioContext.resume()
  192. this.connectWebSocket()
  193. }
  194. }
  195. // 暂停录音
  196. recorderStop() {
  197. // safari下suspend后再次resume录音内容将是空白,设置safari下不做suspend
  198. if (!(/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgen))){
  199. this.audioContext && this.audioContext.suspend()
  200. }
  201. this.setStatus('end')
  202. }
  203. // 处理音频数据
  204. // transAudioData(audioData) {
  205. // audioData = transAudioData.transaction(audioData)
  206. // this.audioData.push(...audioData)
  207. // }
  208. // 对处理后的音频数据进行base64编码,
  209. toBase64(buffer) {
  210. var binary = ''
  211. var bytes = new Uint8Array(buffer)
  212. var len = bytes.byteLength
  213. for (var i = 0; i < len; i++) {
  214. binary += String.fromCharCode(bytes[i])
  215. }
  216. return window.btoa(binary)
  217. }
  218. // 向webSocket发送数据
  219. webSocketSend() {
  220. if (this.webSocket.readyState !== 1) {
  221. return
  222. }
  223. let audioData = this.audioData.splice(0, 1280)
  224. console.log(audioData)
  225. var params = {
  226. common: {
  227. app_id: this.appId,
  228. },
  229. business: {
  230. language: this.language, //小语种可在控制台--语音听写(流式)--方言/语种处添加试用
  231. domain: 'iat',
  232. accent: this.accent, //中文方言可在控制台--语音听写(流式)--方言/语种处添加试用
  233. vad_eos: 60*60*1000,
  234. dwa: 'wpgs', //为使该功能生效,需到控制台开通动态修正功能(该功能免费)
  235. },
  236. data: {
  237. status: 0,
  238. format: 'audio/L16;rate=16000',
  239. encoding: 'raw',
  240. audio: this.toBase64(audioData),
  241. },
  242. }
  243. this.webSocket.send(JSON.stringify(params))
  244. this.handlerInterval = setInterval(() => {
  245. // websocket未连接
  246. if (this.webSocket.readyState !== 1) {
  247. console.log("websocket未连接")
  248. this.audioData = []
  249. clearInterval(this.handlerInterval)
  250. return
  251. }
  252. if (this.audioData.length === 0) {
  253. console.log("自动关闭",this.status)
  254. if (this.status === 'end') {
  255. this.webSocket.send(
  256. JSON.stringify({
  257. data: {
  258. status: 2,
  259. format: 'audio/L16;rate=16000',
  260. encoding: 'raw',
  261. audio: '',
  262. },
  263. })
  264. )
  265. this.audioData = []
  266. clearInterval(this.handlerInterval)
  267. }
  268. return false
  269. }
  270. audioData = this.audioData.splice(0, 1280)
  271. // 中间帧
  272. this.webSocket.send(
  273. JSON.stringify({
  274. data: {
  275. status: 1,
  276. format: 'audio/L16;rate=16000',
  277. encoding: 'raw',
  278. audio: this.toBase64(audioData),
  279. },
  280. })
  281. )
  282. }, 40)
  283. }
  284. result(resultData) {
  285. // 识别结束
  286. let jsonData = JSON.parse(resultData)
  287. if (jsonData.data && jsonData.data.result) {
  288. let data = jsonData.data.result
  289. let str = ''
  290. let resultStr = ''
  291. let ws = data.ws
  292. for (let i = 0; i < ws.length; i++) {
  293. str = str + ws[i].cw[0].w
  294. }
  295. console.log("识别的结果为:",str)
  296. // 开启wpgs会有此字段(前提:在控制台开通动态修正功能)
  297. // 取值为 "apd"时表示该片结果是追加到前面的最终结果;取值为"rpl" 时表示替换前面的部分结果,替换范围为rg字段
  298. if (data.pgs) {
  299. if (data.pgs === 'apd') {
  300. // 将resultTextTemp同步给resultText
  301. this.setResultText({
  302. resultText: this.resultTextTemp,
  303. })
  304. }
  305. // 将结果存储在resultTextTemp中
  306. this.setResultText({
  307. resultTextTemp: this.resultText + str,
  308. })
  309. } else {
  310. this.setResultText({
  311. resultText: this.resultText + str,
  312. })
  313. }
  314. }
  315. if (jsonData.code === 0 && jsonData.data.status === 2) {
  316. this.webSocket.close()
  317. }
  318. if (jsonData.code !== 0) {
  319. this.webSocket.close()
  320. console.log(`${jsonData.code}:${jsonData.message}`)
  321. }
  322. }
  323. start() {
  324. this.recorderStart()
  325. this.setResultText({ resultText: '', resultTextTemp: '' })
  326. }
  327. stop() {
  328. this.recorderStop()
  329. }
  330. }
  331. // ======================开始调用=============================
  332. var vConsole = new VConsole()
  333. let iatRecorder = new IatRecorder()
  334. let countInterval
  335. // 状态改变时处罚
  336. iatRecorder.onWillStatusChange = function(oldStatus, status) {
  337. // 可以在这里进行页面中一些交互逻辑处理:倒计时(听写只有60s),录音的动画,按钮交互等
  338. // 按钮中的文字
  339. let text = {
  340. null: '开始识别', // 最开始状态
  341. init: '开始识别', // 初始化状态
  342. ing: '结束识别', // 正在录音状态
  343. end: '开始识别', // 结束状态
  344. }
  345. let senconds = 0
  346. $('.taste-button')
  347. .removeClass(`status-${oldStatus}`)
  348. .addClass(`status-${status}`)
  349. .text(text[status])
  350. if (status === 'ing') {
  351. $('hr').addClass('hr')
  352. $('.taste-content').css('display', 'none')
  353. $('.start-taste').addClass('flex-display-1')
  354. // 倒计时相关
  355. countInterval = setInterval(()=>{
  356. senconds++
  357. console.log("==========="+senconds)
  358. $('.used-time').text(`0${Math.floor(senconds/60)}${Math.floor(senconds/10)}${senconds%10}`)
  359. /*if (senconds >= 60) {
  360. this.stop()
  361. clearInterval(countInterval)
  362. }*/
  363. }, 1000)
  364. } else if (status === 'init') {
  365. $('.time-box').show()
  366. $('.used-time').text('00:00')
  367. } else {
  368. $('.time-box').hide()
  369. $('hr').removeClass('hr')
  370. clearInterval(countInterval)
  371. }
  372. }
  373. $(function () {
  374. // 监听识别结果的变化
  375. iatRecorder.onTextChange = function(text) {
  376. $('#result_output').text(text)
  377. }
  378. $('#taste_button, .taste-button').click(function() {
  379. if (iatRecorder.status === 'ing') {
  380. iatRecorder.stop()
  381. } else {
  382. iatRecorder.start()
  383. }
  384. })
  385. });

5. 创建 translation.vue 文件 ,(在 语音听写流式API demo js语言 讯飞源文件中取 src\pages\index\index.html)

  1. <template>
  2. <div class="service-item service-item-taste">
  3. <h2 class="service-item-title">产品体验-语音听写(流式版)WebAPI</h2>
  4. <a href="/doc" target="_blank">demo文档说明</a>
  5. <div class="service-item-content service-item-taste-content">
  6. <div class="taste-content">
  7. <button class="taste-button ready-button" id="taste_button">开始识别</button>
  8. </div>
  9. <div class="start-taste">
  10. <div class="start-taste-left">
  11. <div class="time-box">
  12. <span class="start-taste-line">
  13. <hr class="hr hr1">
  14. <hr class="hr hr2">
  15. <hr class="hr hr3">
  16. <hr class="hr hr4">
  17. <hr class="hr hr5">
  18. <hr class="hr hr6">
  19. <hr class="hr hr7">
  20. <hr class="hr hr8">
  21. <hr class="hr hr9">
  22. <hr class="hr hr10">
  23. </span>
  24. <span class="total-time"><span class="used-time">00: 00</span> / 01: 00</span>
  25. </div>
  26. <div class="start-taste-button">
  27. <button class="taste-button start-button">结束识别</button>
  28. </div>
  29. </div>
  30. <div class="output-box" id="result_output"></div>
  31. </div>
  32. </div>
  33. </div>
  34. </template>
  35. <script>
  36. import translation from './translation.js'
  37. export default {
  38. name: 'translation',
  39. data() {
  40. return {
  41. phone: '',
  42. }
  43. },
  44. // mixins: [translation],
  45. created() {
  46. // transWorker
  47. // translation.transW
  48. },
  49. mounted() {
  50. },
  51. methods: {
  52. },
  53. destroyed() {
  54. },
  55. }
  56. </script>
  57. <style >
  58. .service-item-taste button {
  59. cursor: pointer;
  60. }
  61. .service-item-taste .taste-button {
  62. background: #187cff;
  63. border: 1px solid;
  64. border-color: #478eea;
  65. color: #fff;
  66. text-align: center;
  67. border-radius: 3px;
  68. }
  69. .service-item-taste .taste-header .dialect-select {
  70. margin-left: 20px;
  71. height: 26px;
  72. }
  73. .service-item-taste .taste-header .dialect {
  74. margin-left: 20px;
  75. height: 26px;
  76. line-height: 26px;
  77. display: none;
  78. }
  79. .service-item-taste .taste-header a {
  80. border: none;
  81. border-radius: 4px;
  82. color: #fff;
  83. height: 26px;
  84. width: 100px;
  85. float: right;
  86. text-align: center;
  87. line-height: 26px;
  88. }
  89. .service-item-taste .taste-content {
  90. display: -ms-flexbox;
  91. display: flex;
  92. -ms-flex-align: center;
  93. align-items: center;
  94. margin-top: 100px;
  95. }
  96. .service-item-taste .start-taste {
  97. margin-top: 30px;
  98. display: none;
  99. -ms-flex-pack: justify;
  100. justify-content: space-between;
  101. }
  102. .service-item-taste .start-taste.flex-display-1{
  103. display: flex;
  104. }
  105. .service-item-taste .start-taste .start-taste-left {
  106. width: 40%;
  107. margin-left: 30px;
  108. }
  109. .service-item-taste .start-taste .start-taste-left .time-box {
  110. margin-top: 40px;
  111. display: -ms-flexbox;
  112. display: flex;
  113. -ms-flex-pack: center;
  114. justify-content: center;
  115. }
  116. .service-item-taste .start-taste .start-taste-left .time-box .total-time {
  117. margin-left: 20px;
  118. }
  119. .service-item-taste .start-taste .start-taste-left .time-box .start-taste-line {
  120. display: inline-block;
  121. margin-right: 20px;
  122. }
  123. .service-item-taste .start-taste .start-taste-left .time-box .start-taste-line hr {
  124. background-color: #187cff;
  125. width: 3px;
  126. height: 10px;
  127. margin: 0 5px;
  128. display: inline-block;
  129. border: none;
  130. }
  131. .service-item-taste .start-taste .start-taste-left .start-taste-button {
  132. display: -ms-flexbox;
  133. display: flex;
  134. margin-top: 70px;
  135. }
  136. .service-item-taste .start-taste .output-box {
  137. height: 200px;
  138. overflow: auto;
  139. background: #f0f0f0;
  140. width: 50%;
  141. line-height: 1.5;
  142. padding-left: 10px;
  143. padding-top: 10px;
  144. }
  145. .hr {
  146. animation: note 0.2s ease-in-out;
  147. animation-iteration-count: infinite;
  148. animation-direction: alternate;
  149. }
  150. .hr1 {
  151. animation-delay: -1s;
  152. }
  153. .hr2 {
  154. animation-delay: -0.9s;
  155. }
  156. .hr3 {
  157. animation-delay: -0.8s;
  158. }
  159. .hr4 {
  160. animation-delay: -0.7s;
  161. }
  162. .hr5 {
  163. animation-delay: -0.6s;
  164. }
  165. .hr6 {
  166. animation-delay: -0.5s;
  167. }
  168. .hr7 {
  169. animation-delay: -0.4s;
  170. }
  171. .hr8 {
  172. animation-delay: -0.3s;
  173. }
  174. .hr9 {
  175. animation-delay: -0.2s;
  176. }
  177. .hr10 {
  178. animation-delay: -0.1s;
  179. }
  180. @keyframes note {
  181. from {
  182. transform: scaleY(1);
  183. }
  184. to {
  185. transform: scaleY(4);
  186. }
  187. }
  188. .ready-button,
  189. .start-button {
  190. margin: 0 auto;
  191. height: 40px;
  192. width: 160px;
  193. font-size: 16px;
  194. letter-spacing: 6px;
  195. }
  196. .taste-button:hover {
  197. background: #0b99ff;
  198. }
  199. </style>

测试成功

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号