zhaidpjava 2019-07-01
一、背景
作为一名前端同学有时候感觉挺可怜的,复杂的操作都依赖后端同学在服务器端完成。那么,有一天我们自己想玩一个新技术或者后端同学不搭理我们,怎么办?绝望中.....
二、小程序语音识别
接到这个需求,我们明确两个问题:
由小程序文档可知:只支持 mp3格式和 aac格式
微信小程序录音文档
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) } }) } },
注意: