找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 小程序开发 查看内容

搭建https的silk录音文件语音识别服务的调用过程

作者:模板之家 2018-1-2 08:58 7161人关注

实现功能 实现一个智能生活信息查询的小秘书功能,支持查天气、新闻、日历、汇率、笑话、故事、百科、诗词、邮编、区号、菜谱、股票、节目预告,还支持闲聊、算24点、数学计算

实现功能

实现一个智能生活信息查询的小秘书功能,支持查天气、新闻、日历、汇率、笑话、故事、百科、诗词、邮编、区号、菜谱、股票、节目预告,还支持闲聊、算24点、数学计算、单位换算、购物、搜索等功能。

使用方式:

新版上线支持语音识别,按下说话,松开发送。

老版本上支持摇一摇、点界面按钮、手动输入、下拉刷新这四种方式。

扫码试用(左右皆可)

 

界面展示

 
 

开发资源

  • 免费开放语义接口平台 olami.ai
  • 微信小程序平台
  • js, css
  • 我自己搭建的https的语音识别API
接口
源码分析

这里主要介绍新版本首页相关的代码,其它部分代码在 微信小程序——智能小秘“遥知之”源码分享(语义理解基于olami)(注:这个是原来不支持语音识别的版本) 的基础上,变化不怎么大,具体可参考那篇文章。

