当前位置:   article > 正文

科大讯飞实时语音转写_前端 科大讯飞语音识别转文字

前端 科大讯飞语音识别转文字

vue使用科大讯飞实时语音转写示例
官方文档:https://www.xfyun.cn/doc/asr/rtasr/API.html
1.需要申请先APPID、API_KEY
2.把官方案例js的示例下载下来在这里插入图片描述
3.引入了下载的官方案例中的几个文件在这里插入图片描述
4.示例代码

`<template>
    <div class="home">
        <button id="startBtn">
        开始
      </button>
      <button id="endBtn">
        结束
      </button>
      <div id="output"></div>
      <div id="time" v-show="false">00: 00</div>
    </div>
  </template>`
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
export default {
    name: 'HomeView',
    components: {
  
    },
    mounted() {
  
      this.audioInit()
    },
    methods: {
      audioInit() {
        // 初始化语音转写
        let startBtn = document.getElementById("startBtn")
        // 结束按钮
        let endBtn = document.getElementById("endBtn")
        // 接收识别的文本
        let output = document.getElementById("output")
        // 记时器
        let time = document.getElementById("time")
        // 如果使用的是webpack,或者报错 请尝试这种写法  const recorderWorker = new Worker('../assets/js/transformpcm.worker.js')
        const recorderWorker = new Worker(new URL('../utils/transformpcm.worker.js', import.meta.url))
        // 记录处理的缓存音频
        let buffer = []
        let AudioContext = window.AudioContext || window.webkitAudioContext
        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia
  
        recorderWorker.onmessage = function (e) {
          buffer.push(...e.data.buffer)
        }
  
        class IatRecorder {
          constructor(config) {
            this.config = config
            this.state = 'ing'
  
            //以下信息在控制台-我的应用-实时语音转写 页面获取
            this.appId = '5febed45'
            this.apiKey = 'a61ff9579e78926146465f9b48be93c2'
          }
  
          start() {
            this.stop()
            if (navigator.getUserMedia && AudioContext) {
              this.state = 'ing'
              if (!this.recorder) {
                var context = new AudioContext()
                this.context = context
                console.log(context)
                this.recorder = context.createScriptProcessor(0, 1, 1)
  
                var getMediaSuccess = (stream) => {
                  var mediaStream = this.context.createMediaStreamSource(stream)
                  this.mediaStream = mediaStream
                  this.recorder.onaudioprocess = (e) => {
                    this.sendData(e.inputBuffer.getChannelData(0))
                  }
                  this.connectWebsocket()
                }
                var getMediaFail = (e) => {
                  this.recorder = null
                  this.mediaStream = null
                  this.context = null
                  console.log('请求麦克风失败')
                  alert("请求麦克风失败")
                }
                if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
                  navigator.mediaDevices.getUserMedia({
                    audio: true,
                    video: false
                  }).then((stream) => {
                    console.log(stream)
                    getMediaSuccess(stream)
                  }).catch((e) => {
                    console.log(e)
                    getMediaFail(e)
                  })
                } else {
                  navigator.getUserMedia({
                    audio: true,
                    video: false
                  }, (stream) => {
                    getMediaSuccess(stream)
                  }, function (e) {
                    console.log(e)
                    getMediaFail(e)
                  })
                }
              } else {
                this.connectWebsocket()
              }
            } else {
              var isChrome = navigator.userAgent.toLowerCase().match(/chrome/)
              alert("暂不支持使用该浏览器,请使用chrome浏览器")
            }
          }
  
          stop() {
            this.state = 'end'
            try {
              this.mediaStream.disconnect(this.recorder)
              this.recorder.disconnect()
            } catch (e) { }
          }
  
          sendData(buffer) {
            recorderWorker.postMessage({
              command: 'transform',
              buffer: buffer
            })
          }
          // 生成握手参数
          getHandShakeParams() {
            var appId = this.appId
            var secretKey = this.apiKey
            var ts = Math.floor(new Date().getTime() / 1000);//new Date().getTime()/1000+'';
            var signa = hex_md5(appId + ts)//hex_md5(encodeURIComponent(appId + ts));//EncryptUtil.HmacSHA1Encrypt(EncryptUtil.MD5(appId + ts), secretKey);
            var signatureSha = CryptoJSNew.HmacSHA1(signa, secretKey)
            var signature = CryptoJS.enc.Base64.stringify(signatureSha)
            signature = encodeURIComponent(signature)
            return "?appid=" + appId + "&ts=" + ts + "&signa=" + signature + '&pd=court';
          }
          connectWebsocket() {
            var url = 'wss://rtasr.xfyun.cn/v1/ws'
            var urlParam = this.getHandShakeParams()
            
            url = `${url}${urlParam}`
            if ('WebSocket' in window) {
              this.ws = new WebSocket(url)
            } else if ('MozWebSocket' in window) {
              this.ws = new MozWebSocket(url)
            } else {
              alert(notSupportTip)
              return null
            }
            this.ws.onopen = (e) => {
              this.mediaStream.connect(this.recorder)
              this.recorder.connect(this.context.destination)
              setTimeout(() => {
                this.wsOpened(e)
              }, 500)
              this.config.onStart && this.config.onStart(e)
            }
            this.ws.onmessage = (e) => {
              // this.config.onMessage && this.config.onMessage(e)
              this.wsOnMessage(e)
            }
            this.ws.onerror = (e) => {
              this.stop()
              console.log("关闭连接ws.onerror");
              this.config.onError && this.config.onError(e)
            }
            this.ws.onclose = (e) => {
              this.stop()
              console.log("关闭连接ws.onclose");
              // $('.start-button').attr('disabled', false);
              this.config.onClose && this.config.onClose(e)
            }
          }
  
          wsOpened() {
            if (this.ws.readyState !== 1) {
              return
            }
            var audioData = buffer.splice(0, 1280)
            this.ws.send(new Int8Array(audioData))
            this.handlerInterval = setInterval(() => {
              // websocket未连接
              if (this.ws.readyState !== 1) {
                clearInterval(this.handlerInterval)
                return
              }
              if (buffer.length === 0) {
                if (this.state === 'end') {
                  this.ws.send("{\"end\": true}")
                  console.log("发送结束标识");
                  clearInterval(this.handlerInterval)
                }
                return false
              }
              var audioData = buffer.splice(0, 1280)
              if (audioData.length > 0) {
                this.ws.send(new Int8Array(audioData))
              }
            }, 40)
          }
  
          wsOnMessage(e) {
            let jsonData = JSON.parse(e.data)
            if (jsonData.action == "started") {
              // 握手成功
              console.log("握手成功");
            } else if (jsonData.action == "result") {
              // 转写结果
              if (this.config.onMessage && typeof this.config.onMessage == 'function') {
                this.config.onMessage(jsonData.data)
              }
            } else if (jsonData.action == "error") {
              // 连接发生错误
              console.log("出错了:", jsonData);
              alert("连接发发生错误")
            }
          }
  
  
          ArrayBufferToBase64(buffer) {
            var binary = ''
            var bytes = new Uint8Array(buffer)
            var len = bytes.byteLength
            for (var i = 0; i < len; i++) {
              binary += String.fromCharCode(bytes[i])
            }
            return window.btoa(binary)
          }
        }
  
        class IatTaste {
          constructor() {
            var iatRecorder = new IatRecorder({
              onClose: () => {
                this.stop()
                this.reset()
              },
              onError: (data) => {
                this.stop()
                this.reset()
                alert('WebSocket连接失败')
              },
              onMessage: (message) => {
                this.setResult(JSON.parse(message))
              },
              onStart: () => {
                this.counterDown(time)
              }
            })
            this.iatRecorder = iatRecorder
            this.counterDownDOM = time
            this.counterDownTime = 0
          }
          start() {
            this.iatRecorder.start()
          }
  
          stop() {
            this.iatRecorder.stop()
          }
  
          reset() {
            this.counterDownTime = 0
            clearTimeout(this.counterDownTimeout)
            buffer = []
  
          }
  
          init() {
            let self = this
            //开始按钮的事件
            startBtn.onclick = function () {
              if (navigator.getUserMedia && AudioContext && recorderWorker) {
                self.start()
              } else {
                alert(notSupportTip)
              }
            }
  
            //结束按钮的事件
            endBtn.onclick = function () {
              self.stop()
              //reset
              this.counterDownTime = 0
              clearTimeout(this.counterDownTimeout)
              buffer = []
            }
          }
  
          // 转写的结果
          setResult(data) {
            let rtasrResult = []
            rtasrResult[data.seg_id] = data
            rtasrResult.forEach(i => {
              let str = ""
              if (i.cn.st.type == 0) {
                i.cn.st.rt.forEach(j => {
                  j.ws.forEach(k => {
                    k.cw.forEach(l => {
                      str += l.w
                    })
                  })
                })
                console.log(output.value,str)
                console.log(str)
                if(str != '。' && str != '?'&& str != '?') {
                    output.value += str
                }
              }
  
            })
          }
  
          // 计时器
          counterDown() {
            if (this.counterDownTime >= 0 && this.counterDownTime < 10) {
              this.counterDownDOM.innerText = '00: 0' + this.counterDownTime
            } else if (this.counterDownTime >= 10 && this.counterDownTime < 60) {
              this.counterDownDOM.innerText = '00: ' + this.counterDownTime
            } else if (this.counterDownTime % 60 >= 0 && this.counterDownTime % 60 < 10) {
              this.counterDownDOM.innerText = '0' + parseInt(this.counterDownTime / 60) + ': 0' + this.counterDownTime % 60
            } else {
              this.counterDownDOM.innerText = '0' + parseInt(this.counterDownTime / 60) + ': ' + this.counterDownTime % 60
            }
            this.counterDownTime++
            this.counterDownTimeout = setTimeout(() => {
              this.counterDown()
            }, 1000)
          }
        }
        var iatTaste = new IatTaste()
        iatTaste.init()
      }
    }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320

单纯记录使用过程,效果比paddlespeech转写效果要好

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/531189
推荐阅读
相关标签
  

闽ICP备14008679号