index.tsx 15 KB

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