index.js 8.4 KB

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