Wechat applet speech recognition (the front end can play by itself)

Background

As a front-end student, sometimes I feel very pitiful. The complex operations depend on the back-end students to complete on the server. So, one day we want to play a new technology or the back-end students ignore us, what should we do? In desperation

II. Small program speech recognition

 In response to this demand, we have two clear questions:
  1. What formats does applet recording support

According to the applet documentation, only mp3 and aac formats are supported
Wechat applet recording document

  1. What format of audio does iFLYTEK platform need
    Supported formats: pacm or wav, speex and speex Web
    IFLYTEK speech dictation api

3. The goal is to convert the recording of the applet into an audio format recognized by iFLYTEK

  import Mp3 from '@/utils/js-mp3/decode'
  import { md5 } from '@/utils/md5.js'
  import pcm from 'pcm-util'
//Sound recording
    // Get recording permission
    this.getRecordAuth()
    // Get recording object
    const that = this;
    this.recorderManager = wx.getRecorderManager()
    this.recorderManager.onStart(() => {
      console.log('recorder start')
    })
    // Recording format parameters
     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)
      // Read audio from temporary file
      fs.readFile({
        filePath: tempFilePath,
        success (res) {
          console.log('read success')
          that.mp3ToPcm(res.data)
        },
        fail (e) {
          console.log('read fail')
          console.log(e)
        }
      })
    })


//Format conversion
mp3ToPcm (mp3AB) {
    var that = this
    var decoder = Mp3.newDecoder(mp3AB)
    var pcmArrayBuffer = decoder.decode()
    // Same format as recording
    const fromFormat = {
      channels: 1,
      sampleRate: 32000,
      interleaved: true,
      float: false,
      samplesPerFrame: 1152,
      signed: true
    }
    // Format of target audio
    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) + ''
    /** Speech recognition using iFLYTEK platform
        The request parameters are the parameters applied by the user
    */
    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' // Default value
      },
      success (res) {
        console.log('turn success')
        console.log(res)
        console.log(res.data)
      },
      fail: function (res) {
        console.log('turn fail')
        console.log(res)
      }
    })
  }
},

Be careful:

  1. First, apply for application in iFLYTEK platform Application application
  2. Document requesting parameters Interface document of speech recognition
  3. The recording must be tested on the real machine. The simulator can't

js-mp3
pcm Toolkit

Tags: node.js simulator

Posted on Thu, 05 Dec 2019 15:15:46 -0500 by ramtulasi