index.js 8.0 KB

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