微信小程序--------语音识别(前端自己也能玩)

一、背景

作为一名前端同学有时候感觉挺可怜的,复杂的操作都依赖后端同学在服务器端完成。那么,有一天我们自己想玩一个新技术或者后端同学不搭理我们,怎么办?绝望中.....

二、小程序语音识别

 接到这个需求,我们明确两个问题:
  1. 小程序录音支持什么格式

由小程序文档可知:只支持 mp3格式和 aac格式
微信小程序录音文档
clipboard.png

  1. 科大讯飞平台需要什么格式的音频
    支持的格式 pacm或者wav, speex和 speex-web 格式
    科大讯飞语音听写api

clipboard.png

3. 目标 将小程序的录音转为 科大讯飞能识别的音频格式

  import Mp3 from '@/utils/js-mp3/decode'
  import { md5 } from '@/utils/md5.js'
  import pcm from 'pcm-util'
录音
    // 获取录音权限
    this.getRecordAuth()
    // 获取录音对象
    const that = this;
    this.recorderManager = wx.getRecorderManager()
    this.recorderManager.onStart(() => {
      console.log('recorder start')
    })
    // 录音的格式参数
     const options = {
      duration: 11000,
      sampleRate: 32000,
      numberOfChannels: 1,
      encodeBitRate: 64000,
      format: 'mp3',
      frameSize: 6
    }
    this.recorderManager.start(options)
    this.recorderManager.onStop(res => {
      const tempFilePath = res.tempFilePath
      that.duration = res.duration
      const fs = wx.getFileSystemManager()
      console.log('record stop')
      console.log(res)
      // 从临时文件中读取音频
      fs.readFile({
        filePath: tempFilePath,
        success (res) {
          console.log('read success')
          that.mp3ToPcm(res.data)
        },
        fail (e) {
          console.log('read fail')
          console.log(e)
        }
      })
    })


转格式
mp3ToPcm (mp3AB) {
    var that = this
    var decoder = Mp3.newDecoder(mp3AB)
    var pcmArrayBuffer = decoder.decode()
    // 和录音的格式一样
    const fromFormat = {
      channels: 1,
      sampleRate: 32000,
      interleaved: true,
      float: false,
      samplesPerFrame: 1152,
      signed: true
    }
    // 目标音频的格式
    const toFormat = {
      channels: 1,
      sampleRate: 16000,
      bitDepth: 8,
      interleaved: true,
      float: false,
      samplesPerFrame: 576,
      signed: true
    }
    var pcmAB = pcm.convert(pcmArrayBuffer, fromFormat, toFormat)
    const base64 = wx.arrayBufferToBase64(pcmAB)
    var millTime = (new Date().setMilliseconds(0) / 1000) + ''
    /** 调用科大讯飞平台的语音识别
        请求参数都是自己申请应用的参数
    */
    wx.request({
      url: 'http://api.xfyun.cn/v1/service/v1/iat',
      method: 'POST',
      data: {
        audio: base64
      },
      header: {
        'X-Appid': '5be4162d',    
        'X-CurTime': millTime,
        'X-Param': 'eyJlbmdpbmVfdHlwZSI6ICJzbXMxNmsiLCJhdWUiOiAicmF3In0=',
        'X-CheckSum': md5('b243cb9e1ea9d9eb40847967a8ebeef2' + millTime + 'eyJlbmdpbmVfdHlwZSI6ICJzbXMxNmsiLCJhdWUiOiAicmF3In0='),
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success (res) {
        console.log('turn success')
        console.log(res)
        console.log(res.data)
      },
      fail: function (res) {
        console.log('turn fail')
        console.log(res)
      }
    })
  }
},

注意:

  1. 首先在科大讯飞平台申应用申请应用
  2. 请求参数的文档语音识别的接口文档
  3. 录音一定在真机上测试,模拟器不行

js-mp3
pcm工具包

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值