asr.js源码:
  1. /**
  2. * 作者:happycxz
  3. * 时间:2017.09.19
  4. * 源码分享链接:http://blog.csdn.net/happycxz/article/details/78024986
  5. * https的silk语音识别API(专供微信小程序调用):https://api.happycxz.com/test/silk2asr/olami/asr
  6. * 该API服务搭建全过程解析及源码分享贴:http://blog.csdn.net/happycxz/article/details/78016299
  7. * 需要使用此API请联系作者QQ:404499164
  8. * 遵循开放、分享、自由、免费的精神,把开源坚持到底
  9. */
  10.  
  11. //获取应用实例 
  12. var app = getApp()
  13.  
  14. var UTIL = require('../../utils/util.js');
  15. var GUID = require('../../utils/GUID.js');
  16. var NLI = require('../../utils/NLI.js');
  17.  
  18. const appkey = require('../../config').appkey
  19. const appsecret = require('../../config').appsecret
  20.  
  21. //弹幕定时器
  22. var timer;
  23.  
  24. var pageSelf = undefined;
  25.  
  26. var doommList = [];
  27. class Doomm {
  28.   constructor() {
  29.     this.text = UTIL.getRandomItem(app.globalData.corpus);
  30.     this.top = Math.ceil(Math.random() * 40);
  31.     this.time = Math.ceil(Math.random() * 8 + 6);
  32.     this.color = getRandomColor();
  33.     this.display = true;
  34.     let that = this;
  35.     setTimeout(function () {
  36.       doommList.splice(doommList.indexOf(that), 1);
  37.       doommList.push(new Doomm());
  38.  
  39.       pageSelf.setData({
  40.         doommData: doommList
  41.       })
  42.     }, this.time * 1000)
  43.   }
  44. }
  45. function getRandomColor() {
  46.   let rgb = []
  47.   for (let i = 0; i < 3; ++i) {
  48.     let color = Math.floor(Math.random() * 256).toString(16)
  49.     color = color.length == 1 ? '0' + color : color
  50.     rgb.push(color)
  51.   }
  52.   return '#' + rgb.join('')
  53. }
  54.  
  55. Page({
  56.   data: {
  57.     j: 1,//帧动画初始图片 
  58.     isSpeaking: false,//是否正在说话
  59.     outputTxt : "", //输出识别结果
  60.  
  61.     doommData: []
  62.   },
  63.  
  64.   initDoomm: function () {
  65.     doommList.push(new Doomm());
  66.     doommList.push(new Doomm());
  67.     doommList.push(new Doomm());
  68.     this.setData({
  69.       doommData: doommList
  70.     })
  71.   },
  72.  
  73.   onLoad: function () {
  74.     pageSelf = this;
  75.     this.initDoomm();
  76.   },
  77.  
  78.   //手指按下 
  79.   touchdown: function () {
  80.     UTIL.log("手指按下了... new date : " + new Date)
  81.     var _this = this;
  82.     speaking.call(this);
  83.     this.setData({
  84.       isSpeaking: true
  85.     })
  86.     //开始录音 
  87.     wx.startRecord({
  88.       success: function (res) {
  89.         //临时路径,下次进入小程序时无法正常使用
  90.         var tempFilePath = res.tempFilePath;
  91.         UTIL.log('record SUCCESS file path:' + tempFilePath)
  92.         _this.setData({
  93.           recordPath: tempFilePath
  94.         });
  95.       },
  96.       fail: function (res) {
  97.         //录音失败 
  98.         wx.showModal({
  99.           title: '提示',
  100.           content: '录音的姿势不对!',
  101.           showCancel: false,
  102.           success: function (res) {
  103.             if (res.confirm) {
  104.               UTIL.log('用户点击确定')
  105.               return
  106.             }
  107.           }
  108.         })
  109.       }
  110.     })
  111.   },
  112.   //手指抬起 
  113.   touchup: function () {
  114.     UTIL.log("手指抬起了...")
  115.     this.setData({
  116.       isSpeaking: false,
  117.     })
  118.     clearInterval(this.timer)
  119.     wx.stopRecord()
  120.  
  121.     var _this = this
  122.     setTimeout(function () {
  123.       var urls = "https://api.happycxz.com/test/silk2asr/olami/asr";
  124.       UTIL.log(_this.data.recordPath);
  125.       wx.uploadFile({
  126.         url: urls,
  127.         filePath: _this.data.recordPath,
  128.         name: 'file',
  129.         formData: { "appKey": appkey, "appSecret": appsecret, "userId": UTIL.getUserUnique() },
  130.         header: { 'content-type': 'multipart/form-data' },
  131.         success: function (res) {
  132.           UTIL.log('res.data:' + res.data);
  133.  
  134.           var nliResult = getNliFromResult(res.data);
  135.           UTIL.log('nliResult:' + nliResult);
  136.           var stt = getSttFromResult(res.data);
  137.           UTIL.log('stt:' + stt);
  138.  
  139.           var sentenceResult;
  140.           try {
  141.             sentenceResult = NLI.getSentenceFromNliResult(nliResult);
  142.           } catch (e) {
  143.             UTIL.log('touchup() 错误' + e.message + '发生在' + e.lineNumber + '行');
  144.             sentenceResult = '没明白你说的,换个话题?'
  145.           }
  146.  
  147.           var lastOutput = "==>语音识别结果:\n" + stt + "\n\n==>语义处理结果:\n" + sentenceResult;
  148.           _this.setData({
  149.             outputTxt: lastOutput,
  150.           });
  151.           wx.hideToast();
  152.         },
  153.         fail: function (res) {
  154.           UTIL.log(res);
  155.           wx.showModal({
  156.             title: '提示',
  157.             content: "网络请求失败,请确保网络是否正常",
  158.             showCancel: false,
  159.             success: function (res) {
  160.             }
  161.           });
  162.           wx.hideToast();
  163.         }
  164.       });
  165.     }, 1000)
  166.   },
  167.  
  168.   //切换到老版本
  169.   turnToOld: function() {
  170.     wx.navigateTo({
  171.       url: '../index/index',
  172.     })
  173.   },
  174.  
  175. })
  176.  
  177. function getNliFromResult(res_data) {
  178.   var res_data_json = JSON.parse(res_data);
  179.   var res_data_result_json = JSON.parse(res_data_json.result);
  180.   return res_data_result_json.nli;
  181. }
  182.  
  183. function getSttFromResult(res_data) {
  184.   var res_data_json = JSON.parse(res_data);
  185.   var res_data_result_json = JSON.parse(res_data_json.result);
  186.   return res_data_result_json.asr.result;
  187. }
  188.  
  189. //麦克风帧动画 
  190. function speaking() {
  191.   var _this = this;
  192.   //话筒帧动画 
  193.   var i = 1;
  194.   this.timer = setInterval(function () {
  195.     i++;
  196.     i = i % 5;
  197.     _this.setData({
  198.       j: i
  199.     })
  200.   }, 200);
  201. }
复制代码

这部分主要实现录音按钮被按下和松开触发话筒录音及结束录音,当按钮被按下

路过

雷人

握手

鲜花

鸡蛋
原作者: 模板之家 来自: 网络收集

全部回复(0)