index.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. // 音符添加位置信息bbox
  164. addNoteBBox(state.times);
  165. // 一行谱,创建固定的音符指针
  166. createFixedCursor();
  167. calculateDistance();
  168. // state.times = resetFrequency(state.times);
  169. state.times = setNoteHalfTone(state.times);
  170. console.log("🚀 ~ state.times:", state.times, state.subjectId, state);
  171. // 初始化midi音频信息
  172. const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
  173. if (state.isAppPlay) {
  174. const durationNum = songEndTime
  175. initMidi(durationNum, state.midiUrl)
  176. }
  177. state.measureTime = state.times[0]?.measureLength || 0
  178. try {
  179. metronomeData.metro = new Metronome();
  180. metronomeData.metro.init(state.times);
  181. } catch (error) {}
  182. /**
  183. * 2024.1.25
  184. * 设置节拍器,跟练需要播放系统节拍器,所以不需要判断needTick状态
  185. */
  186. // if (state.needTick) {
  187. const beatLengthInMilliseconds = (60 / state.speed) * 1000;
  188. handleInitTick(beatLengthInMilliseconds, osmd?.Sheet?.SheetPlaybackSetting?.Rhythm?.Numerator || 4);
  189. // }
  190. // api_cloudLoading();
  191. state.playBtnDirection = query.imagePos === 'right' ? 'right' : 'left';
  192. state.isAttendClass = (query.imagePos === 'left' || query.imagePos === 'right') ? true : false;
  193. if (state.fingeringInfo.direction === "vertical" && state.setting.displayFingering) {
  194. state.musicScoreBtnDirection = state.playBtnDirection === 'right' ? 'left' : 'right';
  195. } else {
  196. state.musicScoreBtnDirection = state.playBtnDirection;
  197. }
  198. state.musicRendered = true;
  199. evaluatCreateMusicPlayer();
  200. resetPlaybackToStart();
  201. // pushAppMusic();
  202. // console.timeEnd("渲染加载耗时");
  203. };
  204. /** 指法配置 */
  205. const fingerConfig = computed<any>(() => {
  206. if (state.setting.displayFingering && state.fingeringInfo?.name) {
  207. if (state.fingeringInfo.direction === "transverse") {
  208. return {
  209. container: {
  210. paddingBottom: state.platform === IPlatform.PC ? `calc(${state.fingeringInfo.height} + ${state.attendHideMenu ? "0px" : "1.8rem"})` : state.fingeringInfo.height,
  211. },
  212. fingerBox: {
  213. height: state.fingeringInfo.height,
  214. },
  215. };
  216. } else {
  217. console.log('指法',state.playBtnDirection,state.platform)
  218. // 老师端,竖向指法,需要根据功能按钮方向进行设置
  219. if (state.platform === IPlatform.PC) {
  220. return {
  221. container: {
  222. paddingRight: state.playBtnDirection === "left" ? "initial" : state.fingeringInfo.width,
  223. paddingLeft: state.playBtnDirection === "left" ? state.fingeringInfo.width : "initial",
  224. },
  225. fingerBox: {
  226. position: "absolute",
  227. width: state.fingeringInfo.width,
  228. height: "100%",
  229. right: state.playBtnDirection === "left" ? "initial" : 0,
  230. left: state.playBtnDirection === "left" ? 0 : "initial",
  231. top: 0,
  232. },
  233. };
  234. } else {
  235. return {
  236. container: {
  237. paddingRight: state.fingeringInfo.width,
  238. },
  239. fingerBox: {
  240. position: "absolute",
  241. width: state.fingeringInfo.width,
  242. height: "100%",
  243. right: 0,
  244. top: 0,
  245. },
  246. };
  247. }
  248. }
  249. }
  250. return {
  251. container: {},
  252. fingerBox: {},
  253. };
  254. });
  255. // 监听指法显示
  256. watch(
  257. () => state.setting.displayFingering,
  258. () => {
  259. if (state.fingeringInfo.direction === "vertical") {
  260. if (state.setting.displayFingering) {
  261. state.musicScoreBtnDirection = state.playBtnDirection === 'left' ? 'right' : 'left'
  262. } else {
  263. state.musicScoreBtnDirection = state.playBtnDirection
  264. }
  265. nextTick(() => {
  266. resetMusicScore();
  267. });
  268. }
  269. }
  270. );
  271. watch(
  272. () => state.setting.soundEffect,
  273. () => {
  274. store.set("musicscoresetting", state.setting);
  275. }
  276. );
  277. /**播放状态改变时,向父页面发送事件 */
  278. const sendParentMessage = (playState: IAudioState) => {
  279. window.parent.postMessage(
  280. {
  281. api: "headerTogge",
  282. playState: playState,
  283. },
  284. "*"
  285. );
  286. };
  287. // 监听播放状态
  288. watch(
  289. () => state.playState,
  290. () => {
  291. // if (state.platform != IPlatform.PC) {
  292. // detailData.headerHide = state.playState === "play" ? true : false;
  293. // }
  294. detailData.headerHide = state.playState === "play" ? true : false;
  295. sendParentMessage(state.playState);
  296. }
  297. );
  298. /** 指法预览切换 */
  299. watch(
  300. () => detailData.fingerPreView,
  301. () => {
  302. console.log(2342);
  303. window.parent.postMessage(
  304. {
  305. api: "api_fingerPreView",
  306. state: detailData.fingerPreView,
  307. },
  308. "*"
  309. );
  310. }
  311. );
  312. const pageVisible = usePageVisibility();
  313. watch(
  314. () => pageVisible.value,
  315. (val) => {
  316. if (val === "hidden") {
  317. // 如果是播放状态,需要暂停播放
  318. // console.log("页面隐藏停止播放");
  319. if (state.playState === "play") {
  320. togglePlay("paused");
  321. }
  322. }
  323. }
  324. );
  325. onMounted(() => {
  326. window.addEventListener("resize", resetMusicScore);
  327. });
  328. onBeforeUnmount(() => {
  329. window.removeEventListener("resize", resetMusicScore);
  330. });
  331. const browsInfo = browser();
  332. const handleOpenFignerView = () => {
  333. if (!query.modelType) {
  334. detailData.orientation = state.fingeringInfo.orientation || 0;
  335. api_setRequestedOrientation(detailData.orientation);
  336. }
  337. // const url = `${
  338. // /(192|localhost)/.test(location.origin)
  339. // ? "http://192.168.3.114:3000/instrument.html"
  340. // : location.origin + location.pathname
  341. // }#/view-figner`;
  342. // console.log("🚀 ~ url:", url);
  343. // api_openWebView({
  344. // url: url,
  345. // orientation: state.fingeringInfo.orientation || 0,
  346. // });
  347. if (state.playState === "play") {
  348. togglePlay("paused");
  349. setTimeout(() => {
  350. detailData.fingerPreView = true;
  351. }, 500);
  352. return;
  353. }
  354. detailData.fingerPreView = true;
  355. };
  356. const handleCloseFignerView = () => {
  357. if (!query.modelType && detailData.orientation == 1) {
  358. api_setRequestedOrientation(0);
  359. }
  360. detailData.fingerPreView = false;
  361. detailData.fingerPreViewGuide = false;
  362. };
  363. return () => (
  364. <div
  365. class={[styles.detail, state.setting.eyeProtection && "eyeProtection", state.platform === IPlatform.PC && styles.PC, state.isPreView && styles.preViewDetail, state.isSingleLine && styles.singleLineDetail]}
  366. style={{
  367. paddingLeft: detailData.paddingLeft,
  368. background: state.setting.camera ? `rgba(${state.setting.eyeProtection ? "253,244,229" : "255,255,255"} ,${state.setting.cameraOpacity / 100}) !important` : "",
  369. }}
  370. >
  371. <Transition name="van-fade">
  372. {detailData.skeletonLoading && (
  373. <div class={styles.skeleton}>
  374. <Skeleton row={8} />
  375. </div>
  376. )}
  377. </Transition>
  378. {/** 学生端头部标题&功能按钮 */}
  379. {
  380. (!state.isPreView && state.platform !== IPlatform.PC) &&
  381. <div class={[styles.headHeight, detailData.headerHide && styles.headHide]}>{state.musicRendered && <HeaderTop />}</div>
  382. }
  383. {/** 老师端标题 */}
  384. {
  385. state.platform === IPlatform.PC &&
  386. <div class={[styles.pcHead, detailData.headerHide && styles.pcHeadHide]}>
  387. <Title text={state.examSongName} rightView={false} />
  388. </div>
  389. }
  390. <div
  391. id="scrollContainer"
  392. style={{ ...fingerConfig.value.container, height: detailData.headerHide ? "100vh" : "" }}
  393. class={[styles.container, !state.setting.displayCursor && "hideCursor", browsInfo.xiaomi && styles.xiaomi, state.platform === IPlatform.PC && styles.pcContainer]}
  394. onClick={(e: Event) => {
  395. e.stopPropagation();
  396. // if (state.playState === "play" && state.platform != IPlatform.PC) {
  397. // detailData.headerHide = !detailData.headerHide;
  398. // }
  399. if (state.playState === "play") {
  400. detailData.headerHide = !detailData.headerHide;
  401. }
  402. }}
  403. >
  404. {/* 曲谱渲染 */}
  405. {!detailData.isLoading &&
  406. <MusicScore
  407. showPartNames={state.isCombineRender}
  408. onRendered={handleRendered}
  409. />
  410. }
  411. {/* {
  412. state.musicRendered &&
  413. <TestCheck />
  414. } */}
  415. {/* 指法 */}
  416. {state.setting.displayFingering && state.fingeringInfo?.name && !state.isPreView && state.isShowFingering && (
  417. <div style={{ ...fingerConfig.value.fingerBox }}>
  418. <Fingering
  419. style={{
  420. background: state.setting.camera ? `rgba(${state.setting.eyeProtection ? "253,244,229" : "255,255,255"} ,${state.setting.cameraOpacity / 100})` : "",
  421. }}
  422. onOpen={() => handleOpenFignerView()}
  423. />
  424. </div>
  425. )}
  426. </div>
  427. {/** 老师端底部功能按钮 */}
  428. {
  429. (!state.isPreView && state.platform === IPlatform.PC) &&
  430. <div class={[styles.headHeight, detailData.headerHide && styles.pcHeadHideBottom]}>{state.musicRendered && <HeaderTop />}</div>
  431. }
  432. {/* 节拍器,跟练需要播放系统节拍器,所以不需要判断needTick状态 */}
  433. {/* {state.needTick && <Tick />} */}
  434. <Tick />
  435. {/* 曲目渲染完成,再去下载mp3资源 */}
  436. {!detailData.isLoading && !detailData.skeletonLoading && <AudioList />}
  437. {/* {!detailData.isLoading && <TheAudio src={tickWav} />} */}
  438. {/* 预加载延迟检测组建 */}
  439. {!detailData.isLoading && !detailData.skeletonLoading && evaluatingData.preloadJson && !evaluatingData.jsonLoadDone && (
  440. <div class={styles.preJson}>
  441. <DelayCheck isPreLoad={true} />
  442. </div>
  443. )}
  444. {/* 评测 */}
  445. {state.modeType === "evaluating" && (
  446. <>
  447. <Evaluating />
  448. {evaluatingData.rendered && <EvaluatModel />}
  449. </>
  450. )}
  451. {/* 跟练模式 */}
  452. {state.modeType === "follow" && (
  453. <>
  454. <FollowPractice />
  455. <FollowModel />
  456. </>
  457. )}
  458. {/* 切换曲谱 */}
  459. {!query.lessonTrainingId && !query.questionId && state.isConcert && <ToggleMusicSheet />}
  460. {state.musicRendered && !state.isPreView && (
  461. <>
  462. {/* 统计训练时长 */}
  463. {storeData.isApp && <RecordingTime />}
  464. {/* 作业 */}
  465. {query.workRecord && <WorkIndex />}
  466. {/* 曲谱列表 */}
  467. {state.playState == "play" || followData.start || evaluatingData.startBegin || query.workRecord || query.modelType || state.platform === IPlatform.PC || query.isCbs ? null : <TheMusicList />}
  468. </>
  469. )}
  470. <Popup
  471. zIndex={5050}
  472. teleport="body"
  473. v-model:show={detailData.fingerPreView}
  474. position="bottom"
  475. onClosed={() => {
  476. detailData.fingerPreViewAnimation = false;
  477. }}
  478. onOpen={() => {
  479. detailData.fingerPreViewAnimation = true;
  480. }}
  481. onOpened={() => {
  482. detailData.fingerPreViewGuide = true;
  483. }}
  484. >
  485. {detailData.fingerPreViewAnimation && <ViewFigner show={detailData.fingerPreViewGuide} subject={state.fingeringInfo.name} isComponent={true} onClose={handleCloseFignerView} />}
  486. </Popup>
  487. </div>
  488. );
  489. },
  490. });