index.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. import { computed, defineComponent, nextTick, reactive, ref, toRefs } from "vue";
  2. import styles from "./index.module.less";
  3. import { api_back } from "/src/helpers/communication";
  4. import state from "/src/state";
  5. import iconBack from "./image/back_icon.png";
  6. import iconShiyi from "./image/icon-shiyi.svg";
  7. import iconhuifang from "./image/icon-huifang.svg";
  8. import shiyiTop from "./image/shiyi-top.png";
  9. import shiyiClose from "./image/closeImg.png";
  10. import { Grid, GridItem, Popup } from "vant";
  11. import videobg from "./image/videobg.png";
  12. import "plyr/dist/plyr.css";
  13. import Plyr from "plyr";
  14. import { browser } from "/src/utils";
  15. import Note from "../note";
  16. import { storeData } from "/src/store";
  17. import Title from "/src/page-instrument/header-top/title";
  18. import { Vue3Lottie } from "vue3-lottie";
  19. import audioBga from "./image/audioBga.json";
  20. import audioBga1 from "./image/leftCloud.json";
  21. import audioBga2 from "./image/rightCloud.json";
  22. import { EvaluatingReportDriver } from "/src/page-instrument/custom-plugins/guide-driver";
  23. type IItemType = "intonation" | "cadence" | "integrity";
  24. export default defineComponent({
  25. name: "header-top",
  26. props: {
  27. scoreData: {
  28. type: Object,
  29. default: () => ({}),
  30. },
  31. },
  32. setup(props, { expose }) {
  33. const browserInfo = browser();
  34. const { scoreData } = toRefs(props);
  35. const shareData = reactive({
  36. show: false,
  37. shiyiShow: false,
  38. isInitPlyr: false,
  39. _plrl: null as any,
  40. });
  41. const lottieDom = ref<any>();
  42. const lottieDom1 = ref<any>();
  43. const lottieDom2 = ref<any>();
  44. const level: any = {
  45. BEGINNER: "入门级",
  46. ADVANCED: "进阶级",
  47. PERFORMER: "大师级",
  48. };
  49. // 颜色配置
  50. const bgColors = {
  51. high: "#FF66A6",
  52. low: "#FFB900",
  53. right: "#65FFAE",
  54. wrong: "#DA3736",
  55. lack: "#A5CBFF",
  56. not: "#FFFFFF",
  57. fast: "#B366FF",
  58. slow: "#FF7B00",
  59. };
  60. // console.log("🚀 ~ scoreData:", scoreData.value)
  61. const itemType = ref<IItemType>("intonation");
  62. /** 返回 */
  63. const handleBack = () => {
  64. api_back();
  65. };
  66. const handleChange = (type: IItemType) => {
  67. itemType.value = type;
  68. scoreData.value.itemType = type;
  69. };
  70. // 资源类型
  71. const mediaType = computed((): "audio" | "video" => {
  72. const subfix = (scoreData.value.videoFilePath || "").split(".").pop();
  73. if (subfix === "wav" || subfix === "mp3" || subfix === "m4a") {
  74. return "audio";
  75. }
  76. return "video";
  77. });
  78. const openAudioAndVideo = () => {
  79. shareData.show = true;
  80. if (shareData.isInitPlyr) return;
  81. nextTick(() => {
  82. const id = mediaType.value === "audio" ? "#audioSrc" : "#videoSrc";
  83. shareData._plrl = new Plyr(id, {
  84. controls: ["play-large", "play", "progress", "current-time", "duration"],
  85. fullscreen: { enabled: false },
  86. });
  87. // 创建音波数据
  88. if(mediaType.value === "audio"){
  89. setTimeout(() => {
  90. const audioDom = document.querySelector("#audioSrc") as HTMLAudioElement
  91. const canvasDom = document.querySelector("#audioVisualizer") as HTMLCanvasElement
  92. const { pauseVisualDraw, playVisualDraw } = audioVisualDraw(audioDom, canvasDom)
  93. shareData._plrl.on('play', () => {
  94. lottieDom.value.play()
  95. lottieDom1.value.play()
  96. lottieDom2.value.play()
  97. playVisualDraw()
  98. });
  99. shareData._plrl.on('pause', () => {
  100. lottieDom.value.pause()
  101. lottieDom1.value.pause()
  102. lottieDom2.value.pause()
  103. pauseVisualDraw()
  104. });
  105. }, 300); // 弹窗动画是0.25秒 这里用定时器 确保canvas 能获取到宽高
  106. }
  107. shareData.isInitPlyr = true;
  108. });
  109. };
  110. /**
  111. * 音频可视化
  112. * @param audioDom
  113. * @param canvasDom
  114. * @param fftSize 2的幂数,最小为32
  115. */
  116. function audioVisualDraw(audioDom: HTMLAudioElement, canvasDom: HTMLCanvasElement, fftSize = 128) {
  117. type propsType = { canvWidth: number; canvHeight: number; canvFillColor: string; lineColor: string; lineGap: number }
  118. // canvas
  119. const canvasCtx = canvasDom.getContext("2d")!
  120. const { width, height } = canvasDom.getBoundingClientRect()
  121. canvasDom.width = width
  122. canvasDom.height = height
  123. // audio
  124. let audioCtx : AudioContext | null = null
  125. let analyser : AnalyserNode | null = null
  126. let source : MediaElementAudioSourceNode | null = null
  127. const dataArray = new Uint8Array(fftSize / 2)
  128. const draw = (data: Uint8Array, ctx: CanvasRenderingContext2D, { lineGap, canvWidth, canvHeight, canvFillColor, lineColor }: propsType) => {
  129. if (!ctx) return
  130. const w = canvWidth
  131. const h = canvHeight
  132. fillCanvasBackground(ctx, w, h, canvFillColor)
  133. // 可视化
  134. const dataLen = data.length
  135. let step = (w / 2 - lineGap * dataLen) / dataLen
  136. step < 1 && (step = 1)
  137. const midX = w / 2
  138. const midY = h / 2
  139. let xLeft = midX
  140. for (let i = 0; i < dataLen; i++) {
  141. const value = data[i]
  142. const percent = value / 255 // 最大值为255
  143. const barHeight = percent * midY
  144. canvasCtx.fillStyle = lineColor
  145. // 中间加间隙
  146. if (i === 0) {
  147. xLeft -= lineGap / 2
  148. }
  149. canvasCtx.fillRect(xLeft - step, midY - barHeight, step, barHeight)
  150. canvasCtx.fillRect(xLeft - step, midY, step, barHeight)
  151. xLeft -= step + lineGap
  152. }
  153. let xRight = midX
  154. for (let i = 0; i < dataLen; i++) {
  155. const value = data[i]
  156. const percent = value / 255 // 最大值为255
  157. const barHeight = percent * midY
  158. canvasCtx.fillStyle = lineColor
  159. if (i === 0) {
  160. xRight += lineGap / 2
  161. }
  162. canvasCtx.fillRect(xRight, midY - barHeight, step, barHeight)
  163. canvasCtx.fillRect(xRight, midY, step, barHeight)
  164. xRight += step + lineGap
  165. }
  166. }
  167. const fillCanvasBackground = (ctx: CanvasRenderingContext2D, w: number, h: number, colors: string) => {
  168. ctx.clearRect(0, 0, w, h)
  169. ctx.fillStyle = colors
  170. ctx.fillRect(0, 0, w, h)
  171. }
  172. const requestAnimationFrameFun = () => {
  173. requestAnimationFrame(() => {
  174. analyser?.getByteFrequencyData(dataArray)
  175. draw(dataArray, canvasCtx, {
  176. lineGap: 2,
  177. canvWidth: width,
  178. canvHeight: height,
  179. canvFillColor: "transparent",
  180. lineColor: "rgba(255, 255, 255, 0.3)"
  181. })
  182. if (!isPause) {
  183. requestAnimationFrameFun()
  184. }
  185. })
  186. }
  187. let isPause = true
  188. const playVisualDraw = () => {
  189. if (!audioCtx) {
  190. audioCtx = new AudioContext()
  191. source = audioCtx.createMediaElementSource(audioDom)
  192. analyser = audioCtx.createAnalyser()
  193. analyser.fftSize = fftSize
  194. source?.connect(analyser)
  195. analyser.connect(audioCtx.destination)
  196. }
  197. //audioCtx.resume() // 重新更新状态 加了暂停和恢复音频音质发生了变化 所以这里取消了
  198. isPause = false
  199. requestAnimationFrameFun()
  200. }
  201. const pauseVisualDraw = () => {
  202. isPause = true
  203. //audioCtx?.suspend() // 暂停 加了暂停和恢复音频音质发生了变化 所以这里取消了
  204. // source?.disconnect()
  205. // analyser?.disconnect()
  206. }
  207. return {
  208. playVisualDraw,
  209. pauseVisualDraw
  210. }
  211. }
  212. return () => (
  213. <>
  214. <div class={[styles.headerTop, browserInfo.android && styles.android]}>
  215. <div class={styles.left}>
  216. <div class={[styles.back, !storeData.isApp && styles.disabled]} onClick={handleBack}>
  217. <img src={iconBack} />
  218. </div>
  219. <div class={styles.leftContent}>
  220. {/* <div class={styles.lcName}>{state.examSongName}</div> */}
  221. <Title class={styles.lcName} text={state.examSongName} rightView={false} />
  222. <div class={styles.lcScore}>
  223. {level[scoreData.value.heardLevel]}|综合分数:{scoreData.value.score}分
  224. </div>
  225. </div>
  226. </div>
  227. {/* 音准、节奏、完整度纬度 */}
  228. <div class={styles.middle}>
  229. {state.isPercussion ? null : (
  230. <div onClick={() => handleChange("intonation")} class={[styles.cItem, "evaluting-report-1", itemType.value === "intonation" && styles.active]}>
  231. <span class={styles.mScore}>{scoreData.value.intonation}分</span>
  232. <span class={styles.mLabel}>音准</span>
  233. </div>
  234. )}
  235. <div onClick={() => handleChange("cadence")} class={[styles.cItem, "evaluting-report-2", itemType.value === "cadence" && styles.active]}>
  236. <span class={styles.mScore}>{scoreData.value.cadence}分</span>
  237. <span class={styles.mLabel}>节奏</span>
  238. </div>
  239. {state.isPercussion ? null : (
  240. <div onClick={() => handleChange("integrity")} class={[styles.cItem, "evaluting-report-3", itemType.value === "integrity" && styles.active]}>
  241. <span class={styles.mScore}>{scoreData.value.integrity}分</span>
  242. <span class={styles.mLabel}>完成度</span>
  243. </div>
  244. )}
  245. </div>
  246. {/* <div class={styles.center}>
  247. <div class={styles.cItem}>
  248. <div>{level[scoreData.value.heardLevel]}</div>
  249. <div>难度</div>
  250. </div>
  251. <div class={styles.cItem}>
  252. <div>{scoreData.value.score}分</div>
  253. <div>评测分数</div>
  254. </div>
  255. {state.isPercussion ? null : (
  256. <>
  257. <div
  258. onClick={() => handleChange("intonation")}
  259. class={[styles.cItem, itemType.value === "intonation" && styles.active]}
  260. >
  261. <div style={{ color: "rgb(45, 199, 170)" }}>{scoreData.value.intonation}分</div>
  262. <div>音准</div>
  263. </div>
  264. <div
  265. onClick={() => handleChange("cadence")}
  266. class={[styles.cItem, itemType.value === "cadence" && styles.active]}
  267. >
  268. <div style={{ color: "#FF4E19" }}>{scoreData.value.cadence}分</div>
  269. <div>节奏</div>
  270. </div>
  271. <div
  272. onClick={() => handleChange("integrity")}
  273. class={[styles.cItem, itemType.value === "integrity" && styles.active]}
  274. >
  275. <div style={{ color: "rgb(255, 196, 89)" }}>{scoreData.value.integrity}分</div>
  276. <div>完成度</div>
  277. </div>
  278. </>
  279. )}
  280. </div> */}
  281. <div class={styles.right}>
  282. <div style={{ display: scoreData.value.videoFilePath ? "" : "none" }} class={[styles.btn, "evaluting-report-4"]} onClick={openAudioAndVideo}>
  283. <img class={styles.iconBtn} src={iconhuifang} />
  284. <span>回放</span>
  285. </div>
  286. <div class={styles.btn} onClick={() => (shareData.shiyiShow = true)}>
  287. <img class={styles.iconBtn} src={iconShiyi} />
  288. <span>释义</span>
  289. </div>
  290. {/* <div class={styles.btn}>
  291. <img class={styles.iconBtn} src={iconhuifang} />
  292. <span>再来一遍</span>
  293. </div> */}
  294. </div>
  295. {/* 五线谱,简谱类型提示 */}
  296. {scoreData.value.musicType === "staff" ? (
  297. <>
  298. {state.isPercussion ? null : (
  299. <div class={styles.demos}>
  300. {itemType.value === "intonation" && (
  301. <>
  302. <div>
  303. {/* <Note fill="rgba(255, 102, 166, 1)" shadowFill="#FFAB25" shadow x={-2} y={0} /> */}
  304. <Note fill="#FF66A6" />
  305. <span>演奏偏高</span>
  306. </div>
  307. <div>
  308. <Note fill="#FFB900" />
  309. <span>演奏偏低</span>
  310. </div>
  311. </>
  312. )}
  313. {itemType.value === "cadence" && (
  314. <>
  315. <div>
  316. <Note fill="#B366FF" />
  317. <span>节奏偏快</span>
  318. </div>
  319. <div>
  320. <Note fill="#FF7B00" />
  321. <span>节奏偏慢</span>
  322. </div>
  323. </>
  324. )}
  325. {(itemType.value === "intonation" || itemType.value === "cadence") && (
  326. <>
  327. <div>
  328. <Note fill="#65FFAE" />
  329. <span>演奏正确</span>
  330. </div>
  331. <div>
  332. <Note fill="#DA3736" />
  333. <span>演奏错误</span>
  334. </div>
  335. </>
  336. )}
  337. {(itemType.value === "intonation" || itemType.value === "integrity") && (
  338. <div>
  339. <Note fill="#A5CBFF" />
  340. <span>时值不足</span>
  341. </div>
  342. )}
  343. {itemType.value === "integrity" && (
  344. <div>
  345. <Note fill="#65FFAE" />
  346. <span>时值正确</span>
  347. </div>
  348. )}
  349. <div>
  350. <Note fill="#FFFFFF" />
  351. <span>未演奏</span>
  352. </div>
  353. </div>
  354. )}
  355. </>
  356. ) : (
  357. <>
  358. {state.isPercussion ? null : (
  359. <div class={styles.demos}>
  360. {itemType.value === "intonation" && (
  361. <>
  362. <div>
  363. {/* <img class={styles.firstIcon1} src={firstTop} /> */}
  364. <i style={{ background: bgColors.high }}></i>
  365. <span>演奏偏高</span>
  366. </div>
  367. <div>
  368. <i style={{ background: bgColors.low }}></i>
  369. <span>演奏偏低</span>
  370. </div>
  371. </>
  372. )}
  373. {itemType.value === "cadence" && (
  374. <>
  375. <div>
  376. <i style={{ background: bgColors.fast }}></i>
  377. <span>节奏偏快</span>
  378. </div>
  379. <div>
  380. <i style={{ background: bgColors.slow }}></i>
  381. <span>节奏偏慢</span>
  382. </div>
  383. </>
  384. )}
  385. {(itemType.value === "intonation" || itemType.value === "cadence") && (
  386. <>
  387. <div>
  388. <i style={{ background: bgColors.right }}></i>
  389. <span>演奏正确</span>
  390. </div>
  391. <div>
  392. <i style={{ background: bgColors.wrong }}></i>
  393. <span>演奏错误</span>
  394. </div>
  395. </>
  396. )}
  397. {(itemType.value === "intonation" || itemType.value === "integrity") && (
  398. <div>
  399. <i style={{ background: bgColors.lack }}></i>
  400. <span>时值不足</span>
  401. </div>
  402. )}
  403. {itemType.value === "integrity" && (
  404. <div>
  405. <i style={{ background: bgColors.right }}></i>
  406. <span>时值正确</span>
  407. </div>
  408. )}
  409. <div>
  410. <i style={{ background: bgColors.not }}></i>
  411. <span>未演奏</span>
  412. </div>
  413. </div>
  414. )}
  415. </>
  416. )}
  417. <Popup
  418. teleport="body"
  419. class={["popup-custom", "van-scale", styles.popup]}
  420. transition="van-scale"
  421. v-model:show={shareData.show}
  422. closeable
  423. onClose={() => {
  424. shareData._plrl?.pause();
  425. }}
  426. >
  427. <div class={styles.playerBox}>
  428. {mediaType.value === "audio" ? (
  429. <div class={styles.audioBox}>
  430. <canvas class={styles.audioVisualizer} id="audioVisualizer"></canvas>
  431. <Vue3Lottie ref={lottieDom} class={styles.audioBga} animationData={audioBga} autoPlay={false} loop={true}></Vue3Lottie>
  432. <Vue3Lottie ref={lottieDom1} class={styles.audioBga1} animationData={audioBga1} autoPlay={false} loop={true}></Vue3Lottie>
  433. <Vue3Lottie ref={lottieDom2} class={styles.audioBga2} animationData={audioBga2} autoPlay={false} loop={true}></Vue3Lottie>
  434. <audio crossorigin="anonymous" id="audioSrc" src={scoreData.value.videoFilePath} controls="false" preload="metadata" playsinline />
  435. </div>
  436. ) : (
  437. <video id="videoSrc" class={styles.videoBox} src={scoreData.value.videoFilePath} data-poster={videobg} preload="metadata" playsinline />
  438. )}
  439. </div>
  440. </Popup>
  441. <Popup v-model:show={shareData.shiyiShow} class="popup-custom van-scale center-closeBtn shiyiBox" transition="van-scale" teleport="body" closeable>
  442. <img onClick={() => (shareData.shiyiShow = false)} class={styles.shiyiClose} src={shiyiClose} />
  443. {scoreData.value.musicType === "staff" ? (
  444. <div class={styles.shiyiPopup}>
  445. <img class={styles.shiyiTop} src={shiyiTop} />
  446. <div class={styles.items}>
  447. <div class={styles.item}>
  448. {/* <Note fill="rgba(42, 188, 111, 1)" shadowFill="#FFAB25" shadow x={-2} y={0} /> */}
  449. <Note fill="#FF66A6" />
  450. <span>玫红色音符:演奏偏高</span>
  451. </div>
  452. <div class={styles.item}>
  453. <Note fill="#4BED98" />
  454. <span>绿色音符:演奏正确</span>
  455. </div>
  456. <div class={styles.item}>
  457. <Note fill="#FFB900" />
  458. <span>黄色音符:演奏偏低</span>
  459. </div>
  460. <div class={styles.item}>
  461. <Note fill="#DA3736" />
  462. <span>红色音符:演奏错误</span>
  463. </div>
  464. <div class={styles.item}>
  465. <Note fill="#B366FF" />
  466. <span>紫色音符:节奏偏快</span>
  467. </div>
  468. <div class={styles.item}>
  469. <Note fill="#A5CBFF" />
  470. <span>浅蓝色音符:时值不足</span>
  471. </div>
  472. <div class={styles.item}>
  473. <Note fill="#FF7B00" />
  474. <span>橙色音符:节奏偏慢</span>
  475. </div>
  476. <div class={styles.item}>
  477. <Note fill="#FFFFFF" />
  478. <span>白色音符:未演奏</span>
  479. </div>
  480. </div>
  481. </div>
  482. ) : (
  483. <div class={styles.shiyiPopup}>
  484. <img class={styles.shiyiTop} src={shiyiTop} />
  485. <div class={styles.items}>
  486. <div class={styles.itemTone}>
  487. <i style={{ background: bgColors.high }}></i>
  488. <span>玫红色音符:演奏偏高</span>
  489. </div>
  490. <div class={styles.itemTone}>
  491. <i style={{ background: bgColors.right }}></i>
  492. <span>绿色音符:演奏正确</span>
  493. </div>
  494. <div class={styles.itemTone}>
  495. <i style={{ background: bgColors.low }}></i>
  496. <span>黄色音符:演奏偏低</span>
  497. </div>
  498. <div class={styles.itemTone}>
  499. <i style={{ background: bgColors.wrong }}></i>
  500. <span>红色音符:演奏错误</span>
  501. </div>
  502. <div class={styles.itemTone}>
  503. <i style={{ background: bgColors.fast }}></i>
  504. <span>紫色音符:节奏偏快</span>
  505. </div>
  506. <div class={styles.itemTone}>
  507. <i style={{ background: bgColors.lack }}></i>
  508. <span>浅蓝色音符:时值不足</span>
  509. </div>
  510. <div class={styles.itemTone}>
  511. <i style={{ background: bgColors.slow }}></i>
  512. <span>橙色音符:节奏偏慢</span>
  513. </div>
  514. <div class={styles.itemTone}>
  515. <i style={{ background: bgColors.not }}></i>
  516. <span>白色音符:未演奏</span>
  517. </div>
  518. </div>
  519. </div>
  520. )}
  521. </Popup>
  522. </div>
  523. <EvaluatingReportDriver videoFilePath={scoreData.value.videoFilePath} />
  524. </>
  525. );
  526. },
  527. });