index.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import {
  2. getreadInfo
  3. } from '~/api/video'
  4. import {
  5. createStoreBindings
  6. } from 'mobx-miniprogram-bindings'
  7. import {
  8. store
  9. } from '~/store/index'
  10. const aiengine = require('~/utils/ChivoxAiEngine')
  11. const sha1 = require('~/utils/sha1');
  12. let rowH = 0
  13. let videoContext = null
  14. let stl = null
  15. let setTimeoutObj = null
  16. /*创建基础引擎*/
  17. let wsEngine = aiengine.createWsEngine({});
  18. /*微信录音*/
  19. let recorderManager = wx.getRecorderManager();
  20. Page({
  21. data: {
  22. videoInfo: {},
  23. currentRow: null,
  24. state: false,
  25. countDown: {
  26. state: false,
  27. num: 3,
  28. },
  29. contentH: 0,
  30. scrollTop: 0,
  31. //如果readingReset为true就是重读
  32. readingReset: false,
  33. article: [{
  34. id: 1,
  35. text: '传说在很久很久以前,',
  36. time: '0'
  37. }, {
  38. id: 2,
  39. text: '天和地还没有分开,',
  40. time: '4010'
  41. }, {
  42. id: 3,
  43. text: '整个宇宙混沌一团,',
  44. time: '7080'
  45. }, {
  46. id: 4,
  47. text: '像个大鸡蛋。',
  48. time: '10150'
  49. }, {
  50. id: 5,
  51. text: '有个叫盘古的大神,',
  52. time: '13150'
  53. }, {
  54. id: 6,
  55. text: '昏睡了一万八千年。',
  56. time: '16190'
  57. }, {
  58. id: 7,
  59. text: '一天,大神醒来,睁眼一看,',
  60. time: '20030'
  61. }, {
  62. id: 8,
  63. text: '周围黑乎乎一片,',
  64. time: '24210'
  65. }, {
  66. id: 9,
  67. text: '什么也看不见。',
  68. time: '27300'
  69. }]
  70. },
  71. onLoad(options) {
  72. let videoId = options.videoId
  73. this.getreadInfo(videoId)
  74. let data = this.data.article
  75. data = data.map((item, index) => {
  76. item.readTime = data[index + 1] ? data[index + 1].time - item.time : ''
  77. return item
  78. })
  79. this.setData({
  80. article: data,
  81. readingReset: options.reset || false
  82. })
  83. // 手工绑定
  84. this.storeBindings = createStoreBindings(this, {
  85. store,
  86. fields: {
  87. readDetail: 'readDetail'
  88. },
  89. actions: {
  90. setReadDetail: 'setReadDetail'
  91. }
  92. })
  93. if (!options.reset) {
  94. var query = wx.createSelectorQuery();
  95. query.select('.content').boundingClientRect((rect) => {
  96. this.setData({
  97. contentH: rect.height
  98. })
  99. }).exec()
  100. query.select('.row').boundingClientRect((rect) => {
  101. this.rowH = rect.height
  102. }).exec()
  103. }
  104. this.videoContext = wx.createVideoContext('myVideo')
  105. // 录音授权
  106. wx.getSetting({
  107. success(res) {
  108. if (!res.authSetting['scope.record']) {
  109. wx.authorize({
  110. scope: 'scope.record',
  111. success() {
  112. // 用户已经同意小程序使用录音功能,后续调用接口不会弹窗询问
  113. wx.getRecorderManager()
  114. }
  115. })
  116. }
  117. }
  118. })
  119. /*监听评测结果:必须在基础引擎创建后,调用任何评测接口前设置监听,否则有可能收不到相关事件。*/
  120. wsEngine.onResult((res) => {
  121. this.getRecordScore(res)
  122. });
  123. wsEngine.onErrorResult((res) => {
  124. console.log("===收到错误结果=============", res)
  125. });
  126. },
  127. async getreadInfo(videoId) {
  128. let videoInfo = await getreadInfo(videoId)
  129. wx.setNavigationBarTitle({
  130. title: videoInfo.userRead.title
  131. })
  132. this.setData({
  133. videoInfo
  134. })
  135. },
  136. // 开始录制
  137. setCountDown() {
  138. if (this.data.state) {
  139. this.finishRecord()
  140. return
  141. }
  142. this.setData({
  143. 'countDown.state': true
  144. })
  145. this.stl = setInterval(() => {
  146. if (this.data.countDown.num == 0) {
  147. clearInterval(this.stl)
  148. this.setData({
  149. state: true,
  150. countDown: {
  151. state: false,
  152. num: 3
  153. }
  154. })
  155. this.soundRecording()
  156. this.videoContext.play()
  157. this.startRecording()
  158. } else {
  159. this.setData({
  160. 'countDown.num': --this.data.countDown.num
  161. })
  162. }
  163. }, 1000)
  164. },
  165. // 录音
  166. soundRecording() {
  167. /*调用微信开始录音接口,并启动语音评测*/
  168. let timeStamp = new Date().getTime()
  169. let sig = sha1(`16075689600000da${timeStamp}caa8e60da6042731c230fe431ac9c7fd`)
  170. let app = {
  171. applicationId: '16075689600000da',
  172. sig, //签名字符串
  173. alg: 'sha1',
  174. timestamp: timeStamp + '',
  175. userId: wx.getStorageSync('uid')
  176. }
  177. let lessonText = this.data.videoInfo.userRead.lessonText;
  178. wsEngine.start({
  179. request: {
  180. coreType: "cn.pred.raw",
  181. refText: lessonText,
  182. rank: 100,
  183. attachAudioUrl: 1,
  184. result: {
  185. details: {
  186. gop_adjust: 1
  187. }
  188. }
  189. },
  190. app,
  191. audio: {
  192. audioType: "mp3",
  193. channel: 1,
  194. sampleBytes: 2,
  195. sampleRate: 16000
  196. },
  197. success: (res) => {
  198. /*引擎启动成功,可以启动录音机开始录音,并将音频片传给引擎*/
  199. const options = {
  200. sampleRate: 44100, //采样率
  201. numberOfChannels: 1, //录音通道数
  202. encodeBitRate: 192000, //编码码率
  203. format: 'mp3', //音频格式,有效值aac/mp3
  204. frameSize: 50 //指定帧大小,单位 KB
  205. };
  206. //开始录音,在开始录音回调中feed音频片
  207. recorderManager.start(options);
  208. },
  209. fail: (res) => {
  210. console.log("fail============= " + res);
  211. },
  212. });
  213. //监听录音开始事件
  214. recorderManager.onStart(() => {});
  215. //监听录音结束事件
  216. recorderManager.onStop((res) => {
  217. console.log('录音结束', res);
  218. this.setData({
  219. tempFilePath: res.tempFilePath,
  220. });
  221. //录音机结束后,驰声引擎执行结束操作,等待评测返回结果
  222. wsEngine.stop({
  223. success: () => {
  224. console.log('====== wsEngine stop success ======');
  225. },
  226. fail: (res) => {
  227. console.log('录音结束报错', res);
  228. },
  229. });
  230. });
  231. //监听已录制完指定帧大小的文件事件。如果设置了 frameSize,则会回调此事件。
  232. recorderManager.onFrameRecorded((res) => {
  233. const {
  234. frameBuffer
  235. } = res
  236. //TODO 调用feed接口传递音频片给驰声评测引擎
  237. wsEngine.feed({
  238. data: frameBuffer, // frameBuffer为微信录音机回调的音频数据
  239. success: () => {
  240. console.log('feed success.监听已录制完指定帧大小的文件事件')
  241. },
  242. fail: (res) => {
  243. console.log('监听已录制完指定帧大小报错', res)
  244. },
  245. });
  246. });
  247. },
  248. // 结束录制
  249. finishRecord() {
  250. recorderManager.stop();
  251. this.videoContext.stop()
  252. this.videoContext.seek(0)
  253. clearTimeout(this.setTimeoutObj)
  254. clearInterval(this.stl)
  255. this.setData({
  256. state: false,
  257. currentRow: null,
  258. scrollTop: 0
  259. })
  260. },
  261. // 倒计时
  262. startRecording() {
  263. if (this.data.currentRow == null) {
  264. this.setData({
  265. currentRow: 0
  266. })
  267. }
  268. let row = this.data.article[this.data.currentRow]
  269. if (!row.readTime) {
  270. return
  271. }
  272. this.setTimeoutObj = setTimeout(() => {
  273. this.setData({
  274. currentRow: ++this.data.currentRow
  275. })
  276. this.setData({
  277. scrollTop: this.rowH * this.data.currentRow
  278. })
  279. this.startRecording()
  280. },
  281. row.readTime);
  282. },
  283. // 视频播放结束
  284. videoEnd() {
  285. this.finishRecord()
  286. },
  287. // 获取测评结果
  288. getRecordScore(res) {
  289. var res = {
  290. result: {
  291. rank: 100,
  292. res: "chn.pred.G4.D4.0.3",
  293. tone: 50,
  294. integrity: 12,
  295. forceout: 0,
  296. pron: 7,
  297. overall: 8,
  298. fluency: {
  299. pause: 1,
  300. overall: 0,
  301. speed: 73
  302. },
  303. }
  304. }
  305. const result = res.result;
  306. const integrity = Math.floor(result.integrity); //完成度
  307. const tone = Math.floor(result.tone); // 语调声调
  308. const accuracy = Math.floor(result.overall); // 准确度 发音分
  309. const fluency = Math.floor(result.fluency.overall); //流利度
  310. let myOverall = Math.floor(integrity * 0.3 + accuracy * 0.5 + fluency * 0.1 + tone * 0.1);
  311. let detail = {
  312. integrity,
  313. tone,
  314. accuracy,
  315. fluency,
  316. myOverall,
  317. tempFilePath: this.data.tempFilePath,
  318. title: this.data.videoInfo.userRead.title,
  319. id: this.data.videoInfo.userRead.exampleId,
  320. }
  321. this.setReadDetail(detail)
  322. wx.redirectTo({
  323. url: `/pages/score/index`,
  324. })
  325. },
  326. /**
  327. * 生命周期函数--监听页面卸载
  328. */
  329. onUnload() {
  330. clearTimeout(this.setTimeoutObj)
  331. clearInterval(this.stl)
  332. },
  333. onShareAppMessage() {
  334. }
  335. })