index.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. import { Popup, Skeleton } from "vant";
  2. import { computed, defineComponent, nextTick, onBeforeMount, onBeforeUnmount, onMounted, reactive, Transition, watch, watchEffect, defineAsyncComponent } from "vue";
  3. import { formateTimes } from "../../helpers/formateMusic";
  4. import Metronome, { metronomeData } from "../../helpers/metronome";
  5. import state, { EnumMusicRenderType, evaluatCreateMusicPlayer, handleSetSpeed, IAudioState, IPlatform, isRhythmicExercises, resetPlaybackToStart, togglePlay, getMusicDetail, calculateDistance, createFixedCursor, addNoteBBox } from "/src/state";
  6. import { browser, setGlobalData } from "../../utils";
  7. import AudioList from "../../view/audio-list";
  8. import MusicScore, { resetMusicScore } from "../../view/music-score";
  9. import TestCheck from "/src/view/music-score/testCheck";
  10. import { sysMusicScoreAccompanimentQueryPage } from "../api";
  11. import EvaluatModel from "../evaluat-model";
  12. import HeaderTop from "../header-top";
  13. import styles from "./index.module.less";
  14. import { api_cloudAccompanyMessage, api_cloudLoading, api_keepScreenLongLight, api_openCamera, api_openWebView, api_setEventTracking, api_setRequestedOrientation, api_setStatusBarVisibility, isSpecialShapedScreen } from "/src/helpers/communication";
  15. import { getQuery } from "/src/utils/queryString";
  16. import Evaluating, { evaluatingData } from "/src/view/evaluating";
  17. import MeasureSpeed from "/src/view/plugins/measure-speed";
  18. import { mappingVoicePart, subjectFingering } from "/src/view/fingering/fingering-config";
  19. import Fingering from "/src/view/fingering";
  20. import store from "store";
  21. import Tick, { handleInitTick } from "/src/view/tick";
  22. import FollowPractice, { followData } from "/src/view/follow-practice";
  23. import FollowModel from "../follow-model";
  24. import RecordingTime from "../custom-plugins/recording-time";
  25. import WorkIndex from "../custom-plugins/work-index";
  26. import TheMusicList from "../component/the-music-list";
  27. import { storeData } from "/src/store";
  28. import ViewFigner from "../view-figner";
  29. import { recalculateNoteData } from "/src/view/selection";
  30. import ToggleMusicSheet from "/src/view/plugins/toggleMusicSheet";
  31. import { setCustomGradual, setCustomNoteRealValue } from "/src/helpers/customMusicScore"
  32. import { usePageVisibility } from "@vant/use";
  33. import { initMidi } from "/src/helpers/midiPlay"
  34. import TheAudio from "/src/components/the-audio"
  35. import tickWav from "/src/assets/tick.wav";
  36. import Title from "../header-top/title";
  37. const DelayCheck = defineAsyncComponent(() =>
  38. import('/src/page-instrument/evaluat-model/delay-check')
  39. )
  40. /**
  41. * 特殊教材分类id
  42. */
  43. export const classids = [1, 2, 6, 7, 8, 9, 3, 10, 11, 12, 13, 4, 14, 15, 16, 17, 30, 31, 35, 36, 46, 108]; // 大雅金唐, 竖笛教程, 声部训练展开的分类ID
  44. const calcCeilFrequency = (frequency: number) => {
  45. if (frequency < 0) return frequency;
  46. if (frequency) return (frequency * 1000 * 2) / 1000;
  47. return 0;
  48. };
  49. /** 需要处理频率的乐器
  50. */
  51. const resetFrequency = (list: any[]) => {
  52. // const instrumentNames = ["ocarina", "pan-flute", "piccolo", "hulusi-flute"];
  53. const instrumentNames = ["ocarina", "pan-flute", "hulusi-flute"];
  54. if (!state.fingeringInfo?.name || !instrumentNames.includes(state.fingeringInfo.name)) return list;
  55. // console.log(state.subjectId, state.fingeringInfo.name, instrumentNames);
  56. for (let i = 0; i < list.length; i++) {
  57. if (list[i].prevFrequency) list[i].prevFrequency = calcCeilFrequency(list[i].prevFrequency);
  58. if (list[i].frequency) list[i].frequency = calcCeilFrequency(list[i].frequency);
  59. if (list[i].nextFrequency) list[i].nextFrequency = calcCeilFrequency(list[i].nextFrequency);
  60. }
  61. return list;
  62. };
  63. /**
  64. * 乐器指法处理
  65. */
  66. const setNoteHalfTone = (list: any[]) => {
  67. const instrumentNames = ["hulusi-flute"]; // ["ocarina", "pan-flute", "piccolo"];
  68. if (!state.fingeringInfo?.name || !instrumentNames.includes(state.fingeringInfo.name)) return list;
  69. for (let i = 0; i < list.length; i++) {
  70. const note = list[i];
  71. if (note.realKey === 0) continue;
  72. note.realKey = note.realKey + 12;
  73. }
  74. return list;
  75. };
  76. export default defineComponent({
  77. name: "music-list",
  78. setup() {
  79. const query: any = getQuery();
  80. const detailData = reactive({
  81. isLoading: true,
  82. skeletonLoading: true,
  83. paddingLeft: "",
  84. headerHide: false,
  85. fingerPreView: false,
  86. fingerPreViewAnimation: false,
  87. orientation: 0,
  88. fingerPreViewGuide: false,
  89. });
  90. const getAPPData = async () => {
  91. const screenData = await isSpecialShapedScreen();
  92. if (screenData?.content) {
  93. // console.log("🚀 ~ screenData:", screenData.content);
  94. const { isSpecialShapedScreen, notchHeight } = screenData.content;
  95. if (isSpecialShapedScreen) {
  96. detailData.paddingLeft = 25 + "px";
  97. }
  98. }
  99. };
  100. onBeforeMount(async () => {
  101. console.time("渲染加载耗时");
  102. api_keepScreenLongLight();
  103. getAPPData();
  104. api_setStatusBarVisibility();
  105. const settting = store.get("musicscoresetting");
  106. if (settting) {
  107. state.setting = settting;
  108. state.setting.beatVolume = state.setting.beatVolume || 50
  109. if (state.setting.camera) {
  110. const res = await api_openCamera();
  111. // 没有授权
  112. if (res?.content?.reson) {
  113. state.setting.camera = false
  114. store.set("musicscoresetting", state.setting);
  115. }
  116. }
  117. }
  118. });
  119. //给app传伴奏
  120. const pushAppMusic = () => {
  121. api_cloudAccompanyMessage(state.accompany);
  122. };
  123. // console.log(route.params, query)
  124. onMounted(async () => {
  125. (window as any).appName = "colexiu";
  126. const id = query.id || "43554";
  127. // 如果是纯预览模式,0.65倍缩放谱面
  128. state.isPreView = query.isPreView
  129. if (state.isPreView) {
  130. state.zoom = 0.65
  131. }
  132. state.isSingleLine = query.isSingleLine; // 一行谱模式
  133. state.moveType = query.moveType == '2' ? 'uniform' : 'smooth'; // 一行谱平移模式
  134. // Promise.all([sysMusicScoreAccompanimentQueryPage(id)]).then((values) => {
  135. // getMusicInfo(values[0]);
  136. // });
  137. await getMusicDetail(id);
  138. detailData.isLoading = false;
  139. // 如果后台设置了不显示指法,关闭指法开关
  140. if (!state.isShowFingering) {
  141. state.setting.displayFingering = false
  142. }
  143. // api_setEventTracking();
  144. });
  145. /** 渲染完成 */
  146. const handleRendered = (osmd: any) => {
  147. api_cloudLoading();
  148. console.timeEnd("渲染加载耗时");
  149. detailData.skeletonLoading = false;
  150. state.osmd = osmd;
  151. // 没有设置速度使用读取的速度
  152. if (state.originSpeed === 0) {
  153. state.originSpeed = state.speed = (osmd as any).bpm || osmd.Sheet.userStartTempoInBPM || 100;
  154. }
  155. const saveSpeed = (store.get("speeds") || {})[state.examSongId] || state.speed || (osmd as any).bpm || osmd.Sheet.userStartTempoInBPM;
  156. // 加载本地缓存的速度
  157. if (saveSpeed) {
  158. handleSetSpeed(saveSpeed);
  159. }
  160. setCustomGradual();
  161. setCustomNoteRealValue();
  162. state.times = formateTimes(osmd);
  163. // 一行谱
  164. if (state.isSingleLine) {
  165. // 音符添加位置信息bbox
  166. addNoteBBox(state.times);
  167. // 一行谱,创建固定的音符指针
  168. createFixedCursor();
  169. calculateDistance();
  170. }
  171. // state.times = resetFrequency(state.times);
  172. state.times = setNoteHalfTone(state.times);
  173. console.log("🚀 ~ state.times:", state.times, state.subjectId, state);
  174. // 初始化midi音频信息
  175. const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
  176. if (state.isAppPlay) {
  177. const durationNum = songEndTime
  178. initMidi(durationNum, state.midiUrl)
  179. }
  180. state.measureTime = state.times[0]?.measureLength || 0
  181. try {
  182. metronomeData.metro = new Metronome();
  183. metronomeData.metro.init(state.times);
  184. } catch (error) {}
  185. /**
  186. * 2024.1.25
  187. * 设置节拍器,跟练需要播放系统节拍器,所以不需要判断needTick状态
  188. */
  189. // if (state.needTick) {
  190. const beatLengthInMilliseconds = (60 / state.speed) * 1000;
  191. handleInitTick(beatLengthInMilliseconds, osmd?.Sheet?.SheetPlaybackSetting?.Rhythm?.Numerator || 4);
  192. // }
  193. // api_cloudLoading();
  194. state.playBtnDirection = query.imagePos === 'right' ? 'right' : 'left';
  195. state.isAttendClass = (query.imagePos === 'left' || query.imagePos === 'right') ? true : false;
  196. // if (state.fingeringInfo.direction === "vertical" && state.setting.displayFingering) {
  197. // state.musicScoreBtnDirection = state.playBtnDirection === 'right' ? 'left' : 'right';
  198. // } else {
  199. // state.musicScoreBtnDirection = state.playBtnDirection;
  200. // }
  201. state.musicScoreBtnDirection = state.playBtnDirection;
  202. state.musicRendered = true;
  203. evaluatCreateMusicPlayer();
  204. resetPlaybackToStart();
  205. // pushAppMusic();
  206. // console.timeEnd("渲染加载耗时");
  207. };
  208. /** 指法配置 */
  209. const fingerConfig = computed<any>(() => {
  210. if (state.setting.displayFingering && state.fingeringInfo?.name) {
  211. if (state.fingeringInfo.direction === "transverse") {
  212. return {
  213. container: {
  214. paddingBottom: state.platform === IPlatform.PC ? `calc(${state.fingeringInfo.height} + ${state.attendHideMenu ? "0px" : "1.8rem"})` : state.fingeringInfo.height,
  215. },
  216. fingerBox: {
  217. height: state.fingeringInfo.height,
  218. },
  219. };
  220. } else {
  221. console.log('指法',state.playBtnDirection,state.platform)
  222. // 老师端,竖向指法,需要根据功能按钮方向进行设置
  223. if (state.platform === IPlatform.PC) {
  224. return {
  225. container: {
  226. paddingRight: state.playBtnDirection === "right" ? "initial" : state.fingeringInfo.width,
  227. paddingLeft: state.playBtnDirection === "right" ? state.fingeringInfo.width : "initial",
  228. },
  229. fingerBox: {
  230. position: "absolute",
  231. width: state.fingeringInfo.width,
  232. height: "100%",
  233. right: state.playBtnDirection === "right" ? "initial" : 0,
  234. left: state.playBtnDirection === "right" ? 0 : "initial",
  235. top: 0,
  236. },
  237. };
  238. } else {
  239. return {
  240. container: {
  241. paddingRight: state.fingeringInfo.width,
  242. },
  243. fingerBox: {
  244. position: "absolute",
  245. width: state.fingeringInfo.width,
  246. height: "100%",
  247. right: 0,
  248. top: 0,
  249. },
  250. };
  251. }
  252. }
  253. }
  254. return {
  255. container: {},
  256. fingerBox: {},
  257. };
  258. });
  259. // 监听指法显示
  260. watch(
  261. () => state.setting.displayFingering,
  262. () => {
  263. if (state.fingeringInfo.direction === "vertical") {
  264. // if (state.setting.displayFingering) {
  265. // state.musicScoreBtnDirection = state.playBtnDirection === 'left' ? 'right' : 'left'
  266. // } else {
  267. // state.musicScoreBtnDirection = state.playBtnDirection
  268. // }
  269. state.musicScoreBtnDirection = state.playBtnDirection
  270. nextTick(() => {
  271. resetMusicScore();
  272. });
  273. }
  274. }
  275. );
  276. watch(
  277. () => state.setting.soundEffect,
  278. () => {
  279. store.set("musicscoresetting", state.setting);
  280. }
  281. );
  282. /**播放状态改变时,向父页面发送事件 */
  283. const sendParentMessage = (playState: IAudioState) => {
  284. window.parent.postMessage(
  285. {
  286. api: "headerTogge",
  287. playState: playState,
  288. },
  289. "*"
  290. );
  291. };
  292. // 监听播放状态
  293. watch(
  294. () => state.playState,
  295. () => {
  296. // if (state.platform != IPlatform.PC) {
  297. // detailData.headerHide = state.playState === "play" ? true : false;
  298. // }
  299. detailData.headerHide = state.playState === "play" ? true : false;
  300. sendParentMessage(state.playState);
  301. }
  302. );
  303. /** 指法预览切换 */
  304. watch(
  305. () => detailData.fingerPreView,
  306. () => {
  307. console.log(2342);
  308. window.parent.postMessage(
  309. {
  310. api: "api_fingerPreView",
  311. state: detailData.fingerPreView,
  312. },
  313. "*"
  314. );
  315. }
  316. );
  317. const pageVisible = usePageVisibility();
  318. watch(
  319. () => pageVisible.value,
  320. (val) => {
  321. if (val === "hidden") {
  322. // 如果是播放状态,需要暂停播放
  323. // console.log("页面隐藏停止播放");
  324. if (state.playState === "play") {
  325. togglePlay("paused");
  326. }
  327. }
  328. }
  329. );
  330. onMounted(() => {
  331. window.addEventListener("resize", resetMusicScore);
  332. });
  333. onBeforeUnmount(() => {
  334. window.removeEventListener("resize", resetMusicScore);
  335. });
  336. const browsInfo = browser();
  337. const handleOpenFignerView = () => {
  338. if (!query.modelType) {
  339. detailData.orientation = state.fingeringInfo.orientation || 0;
  340. api_setRequestedOrientation(detailData.orientation);
  341. }
  342. // const url = `${
  343. // /(192|localhost)/.test(location.origin)
  344. // ? "http://192.168.3.114:3000/instrument.html"
  345. // : location.origin + location.pathname
  346. // }#/view-figner`;
  347. // console.log("🚀 ~ url:", url);
  348. // api_openWebView({
  349. // url: url,
  350. // orientation: state.fingeringInfo.orientation || 0,
  351. // });
  352. if (state.playState === "play") {
  353. togglePlay("paused");
  354. setTimeout(() => {
  355. detailData.fingerPreView = true;
  356. }, 500);
  357. return;
  358. }
  359. detailData.fingerPreView = true;
  360. };
  361. const handleCloseFignerView = () => {
  362. if (!query.modelType && detailData.orientation == 1) {
  363. api_setRequestedOrientation(0);
  364. }
  365. detailData.fingerPreView = false;
  366. detailData.fingerPreViewGuide = false;
  367. };
  368. return () => (
  369. <div
  370. class={[styles.detail, state.setting.eyeProtection && "eyeProtection", (state.platform === IPlatform.PC && state.zoom > 0.8) && styles.PC, state.isPreView && styles.preViewDetail, state.isSingleLine && styles.singleLineDetail]}
  371. style={{
  372. paddingLeft: detailData.paddingLeft,
  373. background: state.setting.camera ? `rgba(${state.setting.eyeProtection ? "253,244,229" : "255,255,255"} ,${state.setting.cameraOpacity / 100}) !important` : "",
  374. }}
  375. >
  376. <Transition name="van-fade">
  377. {detailData.skeletonLoading && (
  378. <div class={styles.skeleton}>
  379. <Skeleton row={8} />
  380. </div>
  381. )}
  382. </Transition>
  383. {/** 学生端头部标题&功能按钮 */}
  384. {
  385. (!state.isPreView && state.platform !== IPlatform.PC) &&
  386. <div class={[styles.headHeight, detailData.headerHide && styles.headHide]}>{state.musicRendered && <HeaderTop />}</div>
  387. }
  388. {/** 老师端标题 */}
  389. {
  390. state.platform === IPlatform.PC &&
  391. <div class={[styles.pcHead, detailData.headerHide && styles.pcHeadHide]}>
  392. <Title text={state.examSongName} rightView={false} />
  393. </div>
  394. }
  395. <div
  396. id="scrollContainer"
  397. style={{ ...fingerConfig.value.container, height: detailData.headerHide ? "100vh" : "" }}
  398. class={[styles.container, !state.setting.displayCursor && "hideCursor", browsInfo.xiaomi && styles.xiaomi, state.platform === IPlatform.PC && styles.pcContainer]}
  399. onClick={(e: Event) => {
  400. e.stopPropagation();
  401. // if (state.playState === "play" && state.platform != IPlatform.PC) {
  402. // detailData.headerHide = !detailData.headerHide;
  403. // }
  404. if (state.playState === "play") {
  405. detailData.headerHide = !detailData.headerHide;
  406. }
  407. }}
  408. >
  409. {/* 曲谱渲染 */}
  410. {!detailData.isLoading &&
  411. <MusicScore
  412. showPartNames={state.isCombineRender}
  413. onRendered={handleRendered}
  414. />
  415. }
  416. {/* {
  417. state.musicRendered &&
  418. <TestCheck />
  419. } */}
  420. {/* 指法 */}
  421. {state.setting.displayFingering && state.fingeringInfo?.name && !state.isPreView && state.isShowFingering && (
  422. <div style={{ ...fingerConfig.value.fingerBox }}>
  423. <Fingering
  424. style={{
  425. background: state.setting.camera ? `rgba(${state.setting.eyeProtection ? "253,244,229" : "255,255,255"} ,${state.setting.cameraOpacity / 100})` : "",
  426. }}
  427. onOpen={() => handleOpenFignerView()}
  428. />
  429. </div>
  430. )}
  431. </div>
  432. {/** 老师端底部功能按钮 */}
  433. {
  434. (!state.isPreView && state.platform === IPlatform.PC) &&
  435. <div class={[styles.headHeight, detailData.headerHide && styles.pcHeadHideBottom]}>{state.musicRendered && <HeaderTop />}</div>
  436. }
  437. {/* 节拍器,跟练需要播放系统节拍器,所以不需要判断needTick状态 */}
  438. {/* {state.needTick && <Tick />} */}
  439. <Tick />
  440. {/* 曲目渲染完成,再去下载mp3资源 */}
  441. {!detailData.isLoading && !detailData.skeletonLoading && <AudioList />}
  442. {/* {!detailData.isLoading && <TheAudio src={tickWav} />} */}
  443. {/* 预加载延迟检测组建 */}
  444. {!detailData.isLoading && !detailData.skeletonLoading && evaluatingData.preloadJson && !evaluatingData.jsonLoadDone && (
  445. <div class={styles.preJson}>
  446. <DelayCheck isPreLoad={true} />
  447. </div>
  448. )}
  449. {/* 评测 */}
  450. {state.modeType === "evaluating" && (
  451. <>
  452. <Evaluating />
  453. {evaluatingData.rendered && <EvaluatModel />}
  454. </>
  455. )}
  456. {/* 跟练模式 */}
  457. {state.modeType === "follow" && (
  458. <>
  459. <FollowPractice />
  460. <FollowModel />
  461. </>
  462. )}
  463. {/* 切换曲谱 */}
  464. {!query.lessonTrainingId && !query.questionId && state.isConcert && <ToggleMusicSheet />}
  465. {state.musicRendered && !state.isPreView && (
  466. <>
  467. {/* 统计训练时长 */}
  468. {storeData.isApp && <RecordingTime />}
  469. {/* 作业 */}
  470. {query.workRecord && <WorkIndex />}
  471. {/* 曲谱列表 */}
  472. {state.playState == "play" || followData.start || evaluatingData.startBegin || query.workRecord || query.modelType || state.platform === IPlatform.PC || query.isCbs ? null : <TheMusicList />}
  473. </>
  474. )}
  475. <Popup
  476. zIndex={5050}
  477. teleport="body"
  478. v-model:show={detailData.fingerPreView}
  479. position="bottom"
  480. onClosed={() => {
  481. detailData.fingerPreViewAnimation = false;
  482. }}
  483. onOpen={() => {
  484. detailData.fingerPreViewAnimation = true;
  485. }}
  486. onOpened={() => {
  487. detailData.fingerPreViewGuide = true;
  488. }}
  489. >
  490. {detailData.fingerPreViewAnimation && <ViewFigner show={detailData.fingerPreViewGuide} subject={state.fingeringInfo.name} isComponent={true} onClose={handleCloseFignerView} />}
  491. </Popup>
  492. </div>
  493. );
  494. },
  495. });