index.tsx 15 KB

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